Skip to content

Commit 9f6a71b

Browse files
committed
net: add kns structs
1 parent d2d3270 commit 9f6a71b

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/net.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{get_blob, Address, NodeId, Request, SendError};
22
use serde::{Deserialize, Serialize};
3-
use std::collections::BTreeMap;
3+
use std::collections::{BTreeMap, HashMap};
44

55
//
66
// Networking protocol types
@@ -52,14 +52,10 @@ pub enum NetAction {
5252
/// in the future could get from remote provider
5353
KnsUpdate(KnsUpdate),
5454
KnsBatchUpdate(Vec<KnsUpdate>),
55-
/// add a (namehash -> name) to our representation of the PKI
56-
AddName(String, String),
5755
/// get a list of peers we are connected to
5856
GetPeers,
5957
/// get the [`Identity`] struct for a single peer
6058
GetPeer(String),
61-
/// get the [`NodeId`] associated with a given namehash, if any
62-
NamehashToName(NamehashToNameRequest),
6359
/// get a user-readable diagnostics string containing networking inforamtion
6460
GetDiagnostics,
6561
/// sign the attached blob payload, sign with our node's networking key.
@@ -102,13 +98,48 @@ pub enum NetResponse {
10298
//
10399
// KNS parts of the networking protocol
104100
//
101+
#[derive(Debug, Serialize, Deserialize)]
102+
pub enum IndexerRequests {
103+
NamehashToName(NamehashToNameRequest),
104+
NodeInfo(NodeInfoRequest),
105+
GetState(GetStateRequest),
106+
}
107+
108+
#[derive(Debug, Serialize, Deserialize)]
109+
pub enum IndexerResponses {
110+
Name(Option<String>),
111+
NodeInfo(Option<KnsUpdate>),
112+
// necessary? printout similar
113+
GetState(KnsState),
114+
}
115+
116+
#[derive(Clone, Debug, Serialize, Deserialize)]
117+
pub struct KnsState {
118+
chain_id: u64,
119+
contract_address: String,
120+
names: HashMap<String, String>,
121+
// include TBA in KnsUpdate? now that it's official struct in here too...
122+
nodes: HashMap<String, KnsUpdate>,
123+
block: u64,
124+
}
105125

106126
#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
107127
pub struct NamehashToNameRequest {
108128
pub hash: String,
109129
pub block: Option<u64>,
110130
}
111131

132+
#[derive(Debug, Serialize, Deserialize)]
133+
pub struct NodeInfoRequest {
134+
pub name: String,
135+
pub block: u64,
136+
}
137+
138+
#[derive(Debug, Serialize, Deserialize)]
139+
pub struct GetStateRequest {
140+
pub block: u64,
141+
}
142+
112143
#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
113144
pub struct KnsUpdate {
114145
pub name: String, // actual username / domain name
@@ -177,18 +208,16 @@ pub fn get_name(
177208
) -> anyhow::Result<String> {
178209
let res = Request::to(("our", "kns_indexer", "kns_indexer", "sys"))
179210
.body(
180-
serde_json::to_vec(&NetAction::NamehashToName(NamehashToNameRequest {
211+
serde_json::to_vec(&IndexerRequests::NamehashToName(NamehashToNameRequest {
181212
hash: namehash.to_string(),
182213
block: block,
183214
}))
184215
.unwrap(),
185216
)
186217
.send_and_await_response(timeout.unwrap_or(5))??;
187218

188-
let response = rmp_serde::from_slice::<NetResponse>(res.body())?;
189-
if let NetResponse::Name(name) = response {
190-
// is returning an option optimal?
191-
// getting an error for send/malformatted hash/not found seems better
219+
let response = serde_json::from_slice::<IndexerResponses>(res.body())?;
220+
if let IndexerResponses::Name(name) = response {
192221
if let Some(name) = name {
193222
return Ok(name);
194223
} else {

0 commit comments

Comments
 (0)