Skip to content
Snippets Groups Projects
Unverified Commit 80d6d094 authored by dharjeezy's avatar dharjeezy
Browse files

make use of an Arc

parent 66bfbf45
Branches
No related merge requests found
Pipeline #510640 waiting for manual action with stages
in 6 minutes and 34 seconds
...@@ -22,8 +22,7 @@ use futures::prelude::*; ...@@ -22,8 +22,7 @@ use futures::prelude::*;
use sc_network::MAX_RESPONSE_SIZE; use sc_network::MAX_RESPONSE_SIZE;
use sc_network_common::ExHashT; use sc_network_common::ExHashT;
use sp_runtime::traits::Block as BlockT; use sp_runtime::traits::Block as BlockT;
use std::{collections::HashMap, future::Future, pin::Pin, time}; use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc, time};
use std::sync::Arc;
/// Interval at which we propagate transactions; /// Interval at which we propagate transactions;
pub(crate) const PROPAGATE_TIMEOUT: time::Duration = time::Duration::from_millis(2900); 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 ...@@ -58,7 +57,7 @@ pub type TransactionImportFuture = Pin<Box<dyn Future<Output = TransactionImport
/// Transaction pool interface /// Transaction pool interface
pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync { pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync {
/// Get transactions from the pool that are ready to be propagated. /// 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. /// Get hash of transaction.
fn hash_of(&self, transaction: &Arc<B::Extrinsic>) -> H; fn hash_of(&self, transaction: &Arc<B::Extrinsic>) -> H;
/// Import a transaction into the pool. /// Import a transaction into the pool.
...@@ -80,15 +79,15 @@ pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync { ...@@ -80,15 +79,15 @@ pub trait TransactionPool<H: ExHashT, B: BlockT>: Send + Sync {
pub struct EmptyTransactionPool; pub struct EmptyTransactionPool;
impl<H: ExHashT + Default, B: BlockT> TransactionPool<H, B> for 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() Vec::new()
} }
fn hash_of(&self, _transaction: &B::Extrinsic) -> H { fn hash_of(&self, _transaction: &Arc<B::Extrinsic>) -> H {
Default::default() Default::default()
} }
fn import(&self, _transaction: B::Extrinsic) -> TransactionImportFuture { fn import(&self, _transaction: Arc<B::Extrinsic>) -> TransactionImportFuture {
Box::pin(future::ready(TransactionImport::KnownGood)) Box::pin(future::ready(TransactionImport::KnownGood))
} }
......
...@@ -420,7 +420,7 @@ where ...@@ -420,7 +420,7 @@ where
break 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()); peer.known_transactions.insert(hash.clone());
self.network.report_peer(who, rep::ANY_TRANSACTION); self.network.report_peer(who, rep::ANY_TRANSACTION);
...@@ -428,7 +428,7 @@ where ...@@ -428,7 +428,7 @@ where
match self.pending_transactions_peers.entry(hash.clone()) { match self.pending_transactions_peers.entry(hash.clone()) {
Entry::Vacant(entry) => { Entry::Vacant(entry) => {
self.pending_transactions.push(PendingTransaction { self.pending_transactions.push(PendingTransaction {
validation: self.transaction_pool.import(t), validation: self.transaction_pool.import(Arc::new(t)),
tx_hash: hash, tx_hash: hash,
}); });
entry.insert(vec![who]); entry.insert(vec![who]);
...@@ -460,7 +460,8 @@ where ...@@ -460,7 +460,8 @@ where
debug!(target: LOG_TARGET, "Propagating transaction [{:?}]", hash); debug!(target: LOG_TARGET, "Propagating transaction [{:?}]", hash);
if let Some(transaction) = self.transaction_pool.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); self.transaction_pool.on_broadcasted(propagated_to);
} else { } else {
debug!(target: "sync", "Propagating transaction failure [{:?}]", hash); debug!(target: "sync", "Propagating transaction failure [{:?}]", hash);
...@@ -469,7 +470,7 @@ where ...@@ -469,7 +470,7 @@ where
fn do_propagate_transactions( fn do_propagate_transactions(
&mut self, &mut self,
transactions: &[(H, B::Extrinsic)], transactions: &[(H, Arc<B::Extrinsic>)],
) -> HashMap<H, Vec<String>> { ) -> HashMap<H, Vec<String>> {
let mut propagated_to = HashMap::<_, Vec<_>>::new(); let mut propagated_to = HashMap::<_, Vec<_>>::new();
let mut propagated_transactions = 0; let mut propagated_transactions = 0;
......
...@@ -474,7 +474,7 @@ impl<C, P> TransactionPoolAdapter<C, P> { ...@@ -474,7 +474,7 @@ impl<C, P> TransactionPoolAdapter<C, P> {
/// Get transactions for propagation. /// Get transactions for propagation.
/// ///
/// Function extracted to simplify the test and prevent creating `ServiceFactory`. /// 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 where
Pool: TransactionPool<Block = B, Hash = H, Error = E>, Pool: TransactionPool<Block = B, Hash = H, Error = E>,
B: BlockT, B: BlockT,
...@@ -485,7 +485,7 @@ where ...@@ -485,7 +485,7 @@ where
.filter(|t| t.is_propagable()) .filter(|t| t.is_propagable())
.map(|t| { .map(|t| {
let hash = t.hash().clone(); let hash = t.hash().clone();
let ex: B::Extrinsic = (**t.data()).clone(); let ex = t.data().clone();
(hash, ex) (hash, ex)
}) })
.collect() .collect()
...@@ -506,15 +506,15 @@ where ...@@ -506,15 +506,15 @@ where
H: std::hash::Hash + Eq + sp_runtime::traits::Member + sp_runtime::traits::MaybeSerialize, H: std::hash::Hash + Eq + sp_runtime::traits::Member + sp_runtime::traits::MaybeSerialize,
E: 'static + IntoPoolError + From<sc_transaction_pool_api::error::Error>, 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) transactions_to_propagate(&*self.pool)
} }
fn hash_of(&self, transaction: &B::Extrinsic) -> H { fn hash_of(&self, transaction: &Arc<B::Extrinsic>) -> H {
self.pool.hash_of(transaction) 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 encoded = transaction.encode();
let uxt = match Decode::decode(&mut &encoded[..]) { let uxt = match Decode::decode(&mut &encoded[..]) {
Ok(uxt) => uxt, Ok(uxt) => uxt,
...@@ -614,6 +614,6 @@ mod tests { ...@@ -614,6 +614,6 @@ mod tests {
// then // then
assert_eq!(transactions.len(), 1); assert_eq!(transactions.len(), 1);
assert!(TransferData::try_from(&transactions[0].1).is_ok()); assert!(TransferData::try_from(&*transactions[0].1).is_ok());
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment