From ac408e6c2f8b15d2d4b3479552ab3e3f21461153 Mon Sep 17 00:00:00 2001 From: dr-frmr Date: Mon, 13 May 2024 13:21:38 -0600 Subject: [PATCH] fix: format eth providers as set so they are never duplicated --- kinode/src/eth/mod.rs | 28 ++++++++++++++++++---------- kinode/src/main.rs | 2 +- kinode/src/register.rs | 10 ++++++++-- lib/src/core.rs | 2 +- lib/src/eth.rs | 6 +++--- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/kinode/src/eth/mod.rs b/kinode/src/eth/mod.rs index 7925388e..bee8c092 100644 --- a/kinode/src/eth/mod.rs +++ b/kinode/src/eth/mod.rs @@ -63,18 +63,26 @@ impl ActiveProviders { kns_update, use_as_provider, } => { - self.nodes.push(NodeProvider { - trusted: new.trusted, - usable: use_as_provider, - kns_update, - }); + self.remove_provider(&kns_update.name); + self.nodes.insert( + 0, + NodeProvider { + trusted: new.trusted, + usable: use_as_provider, + kns_update, + }, + ); } NodeOrRpcUrl::RpcUrl(url) => { - self.urls.push(UrlProvider { - trusted: new.trusted, - url, - pubsub: None, - }); + self.remove_provider(&url); + self.urls.insert( + 0, + UrlProvider { + trusted: new.trusted, + url, + pubsub: None, + }, + ); } } } diff --git a/kinode/src/main.rs b/kinode/src/main.rs index 399263a4..8650c7c4 100644 --- a/kinode/src/main.rs +++ b/kinode/src/main.rs @@ -86,7 +86,7 @@ async fn main() { Err(_) => serde_json::from_str(DEFAULT_ETH_PROVIDERS).unwrap(), }; if let Some(rpc) = rpc { - eth_provider_config.push(lib::eth::ProviderConfig { + eth_provider_config.insert(lib::eth::ProviderConfig { chain_id: CHAIN_ID, trusted: true, provider: lib::eth::NodeOrRpcUrl::RpcUrl(rpc.to_string()), diff --git a/kinode/src/register.rs b/kinode/src/register.rs index e0668c13..6743c9c0 100644 --- a/kinode/src/register.rs +++ b/kinode/src/register.rs @@ -125,8 +125,13 @@ pub async fn register( } else { "wss://optimism-rpc.publicnode.com".to_string() }; - let connector = WsConnect { url, auth: None }; - let Ok(client) = ClientBuilder::default().ws(connector).await else { + println!("Connecting to Optimism RPC at {url}"); + // this fails occasionally in certain networking environments. i'm not sure why. + // frustratingly, the exact same call does not fail in the eth module. more investigation needed. + let Ok(client) = ClientBuilder::default() + .ws(WsConnect { url, auth: None }) + .await + else { panic!( "Error: runtime could not connect to ETH RPC.\n\ This is necessary in order to verify node identity onchain.\n\ @@ -134,6 +139,7 @@ pub async fn register( the --rpc flag, and you are connected to the internet." ); }; + println!("Connected to Optimism RPC"); let provider = Arc::new(Provider::new_with_client(client)); diff --git a/lib/src/core.rs b/lib/src/core.rs index d4370baf..dd5e4b5b 100644 --- a/lib/src/core.rs +++ b/lib/src/core.rs @@ -1583,7 +1583,7 @@ pub enum NetResponse { Verified(bool), } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Hash, Eq, PartialEq)] pub struct KnsUpdate { pub name: String, // actual username / domain name pub owner: String, diff --git a/lib/src/eth.rs b/lib/src/eth.rs index 2c071e5b..6d449be5 100644 --- a/lib/src/eth.rs +++ b/lib/src/eth.rs @@ -141,17 +141,17 @@ pub struct AccessSettings { pub deny: HashSet, // blacklist for access (always used) } -pub type SavedConfigs = Vec; +pub type SavedConfigs = HashSet; /// Provider config. Can currently be a node or a ws provider instance. -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Hash, Eq, PartialEq)] pub struct ProviderConfig { pub chain_id: u64, pub trusted: bool, pub provider: NodeOrRpcUrl, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize, Hash, Eq, PartialEq)] pub enum NodeOrRpcUrl { Node { kns_update: crate::core::KnsUpdate,