From 46de2683fff8392c03754f8e34b6af14232a6345 Mon Sep 17 00:00:00 2001 From: grumbach Date: Wed, 8 Jan 2025 03:20:16 +0900 Subject: [PATCH] feat: centralize evm network selection code --- ant-cli/src/actions/connect.rs | 3 +- ant-cli/src/commands/wallet.rs | 2 +- ant-cli/src/evm_network.rs | 26 --------------- ant-cli/src/main.rs | 1 - ant-evm/src/lib.rs | 2 +- .../src/bin/cli/subcommands/evm_network.rs | 4 +-- ant-node/src/bin/antnode/main.rs | 7 ++-- autonomi/src/lib.rs | 3 +- autonomi/tests/wallet.rs | 6 ++-- evmlib/src/utils.rs | 32 +++++++++++++++++-- test-utils/src/evm.rs | 12 +++---- 11 files changed, 46 insertions(+), 52 deletions(-) delete mode 100644 ant-cli/src/evm_network.rs diff --git a/ant-cli/src/actions/connect.rs b/ant-cli/src/actions/connect.rs index 5ae12694ad..118b2df60d 100644 --- a/ant-cli/src/actions/connect.rs +++ b/ant-cli/src/actions/connect.rs @@ -6,9 +6,8 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use crate::evm_network::get_evm_network; use crate::network::NetworkPeers; -use autonomi::{Client, ClientConfig}; +use autonomi::{get_evm_network, Client, ClientConfig}; use color_eyre::eyre::bail; use color_eyre::eyre::Result; use indicatif::ProgressBar; diff --git a/ant-cli/src/commands/wallet.rs b/ant-cli/src/commands/wallet.rs index c11bc41802..9e76128bbe 100644 --- a/ant-cli/src/commands/wallet.rs +++ b/ant-cli/src/commands/wallet.rs @@ -6,10 +6,10 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use crate::evm_network::get_evm_network; use crate::wallet::fs::{select_wallet_private_key, store_private_key}; use crate::wallet::input::request_password; use crate::wallet::DUMMY_NETWORK; +use autonomi::get_evm_network; use autonomi::Wallet; use color_eyre::eyre::eyre; use color_eyre::Result; diff --git a/ant-cli/src/evm_network.rs b/ant-cli/src/evm_network.rs deleted file mode 100644 index 34e09ecc0f..0000000000 --- a/ant-cli/src/evm_network.rs +++ /dev/null @@ -1,26 +0,0 @@ -use autonomi::{get_evm_network_from_env, local_evm_network_from_csv, Network}; -use color_eyre::eyre::{Context, Result}; - -use std::sync::OnceLock; - -static EVM_NETWORK: OnceLock = OnceLock::new(); - -pub(crate) fn get_evm_network(local: bool) -> Result { - if let Some(network) = EVM_NETWORK.get() { - return Ok(network.clone()); - } - - let res = match get_evm_network_from_env() { - Ok(evm_network) => Ok(evm_network), - Err(_) if local => { - Ok(local_evm_network_from_csv().wrap_err("Failed to get local EVM network")?) - } - Err(_) => Ok(Default::default()), - }; - - if let Ok(network) = res.as_ref() { - let _ = EVM_NETWORK.set(network.clone()); - } - - res -} diff --git a/ant-cli/src/main.rs b/ant-cli/src/main.rs index b7f6bc8fcb..e0fe5cf644 100644 --- a/ant-cli/src/main.rs +++ b/ant-cli/src/main.rs @@ -12,7 +12,6 @@ extern crate tracing; mod access; mod actions; mod commands; -mod evm_network; mod opt; mod utils; mod wallet; diff --git a/ant-evm/src/lib.rs b/ant-evm/src/lib.rs index 678db86c72..e8d5e92784 100644 --- a/ant-evm/src/lib.rs +++ b/ant-evm/src/lib.rs @@ -19,7 +19,7 @@ pub use evmlib::cryptography; #[cfg(feature = "external-signer")] pub use evmlib::external_signer; pub use evmlib::utils; -pub use evmlib::utils::{get_evm_network_from_env, local_evm_network_from_csv}; +pub use evmlib::utils::get_evm_network; pub use evmlib::utils::{DATA_PAYMENTS_ADDRESS, PAYMENT_TOKEN_ADDRESS, RPC_URL}; pub use evmlib::wallet::Error as EvmWalletError; pub use evmlib::wallet::Wallet as EvmWallet; diff --git a/ant-node-manager/src/bin/cli/subcommands/evm_network.rs b/ant-node-manager/src/bin/cli/subcommands/evm_network.rs index 10028ed468..59d6078ad3 100644 --- a/ant-node-manager/src/bin/cli/subcommands/evm_network.rs +++ b/ant-node-manager/src/bin/cli/subcommands/evm_network.rs @@ -6,7 +6,7 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use ant_evm::{utils::local_evm_network_from_csv, EvmNetwork}; +use ant_evm::{get_evm_network, EvmNetwork}; use clap::Subcommand; use color_eyre::Result; @@ -50,7 +50,7 @@ impl TryInto for EvmNetworkCommand { Self::EvmArbitrumSepolia => Ok(EvmNetwork::ArbitrumSepolia), Self::EvmArbitrumSepoliaTest => Ok(EvmNetwork::ArbitrumSepoliaTest), Self::EvmLocal => { - let network = local_evm_network_from_csv()?; + let network = get_evm_network(true)?; Ok(network) } Self::EvmCustom { diff --git a/ant-node/src/bin/antnode/main.rs b/ant-node/src/bin/antnode/main.rs index 2ee4cc6d34..665b47518c 100644 --- a/ant-node/src/bin/antnode/main.rs +++ b/ant-node/src/bin/antnode/main.rs @@ -14,7 +14,7 @@ mod subcommands; use crate::subcommands::EvmNetworkCommand; use ant_bootstrap::{BootstrapCacheConfig, BootstrapCacheStore, PeersArgs}; -use ant_evm::{get_evm_network_from_env, local_evm_network_from_csv, EvmNetwork, RewardsAddress}; +use ant_evm::{get_evm_network, EvmNetwork, RewardsAddress}; use ant_logging::metrics::init_metrics; use ant_logging::{Level, LogFormat, LogOutputDest, ReloadHandle}; use ant_node::{Marker, NodeBuilder, NodeEvent, NodeEventsReceiver}; @@ -264,9 +264,8 @@ fn main() -> Result<()> { let evm_network: EvmNetwork = match opt.evm_network.as_ref() { Some(evm_network) => Ok(evm_network.clone().into()), - None => match get_evm_network_from_env() { - Ok(evm_network) => Ok(evm_network), - Err(_) if opt.peers.local => Ok(local_evm_network_from_csv()?), + None => match get_evm_network(opt.peers.local) { + Ok(net) => Ok(net), Err(_) => Err(eyre!( "EVM network not specified. Please specify a network using the subcommand or by setting the `EVM_NETWORK` environment variable." )), diff --git a/autonomi/src/lib.rs b/autonomi/src/lib.rs index bfb1d705b2..fe741c8003 100644 --- a/autonomi/src/lib.rs +++ b/autonomi/src/lib.rs @@ -65,8 +65,7 @@ extern crate tracing; pub mod client; pub mod self_encryption; -pub use ant_evm::get_evm_network_from_env; -pub use ant_evm::local_evm_network_from_csv; +pub use ant_evm::utils::get_evm_network; pub use ant_evm::Amount; pub use ant_evm::EvmNetwork as Network; pub use ant_evm::EvmWallet as Wallet; diff --git a/autonomi/tests/wallet.rs b/autonomi/tests/wallet.rs index 33880ca5ab..6347edaae7 100644 --- a/autonomi/tests/wallet.rs +++ b/autonomi/tests/wallet.rs @@ -6,7 +6,7 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use ant_evm::get_evm_network_from_env; +use ant_evm::get_evm_network; use ant_evm::EvmWallet; use ant_evm::{Amount, RewardsAddress}; use ant_logging::LogBuilder; @@ -17,7 +17,7 @@ use test_utils::evm::get_funded_wallet; async fn from_private_key() { let private_key = "0xdb1049e76a813c94be0df47ec3e20533ca676b1b9fef2ddbce9daa117e4da4aa"; let network = - get_evm_network_from_env().expect("Could not get EVM network from environment variables"); + get_evm_network(true).expect("Could not get EVM network from environment variables"); let wallet = EvmWallet::new_from_private_key(network, private_key).unwrap(); assert_eq!( @@ -31,7 +31,7 @@ async fn send_tokens() { let _log_appender_guard = LogBuilder::init_single_threaded_tokio_test("wallet", false); let network = - get_evm_network_from_env().expect("Could not get EVM network from environment variables"); + get_evm_network(true).expect("Could not get EVM network from environment variables"); let wallet = get_funded_wallet(); let receiving_wallet = EvmWallet::new_with_random_wallet(network); diff --git a/evmlib/src/utils.rs b/evmlib/src/utils.rs index 1717b2d517..40c92ca648 100644 --- a/evmlib/src/utils.rs +++ b/evmlib/src/utils.rs @@ -47,6 +47,34 @@ pub fn dummy_hash() -> Hash { Hash::new(rand::rngs::OsRng.gen()) } +use std::sync::OnceLock; + +static EVM_NETWORK: OnceLock = OnceLock::new(); + +/// Initialize the EVM Network parameters from environment variables or local CSV file. +/// +/// It will first try to get the network from the environment variables. +/// If it fails and `local` is true, it will try to get the network from the local CSV file. +/// If both fail, it will return the default network. +pub fn get_evm_network(local: bool) -> Result { + if let Some(network) = EVM_NETWORK.get() { + return Ok(network.clone()); + } + + let res = match get_evm_network_from_env() { + Ok(evm_network) => Ok(evm_network), + Err(_) if local => Ok(local_evm_network_from_csv() + .map_err(|e| Error::FailedToGetEvmNetwork(e.to_string()))?), + Err(_) => Ok(Network::default()), + }; + + if let Ok(network) = res.as_ref() { + let _ = EVM_NETWORK.set(network.clone()); + } + + res +} + pub fn get_evm_testnet_csv_path() -> Result { let file = data_dir() .ok_or(Error::FailedToGetEvmNetwork( @@ -60,7 +88,7 @@ pub fn get_evm_testnet_csv_path() -> Result { /// Get the `Network` from environment variables. /// /// Returns an error if we cannot obtain the network from any means. -pub fn get_evm_network_from_env() -> Result { +fn get_evm_network_from_env() -> Result { let evm_vars = [ env::var(RPC_URL) .ok() @@ -126,7 +154,7 @@ pub fn get_evm_network_from_env() -> Result { } /// Get the `Network::Custom` from the local EVM testnet CSV file -pub fn local_evm_network_from_csv() -> Result { +fn local_evm_network_from_csv() -> Result { // load the csv let csv_path = get_evm_testnet_csv_path()?; diff --git a/test-utils/src/evm.rs b/test-utils/src/evm.rs index 989da8b1a4..e3db3dd2ba 100644 --- a/test-utils/src/evm.rs +++ b/test-utils/src/evm.rs @@ -6,15 +6,12 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use color_eyre::{ - eyre::{bail, Context}, - Result, -}; -use evmlib::{utils::local_evm_network_from_csv, wallet::Wallet, Network}; +use color_eyre::{eyre::bail, Result}; +use evmlib::{utils::get_evm_network, wallet::Wallet, Network}; use std::env; pub fn get_funded_wallet() -> evmlib::wallet::Wallet { - let network = local_evm_network_from_csv().expect("Failed to get local EVM network from CSV"); + let network = get_evm_network(true).expect("Failed to get local EVM network from CSV"); if matches!(network, Network::ArbitrumOne) { panic!("You're trying to use ArbitrumOne network. Use a custom network for testing."); } @@ -28,8 +25,7 @@ pub fn get_funded_wallet() -> evmlib::wallet::Wallet { } pub fn get_new_wallet() -> Result { - let network = - local_evm_network_from_csv().wrap_err("Failed to get local EVM network from CSV")?; + let network = get_evm_network(true).expect("Failed to get local EVM network from CSV"); if matches!(network, Network::ArbitrumOne) { bail!("You're trying to use ArbitrumOne network. Use a custom network for testing."); }