diff --git a/polkadot/transaction-pool/src/lib.rs b/polkadot/transaction-pool/src/lib.rs index 676b0d9773..96ad1050d9 100644 --- a/polkadot/transaction-pool/src/lib.rs +++ b/polkadot/transaction-pool/src/lib.rs @@ -38,7 +38,7 @@ mod error; use std::{ cmp::Ordering, - collections::HashMap, + collections::{BTreeMap, HashMap}, ops::Deref, sync::Arc, }; @@ -54,6 +54,7 @@ use extrinsic_pool::{ use polkadot_api::PolkadotApi; use primitives::{AccountId, BlockId, Hash, Index, UncheckedExtrinsic as FutureProofUncheckedExtrinsic}; use runtime::{Address, UncheckedExtrinsic}; +use substrate_primitives::Bytes; use substrate_runtime_primitives::traits::{Bounded, Checkable, Hash as HashT, BlakeTwo256}; pub use extrinsic_pool::txpool::{Options, Status, LightStatus, VerifiedTransaction as VerifiedTransactionOps}; @@ -182,9 +183,14 @@ impl txpool::Scoring for Scoring { } } - fn should_replace(&self, old: &VerifiedTransaction, _new: &VerifiedTransaction) -> bool { - // Always replace not fully verified transactions. - !old.is_fully_verified() + fn should_replace(&self, old: &VerifiedTransaction, _new: &VerifiedTransaction) -> Choice { + if old.is_fully_verified() { + // Don't allow new transactions if we are reaching the limit. + Choice::RejectNew + } else { + // Always replace not fully verified transactions. + Choice::ReplaceOld + } } } @@ -415,6 +421,7 @@ impl ExtrinsicPool for Transact A: PolkadotApi, { type Error = Error; + type InPool = BTreeMap>; fn submit(&self, block: BlockId, xts: Vec) -> Result> { xts.into_iter() @@ -446,6 +453,17 @@ impl ExtrinsicPool for Transact fn import_notification_stream(&self) -> EventStream { self.inner.import_notification_stream() } + + fn all(&self) -> Self::InPool { + self.inner.all(|it| it.fold(Default::default(), |mut map: Self::InPool, tx| { + // Map with `null` key is not serializable, so we fallback to default accountId. + map.entry(tx.sender().unwrap_or_default()) + .or_insert_with(Vec::new) + // use bytes type to make it serialize nicer. + .push(Bytes(tx.primitive_extrinsic())); + map + })) + } } #[cfg(test)]