Refactor ZombieNode to cache provider using OnceCell

This commit is contained in:
Marios Christou
2025-10-03 16:26:17 +03:00
parent 2e3a81cdd3
commit 269e8cfe80
+44 -20
View File
@@ -44,9 +44,12 @@ use alloy::{
network::{Ethereum, EthereumWallet, NetworkWallet}, network::{Ethereum, EthereumWallet, NetworkWallet},
primitives::{Address, BlockHash, BlockNumber, BlockTimestamp, StorageKey, TxHash, U256}, primitives::{Address, BlockHash, BlockNumber, BlockTimestamp, StorageKey, TxHash, U256},
providers::{ providers::{
Provider, ProviderBuilder, Identity, Provider, ProviderBuilder, RootProvider,
ext::DebugApi, ext::DebugApi,
fillers::{CachedNonceManager, ChainIdFiller, FillProvider, NonceFiller, TxFiller}, fillers::{
CachedNonceManager, ChainIdFiller, FillProvider, JoinFill, NonceFiller, TxFiller,
WalletFiller,
},
}, },
rpc::types::{ rpc::types::{
EIP1186AccountProofResponse, TransactionReceipt, EIP1186AccountProofResponse, TransactionReceipt,
@@ -63,6 +66,7 @@ use revive_dt_node_interaction::EthereumNode;
use serde_json::{Value as JsonValue, json}; use serde_json::{Value as JsonValue, json};
use sp_core::crypto::Ss58Codec; use sp_core::crypto::Ss58Codec;
use sp_runtime::AccountId32; use sp_runtime::AccountId32;
use tokio::sync::OnceCell;
use tracing::instrument; use tracing::instrument;
use zombienet_sdk::{LocalFileSystem, NetworkConfigBuilder, NetworkConfigExt}; use zombienet_sdk::{LocalFileSystem, NetworkConfigBuilder, NetworkConfigExt};
@@ -94,6 +98,20 @@ pub struct ZombieNode {
network: Option<zombienet_sdk::Network<LocalFileSystem>>, network: Option<zombienet_sdk::Network<LocalFileSystem>>,
eth_rpc_process: Option<Process>, eth_rpc_process: Option<Process>,
node_rpc_port: Option<u16>, node_rpc_port: Option<u16>,
#[allow(clippy::type_complexity)]
provider: OnceCell<
FillProvider<
JoinFill<
JoinFill<
JoinFill<JoinFill<Identity, FallbackGasFiller>, ChainIdFiller>,
NonceFiller,
>,
WalletFiller<Arc<EthereumWallet>>,
>,
RootProvider<ReviveNetwork>,
ReviveNetwork,
>,
>,
} }
impl ZombieNode { impl ZombieNode {
@@ -141,6 +159,7 @@ impl ZombieNode {
eth_rpc_process: None, eth_rpc_process: None,
connection_string: String::new(), connection_string: String::new(),
node_rpc_port: None, node_rpc_port: None,
provider: Default::default(),
} }
} }
@@ -366,26 +385,31 @@ impl ZombieNode {
async fn provider( async fn provider(
&self, &self,
) -> anyhow::Result< ) -> anyhow::Result<
FillProvider<impl TxFiller<ReviveNetwork>, impl Provider<ReviveNetwork>, ReviveNetwork>, FillProvider<
impl TxFiller<ReviveNetwork>,
impl Provider<ReviveNetwork> + Clone,
ReviveNetwork,
>,
> { > {
let Some(_network) = &self.network else { self.provider
anyhow::bail!("Node not initialized, call spawn() first"); .get_or_try_init(|| async move {
}; ProviderBuilder::new()
.disable_recommended_fillers()
ProviderBuilder::new() .network::<ReviveNetwork>()
.disable_recommended_fillers() .filler(FallbackGasFiller::new(
.network::<ReviveNetwork>() 1_000_000_000,
.filler(FallbackGasFiller::new( 1_000_000_000,
1_000_000_000, 1_000_000_000,
1_000_000_000, ))
1_000_000_000, .filler(self.chain_id_filler.clone())
)) .filler(NonceFiller::new(self.nonce_manager.clone()))
.filler(self.chain_id_filler.clone()) .wallet(self.wallet.clone())
.filler(NonceFiller::new(self.nonce_manager.clone())) .connect(&self.connection_string)
.wallet(self.wallet.clone()) .await
.connect(&self.connection_string) .map_err(Into::into)
})
.await .await
.context("Failed to connect to parachain Ethereum RPC") .cloned()
} }
} }