From 80d6d09417371a6e5c04a20c9d37246644cd9e1b Mon Sep 17 00:00:00 2001 From: dharjeezy <dharjeezy@gmail.com> Date: Sat, 4 Jan 2025 17:40:36 +0100 Subject: [PATCH] make use of an Arc --- .../client/network/transactions/src/config.rs | 11 +++++------ substrate/client/network/transactions/src/lib.rs | 9 +++++---- substrate/client/service/src/lib.rs | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/substrate/client/network/transactions/src/config.rs b/substrate/client/network/transactions/src/config.rs index 7c49cfa8d43..6b4725a2b8d 100644 --- a/substrate/client/network/transactions/src/config.rs +++ b/substrate/client/network/transactions/src/config.rs @@ -22,8 +22,7 @@ use futures::prelude::*; use sc_network::MAX_RESPONSE_SIZE; use sc_network_common::ExHashT; use sp_runtime::traits::Block as BlockT; -use std::{collections::HashMap, future::Future, pin::Pin, time}; -use std::sync::Arc; +use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc, time}; /// Interval at which we propagate transactions; pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900); @@ -58,7 +57,7 @@ pub type TransactionImportFuture = Pin<Box<dyn Future<Output = TransactionImport /// Transaction pool interface pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync { /// Get transactions from the pool that are ready to be propagated. - fn transactions(&self) -> Vec<(H, B::Extrinsic)>; + fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)>; /// Get hash of transaction. fn hash_of(&self, transaction: &Arc<B::Extrinsic>) -> H; /// Import a transaction into the pool. @@ -80,15 +79,15 @@ pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync { pub struct EmptyTransactionPool; impl<H: ExHashT + Default, B: BlockT> TransactionPool<H, B> for EmptyTransactionPool { - fn transactions(&self) -> Vec<(H, B::Extrinsic)> { + fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)> { Vec::new() } - fn hash_of(&self, _transaction: &B::Extrinsic) -> H { + fn hash_of(&self, _transaction: &Arc<B::Extrinsic>) -> H { Default::default() } - fn import(&self, _transaction: B::Extrinsic) -> TransactionImportFuture { + fn import(&self, _transaction: Arc<B::Extrinsic>) -> TransactionImportFuture { Box::pin(future::ready(TransactionImport::KnownGood)) } diff --git a/substrate/client/network/transactions/src/lib.rs b/substrate/client/network/transactions/src/lib.rs index 44fa702ef6d..05181d75a8a 100644 --- a/substrate/client/network/transactions/src/lib.rs +++ b/substrate/client/network/transactions/src/lib.rs @@ -420,7 +420,7 @@ where break } - let hash = self.transaction_pool.hash_of(&t); + let hash = self.transaction_pool.hash_of(&Arc::new(t.clone())); peer.known_transactions.insert(hash.clone()); self.network.report_peer(who, rep::ANY_TRANSACTION); @@ -428,7 +428,7 @@ where match self.pending_transactions_peers.entry(hash.clone()) { Entry::Vacant(entry) => { self.pending_transactions.push(PendingTransaction { - validation: self.transaction_pool.import(t), + validation: self.transaction_pool.import(Arc::new(t)), tx_hash: hash, }); entry.insert(vec![who]); @@ -460,7 +460,8 @@ where debug!(target: LOG_TARGET, "Propagating transaction [{:?}]", hash); if let Some(transaction) = self.transaction_pool.transaction(hash) { - let propagated_to = self.do_propagate_transactions(&[(hash.clone(), transaction)]); + let propagated_to = + self.do_propagate_transactions(&[(hash.clone(), Arc::new(transaction))]); self.transaction_pool.on_broadcasted(propagated_to); } else { debug!(target: "sync", "Propagating transaction failure [{:?}]", hash); @@ -469,7 +470,7 @@ where fn do_propagate_transactions( &mut self, - transactions: &[(H, B::Extrinsic)], + transactions: &[(H, Arc<B::Extrinsic>)], ) -> HashMap<H, Vec<String>> { let mut propagated_to = HashMap::<_, Vec<_>>::new(); let mut propagated_transactions = 0; diff --git a/substrate/client/service/src/lib.rs b/substrate/client/service/src/lib.rs index 2a3144a33e1..8b82bfa4230 100644 --- a/substrate/client/service/src/lib.rs +++ b/substrate/client/service/src/lib.rs @@ -474,7 +474,7 @@ impl<C, P> TransactionPoolAdapter<C, P> { /// Get transactions for propagation. /// /// Function extracted to simplify the test and prevent creating `ServiceFactory`. -fn transactions_to_propagate<Pool, B, H, E>(pool: &Pool) -> Vec<(H, B::Extrinsic)> +fn transactions_to_propagate<Pool, B, H, E>(pool: &Pool) -> Vec<(H, Arc<B::Extrinsic>)> where Pool: TransactionPool<Block = B, Hash = H, Error = E>, B: BlockT, @@ -485,7 +485,7 @@ where .filter(|t| t.is_propagable()) .map(|t| { let hash = t.hash().clone(); - let ex: B::Extrinsic = (**t.data()).clone(); + let ex = t.data().clone(); (hash, ex) }) .collect() @@ -506,15 +506,15 @@ where H: std::hash::Hash + Eq + sp_runtime::traits::Member + sp_runtime::traits::MaybeSerialize, E: 'static + IntoPoolError + From<sc_transaction_pool_api::error::Error>, { - fn transactions(&self) -> Vec<(H, B::Extrinsic)> { + fn transactions(&self) -> Vec<(H, Arc<B::Extrinsic>)> { transactions_to_propagate(&*self.pool) } - fn hash_of(&self, transaction: &B::Extrinsic) -> H { - self.pool.hash_of(transaction) + fn hash_of(&self, transaction: &Arc<B::Extrinsic>) -> H { + self.pool.hash_of(transaction.as_ref()) } - fn import(&self, transaction: B::Extrinsic) -> TransactionImportFuture { + fn import(&self, transaction: Arc<B::Extrinsic>) -> TransactionImportFuture { let encoded = transaction.encode(); let uxt = match Decode::decode(&mut &encoded[..]) { Ok(uxt) => uxt, @@ -614,6 +614,6 @@ mod tests { // then assert_eq!(transactions.len(), 1); - assert!(TransferData::try_from(&transactions[0].1).is_ok()); + assert!(TransferData::try_from(&*transactions[0].1).is_ok()); } } -- GitLab