From ab7eae6f88075d2af298e651b2aabf0c14f8c4a8 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sat, 26 May 2018 19:14:47 +0800 Subject: [PATCH] Update transaction-pool dependency and remove ethereum-types (#174) --- polkadot/transaction-pool/Cargo.toml | 3 +- polkadot/transaction-pool/src/lib.rs | 51 ++++++++++++---------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/polkadot/transaction-pool/Cargo.toml b/polkadot/transaction-pool/Cargo.toml index b8bf7f001c..d8cf5ebd80 100644 --- a/polkadot/transaction-pool/Cargo.toml +++ b/polkadot/transaction-pool/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Parity Technologies "] [dependencies] log = "0.4.0" -transaction-pool = "1.9.0" +transaction-pool = "1.12.0" error-chain = "0.11" polkadot-api = { path = "../api" } polkadot-primitives = { path = "../primitives" } @@ -16,4 +16,3 @@ substrate-primitives = { path = "../../substrate/primitives" } substrate-runtime-primitives = { path = "../../substrate/runtime/primitives" } substrate-codec = { path = "../../substrate/codec" } ed25519 = { path = "../../substrate/ed25519" } -ethereum-types = "0.2" diff --git a/polkadot/transaction-pool/src/lib.rs b/polkadot/transaction-pool/src/lib.rs index d375c7c371..faa3d09a0b 100644 --- a/polkadot/transaction-pool/src/lib.rs +++ b/polkadot/transaction-pool/src/lib.rs @@ -15,7 +15,6 @@ // along with Polkadot. If not, see . extern crate ed25519; -extern crate ethereum_types; extern crate substrate_codec as codec; extern crate substrate_primitives as substrate_primitives; extern crate substrate_runtime_primitives as substrate_runtime_primitives; @@ -35,22 +34,15 @@ use std::cmp::Ordering; use std::sync::Arc; use polkadot_api::PolkadotApi; -use primitives::{AccountId, Timestamp}; +use primitives::{AccountId, Timestamp, Hash}; use primitives::parachain::CandidateReceipt; use runtime::{Block, UncheckedExtrinsic, TimestampCall, ParachainsCall, Call}; use substrate_runtime_primitives::traits::{Bounded, Checkable}; -use transaction_pool::{Pool, Readiness}; +use transaction_pool::{Transaction, Pool, Readiness}; use transaction_pool::scoring::{Change, Choice}; -// TODO: make queue generic over hash and sender so we don't need ethereum-types -pub use ethereum_types::{Address as TruncatedAccountId, H256 as TransactionHash}; pub use transaction_pool::{Options, Status, LightStatus, NoopListener, VerifiedTransaction as VerifiedTransactionOps}; -/// Truncate an account ID to 160 bits. -pub fn truncate_id(id: &AccountId) -> TruncatedAccountId { - TruncatedAccountId::from_slice(&id[..20]) -} - /// Useful functions for working with Polkadot blocks. pub struct PolkadotBlock { block: Block, @@ -152,7 +144,7 @@ error_chain! { display("Transaction had bad signature."), } /// Attempted to queue a transaction that is already in the pool. - AlreadyImported(hash: TransactionHash) { + AlreadyImported(hash: Hash) { description("Transaction is already in the pool."), display("Transaction {:?} is already in the pool.", hash), } @@ -168,8 +160,8 @@ error_chain! { #[derive(Debug, Clone)] pub struct VerifiedTransaction { inner: ::Checked, - hash: TransactionHash, - address: TruncatedAccountId, + hash: Hash, + address: AccountId, insertion_id: u64, encoded_size: usize, } @@ -186,7 +178,7 @@ impl VerifiedTransaction { Ok(xt) => { // TODO: make transaction-pool use generic types. let hash = substrate_primitives::hashing::blake2_256(&message); - let address = truncate_id(&xt.signed); + let address = xt.signed; Ok(VerifiedTransaction { inner: xt, hash: hash.into(), @@ -210,12 +202,12 @@ impl VerifiedTransaction { } /// Get the 256-bit hash of this transaction. - pub fn hash(&self) -> &TransactionHash { + pub fn hash(&self) -> &Hash { &self.hash } - /// Get the truncated account ID of the sender of this transaction. - pub fn sender(&self) -> &TruncatedAccountId { + /// Get the account ID of the sender of this transaction. + pub fn sender(&self) -> &AccountId { &self.address } @@ -232,28 +224,29 @@ impl AsRef< ::Checked > for VerifiedTransaction } impl transaction_pool::VerifiedTransaction for VerifiedTransaction { - fn hash(&self) -> &TransactionHash { + type Hash = Hash; + type Sender = AccountId; + + fn hash(&self) -> &Hash { &self.hash } - fn sender(&self) -> &TruncatedAccountId { + fn sender(&self) -> &AccountId { &self.address } fn mem_usage(&self) -> usize { 1 // TODO } - - fn insertion_id(&self) -> u64 { - self.insertion_id - } } /// Scoring implementation for polkadot transactions. +#[derive(Debug)] pub struct Scoring; impl transaction_pool::Scoring for Scoring { type Score = u64; + type Event = (); fn compare(&self, old: &VerifiedTransaction, other: &VerifiedTransaction) -> Ordering { old.inner.index.cmp(&other.inner.index) @@ -265,9 +258,9 @@ impl transaction_pool::Scoring for Scoring { fn update_scores( &self, - xts: &[Arc], + xts: &[Transaction], scores: &mut [Self::Score], - _change: Change + _change: Change<()> ) { for i in 0..xts.len() { // all the same score since there are no fees. @@ -312,7 +305,7 @@ impl<'a, T: 'a + PolkadotApi> Ready<'a, T> { impl<'a, T: 'a + PolkadotApi> transaction_pool::Ready for Ready<'a, T> { fn is_ready(&mut self, xt: &VerifiedTransaction) -> Readiness { let sender = xt.inner.signed; - trace!(target: "transaction-pool", "Checking readiness of {} (from {})", xt.hash, TransactionHash::from(sender)); + trace!(target: "transaction-pool", "Checking readiness of {} (from {})", xt.hash, Hash::from(sender)); // TODO: find a way to handle index error properly -- will need changes to // transaction-pool trait. @@ -325,7 +318,7 @@ impl<'a, T: 'a + PolkadotApi> transaction_pool::Ready for R match xt.inner.index.cmp(&next_index) { Ordering::Greater => Readiness::Future, Ordering::Equal => Readiness::Ready, - Ordering::Less => Readiness::Stalled, // TODO: this is not "stalled" but rather stale and can be discarded. + Ordering::Less => Readiness::Stale, // TODO: this is not "stalled" but rather stale and can be discarded. } } } @@ -375,12 +368,12 @@ impl TransactionPool { } /// Remove from the pool. - pub fn remove(&mut self, hash: &TransactionHash, is_valid: bool) -> Option> { + pub fn remove(&mut self, hash: &Hash, is_valid: bool) -> Option> { self.inner.remove(hash, is_valid) } /// Cull transactions from the queue. - pub fn cull(&mut self, senders: Option<&[TruncatedAccountId]>, ready: Ready) -> usize { + pub fn cull(&mut self, senders: Option<&[AccountId]>, ready: Ready) -> usize { self.inner.cull(senders, ready) }