Skip to content

Commit

Permalink
keep only the newest eth provider for a given url
Browse files Browse the repository at this point in the history
  • Loading branch information
nick1udwig committed Feb 7, 2025
1 parent 6044f08 commit a72d018
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kinode/packages/settings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions kinode/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ async fn main() {
);

// default eth providers/routers
let mut is_eth_provider_config_updated = false;
let mut eth_provider_config: lib::eth::SavedConfigs = if let Ok(contents) =
tokio::fs::read_to_string(home_directory_path.join(".eth_providers")).await
{
Expand All @@ -121,6 +122,7 @@ async fn main() {
serde_json::from_str(DEFAULT_ETH_PROVIDERS).unwrap()
}
} else {
is_eth_provider_config_updated = true;
serde_json::from_str(DEFAULT_ETH_PROVIDERS).unwrap()
};
if let Some(rpc) = rpc {
Expand All @@ -135,18 +137,12 @@ async fn main() {
},
},
);
// save the new provider config
tokio::fs::write(
home_directory_path.join(".eth_providers"),
serde_json::to_string(&eth_provider_config).unwrap(),
)
.await
.expect("failed to save new eth provider config!");
is_eth_provider_config_updated = true;
}
if let Some(rpc_config) = rpc_config {
let rpc_config = tokio::fs::read_to_string(rpc_config)
.await
.expect("could not read rpc-config");
.expect("cant read rpc-config");
let rpc_config: Vec<RpcUrlConfigInput> =
serde_json::from_str(&rpc_config).expect("rpc-config had invalid format");
for RpcUrlConfigInput { url, auth } in rpc_config {
Expand All @@ -159,6 +155,9 @@ async fn main() {
},
);
}
is_eth_provider_config_updated = true;
}
if is_eth_provider_config_updated {
// save the new provider config
tokio::fs::write(
home_directory_path.join(".eth_providers"),
Expand Down
16 changes: 15 additions & 1 deletion lib/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,21 @@ impl SavedConfigs {
/// insert while enforcing that each config is unique
pub fn insert(&mut self, index: usize, config: ProviderConfig) {
// filter out any configs which are the same as incoming config
self.0.retain(|c| c != &config);
if let NodeOrRpcUrl::RpcUrl { ref url, .. } = config.provider {
// remove old occurrences with matching URLs to make sure
// auth is what was passed in and never an old value
self.0.retain(|c| {
if let NodeOrRpcUrl::RpcUrl {
url: ref old_url, ..
} = c.provider
{
return url != old_url;
}
c != &config
});
} else {
self.0.retain(|c| c != &config);
}
self.0.insert(index, config);
}
}
Expand Down

0 comments on commit a72d018

Please sign in to comment.