Skip to content

Commit

Permalink
net: add kns structs
Browse files Browse the repository at this point in the history
  • Loading branch information
bitful-pannul committed Jul 15, 2024
1 parent d2d3270 commit 9f6a71b
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{get_blob, Address, NodeId, Request, SendError};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};

//
// Networking protocol types
Expand Down Expand Up @@ -52,14 +52,10 @@ pub enum NetAction {
/// in the future could get from remote provider
KnsUpdate(KnsUpdate),
KnsBatchUpdate(Vec<KnsUpdate>),
/// add a (namehash -> name) to our representation of the PKI
AddName(String, String),
/// get a list of peers we are connected to
GetPeers,
/// get the [`Identity`] struct for a single peer
GetPeer(String),
/// get the [`NodeId`] associated with a given namehash, if any
NamehashToName(NamehashToNameRequest),
/// get a user-readable diagnostics string containing networking inforamtion
GetDiagnostics,
/// sign the attached blob payload, sign with our node's networking key.
Expand Down Expand Up @@ -102,13 +98,48 @@ pub enum NetResponse {
//
// KNS parts of the networking protocol
//
#[derive(Debug, Serialize, Deserialize)]
pub enum IndexerRequests {
NamehashToName(NamehashToNameRequest),
NodeInfo(NodeInfoRequest),
GetState(GetStateRequest),
}

#[derive(Debug, Serialize, Deserialize)]
pub enum IndexerResponses {
Name(Option<String>),
NodeInfo(Option<KnsUpdate>),
// necessary? printout similar
GetState(KnsState),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KnsState {
chain_id: u64,
contract_address: String,
names: HashMap<String, String>,
// include TBA in KnsUpdate? now that it's official struct in here too...
nodes: HashMap<String, KnsUpdate>,
block: u64,
}

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

#[derive(Debug, Serialize, Deserialize)]
pub struct NodeInfoRequest {
pub name: String,
pub block: u64,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetStateRequest {
pub block: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct KnsUpdate {
pub name: String, // actual username / domain name
Expand Down Expand Up @@ -177,18 +208,16 @@ pub fn get_name(
) -> anyhow::Result<String> {
let res = Request::to(("our", "kns_indexer", "kns_indexer", "sys"))
.body(
serde_json::to_vec(&NetAction::NamehashToName(NamehashToNameRequest {
serde_json::to_vec(&IndexerRequests::NamehashToName(NamehashToNameRequest {
hash: namehash.to_string(),
block: block,
}))
.unwrap(),
)
.send_and_await_response(timeout.unwrap_or(5))??;

let response = rmp_serde::from_slice::<NetResponse>(res.body())?;
if let NetResponse::Name(name) = response {
// is returning an option optimal?
// getting an error for send/malformatted hash/not found seems better
let response = serde_json::from_slice::<IndexerResponses>(res.body())?;
if let IndexerResponses::Name(name) = response {
if let Some(name) = name {
return Ok(name);
} else {
Expand Down

0 comments on commit 9f6a71b

Please sign in to comment.