|
1 | 1 | use crate::{get_blob, Address, NodeId, Request, SendError};
|
2 | 2 | use serde::{Deserialize, Serialize};
|
3 |
| -use std::collections::BTreeMap; |
| 3 | +use std::collections::{BTreeMap, HashMap}; |
4 | 4 |
|
5 | 5 | //
|
6 | 6 | // Networking protocol types
|
@@ -52,14 +52,10 @@ pub enum NetAction {
|
52 | 52 | /// in the future could get from remote provider
|
53 | 53 | KnsUpdate(KnsUpdate),
|
54 | 54 | KnsBatchUpdate(Vec<KnsUpdate>),
|
55 |
| - /// add a (namehash -> name) to our representation of the PKI |
56 |
| - AddName(String, String), |
57 | 55 | /// get a list of peers we are connected to
|
58 | 56 | GetPeers,
|
59 | 57 | /// get the [`Identity`] struct for a single peer
|
60 | 58 | GetPeer(String),
|
61 |
| - /// get the [`NodeId`] associated with a given namehash, if any |
62 |
| - NamehashToName(NamehashToNameRequest), |
63 | 59 | /// get a user-readable diagnostics string containing networking inforamtion
|
64 | 60 | GetDiagnostics,
|
65 | 61 | /// sign the attached blob payload, sign with our node's networking key.
|
@@ -102,13 +98,48 @@ pub enum NetResponse {
|
102 | 98 | //
|
103 | 99 | // KNS parts of the networking protocol
|
104 | 100 | //
|
| 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 | +} |
105 | 125 |
|
106 | 126 | #[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
|
107 | 127 | pub struct NamehashToNameRequest {
|
108 | 128 | pub hash: String,
|
109 | 129 | pub block: Option<u64>,
|
110 | 130 | }
|
111 | 131 |
|
| 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 | + |
112 | 143 | #[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)]
|
113 | 144 | pub struct KnsUpdate {
|
114 | 145 | pub name: String, // actual username / domain name
|
@@ -177,18 +208,16 @@ pub fn get_name(
|
177 | 208 | ) -> anyhow::Result<String> {
|
178 | 209 | let res = Request::to(("our", "kns_indexer", "kns_indexer", "sys"))
|
179 | 210 | .body(
|
180 |
| - serde_json::to_vec(&NetAction::NamehashToName(NamehashToNameRequest { |
| 211 | + serde_json::to_vec(&IndexerRequests::NamehashToName(NamehashToNameRequest { |
181 | 212 | hash: namehash.to_string(),
|
182 | 213 | block: block,
|
183 | 214 | }))
|
184 | 215 | .unwrap(),
|
185 | 216 | )
|
186 | 217 | .send_and_await_response(timeout.unwrap_or(5))??;
|
187 | 218 |
|
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 { |
192 | 221 | if let Some(name) = name {
|
193 | 222 | return Ok(name);
|
194 | 223 | } else {
|
|
0 commit comments