From 8c789663b8187427b4ff46820c832d5e2b4ab495 Mon Sep 17 00:00:00 2001 From: Eric Woolsey Date: Mon, 4 Nov 2024 13:02:03 -0800 Subject: [PATCH] fix for conflicting alloy signers --- src/main.rs | 88 +++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/src/main.rs b/src/main.rs index 17145fc..08fa9a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ pub mod relay; pub mod tx_sitter; pub mod utils; +use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; @@ -31,6 +32,7 @@ use tracing_subscriber::util::SubscriberInitExt; use self::abi::IWorldIDIdentityManager::TreeChanged; use self::block_scanner::BlockScanner; use self::config::Config; +use self::relay::signer::AlloySignerProvider; /// This service syncs the state of the World Tree and spawns a server that can deliver inclusion proofs for a given identity. #[derive(Parser, Debug)] @@ -181,45 +183,48 @@ pub async fn run(config: Config) -> Result<()> { /// Additionally initializes the signers from the global wallet configuration if present, /// otherwise from the bridged network configuration. fn init_relays(cfg: Config) -> Result> { - // Optinally use a global wallet configuration for all networks without a specific wallet configuration. - let global_signer = if let Some(wallet) = cfg.canonical_network.wallet { - match wallet { - WalletConfig::Mnemonic { mnemonic } => { - let signer = MnemonicBuilder::::default() - .phrase(mnemonic) - .index(0)? - .build()?; - let wallet = EthereumWallet::new(signer); + // A global signer is required when using an [`AlloySigner`] + // in order to keep the transaction nonce in sync. + let mut alloy_signer_providers = + HashMap::>::new(); - let provider = - cfg.canonical_network.provider.signer(wallet.clone()); - - Some(Arc::new(provider)) - } - _ => None, - } - } else { - None - }; cfg.bridged_networks .iter() .map(|bridged| { - let wallet_config = bridged.wallet.as_ref(); + let wallet_config = bridged + .wallet + .clone() + .or(cfg.canonical_network.wallet.clone()) + .ok_or_else(|| eyre!("No wallet configuration found"))?; + match bridged.ty { NetworkType::Evm => match wallet_config { - Some(WalletConfig::Mnemonic { mnemonic }) => { - let signer = MnemonicBuilder::::default() - .phrase(mnemonic) - .index(0)? - .build()?; - let wallet = EthereumWallet::new(signer); - let provider = cfg - .canonical_network - .provider - .signer(wallet.clone()); + WalletConfig::Mnemonic { mnemonic } => { + let provider = match alloy_signer_providers + .get(&mnemonic) + { + Some(provider) => provider.clone(), + None => { + let signer = + MnemonicBuilder::::default() + .phrase(&mnemonic) + .index(0)? + .build()?; + let wallet = EthereumWallet::new(signer); + let provider = Arc::new( + cfg.canonical_network + .provider + .signer(wallet.clone()), + ); + alloy_signer_providers + .insert(mnemonic.clone(), provider.clone()); + provider + } + }; + let alloy_signer = AlloySigner::new( bridged.state_bridge_addr, - Arc::new(provider), + provider, ); Ok(Relayer::EVMRelay(EVMRelay::new( @@ -228,11 +233,11 @@ fn init_relays(cfg: Config) -> Result> { bridged.provider.rpc_endpoint.clone(), ))) } - Some(WalletConfig::TxSitter { url, gas_limit }) => { + WalletConfig::TxSitter { url, gas_limit } => { let signer = TxSitterSigner::new( url.as_str(), bridged.state_bridge_addr, - *gas_limit, + gas_limit, ); Ok(Relayer::EVMRelay(EVMRelay::new( @@ -241,23 +246,6 @@ fn init_relays(cfg: Config) -> Result> { bridged.provider.rpc_endpoint.clone(), ))) } - None => { - if let Some(global_signer) = &global_signer { - info!(network = %bridged.name, "Using global wallet configuration for bridged network"); - let alloy_signer = AlloySigner::new( - bridged.state_bridge_addr, - global_signer.clone(), - ); - - Ok(Relayer::EVMRelay(EVMRelay::new( - Signer::AlloySigner(alloy_signer), - bridged.world_id_addr, - bridged.provider.rpc_endpoint.clone(), - ))) - } else { - Err(eyre!("No wallet configuration found")) - } - } }, NetworkType::Svm => unimplemented!(), NetworkType::Scroll => unimplemented!(),