diff --git a/parity/rpc_apis.rs b/parity/rpc_apis.rs index 491dbc38e..22520f266 100644 --- a/parity/rpc_apis.rs +++ b/parity/rpc_apis.rs @@ -150,7 +150,7 @@ pub fn setup_rpc(server: T, deps: Arc, apis: ApiSet server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate()); if deps.signer_port.is_some() { - server.add_delegate(EthSigningQueueClient::new(&deps.signer_queue, &deps.miner).to_delegate()); + server.add_delegate(EthSigningQueueClient::new(&deps.signer_queue, &deps.client, &deps.miner).to_delegate()); } else { server.add_delegate(EthSigningUnsafeClient::new(&deps.client, &deps.secret_store, &deps.miner).to_delegate()); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index e1f1ba6df..f3a59b698 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -37,7 +37,7 @@ use ethcore::filter::Filter as EthcoreFilter; use self::ethash::SeedHashCompute; use v1::traits::Eth; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, CallRequest, OptionalValue, Index, Filter, Log, Receipt}; -use v1::impls::{dispatch_transaction, error_codes}; +use v1::impls::{default_gas_price, dispatch_transaction, error_codes}; use serde; /// Eth rpc implementation. @@ -153,23 +153,14 @@ impl EthClient where } } - fn default_gas_price(&self) -> Result { - let miner = take_weak!(self.miner); - Ok(take_weak!(self.client) - .gas_price_statistics(100, 8) - .map(|x| x[4]) - .unwrap_or_else(|_| miner.sensible_gas_price()) - ) - } - fn sign_call(&self, request: CallRequest) -> Result { - let client = take_weak!(self.client); + let (client, miner) = (take_weak!(self.client), take_weak!(self.miner)); let from = request.from.unwrap_or(Address::zero()); Ok(EthTransaction { nonce: request.nonce.unwrap_or_else(|| client.latest_nonce(&from)), action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or(U256::from(50_000_000)), - gas_price: request.gas_price.unwrap_or_else(|| self.default_gas_price().expect("call only fails if client or miner are unavailable; client and miner are both available to be here; qed")), + gas_price: request.gas_price.unwrap_or_else(|| default_gas_price(&*client, &*miner)), value: request.value.unwrap_or_else(U256::zero), data: request.data.map_or_else(Vec::new, |d| d.to_vec()) }.fake_sign(from)) @@ -296,7 +287,10 @@ impl Eth for EthClient where fn gas_price(&self, params: Params) -> Result { match params { - Params::None => to_value(&try!(self.default_gas_price())), + Params::None => { + let (client, miner) = (take_weak!(self.client), take_weak!(self.miner)); + to_value(&default_gas_price(&*client, &*miner)) + } _ => Err(Error::invalid_params()) } } diff --git a/rpc/src/v1/impls/eth_signing.rs b/rpc/src/v1/impls/eth_signing.rs index ad0bb035a..c5103fd2d 100644 --- a/rpc/src/v1/impls/eth_signing.rs +++ b/rpc/src/v1/impls/eth_signing.rs @@ -25,38 +25,42 @@ use ethcore::account_provider::AccountProvider; use v1::helpers::{SigningQueue, ConfirmationsQueue}; use v1::traits::EthSigning; use v1::types::{TransactionRequest, Bytes}; -use v1::impls::sign_and_dispatch; +use v1::impls::{default_gas_price, sign_and_dispatch}; + +fn fill_optional_fields(request: &mut TransactionRequest, client: &C, miner: &M) + where C: MiningBlockChainClient, M: MinerService { + if request.gas.is_none() { + request.gas = Some(miner.sensible_gas_limit()); + } + if request.gas_price.is_none() { + request.gas_price = Some(default_gas_price(client, miner)); + } + if request.data.is_none() { + request.data = Some(Bytes::new(Vec::new())); + } +} /// Implementation of functions that require signing when no trusted signer is used. -pub struct EthSigningQueueClient { +pub struct EthSigningQueueClient where C: MiningBlockChainClient, M: MinerService { queue: Weak, + client: Weak, miner: Weak, } -impl EthSigningQueueClient { +impl EthSigningQueueClient where C: MiningBlockChainClient, M: MinerService { /// Creates a new signing queue client given shared signing queue. - pub fn new(queue: &Arc, miner: &Arc) -> Self { + pub fn new(queue: &Arc, client: &Arc, miner: &Arc) -> Self { EthSigningQueueClient { queue: Arc::downgrade(queue), + client: Arc::downgrade(client), miner: Arc::downgrade(miner), } } - - fn fill_optional_fields(&self, miner: Arc, mut request: TransactionRequest) -> TransactionRequest { - if let None = request.gas { - request.gas = Some(miner.sensible_gas_limit()); - } - if let None = request.gas_price { - request.gas_price = Some(miner.sensible_gas_price()); - } - if let None = request.data { - request.data = Some(Bytes::new(Vec::new())); - } - request - } } -impl EthSigning for EthSigningQueueClient { +impl EthSigning for EthSigningQueueClient + where C: MiningBlockChainClient + 'static, M: MinerService + 'static +{ fn sign(&self, _params: Params) -> Result { warn!("Invoking eth_sign is not yet supported with signer enabled."); @@ -66,10 +70,11 @@ impl EthSigning for EthSigningQueueClient { fn send_transaction(&self, params: Params) -> Result { from_params::<(TransactionRequest, )>(params) - .and_then(|(request, )| { + .and_then(|(mut request, )| { let queue = take_weak!(self.queue); - let miner = take_weak!(self.miner); - let request = self.fill_optional_fields(miner, request); + let (client, miner) = (take_weak!(self.client), take_weak!(self.miner)); + + fill_optional_fields(&mut request, &*client, &*miner); let id = queue.add_request(request); let result = id.wait_with_timeout(); result.unwrap_or_else(|| to_value(&H256::new())) diff --git a/rpc/src/v1/impls/mod.rs b/rpc/src/v1/impls/mod.rs index 099585f60..66a5b9c58 100644 --- a/rpc/src/v1/impls/mod.rs +++ b/rpc/src/v1/impls/mod.rs @@ -100,7 +100,7 @@ fn prepare_transaction(client: &C, miner: &M, request: TransactionRequest) action: request.to.map_or(Action::Create, Action::Call), gas: request.gas.unwrap_or_else(|| miner.sensible_gas_limit()), - gas_price: request.gas_price.unwrap_or_else(|| miner.sensible_gas_price()), + gas_price: request.gas_price.unwrap_or_else(|| default_gas_price(client, miner)), value: request.value.unwrap_or_else(U256::zero), data: request.data.map_or_else(Vec::new, |b| b.to_vec()), } @@ -134,6 +134,14 @@ fn sign_and_dispatch(client: &C, miner: &M, request: TransactionRequest, a dispatch_transaction(&*client, &*miner, signed_transaction) } +fn default_gas_price(client: &C, miner: &M) -> U256 where C: MiningBlockChainClient, M: MinerService { + client + .gas_price_statistics(100, 8) + .map(|x| x[4]) + .unwrap_or_else(|_| miner.sensible_gas_price()) +} + + fn signing_error(error: AccountError) -> Error { Error { code: ErrorCode::ServerError(error_codes::ACCOUNT_LOCKED), diff --git a/rpc/src/v1/tests/mocked/eth_signing.rs b/rpc/src/v1/tests/mocked/eth_signing.rs index afa6886fe..a2755ce18 100644 --- a/rpc/src/v1/tests/mocked/eth_signing.rs +++ b/rpc/src/v1/tests/mocked/eth_signing.rs @@ -21,9 +21,11 @@ use v1::traits::EthSigning; use v1::helpers::{ConfirmationsQueue, SigningQueue}; use v1::tests::helpers::TestMinerService; use util::{Address, FixedHash}; +use ethcore::client::TestBlockChainClient; struct EthSigningTester { pub queue: Arc, + pub client: Arc, pub miner: Arc, pub io: IoHandler, } @@ -31,12 +33,14 @@ struct EthSigningTester { impl Default for EthSigningTester { fn default() -> Self { let queue = Arc::new(ConfirmationsQueue::default()); + let client = Arc::new(TestBlockChainClient::default()); let miner = Arc::new(TestMinerService::default()); let io = IoHandler::new(); - io.add_delegate(EthSigningQueueClient::new(&queue, &miner).to_delegate()); + io.add_delegate(EthSigningQueueClient::new(&queue, &client, &miner).to_delegate()); EthSigningTester { queue: queue, + client: client, miner: miner, io: io, }