From 4c8780f1886ca01346501a25a34e70385b7ee3c2 Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Thu, 9 Nov 2017 19:49:34 +0100 Subject: [PATCH] Use nonce reservation per address --- parity/rpc_apis.rs | 13 ++++++------- rpc/src/v1/helpers/dispatch.rs | 30 ++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/parity/rpc_apis.rs b/parity/rpc_apis.rs index be9dbdeb1..6c48adb69 100644 --- a/parity/rpc_apis.rs +++ b/parity/rpc_apis.rs @@ -239,10 +239,10 @@ impl FullDependencies { use parity_rpc::v1::*; macro_rules! add_signing_methods { - ($namespace:ident, $handler:expr, $deps:expr, $nonces:expr) => { + ($namespace:ident, $handler:expr, $deps:expr) => { { let deps = &$deps; - let dispatcher = FullDispatcher::new(deps.client.clone(), deps.miner.clone(), $nonces); + let dispatcher = FullDispatcher::new(deps.client.clone(), deps.miner.clone(), deps.fetch.pool()); if deps.signer_service.is_enabled() { $handler.extend_with($namespace::to_delegate(SigningQueueClient::new(&deps.signer_service, dispatcher, deps.remote.clone(), &deps.secret_store))) } else { @@ -252,11 +252,10 @@ impl FullDependencies { } } - let nonces = Arc::new(Mutex::new(dispatch::Reservations::with_pool(self.fetch.pool()))); let dispatcher = FullDispatcher::new( self.client.clone(), self.miner.clone(), - nonces.clone(), + self.fetch.pool(), ); for api in apis { match *api { @@ -286,7 +285,7 @@ impl FullDependencies { let filter_client = EthFilterClient::new(self.client.clone(), self.miner.clone()); handler.extend_with(filter_client.to_delegate()); - add_signing_methods!(EthSigning, handler, self, nonces.clone()); + add_signing_methods!(EthSigning, handler, self); } }, Api::EthPubSub => { @@ -323,7 +322,7 @@ impl FullDependencies { ).to_delegate()); if !for_generic_pubsub { - add_signing_methods!(ParitySigning, handler, self, nonces.clone()); + add_signing_methods!(ParitySigning, handler, self); } }, Api::ParityPubSub => { @@ -440,7 +439,7 @@ impl LightDependencies { self.on_demand.clone(), self.cache.clone(), self.transaction_queue.clone(), - Arc::new(Mutex::new(dispatch::Reservations::with_pool(self.fetch.pool()))), + self.fetch.pool(), ); macro_rules! add_signing_methods { diff --git a/rpc/src/v1/helpers/dispatch.rs b/rpc/src/v1/helpers/dispatch.rs index c556226b5..9220790e8 100644 --- a/rpc/src/v1/helpers/dispatch.rs +++ b/rpc/src/v1/helpers/dispatch.rs @@ -19,6 +19,7 @@ use std::fmt::Debug; use std::ops::Deref; use std::sync::Arc; +use std::collections::HashMap; use light::cache::Cache as LightDataCache; use light::client::LightChainClient; @@ -32,6 +33,7 @@ use util::Address; use bytes::Bytes; use parking_lot::{Mutex, RwLock}; use stats::Corpus; +use futures_cpupool::CpuPool; use ethkey::Signature; use ethsync::LightSync; @@ -87,16 +89,20 @@ pub trait Dispatcher: Send + Sync + Clone { pub struct FullDispatcher { client: Arc, miner: Arc, - nonces: Arc>, + nonces: Arc>>, + pool: CpuPool, } impl FullDispatcher { /// Create a `FullDispatcher` from Arc references to a client and miner. - pub fn new(client: Arc, miner: Arc, nonces: Arc>) -> Self { + pub fn new(client: Arc, miner: Arc, pool: CpuPool) -> Self { + let nonces = Arc::new(Mutex::new(HashMap::new())); + FullDispatcher { client, miner, nonces, + pool, } } } @@ -107,6 +113,7 @@ impl Clone for FullDispatcher { client: self.client.clone(), miner: self.miner.clone(), nonces: self.nonces.clone(), + pool: self.pool.clone(), } } } @@ -162,7 +169,10 @@ impl Dispatcher for FullDispatcher>, /// Nonce reservations - pub nonces: Arc>, + pub nonces: Arc>>, + /// Cpu pool + pub pool: CpuPool, } impl LightDispatcher { @@ -265,8 +277,10 @@ impl LightDispatcher { on_demand: Arc, cache: Arc>, transaction_queue: Arc>, - nonces: Arc>, + pool: CpuPool, ) -> Self { + let nonces = Arc::new(Mutex::new(HashMap::new())); + LightDispatcher { sync, client, @@ -274,6 +288,7 @@ impl LightDispatcher { cache, transaction_queue, nonces, + pool, } } @@ -379,10 +394,13 @@ impl Dispatcher for LightDispatcher { } let nonces = self.nonces.clone(); + let pool = self.pool.clone(); Box::new(self.next_nonce(filled.from) .map_err(|_| errors::no_light_peers()) .and_then(move |nonce| { - let reserved = nonces.lock().reserve_nonce(nonce); + let reserved = nonces.lock().entry(filled.from) + .or_insert(nonce::Reservations::with_pool(pool)) + .reserve_nonce(nonce); ProspectiveSigner::new(accounts, filled, chain_id, reserved, password) })) }