From 2b7fae8fa64073112361cc9703baac2db0947556 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 25 May 2016 17:35:15 +0200 Subject: [PATCH 01/16] add state_at_id and balance_at_id, integrate with RPC --- ethcore/src/client/client.rs | 17 +++++++++++++++++ ethcore/src/client/mod.rs | 14 +++++++++++++- miner/src/lib.rs | 6 +++--- rpc/src/v1/impls/eth.rs | 17 ++++++++++++++--- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 27081471e..2d4d20c12 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -346,6 +346,16 @@ impl Client where V: Verifier { imported } + /// Attempt to get a copy of a specific block's state. + /// + /// This can fail (but may not) if the DB prunes state. + pub fn state_at_id(&self, id: BlockID) -> Option { + self.block_header(id).map(|header| { + let db = self.state_db.lock().unwrap().boxed_clone(); + State::from_existing(db, HeaderView::new(&header).state_root(), self.engine.account_start_nonce()) + }) + } + /// Get a copy of the best block's state. pub fn state(&self) -> State { State::from_existing(self.state_db.lock().unwrap().boxed_clone(), HeaderView::new(&self.best_block_header()).state_root(), self.engine.account_start_nonce()) @@ -557,6 +567,13 @@ impl BlockChainClient for Client where V: Verifier { self.state().balance(address) } + fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { + match id { + BlockID::Latest => Some(self.balance(address)), + id => self.state_at_id(id).map(|s| s.balance(address)), + } + } + fn storage_at(&self, address: &Address, position: &H256) -> H256 { self.state().storage_at(address, position) } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 4a3d2c5f4..0ac255c7d 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -74,9 +74,21 @@ pub trait BlockChainClient : Sync + Send { /// Get address code. fn code(&self, address: &Address) -> Option; - /// Get address balance. + /// Get address balance at latest state. fn balance(&self, address: &Address) -> U256; + /// Account balance at a specific block ID. + /// + /// Will fail if the block is not valid or the block's root hash has been + /// pruned from the DB. + fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.balance(address)) + } else { + None + } + } + /// Get value of the storage at given position. fn storage_at(&self, address: &Address, position: &H256) -> H256; diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 211c61b1f..e9ac7aba2 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -63,8 +63,8 @@ pub use external::{ExternalMiner, ExternalMinerService}; use std::collections::BTreeMap; use util::{H256, U256, Address, Bytes}; use ethcore::client::{BlockChainClient, Executed}; -use ethcore::block::{ClosedBlock}; -use ethcore::receipt::{Receipt}; +use ethcore::block::ClosedBlock; +use ethcore::receipt::Receipt; use ethcore::error::{Error, ExecutionError}; use ethcore::transaction::SignedTransaction; @@ -154,7 +154,7 @@ pub trait MinerService : Send + Sync { /// Suggested gas limit. fn sensible_gas_limit(&self) -> U256 { x!(21000) } - /// Account balance + /// Latest account balance in pending state. fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256; /// Call into contract code using pending state. diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index a13512357..5b4fc6c89 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -26,9 +26,9 @@ use ethminer::{MinerService, AccountDetails, ExternalMinerService}; use jsonrpc_core::*; use util::numbers::*; use util::sha3::*; -use util::bytes::{ToPretty}; +use util::bytes::ToPretty; use util::rlp::{encode, decode, UntrustedRlp, View}; -use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; +use ethcore::client::{self, BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; use ethcore::views::*; use ethcore::ethereum::Ethash; @@ -257,6 +257,17 @@ fn pending_logs(miner: &M, filter: &EthcoreFilter) -> Vec where M: Miner result } +// must be in range [-32099, -32000] +const UNSUPPORTED_REQUEST_CODE: i64 = -32000; + +fn make_unsupported_err() -> Error { + Error { + code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE), + message: "Unsupported request.".into(), + data: None + } +} + impl Eth for EthClient where C: BlockChainClient + 'static, S: SyncProvider + 'static, @@ -343,7 +354,7 @@ impl Eth for EthClient where .and_then(|(address, block_number,)| match block_number { BlockNumber::Latest => to_value(&take_weak!(self.client).balance(&address)), BlockNumber::Pending => to_value(&take_weak!(self.miner).balance(take_weak!(self.client).deref(), &address)), - _ => Err(Error::invalid_params()), + id => to_value(&try!(take_weak!(self.client).balance_at_id(&address, id.into()).ok_or_else(make_unsupported_err))), }) } From 3405f3eab13894e17e60c4b7135978bb27e191f6 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Wed, 25 May 2016 17:54:20 +0200 Subject: [PATCH 02/16] implement storage_at_id --- ethcore/src/client/client.rs | 7 +++++++ ethcore/src/client/mod.rs | 12 +++++++++++- rpc/src/v1/impls/eth.rs | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 2d4d20c12..c28493d7a 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -578,6 +578,13 @@ impl BlockChainClient for Client where V: Verifier { self.state().storage_at(address, position) } + fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { + match id { + BlockID::Latest => Some(self.storage_at(address, position)), + id => self.state_at_id(id).map(|s| s.storage_at(address, position)), + } + } + fn transaction(&self, id: TransactionID) -> Option { self.transaction_address(id).and_then(|address| self.chain.transaction(&address)) } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 0ac255c7d..75517190f 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -79,6 +79,7 @@ pub trait BlockChainClient : Sync + Send { /// Account balance at a specific block ID. /// + /// Must not fail if `id` is `BlockID::Latest`. /// Will fail if the block is not valid or the block's root hash has been /// pruned from the DB. fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { @@ -89,9 +90,18 @@ pub trait BlockChainClient : Sync + Send { } } - /// Get value of the storage at given position. + /// Get value of the storage at given position from the latest state. fn storage_at(&self, address: &Address, position: &H256) -> H256; + /// Get value of the storage at given position form the latest state. + fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.storage_at(address, position)) + } else { + None + } + } + /// Get transaction with given hash. fn transaction(&self, id: TransactionID) -> Option; diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 5b4fc6c89..2751444e2 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -361,9 +361,9 @@ impl Eth for EthClient where fn storage_at(&self, params: Params) -> Result { from_params_default_third::(params) .and_then(|(address, position, block_number,)| match block_number { - BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(take_weak!(self.client).deref(), &address, &H256::from(position)))), + BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))), BlockNumber::Latest => to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))), - _ => Err(Error::invalid_params()), + id => to_value(&try!(take_weak!(self.client).storage_at_id(&address, &H256::from(position), id.into()).ok_or_else(make_unsupported_err))), }) } From 86eab79d9de8946d67922a0bea2360384e54e413 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 11:46:45 +0200 Subject: [PATCH 03/16] consolidate [balance/storage]_at and _at_id functionality --- ethcore/src/client/client.rs | 29 ++++++++++------------------ ethcore/src/client/mod.rs | 32 +++++++------------------------ ethcore/src/client/test_client.rs | 16 ++++++++++++---- miner/src/miner.rs | 14 ++++++++++---- rpc/src/v1/impls/eth.rs | 13 +++++++------ sync/src/chain.rs | 2 +- 6 files changed, 47 insertions(+), 59 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index c28493d7a..1b31a7b3b 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -349,7 +349,12 @@ impl Client where V: Verifier { /// Attempt to get a copy of a specific block's state. /// /// This can fail (but may not) if the DB prunes state. - pub fn state_at_id(&self, id: BlockID) -> Option { + pub fn state_at(&self, id: BlockID) -> Option { + // fast path for latest state. + if let BlockID::Latest = id.clone() { + return Some(self.state()) + } + self.block_header(id).map(|header| { let db = self.state_db.lock().unwrap().boxed_clone(); State::from_existing(db, HeaderView::new(&header).state_root(), self.engine.account_start_nonce()) @@ -563,26 +568,12 @@ impl BlockChainClient for Client where V: Verifier { self.state().code(address) } - fn balance(&self, address: &Address) -> U256 { - self.state().balance(address) + fn balance(&self, address: &Address, id: BlockID) -> Option { + self.state_at(id).map(|s| s.balance(address)) } - fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { - match id { - BlockID::Latest => Some(self.balance(address)), - id => self.state_at_id(id).map(|s| s.balance(address)), - } - } - - fn storage_at(&self, address: &Address, position: &H256) -> H256 { - self.state().storage_at(address, position) - } - - fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { - match id { - BlockID::Latest => Some(self.storage_at(address, position)), - id => self.state_at_id(id).map(|s| s.storage_at(address, position)), - } + fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option { + self.state_at(id).map(|s| s.storage_at(address, position)) } fn transaction(&self, id: TransactionID) -> Option { diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 75517190f..988dac023 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -74,33 +74,15 @@ pub trait BlockChainClient : Sync + Send { /// Get address code. fn code(&self, address: &Address) -> Option; - /// Get address balance at latest state. - fn balance(&self, address: &Address) -> U256; - - /// Account balance at a specific block ID. + /// Get address balance at the given block's state. /// - /// Must not fail if `id` is `BlockID::Latest`. - /// Will fail if the block is not valid or the block's root hash has been - /// pruned from the DB. - fn balance_at_id(&self, address: &Address, id: BlockID) -> Option { - if let BlockID::Latest = id { - Some(self.balance(address)) - } else { - None - } - } + /// Returns None if and only if the block's root hash has been pruned from the DB. + fn balance(&self, address: &Address, id: BlockID) -> Option; - /// Get value of the storage at given position from the latest state. - fn storage_at(&self, address: &Address, position: &H256) -> H256; - - /// Get value of the storage at given position form the latest state. - fn storage_at_id(&self, address: &Address, position: &H256, id: BlockID) -> Option { - if let BlockID::Latest = id { - Some(self.storage_at(address, position)) - } else { - None - } - } + /// Get value of the storage at given position at the given block. + /// + /// Returns None if and only if the block's root hash has been pruned from the DB. + fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option; /// Get transaction with given hash. fn transaction(&self, id: TransactionID) -> Option; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index a60218a31..3e7608395 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -253,12 +253,20 @@ impl BlockChainClient for TestBlockChainClient { self.code.read().unwrap().get(address).cloned() } - fn balance(&self, address: &Address) -> U256 { - self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) + fn balance(&self, address: &Address, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)) + } else { + None + } } - fn storage_at(&self, address: &Address, position: &H256) -> H256 { - self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new) + fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option { + if let BlockID::Latest = id { + Some(self.storage.read().unwrap().get(&(address.clone(), position.clone())).cloned().unwrap_or_else(H256::new)) + } else { + None + } } fn transaction(&self, _id: TransactionID) -> Option { diff --git a/miner/src/miner.rs b/miner/src/miner.rs index f159cac1c..efb2f2c77 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -168,7 +168,7 @@ impl Miner { let mut queue = self.transaction_queue.lock().unwrap(); let fetch_account = |a: &Address| AccountDetails { nonce: chain.nonce(a), - balance: chain.balance(a), + balance: chain.balance(a, BlockID::Latest).unwrap(), }; for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); @@ -290,12 +290,18 @@ impl MinerService for Miner { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.balance(address), |b| b.block().fields().state.balance(address)) + sealing_work.peek_last_ref().map_or_else( + || chain.balance(address, BlockID::Latest).unwrap(), + |b| b.block().fields().state.balance(address) + ) } fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.storage_at(address, position), |b| b.block().fields().state.storage_at(address, position)) + sealing_work.peek_last_ref().map_or_else( + || chain.storage_at(address, position, BlockID::Latest).unwrap(), + |b| b.block().fields().state.storage_at(address, position) + ) } fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { @@ -546,7 +552,7 @@ impl MinerService for Miner { } let _ = self.import_transactions(txs, |a| AccountDetails { nonce: chain.nonce(a), - balance: chain.balance(a), + balance: chain.balance(a, BlockID::Latest).unwrap(), }); }); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 2751444e2..2aa5ff3f2 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -28,7 +28,7 @@ use util::numbers::*; use util::sha3::*; use util::bytes::ToPretty; use util::rlp::{encode, decode, UntrustedRlp, View}; -use ethcore::client::{self, BlockChainClient, BlockID, TransactionID, UncleID}; +use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; use ethcore::views::*; use ethcore::ethereum::Ethash; @@ -200,7 +200,7 @@ impl EthClient where miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { AccountDetails { nonce: client.nonce(&a), - balance: client.balance(&a), + balance: client.balance(&a, BlockID::Latest).unwrap(), } }) }; @@ -352,9 +352,8 @@ impl Eth for EthClient where fn balance(&self, params: Params) -> Result { from_params_default_second(params) .and_then(|(address, block_number,)| match block_number { - BlockNumber::Latest => to_value(&take_weak!(self.client).balance(&address)), BlockNumber::Pending => to_value(&take_weak!(self.miner).balance(take_weak!(self.client).deref(), &address)), - id => to_value(&try!(take_weak!(self.client).balance_at_id(&address, id.into()).ok_or_else(make_unsupported_err))), + id => to_value(&try!(take_weak!(self.client).balance(&address, id.into()).ok_or_else(make_unsupported_err))), }) } @@ -362,8 +361,10 @@ impl Eth for EthClient where from_params_default_third::(params) .and_then(|(address, position, block_number,)| match block_number { BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))), - BlockNumber::Latest => to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))), - id => to_value(&try!(take_weak!(self.client).storage_at_id(&address, &H256::from(position), id.into()).ok_or_else(make_unsupported_err))), + id => match take_weak!(self.client).storage_at(&address, &H256::from(position), id.into()) { + Some(s) => to_value(&U256::from(s)), + None => Err(make_unsupported_err()), // None is only returned on unsupported requests. + } }) } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 2a59d36df..5f9cf6371 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -901,7 +901,7 @@ impl ChainSync { let chain = io.chain(); let fetch_account = |a: &Address| AccountDetails { nonce: chain.nonce(a), - balance: chain.balance(a), + balance: chain.balance(a, BlockID::Latest).unwrap(), }; let _ = self.miner.import_transactions(transactions, fetch_account); Ok(()) From 3c7e4b8c6cebb9d108fbb4f370d36a806657206a Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:40:29 +0200 Subject: [PATCH 04/16] added nonce, nonce_latest --- ethcore/src/client/client.rs | 8 +++++--- ethcore/src/client/mod.rs | 11 +++++++++-- ethcore/src/client/test_client.rs | 7 +++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index 1b31a7b3b..13d678c14 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -348,7 +348,8 @@ impl Client where V: Verifier { /// Attempt to get a copy of a specific block's state. /// - /// This can fail (but may not) if the DB prunes state. + /// This will not fail if given BlockID::Latest. + /// Otherwise, this can fail (but may not) if the DB prunes state. pub fn state_at(&self, id: BlockID) -> Option { // fast path for latest state. if let BlockID::Latest = id.clone() { @@ -556,10 +557,11 @@ impl BlockChainClient for Client where V: Verifier { Self::block_hash(&self.chain, id).and_then(|hash| self.chain.block_details(&hash)).map(|d| d.total_difficulty) } - fn nonce(&self, address: &Address) -> U256 { - self.state().nonce(address) + fn nonce(&self, address: &Address, id: BlockID) -> Option { + self.state_at(id).map(|s| s.nonce(address)) } + fn block_hash(&self, id: BlockID) -> Option { Self::block_hash(&self.chain, id) } diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 988dac023..3cb4101f7 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -65,8 +65,15 @@ pub trait BlockChainClient : Sync + Send { /// Get block total difficulty. fn block_total_difficulty(&self, id: BlockID) -> Option; - /// Get address nonce. - fn nonce(&self, address: &Address) -> U256; + /// Attempt to get address nonce at given block. + /// May not fail on BlockID::Latest. + fn nonce(&self, address: &Address, id: BlockID) -> Option; + + fn nonce_latest(&self, address: &Address) -> U256 { + self.nonce(address, BlockID::Latest) + .expect("nonce will return Some when given BlockID::Latest. nonce was given BlockID::Latest. \ + Therefore nonce has returned Some; qed") + } /// Get block hash. fn block_hash(&self, id: BlockID) -> Option; diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 3e7608395..de2973029 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -245,8 +245,11 @@ impl BlockChainClient for TestBlockChainClient { Self::block_hash(self, id) } - fn nonce(&self, address: &Address) -> U256 { - self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) + fn nonce(&self, address: &Address, id: BlockID) -> Option { + match id { + BlockID::Latest => Some(self.nonces.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero)), + _ => None, + } } fn code(&self, address: &Address) -> Option { From 5afa4621f9369b007dd306474b2dafe86ebb6cb7 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:45:43 +0200 Subject: [PATCH 05/16] added balance_latest, storage_at_latest utilities with modus ponens panickers --- ethcore/src/client/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 3cb4101f7..39f007281 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -83,14 +83,30 @@ pub trait BlockChainClient : Sync + Send { /// Get address balance at the given block's state. /// + /// May not return None if given BlockID::Latest. /// Returns None if and only if the block's root hash has been pruned from the DB. fn balance(&self, address: &Address, id: BlockID) -> Option; - /// Get value of the storage at given position at the given block. + /// Get address balance at the latest block's state. + fn balance_latest(&self, address: &Address) -> U256 { + self.balance(address, BlockID::Latest) + .expect("balance will return Some if given BlockID::Latest. balance was given BlockID::Latest \ + Therefore balance has returned Some; qed") + } + + /// Get value of the storage at given position at the given block's state. /// + /// May not return None if given BlockID::Latest. /// Returns None if and only if the block's root hash has been pruned from the DB. fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option; + /// Get value of the storage at given position at the latest block's state. + fn storage_at_latest(&self, address: &Address, position: &H256) -> H256 { + self.storage_at(address, position, BlockID::Latest) + .expect("storage_at will return Some if given BlockID::Latest. storage_at was given BlockID::Latest. \ + Therefore storage_at has returned Some; qed") + } + /// Get transaction with given hash. fn transaction(&self, id: TransactionID) -> Option; From a3b1cdb175b1722bda1f09b87e1cfd133876f814 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:52:33 +0200 Subject: [PATCH 06/16] add docs for nonce_latest --- ethcore/src/client/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 39f007281..7ffa0c5d8 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -69,6 +69,7 @@ pub trait BlockChainClient : Sync + Send { /// May not fail on BlockID::Latest. fn nonce(&self, address: &Address, id: BlockID) -> Option; + /// Get address nonce at the latest block's state. fn nonce_latest(&self, address: &Address) -> U256 { self.nonce(address, BlockID::Latest) .expect("nonce will return Some when given BlockID::Latest. nonce was given BlockID::Latest. \ From c2a4ed6fc48bd1e8bab19d89bc1fcfa714f24d3f Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:54:18 +0200 Subject: [PATCH 07/16] change nonce, balance, storage_at to *_latest counterparts --- miner/src/miner.rs | 16 ++++++++-------- sync/src/chain.rs | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/miner/src/miner.rs b/miner/src/miner.rs index efb2f2c77..9fb771bb7 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -167,8 +167,8 @@ impl Miner { }; let mut queue = self.transaction_queue.lock().unwrap(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce(a), - balance: chain.balance(a, BlockID::Latest).unwrap(), + nonce: chain.nonce_latest(a), + balance: chain.balance_latest(a), }; for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); @@ -291,7 +291,7 @@ impl MinerService for Miner { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.balance(address, BlockID::Latest).unwrap(), + || chain.balance_latest(address), |b| b.block().fields().state.balance(address) ) } @@ -299,14 +299,14 @@ impl MinerService for Miner { fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.storage_at(address, position, BlockID::Latest).unwrap(), + || chain.storage_at_latest(address, position), |b| b.block().fields().state.storage_at(address, position) ) } fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.nonce(address), |b| b.block().fields().state.nonce(address)) + sealing_work.peek_last_ref().map_or_else(|| chain.nonce_latest(address), |b| b.block().fields().state.nonce(address)) } fn code(&self, chain: &BlockChainClient, address: &Address) -> Option { @@ -551,8 +551,8 @@ impl MinerService for Miner { let _sender = tx.sender(); } let _ = self.import_transactions(txs, |a| AccountDetails { - nonce: chain.nonce(a), - balance: chain.balance(a, BlockID::Latest).unwrap(), + nonce: chain.nonce_latest(a), + balance: chain.balance_latest(a), }); }); } @@ -572,7 +572,7 @@ impl MinerService for Miner { }) .collect::>(); for sender in to_remove.into_iter() { - transaction_queue.remove_all(sender, chain.nonce(&sender)); + transaction_queue.remove_all(sender, chain.nonce_latest(&sender)); } }); } diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 5f9cf6371..7f8e21e58 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -900,8 +900,8 @@ impl ChainSync { } let chain = io.chain(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce(a), - balance: chain.balance(a, BlockID::Latest).unwrap(), + nonce: chain.nonce_latest(a), + balance: chain.balance_latest(a), }; let _ = self.miner.import_transactions(transactions, fetch_account); Ok(()) From 30eee767676a6819410b48aeeeac97defb5aefa3 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 12:57:15 +0200 Subject: [PATCH 08/16] use new nonce function in eth_TransactionCount --- rpc/src/v1/impls/eth.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 2aa5ff3f2..0d9f7211d 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -164,7 +164,7 @@ impl EthClient where .or_else(|| miner .last_nonce(&request.from) .map(|nonce| nonce + U256::one())) - .unwrap_or_else(|| client.nonce(&request.from)), + .unwrap_or_else(|| client.nonce_latest(&request.from)), 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()), @@ -181,7 +181,7 @@ impl EthClient where let miner = take_weak!(self.miner); let from = request.from.unwrap_or(Address::zero()); Ok(EthTransaction { - nonce: request.nonce.unwrap_or_else(|| client.nonce(&from)), + nonce: request.nonce.unwrap_or_else(|| client.nonce_latest(&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(|| miner.sensible_gas_price()), @@ -199,8 +199,8 @@ impl EthClient where miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { AccountDetails { - nonce: client.nonce(&a), - balance: client.balance(&a, BlockID::Latest).unwrap(), + nonce: client.nonce_latest(&a), + balance: client.balance_latest(&a), } }) }; @@ -372,8 +372,7 @@ impl Eth for EthClient where from_params_default_second(params) .and_then(|(address, block_number,)| match block_number { BlockNumber::Pending => to_value(&take_weak!(self.miner).nonce(take_weak!(self.client).deref(), &address)), - BlockNumber::Latest => to_value(&take_weak!(self.client).nonce(&address)), - _ => Err(Error::invalid_params()), + id => to_value(&take_weak!(self.client).nonce(&address, id.into())), }) } From 3f8936263079da2cccb540f609ea0db352a596de Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 22:17:55 +0200 Subject: [PATCH 09/16] rename x_latest to latest_x in BlockChainClient --- ethcore/src/client/mod.rs | 6 +++--- miner/src/miner.rs | 16 ++++++++-------- rpc/src/v1/impls/eth.rs | 8 ++++---- sync/src/chain.rs | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index 7ffa0c5d8..8e0a7b2dd 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -70,7 +70,7 @@ pub trait BlockChainClient : Sync + Send { fn nonce(&self, address: &Address, id: BlockID) -> Option; /// Get address nonce at the latest block's state. - fn nonce_latest(&self, address: &Address) -> U256 { + fn latest_nonce(&self, address: &Address) -> U256 { self.nonce(address, BlockID::Latest) .expect("nonce will return Some when given BlockID::Latest. nonce was given BlockID::Latest. \ Therefore nonce has returned Some; qed") @@ -89,7 +89,7 @@ pub trait BlockChainClient : Sync + Send { fn balance(&self, address: &Address, id: BlockID) -> Option; /// Get address balance at the latest block's state. - fn balance_latest(&self, address: &Address) -> U256 { + fn latest_balance(&self, address: &Address) -> U256 { self.balance(address, BlockID::Latest) .expect("balance will return Some if given BlockID::Latest. balance was given BlockID::Latest \ Therefore balance has returned Some; qed") @@ -102,7 +102,7 @@ pub trait BlockChainClient : Sync + Send { fn storage_at(&self, address: &Address, position: &H256, id: BlockID) -> Option; /// Get value of the storage at given position at the latest block's state. - fn storage_at_latest(&self, address: &Address, position: &H256) -> H256 { + fn latest_storage_at(&self, address: &Address, position: &H256) -> H256 { self.storage_at(address, position, BlockID::Latest) .expect("storage_at will return Some if given BlockID::Latest. storage_at was given BlockID::Latest. \ Therefore storage_at has returned Some; qed") diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 9fb771bb7..3860a79e6 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -167,8 +167,8 @@ impl Miner { }; let mut queue = self.transaction_queue.lock().unwrap(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce_latest(a), - balance: chain.balance_latest(a), + nonce: chain.latest_nonce(a), + balance: chain.latest_balance(a), }; for hash in invalid_transactions.into_iter() { queue.remove_invalid(&hash, &fetch_account); @@ -291,7 +291,7 @@ impl MinerService for Miner { fn balance(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.balance_latest(address), + || chain.latest_balance(address), |b| b.block().fields().state.balance(address) ) } @@ -299,14 +299,14 @@ impl MinerService for Miner { fn storage_at(&self, chain: &BlockChainClient, address: &Address, position: &H256) -> H256 { let sealing_work = self.sealing_work.lock().unwrap(); sealing_work.peek_last_ref().map_or_else( - || chain.storage_at_latest(address, position), + || chain.latest_storage_at(address, position), |b| b.block().fields().state.storage_at(address, position) ) } fn nonce(&self, chain: &BlockChainClient, address: &Address) -> U256 { let sealing_work = self.sealing_work.lock().unwrap(); - sealing_work.peek_last_ref().map_or_else(|| chain.nonce_latest(address), |b| b.block().fields().state.nonce(address)) + sealing_work.peek_last_ref().map_or_else(|| chain.latest_nonce(address), |b| b.block().fields().state.nonce(address)) } fn code(&self, chain: &BlockChainClient, address: &Address) -> Option { @@ -551,8 +551,8 @@ impl MinerService for Miner { let _sender = tx.sender(); } let _ = self.import_transactions(txs, |a| AccountDetails { - nonce: chain.nonce_latest(a), - balance: chain.balance_latest(a), + nonce: chain.latest_nonce(a), + balance: chain.latest_balance(a), }); }); } @@ -572,7 +572,7 @@ impl MinerService for Miner { }) .collect::>(); for sender in to_remove.into_iter() { - transaction_queue.remove_all(sender, chain.nonce_latest(&sender)); + transaction_queue.remove_all(sender, chain.latest_nonce(&sender)); } }); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 0d9f7211d..7b7daeab9 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -164,7 +164,7 @@ impl EthClient where .or_else(|| miner .last_nonce(&request.from) .map(|nonce| nonce + U256::one())) - .unwrap_or_else(|| client.nonce_latest(&request.from)), + .unwrap_or_else(|| client.latest_nonce(&request.from)), 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()), @@ -181,7 +181,7 @@ impl EthClient where let miner = take_weak!(self.miner); let from = request.from.unwrap_or(Address::zero()); Ok(EthTransaction { - nonce: request.nonce.unwrap_or_else(|| client.nonce_latest(&from)), + 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(|| miner.sensible_gas_price()), @@ -199,8 +199,8 @@ impl EthClient where miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { AccountDetails { - nonce: client.nonce_latest(&a), - balance: client.balance_latest(&a), + nonce: client.latest_nonce(&a), + balance: client.latest_balance(&a), } }) }; diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 7f8e21e58..e66fce0d5 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -900,8 +900,8 @@ impl ChainSync { } let chain = io.chain(); let fetch_account = |a: &Address| AccountDetails { - nonce: chain.nonce_latest(a), - balance: chain.balance_latest(a), + nonce: chain.latest_nonce(a), + balance: chain.latest_balance(a), }; let _ = self.miner.import_transactions(transactions, fetch_account); Ok(()) From a272f8570c9aba7de1f5e889756665386dc832ce Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 27 May 2016 16:25:06 +0200 Subject: [PATCH 10/16] correct indentation --- rpc/src/v1/impls/eth.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 7b7daeab9..2225cbc61 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -262,9 +262,9 @@ const UNSUPPORTED_REQUEST_CODE: i64 = -32000; fn make_unsupported_err() -> Error { Error { - code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE), - message: "Unsupported request.".into(), - data: None + code: ErrorCode::ServerError(UNSUPPORTED_REQUEST_CODE), + message: "Unsupported request.".into(), + data: None } } @@ -362,8 +362,8 @@ impl Eth for EthClient where .and_then(|(address, position, block_number,)| match block_number { BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)))), id => match take_weak!(self.client).storage_at(&address, &H256::from(position), id.into()) { - Some(s) => to_value(&U256::from(s)), - None => Err(make_unsupported_err()), // None is only returned on unsupported requests. + Some(s) => to_value(&U256::from(s)), + None => Err(make_unsupported_err()), // None is only returned on unsupported requests. } }) } From db2efe848561de43454adcea52991778cbf6f882 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 20:07:47 +0200 Subject: [PATCH 11/16] move signAndSendTransaction to Personal trait. --- rpc/src/v1/traits/eth.rs | 4 ---- rpc/src/v1/traits/personal.rs | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rpc/src/v1/traits/eth.rs b/rpc/src/v1/traits/eth.rs index 9cded5eb9..a28f72c5c 100644 --- a/rpc/src/v1/traits/eth.rs +++ b/rpc/src/v1/traits/eth.rs @@ -80,9 +80,6 @@ pub trait Eth: Sized + Send + Sync + 'static { /// Sends transaction. fn send_transaction(&self, _: Params) -> Result { rpc_unimplemented!() } - /// Sends transaction and signs it in single call. The account is not unlocked in such case. - fn sign_and_send_transaction(&self, _: Params) -> Result { rpc_unimplemented!() } - /// Sends signed transaction. fn send_raw_transaction(&self, _: Params) -> Result { rpc_unimplemented!() } @@ -155,7 +152,6 @@ pub trait Eth: Sized + Send + Sync + 'static { delegate.add_method("eth_getCode", Eth::code_at); delegate.add_method("eth_sign", Eth::sign); delegate.add_method("eth_sendTransaction", Eth::send_transaction); - delegate.add_method("eth_signAndSendTransaction", Eth::sign_and_send_transaction); delegate.add_method("eth_sendRawTransaction", Eth::send_raw_transaction); delegate.add_method("eth_call", Eth::call); delegate.add_method("eth_estimateGas", Eth::estimate_gas); diff --git a/rpc/src/v1/traits/personal.rs b/rpc/src/v1/traits/personal.rs index 0cf72951c..0619d7ada 100644 --- a/rpc/src/v1/traits/personal.rs +++ b/rpc/src/v1/traits/personal.rs @@ -30,12 +30,16 @@ pub trait Personal: Sized + Send + Sync + 'static { /// Unlocks specified account for use (can only be one unlocked account at one moment) fn unlock_account(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Sends transaction and signs it in single call. The account is not unlocked in such case. + fn sign_and_send_transaction(&self, _: Params) -> Result { rpc_unimplemented!() } + /// Should be used to convert object to io delegate. fn to_delegate(self) -> IoDelegate { let mut delegate = IoDelegate::new(Arc::new(self)); delegate.add_method("personal_listAccounts", Personal::accounts); delegate.add_method("personal_newAccount", Personal::new_account); delegate.add_method("personal_unlockAccount", Personal::unlock_account); + delegate.add_method("personal_signAndSendTransaction", Personal::sign_and_send_transaction); delegate } } From c9efb56e19601d35ddd6e815a438de19dca6dc09 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 20:09:10 +0200 Subject: [PATCH 12/16] move sign_and_send_transaction implementation to personal --- rpc/src/v1/impls/eth.rs | 13 +----- rpc/src/v1/impls/personal.rs | 79 +++++++++++++++++++++++++++++++++--- rpc/src/v1/types/bytes.rs | 2 +- 3 files changed, 76 insertions(+), 18 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 2225cbc61..bfa1b75ec 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -197,7 +197,7 @@ impl EthClient where let client = take_weak!(self.client); let miner = take_weak!(self.miner); - miner.import_own_transaction(client.deref(), signed_transaction, |a: &Address| { + miner.import_own_transaction(&*client, signed_transaction, |a: &Address| { AccountDetails { nonce: client.latest_nonce(&a), balance: client.latest_balance(&a), @@ -550,17 +550,6 @@ impl Eth for EthClient where }) } - fn sign_and_send_transaction(&self, params: Params) -> Result { - from_params::<(TransactionRequest, String)>(params) - .and_then(|(request, password)| { - let accounts = take_weak!(self.accounts); - match accounts.locked_account_secret(&request.from, &password) { - Ok(secret) => self.sign_and_dispatch(request, secret), - Err(_) => to_value(&H256::zero()), - } - }) - } - fn send_raw_transaction(&self, params: Params) -> Result { from_params::<(Bytes, )>(params) .and_then(|(raw_transaction, )| { diff --git a/rpc/src/v1/impls/personal.rs b/rpc/src/v1/impls/personal.rs index 9fec1af6b..4f2bae3a3 100644 --- a/rpc/src/v1/impls/personal.rs +++ b/rpc/src/v1/impls/personal.rs @@ -18,24 +18,82 @@ use std::sync::{Arc, Weak}; use jsonrpc_core::*; use v1::traits::Personal; +use v1::types::TransactionRequest; +use util::bytes::ToPretty; use util::keys::store::*; -use util::Address; +use util::numbers::*; +use util::rlp::encode; +use ethcore::client::BlockChainClient; +use ethcore::transaction::{Action, SignedTransaction, Transaction as EthTransaction}; +use ethminer::{AccountDetails, MinerService}; /// Account management (personal) rpc implementation. -pub struct PersonalClient where A: AccountProvider { +pub struct PersonalClient + where A: AccountProvider, C: BlockChainClient, M: MinerService { accounts: Weak, + client: Weak, + miner: Weak, } -impl PersonalClient where A: AccountProvider { +impl PersonalClient + where A: AccountProvider, C: BlockChainClient, M: MinerService { /// Creates new PersonalClient - pub fn new(store: &Arc) -> Self { + pub fn new(store: &Arc, client: &Arc, miner: &Arc) -> Self { PersonalClient { accounts: Arc::downgrade(store), + client: Arc::downgrade(client), + miner: Arc::downgrade(miner), } } + + fn dispatch_transaction(&self, signed_transaction: SignedTransaction) -> Result { + let hash = signed_transaction.hash(); + + let import = { + let client = take_weak!(self.client); + let miner = take_weak!(self.miner); + + miner.import_own_transaction(&*client, signed_transaction, |a: &Address| { + AccountDetails { + nonce: client.nonce(&a), + balance: client.balance(&a), + } + }) + }; + + match import { + Ok(_) => to_value(&hash), + Err(e) => { + warn!("Error sending transaction: {:?}", e); + to_value(&H256::zero()) + } + } + } + + fn sign_and_dispatch(&self, request: TransactionRequest, secret: H256) -> Result { + let signed_transaction = { + let client = take_weak!(self.client); + let miner = take_weak!(self.miner); + EthTransaction { + nonce: request.nonce + .or_else(|| miner + .last_nonce(&request.from) + .map(|nonce| nonce + U256::one())) + .unwrap_or_else(|| client.nonce(&request.from)), + 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()), + value: request.value.unwrap_or_else(U256::zero), + data: request.data.map_or_else(Vec::new, |b| b.to_vec()), + }.sign(&secret) + }; + trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty()); + self.dispatch_transaction(signed_transaction) + } } -impl Personal for PersonalClient where A: AccountProvider + 'static { +impl Personal for PersonalClient + where A: AccountProvider, C: BlockChainClient, M: MinerService { fn accounts(&self, _: Params) -> Result { let store = take_weak!(self.accounts); match store.accounts() { @@ -66,4 +124,15 @@ impl Personal for PersonalClient where A: AccountProvider + 'static { } }) } + + fn sign_and_send_transaction(&self, params: Params) -> Result { + from_params::<(TransactionRequest, String)>(params) + .and_then(|(request, password)| { + let accounts = take_weak!(self.accounts); + match accounts.locked_account_secret(&request.from, &password) { + Ok(secret) => self.sign_and_dispatch(request, secret), + Err(_) => to_value(&H256::zero()), + } + }) + } } diff --git a/rpc/src/v1/types/bytes.rs b/rpc/src/v1/types/bytes.rs index 4febacec9..0dc2d3b31 100644 --- a/rpc/src/v1/types/bytes.rs +++ b/rpc/src/v1/types/bytes.rs @@ -28,7 +28,7 @@ impl Bytes { pub fn new(bytes: Vec) -> Bytes { Bytes(bytes) } - pub fn to_vec(self) -> Vec { let Bytes(x) = self; x } + pub fn to_vec(self) -> Vec { self.0 } } impl Serialize for Bytes { From 194ca19720939392a77b8e7677f8c9d418ec83b3 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 20:10:15 +0200 Subject: [PATCH 13/16] move tests to personal --- rpc/src/v1/tests/eth.rs | 0 rpc/src/v1/tests/mocked/eth.rs | 75 ---------------- rpc/src/v1/tests/mocked/personal.rs | 129 +++++++++++++++++++++++++--- rpc/src/v1/tests/mod.rs | 4 +- 4 files changed, 119 insertions(+), 89 deletions(-) delete mode 100644 rpc/src/v1/tests/eth.rs diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs deleted file mode 100644 index e69de29bb..000000000 diff --git a/rpc/src/v1/tests/mocked/eth.rs b/rpc/src/v1/tests/mocked/eth.rs index d52fc9f4c..32a2cd99a 100644 --- a/rpc/src/v1/tests/mocked/eth.rs +++ b/rpc/src/v1/tests/mocked/eth.rs @@ -521,81 +521,6 @@ fn rpc_eth_send_transaction() { assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); } -#[test] -fn rpc_eth_sign_and_send_transaction_with_invalid_password() { - let account = TestAccount::new("password123"); - let address = account.address(); - - let tester = EthTester::default(); - tester.accounts_provider.accounts.write().unwrap().insert(address.clone(), account); - let request = r#"{ - "jsonrpc": "2.0", - "method": "eth_signAndSendTransaction", - "params": [{ - "from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a" - }, "password321"], - "id": 1 - }"#; - - let response = r#"{"jsonrpc":"2.0","result":"0x0000000000000000000000000000000000000000000000000000000000000000","id":1}"#; - - assert_eq!(tester.io.handle_request(request.as_ref()), Some(response.into())); -} - -#[test] -fn rpc_eth_sign_and_send_transaction() { - let account = TestAccount::new("password123"); - let address = account.address(); - let secret = account.secret.clone(); - - let tester = EthTester::default(); - tester.accounts_provider.accounts.write().unwrap().insert(address.clone(), account); - let request = r#"{ - "jsonrpc": "2.0", - "method": "eth_signAndSendTransaction", - "params": [{ - "from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", - "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567", - "gas": "0x76c0", - "gasPrice": "0x9184e72a000", - "value": "0x9184e72a" - }, "password123"], - "id": 1 - }"#; - - let t = Transaction { - nonce: U256::zero(), - gas_price: U256::from(0x9184e72a000u64), - gas: U256::from(0x76c0), - action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()), - value: U256::from(0x9184e72au64), - data: vec![] - }.sign(&secret); - - let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#; - - assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); - - tester.miner.last_nonces.write().unwrap().insert(address.clone(), U256::zero()); - - let t = Transaction { - nonce: U256::one(), - gas_price: U256::from(0x9184e72a000u64), - gas: U256::from(0x76c0), - action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()), - value: U256::from(0x9184e72au64), - data: vec![] - }.sign(&secret); - - let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#; - - assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); -} - #[test] #[ignore] fn rpc_eth_send_raw_transaction() { diff --git a/rpc/src/v1/tests/mocked/personal.rs b/rpc/src/v1/tests/mocked/personal.rs index 10cac9790..991b13cba 100644 --- a/rpc/src/v1/tests/mocked/personal.rs +++ b/rpc/src/v1/tests/mocked/personal.rs @@ -15,11 +15,29 @@ // along with Parity. If not, see . use std::sync::Arc; +use std::str::FromStr; +use std::collections::HashMap; use jsonrpc_core::IoHandler; use util::numbers::*; use util::keys::{TestAccount, TestAccountProvider}; use v1::{PersonalClient, Personal}; -use std::collections::*; +use v1::tests::helpers::TestMinerService; +use ethcore::client::TestBlockChainClient; +use ethcore::transaction::{Action, Transaction}; + +struct PersonalTester { + accounts: Arc, + io: IoHandler, + miner: Arc, + // these unused fields are necessary to keep the data alive + // as the handler has only weak pointers. + _client: Arc, +} + +fn blockchain_client() -> Arc { + let client = TestBlockChainClient::new(); + Arc::new(client) +} fn accounts_provider() -> Arc { let accounts = HashMap::new(); @@ -27,18 +45,33 @@ fn accounts_provider() -> Arc { Arc::new(ap) } -fn setup() -> (Arc, IoHandler) { - let test_provider = accounts_provider(); - let personal = PersonalClient::new(&test_provider); +fn miner_service() -> Arc { + Arc::new(TestMinerService::default()) +} + +fn setup() -> PersonalTester { + let accounts = accounts_provider(); + let client = blockchain_client(); + let miner = miner_service(); + let personal = PersonalClient::new(&accounts, &client, &miner); + let io = IoHandler::new(); io.add_delegate(personal.to_delegate()); - (test_provider, io) + + let tester = PersonalTester { + accounts: accounts, + io: io, + miner: miner, + _client: client, + }; + + tester } #[test] fn accounts() { - let (test_provider, io) = setup(); - test_provider.accounts + let tester = setup(); + tester.accounts.accounts .write() .unwrap() .insert(Address::from(1), TestAccount::new("test")); @@ -46,17 +79,17 @@ fn accounts() { let request = r#"{"jsonrpc": "2.0", "method": "personal_listAccounts", "params": [], "id": 1}"#; let response = r#"{"jsonrpc":"2.0","result":["0x0000000000000000000000000000000000000001"],"id":1}"#; - assert_eq!(io.handle_request(request), Some(response.to_owned())); + assert_eq!(tester.io.handle_request(request), Some(response.to_owned())); } #[test] fn new_account() { - let (test_provider, io) = setup(); + let tester = setup(); let request = r#"{"jsonrpc": "2.0", "method": "personal_newAccount", "params": ["pass"], "id": 1}"#; - let res = io.handle_request(request); + let res = tester.io.handle_request(request); - let accounts = test_provider.accounts.read().unwrap(); + let accounts = tester.accounts.accounts.read().unwrap(); assert_eq!(accounts.len(), 1); let address = accounts @@ -70,3 +103,77 @@ fn new_account() { assert_eq!(res, Some(response)); } +#[test] +fn sign_and_send_transaction_with_invalid_password() { + let account = TestAccount::new("password123"); + let address = account.address(); + + let tester = setup(); + tester.accounts.accounts.write().unwrap().insert(address.clone(), account); + let request = r#"{ + "jsonrpc": "2.0", + "method": "personal_signAndSendTransaction", + "params": [{ + "from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", + "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567", + "gas": "0x76c0", + "gasPrice": "0x9184e72a000", + "value": "0x9184e72a" + }, "password321"], + "id": 1 + }"#; + + let response = r#"{"jsonrpc":"2.0","result":"0x0000000000000000000000000000000000000000000000000000000000000000","id":1}"#; + + assert_eq!(tester.io.handle_request(request.as_ref()), Some(response.into())); +} + +#[test] +fn sign_and_send_transaction() { + let account = TestAccount::new("password123"); + let address = account.address(); + let secret = account.secret.clone(); + + let tester = setup(); + tester.accounts.accounts.write().unwrap().insert(address.clone(), account); + let request = r#"{ + "jsonrpc": "2.0", + "method": "personal_signAndSendTransaction", + "params": [{ + "from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", + "to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567", + "gas": "0x76c0", + "gasPrice": "0x9184e72a000", + "value": "0x9184e72a" + }, "password123"], + "id": 1 + }"#; + + let t = Transaction { + nonce: U256::zero(), + gas_price: U256::from(0x9184e72a000u64), + gas: U256::from(0x76c0), + action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()), + value: U256::from(0x9184e72au64), + data: vec![] + }.sign(&secret); + + let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#; + + assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); + + tester.miner.last_nonces.write().unwrap().insert(address.clone(), U256::zero()); + + let t = Transaction { + nonce: U256::one(), + gas_price: U256::from(0x9184e72a000u64), + gas: U256::from(0x76c0), + action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()), + value: U256::from(0x9184e72au64), + data: vec![] + }.sign(&secret); + + let response = r#"{"jsonrpc":"2.0","result":""#.to_owned() + format!("0x{:?}", t.hash()).as_ref() + r#"","id":1}"#; + + assert_eq!(tester.io.handle_request(request.as_ref()), Some(response)); +} \ No newline at end of file diff --git a/rpc/src/v1/tests/mod.rs b/rpc/src/v1/tests/mod.rs index f5e7d1404..d3519eb1e 100644 --- a/rpc/src/v1/tests/mod.rs +++ b/rpc/src/v1/tests/mod.rs @@ -3,6 +3,4 @@ pub mod helpers; #[cfg(test)] -mod mocked; -#[cfg(test)] -mod eth; \ No newline at end of file +mod mocked; \ No newline at end of file From ba600ac06a50db56894b93c5b3ae7d720b12a63f Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 20:18:14 +0200 Subject: [PATCH 14/16] have parity create the PersonalClient properly --- parity/dapps.rs | 4 ++-- parity/rpc.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/parity/dapps.rs b/parity/dapps.rs index 1646d7f35..986e3dd07 100644 --- a/parity/dapps.rs +++ b/parity/dapps.rs @@ -22,7 +22,7 @@ use ethsync::EthSync; use ethminer::{Miner, ExternalMiner}; use util::RotatingLogger; use util::panics::PanicHandler; -use util::keys::store::{AccountService}; +use util::keys::store::AccountService; use util::network_settings::NetworkSettings; use die::*; @@ -100,7 +100,7 @@ pub fn setup_dapps_server( server.add_delegate(NetClient::new(&deps.sync).to_delegate()); server.add_delegate(EthClient::new(&deps.client, &deps.sync, &deps.secret_store, &deps.miner, &deps.external_miner).to_delegate()); server.add_delegate(EthFilterClient::new(&deps.client, &deps.miner).to_delegate()); - server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate()); + server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate()); server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate()); let start_result = match auth { diff --git a/parity/rpc.rs b/parity/rpc.rs index e1782e9b6..60766263b 100644 --- a/parity/rpc.rs +++ b/parity/rpc.rs @@ -24,7 +24,7 @@ use ethsync::EthSync; use ethminer::{Miner, ExternalMiner}; use util::RotatingLogger; use util::panics::PanicHandler; -use util::keys::store::{AccountService}; +use util::keys::store::AccountService; use util::network_settings::NetworkSettings; use die::*; use jsonipc; @@ -106,7 +106,7 @@ fn setup_rpc_server(apis: Vec<&str>, deps: &Arc) -> Server { }, "personal" => { modules.insert("personal".to_owned(), "1.0".to_owned()); - server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate()) + server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner).to_delegate()) }, "ethcore" => { modules.insert("ethcore".to_owned(), "1.0".to_owned()); From b28a8411a4e627d7244cdd1d72747837a283ff5e Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 26 May 2016 21:14:01 +0200 Subject: [PATCH 15/16] refactor dispatch_transaction and sign_and_dispatch into impls module this has the added benefit of allowing the removal of redundant upgrades. --- rpc/src/v1/impls/eth.rs | 53 +++------------------------------- rpc/src/v1/impls/mod.rs | 56 ++++++++++++++++++++++++++++++++++++ rpc/src/v1/impls/personal.rs | 53 ++-------------------------------- 3 files changed, 63 insertions(+), 99 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index bfa1b75ec..7627dd61e 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -22,11 +22,10 @@ use std::collections::HashSet; use std::sync::{Arc, Weak, Mutex}; use std::ops::Deref; use ethsync::{SyncProvider, SyncState}; -use ethminer::{MinerService, AccountDetails, ExternalMinerService}; +use ethminer::{MinerService, ExternalMinerService}; use jsonrpc_core::*; use util::numbers::*; use util::sha3::*; -use util::bytes::ToPretty; use util::rlp::{encode, decode, UntrustedRlp, View}; use ethcore::client::{BlockChainClient, BlockID, TransactionID, UncleID}; use ethcore::block::IsBlock; @@ -39,6 +38,7 @@ use self::ethash::SeedHashCompute; use v1::traits::{Eth, EthFilter}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, CallRequest, OptionalValue, Index, Filter, Log, Receipt}; use v1::helpers::{PollFilter, PollManager}; +use v1::impls::{dispatch_transaction, sign_and_dispatch}; use util::keys::store::AccountProvider; use serde; @@ -155,27 +155,6 @@ impl EthClient where } } - fn sign_and_dispatch(&self, request: TransactionRequest, secret: H256) -> Result { - let signed_transaction = { - let client = take_weak!(self.client); - let miner = take_weak!(self.miner); - EthTransaction { - nonce: request.nonce - .or_else(|| miner - .last_nonce(&request.from) - .map(|nonce| nonce + U256::one())) - .unwrap_or_else(|| client.latest_nonce(&request.from)), - 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()), - value: request.value.unwrap_or_else(U256::zero), - data: request.data.map_or_else(Vec::new, |d| d.to_vec()), - }.sign(&secret) - }; - trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty()); - self.dispatch_transaction(signed_transaction) - } - fn sign_call(&self, request: CallRequest) -> Result { let client = take_weak!(self.client); let miner = take_weak!(self.miner); @@ -189,30 +168,6 @@ impl EthClient where data: request.data.map_or_else(Vec::new, |d| d.to_vec()) }.fake_sign(from)) } - - fn dispatch_transaction(&self, signed_transaction: SignedTransaction) -> Result { - let hash = signed_transaction.hash(); - - let import = { - let client = take_weak!(self.client); - let miner = take_weak!(self.miner); - - miner.import_own_transaction(&*client, signed_transaction, |a: &Address| { - AccountDetails { - nonce: client.latest_nonce(&a), - balance: client.latest_balance(&a), - } - }) - }; - - match import { - Ok(_) => to_value(&hash), - Err(e) => { - warn!("Error sending transaction: {:?}", e); - to_value(&H256::zero()) - } - } - } } const MAX_QUEUE_SIZE_TO_MINE_ON: usize = 4; // because uncles go back 6. @@ -544,7 +499,7 @@ impl Eth for EthClient where .and_then(|(request, )| { let accounts = take_weak!(self.accounts); match accounts.account_secret(&request.from) { - Ok(secret) => self.sign_and_dispatch(request, secret), + Ok(secret) => sign_and_dispatch(&self.client, &self.miner, request, secret), Err(_) => to_value(&H256::zero()) } }) @@ -555,7 +510,7 @@ impl Eth for EthClient where .and_then(|(raw_transaction, )| { let raw_transaction = raw_transaction.to_vec(); match UntrustedRlp::new(&raw_transaction).as_val() { - Ok(signed_transaction) => self.dispatch_transaction(signed_transaction), + Ok(signed_transaction) => dispatch_transaction(&*take_weak!(self.client), &*take_weak!(self.miner), signed_transaction), Err(_) => to_value(&H256::zero()), } }) diff --git a/rpc/src/v1/impls/mod.rs b/rpc/src/v1/impls/mod.rs index 582d61f03..b79772103 100644 --- a/rpc/src/v1/impls/mod.rs +++ b/rpc/src/v1/impls/mod.rs @@ -41,3 +41,59 @@ pub use self::ethcore::EthcoreClient; pub use self::traces::TracesClient; pub use self::rpc::RpcClient; +use v1::types::TransactionRequest; +use std::sync::Weak; +use ethminer::{AccountDetails, MinerService}; +use ethcore::client::BlockChainClient; +use ethcore::transaction::{Action, SignedTransaction, Transaction}; +use util::numbers::*; +use util::rlp::encode; +use util::bytes::ToPretty; +use jsonrpc_core::{Error, to_value, Value}; + +fn dispatch_transaction(client: &C, miner: &M, signed_transaction: SignedTransaction) -> Result + where C: BlockChainClient, M: MinerService { + let hash = signed_transaction.hash(); + + let import = { + miner.import_own_transaction(client, signed_transaction, |a: &Address| { + AccountDetails { + nonce: client.latest_nonce(&a), + balance: client.latest_balance(&a), + } + }) + }; + + match import { + Ok(_) => to_value(&hash), + Err(e) => { + warn!("Error sending transaction: {:?}", e); + to_value(&H256::zero()) + } + } +} + +fn sign_and_dispatch(client: &Weak, miner: &Weak, request: TransactionRequest, secret: H256) -> Result + where C: BlockChainClient, M: MinerService { + let client = take_weak!(client); + let miner = take_weak!(miner); + + let signed_transaction = { + Transaction { + nonce: request.nonce + .or_else(|| miner + .last_nonce(&request.from) + .map(|nonce| nonce + U256::one())) + .unwrap_or_else(|| client.latest_nonce(&request.from)), + + 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()), + value: request.value.unwrap_or_else(U256::zero), + data: request.data.map_or_else(Vec::new, |b| b.to_vec()), + }.sign(&secret) + }; + + trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty()); + dispatch_transaction(&*client, &*miner, signed_transaction) +} \ No newline at end of file diff --git a/rpc/src/v1/impls/personal.rs b/rpc/src/v1/impls/personal.rs index 4f2bae3a3..19e902996 100644 --- a/rpc/src/v1/impls/personal.rs +++ b/rpc/src/v1/impls/personal.rs @@ -19,13 +19,11 @@ use std::sync::{Arc, Weak}; use jsonrpc_core::*; use v1::traits::Personal; use v1::types::TransactionRequest; -use util::bytes::ToPretty; +use v1::impls::sign_and_dispatch; use util::keys::store::*; use util::numbers::*; -use util::rlp::encode; use ethcore::client::BlockChainClient; -use ethcore::transaction::{Action, SignedTransaction, Transaction as EthTransaction}; -use ethminer::{AccountDetails, MinerService}; +use ethminer::MinerService; /// Account management (personal) rpc implementation. pub struct PersonalClient @@ -45,51 +43,6 @@ impl PersonalClient miner: Arc::downgrade(miner), } } - - fn dispatch_transaction(&self, signed_transaction: SignedTransaction) -> Result { - let hash = signed_transaction.hash(); - - let import = { - let client = take_weak!(self.client); - let miner = take_weak!(self.miner); - - miner.import_own_transaction(&*client, signed_transaction, |a: &Address| { - AccountDetails { - nonce: client.nonce(&a), - balance: client.balance(&a), - } - }) - }; - - match import { - Ok(_) => to_value(&hash), - Err(e) => { - warn!("Error sending transaction: {:?}", e); - to_value(&H256::zero()) - } - } - } - - fn sign_and_dispatch(&self, request: TransactionRequest, secret: H256) -> Result { - let signed_transaction = { - let client = take_weak!(self.client); - let miner = take_weak!(self.miner); - EthTransaction { - nonce: request.nonce - .or_else(|| miner - .last_nonce(&request.from) - .map(|nonce| nonce + U256::one())) - .unwrap_or_else(|| client.nonce(&request.from)), - 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()), - value: request.value.unwrap_or_else(U256::zero), - data: request.data.map_or_else(Vec::new, |b| b.to_vec()), - }.sign(&secret) - }; - trace!(target: "miner", "send_transaction: dispatching tx: {}", encode(&signed_transaction).to_vec().pretty()); - self.dispatch_transaction(signed_transaction) - } } impl Personal for PersonalClient @@ -130,7 +83,7 @@ impl Personal for PersonalClient .and_then(|(request, password)| { let accounts = take_weak!(self.accounts); match accounts.locked_account_secret(&request.from, &password) { - Ok(secret) => self.sign_and_dispatch(request, secret), + Ok(secret) => sign_and_dispatch(&self.client, &self.miner, request, secret), Err(_) => to_value(&H256::zero()), } }) From 6f93ecf1d2d1217cdd3c81e1085e453409faafd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Fri, 27 May 2016 15:46:07 +0200 Subject: [PATCH 16/16] Exposing types from RPC --- Cargo.lock | 2 +- parity/signer.rs | 2 +- rpc/Cargo.toml | 1 - rpc/src/lib.rs | 1 - rpc/src/v1/mod.rs | 4 +-- rpc/src/v1/types/block.rs | 23 ++++++++++++ rpc/src/v1/types/block_number.rs | 4 +++ {signer/src => rpc/src/v1}/types/bytes.rs | 0 rpc/src/v1/types/call_request.rs | 8 +++++ rpc/src/v1/types/filter.rs | 11 ++++++ rpc/src/v1/types/index.rs | 1 + rpc/src/v1/types/log.rs | 10 ++++++ rpc/src/v1/types/mod.rs | 2 ++ rpc/src/v1/types/mod.rs.in | 6 ++-- rpc/src/v1/types/optionals.rs | 3 ++ rpc/src/v1/types/receipt.rs | 9 +++++ rpc/src/v1/types/sync.rs | 7 ++++ rpc/src/v1/types/trace.rs | 35 +++++++++++++++++++ rpc/src/v1/types/trace_filter.rs | 5 +++ rpc/src/v1/types/transaction.rs | 12 +++++++ .../src/v1}/types/transaction_request.rs | 4 +-- signer/Cargo.toml | 3 +- signer/src/lib.rs | 2 +- signer/src/signing_queue.rs | 4 +-- signer/src/types/mod.rs.in | 11 ++++-- signer/src/ws_server.rs | 4 +-- 26 files changed, 156 insertions(+), 18 deletions(-) rename {signer/src => rpc/src/v1}/types/bytes.rs (100%) rename {signer/src => rpc/src/v1}/types/transaction_request.rs (98%) diff --git a/Cargo.lock b/Cargo.lock index 66df9192a..6298ef7ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,7 +331,6 @@ dependencies = [ "clippy 0.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "ethash 1.2.0", "ethcore 1.2.0", - "ethcore-signer 1.2.0", "ethcore-util 1.2.0", "ethminer 1.2.0", "ethsync 1.2.0", @@ -353,6 +352,7 @@ version = "1.2.0" dependencies = [ "clippy 0.0.69 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ethcore-rpc 1.2.0", "ethcore-util 1.2.0", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/parity/signer.rs b/parity/signer.rs index e7f7a9cb9..c78ad36ad 100644 --- a/parity/signer.rs +++ b/parity/signer.rs @@ -54,7 +54,7 @@ pub fn start(conf: Configuration, deps: Dependencies) -> Option { } #[cfg(not(feature = "ethcore-signer"))] -pub fn start(conf: Configuration) -> !{ +pub fn start(conf: Configuration) -> Option { if !conf.enabled { return None; } diff --git a/rpc/Cargo.toml b/rpc/Cargo.toml index c8bfa0485..2cdbb0a2b 100644 --- a/rpc/Cargo.toml +++ b/rpc/Cargo.toml @@ -19,7 +19,6 @@ ethcore = { path = "../ethcore" } ethash = { path = "../ethash" } ethsync = { path = "../sync" } ethminer = { path = "../miner" } -ethcore-signer = { path = "../signer" } rustc-serialize = "0.3" transient-hashmap = "0.1" serde_macros = { version = "0.7.0", optional = true } diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index ae824adf2..7d9818615 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -27,7 +27,6 @@ extern crate serde_json; extern crate jsonrpc_core; extern crate jsonrpc_http_server; extern crate ethcore_util as util; -extern crate ethcore_signer as signer; extern crate ethcore; extern crate ethsync; extern crate ethminer; diff --git a/rpc/src/v1/mod.rs b/rpc/src/v1/mod.rs index f3bb450f3..b1ab256c0 100644 --- a/rpc/src/v1/mod.rs +++ b/rpc/src/v1/mod.rs @@ -18,12 +18,12 @@ //! //! Compliant with ethereum rpc. -pub mod traits; mod impls; -mod types; mod helpers; +pub mod traits; pub mod tests; +pub mod types; pub use self::traits::{Web3, Eth, EthFilter, Personal, Net, Ethcore, Traces, Rpc}; pub use self::impls::*; diff --git a/rpc/src/v1/types/block.rs b/rpc/src/v1/types/block.rs index c38949c08..5810c85e5 100644 --- a/rpc/src/v1/types/block.rs +++ b/rpc/src/v1/types/block.rs @@ -18,9 +18,12 @@ use serde::{Serialize, Serializer}; use util::numbers::*; use v1::types::{Bytes, Transaction, OptionalValue}; +/// Block Transactions #[derive(Debug)] pub enum BlockTransactions { + /// Only hashes Hashes(Vec), + /// Full transactions Full(Vec) } @@ -34,38 +37,58 @@ impl Serialize for BlockTransactions { } } +/// Block representation #[derive(Debug, Serialize)] pub struct Block { + /// Hash of the block pub hash: OptionalValue, + /// Hash of the parent #[serde(rename="parentHash")] pub parent_hash: H256, + /// Hash of the uncles #[serde(rename="sha3Uncles")] pub uncles_hash: H256, + /// Authors address pub author: Address, // TODO: get rid of this one + /// ? pub miner: Address, + /// State root hash #[serde(rename="stateRoot")] pub state_root: H256, + /// Transactions root hash #[serde(rename="transactionsRoot")] pub transactions_root: H256, + /// Transactions receipts root hash #[serde(rename="receiptsRoot")] pub receipts_root: H256, + /// Block number pub number: OptionalValue, + /// Gas Used #[serde(rename="gasUsed")] pub gas_used: U256, + /// Gas Limit #[serde(rename="gasLimit")] pub gas_limit: U256, + /// Extra data #[serde(rename="extraData")] pub extra_data: Bytes, + /// Logs bloom #[serde(rename="logsBloom")] pub logs_bloom: H2048, + /// Timestamp pub timestamp: U256, + /// Difficulty pub difficulty: U256, + /// Total difficulty #[serde(rename="totalDifficulty")] pub total_difficulty: U256, + /// Seal fields #[serde(rename="sealFields")] pub seal_fields: Vec, + /// Uncles' hashes pub uncles: Vec, + /// Transactions pub transactions: BlockTransactions } diff --git a/rpc/src/v1/types/block_number.rs b/rpc/src/v1/types/block_number.rs index 071486afd..e2d150c66 100644 --- a/rpc/src/v1/types/block_number.rs +++ b/rpc/src/v1/types/block_number.rs @@ -21,9 +21,13 @@ use ethcore::client::BlockID; /// Represents rpc api block number param. #[derive(Debug, PartialEq, Clone)] pub enum BlockNumber { + /// Number Num(u64), + /// Latest block Latest, + /// Earliest block (genesis) Earliest, + /// Pending block (being mined) Pending } diff --git a/signer/src/types/bytes.rs b/rpc/src/v1/types/bytes.rs similarity index 100% rename from signer/src/types/bytes.rs rename to rpc/src/v1/types/bytes.rs diff --git a/rpc/src/v1/types/call_request.rs b/rpc/src/v1/types/call_request.rs index 045b1cd9b..50ebbd1f0 100644 --- a/rpc/src/v1/types/call_request.rs +++ b/rpc/src/v1/types/call_request.rs @@ -18,15 +18,23 @@ use util::hash::Address; use util::numbers::U256; use v1::types::Bytes; +/// Call request #[derive(Debug, Default, PartialEq, Deserialize)] pub struct CallRequest { + /// From pub from: Option
, + /// To pub to: Option
, + /// Gas Price #[serde(rename="gasPrice")] pub gas_price: Option, + /// Gas pub gas: Option, + /// Value pub value: Option, + /// Data pub data: Option, + /// Nonce pub nonce: Option, } diff --git a/rpc/src/v1/types/filter.rs b/rpc/src/v1/types/filter.rs index e50ec9c32..77a3f0500 100644 --- a/rpc/src/v1/types/filter.rs +++ b/rpc/src/v1/types/filter.rs @@ -22,10 +22,14 @@ use v1::types::BlockNumber; use ethcore::filter::Filter as EthFilter; use ethcore::client::BlockID; +/// Variadic value #[derive(Debug, PartialEq, Clone)] pub enum VariadicValue where T: Deserialize { + /// Single Single(T), + /// List Multiple(Vec), + /// None Null, } @@ -44,17 +48,24 @@ impl Deserialize for VariadicValue where T: Deserialize { } } +/// Filter Address pub type FilterAddress = VariadicValue
; +/// Topic pub type Topic = VariadicValue; +/// Filter #[derive(Debug, PartialEq, Clone, Deserialize)] #[serde(deny_unknown_fields)] pub struct Filter { + /// From Block #[serde(rename="fromBlock")] pub from_block: Option, + /// To Block #[serde(rename="toBlock")] pub to_block: Option, + /// Address pub address: Option, + /// Topics pub topics: Option>, } diff --git a/rpc/src/v1/types/index.rs b/rpc/src/v1/types/index.rs index e7cbbd255..d7b17aea2 100644 --- a/rpc/src/v1/types/index.rs +++ b/rpc/src/v1/types/index.rs @@ -22,6 +22,7 @@ use serde::de::Visitor; pub struct Index(usize); impl Index { + /// Convert to usize pub fn value(&self) -> usize { self.0 } diff --git a/rpc/src/v1/types/log.rs b/rpc/src/v1/types/log.rs index 426ca68f1..72a482d1b 100644 --- a/rpc/src/v1/types/log.rs +++ b/rpc/src/v1/types/log.rs @@ -18,21 +18,31 @@ use util::numbers::*; use ethcore::log_entry::{LocalizedLogEntry, LogEntry}; use v1::types::Bytes; +/// Log #[derive(Debug, Serialize, PartialEq, Eq, Hash, Clone)] pub struct Log { + /// Address pub address: Address, + /// Topics pub topics: Vec, + /// Data pub data: Bytes, + /// Block Hash #[serde(rename="blockHash")] pub block_hash: Option, + /// Block Number #[serde(rename="blockNumber")] pub block_number: Option, + /// Transaction Hash #[serde(rename="transactionHash")] pub transaction_hash: Option, + /// Transaction Index #[serde(rename="transactionIndex")] pub transaction_index: Option, + /// Log Index #[serde(rename="logIndex")] pub log_index: Option, + /// Log Type #[serde(rename="type")] pub log_type: String, } diff --git a/rpc/src/v1/types/mod.rs b/rpc/src/v1/types/mod.rs index adf9be071..c1bc34407 100644 --- a/rpc/src/v1/types/mod.rs +++ b/rpc/src/v1/types/mod.rs @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +//! Structures used in RPC communication + #[cfg(feature = "serde_macros")] include!("mod.rs.in"); diff --git a/rpc/src/v1/types/mod.rs.in b/rpc/src/v1/types/mod.rs.in index 9d7fc882a..824a061ef 100644 --- a/rpc/src/v1/types/mod.rs.in +++ b/rpc/src/v1/types/mod.rs.in @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +mod bytes; mod block; mod block_number; mod filter; @@ -22,13 +23,13 @@ mod log; mod optionals; mod sync; mod transaction; +mod transaction_request; mod call_request; mod receipt; mod trace; mod trace_filter; -pub use signer::types::bytes::Bytes; -pub use signer::types::transaction_request::TransactionRequest; +pub use self::bytes::Bytes; pub use self::block::{Block, BlockTransactions}; pub use self::block_number::BlockNumber; pub use self::filter::Filter; @@ -37,6 +38,7 @@ pub use self::log::Log; pub use self::optionals::OptionalValue; pub use self::sync::{SyncStatus, SyncInfo}; pub use self::transaction::Transaction; +pub use self::transaction_request::TransactionRequest; pub use self::call_request::CallRequest; pub use self::receipt::Receipt; pub use self::trace::Trace; diff --git a/rpc/src/v1/types/optionals.rs b/rpc/src/v1/types/optionals.rs index 5db272251..2ed272ade 100644 --- a/rpc/src/v1/types/optionals.rs +++ b/rpc/src/v1/types/optionals.rs @@ -17,9 +17,12 @@ use serde::{Serialize, Serializer}; use serde_json::Value; +/// Optional value #[derive(Debug)] pub enum OptionalValue where T: Serialize { + /// Some Value(T), + /// None Null } diff --git a/rpc/src/v1/types/receipt.rs b/rpc/src/v1/types/receipt.rs index 51d914e1a..32a7f5945 100644 --- a/rpc/src/v1/types/receipt.rs +++ b/rpc/src/v1/types/receipt.rs @@ -19,22 +19,31 @@ use util::hash::{Address, H256}; use v1::types::Log; use ethcore::receipt::LocalizedReceipt; +/// Receipt #[derive(Debug, Serialize)] pub struct Receipt { + /// Transaction Hash #[serde(rename="transactionHash")] pub transaction_hash: H256, + /// Transaction index #[serde(rename="transactionIndex")] pub transaction_index: U256, + /// Block hash #[serde(rename="blockHash")] pub block_hash: H256, + /// Block number #[serde(rename="blockNumber")] pub block_number: U256, + /// Cumulative gas used #[serde(rename="cumulativeGasUsed")] pub cumulative_gas_used: U256, + /// Gas used #[serde(rename="gasUsed")] pub gas_used: U256, + /// Contract address #[serde(rename="contractAddress")] pub contract_address: Option
, + /// Logs pub logs: Vec, } diff --git a/rpc/src/v1/types/sync.rs b/rpc/src/v1/types/sync.rs index c0e480140..6d750425e 100644 --- a/rpc/src/v1/types/sync.rs +++ b/rpc/src/v1/types/sync.rs @@ -17,19 +17,26 @@ use serde::{Serialize, Serializer}; use util::numbers::*; +/// Sync info #[derive(Default, Debug, Serialize, PartialEq)] pub struct SyncInfo { + /// Starting block #[serde(rename="startingBlock")] pub starting_block: U256, + /// Current block #[serde(rename="currentBlock")] pub current_block: U256, + /// Highest block seen so far #[serde(rename="highestBlock")] pub highest_block: U256, } +/// Sync status #[derive(Debug, PartialEq)] pub enum SyncStatus { + /// Info when syncing Info(SyncInfo), + /// Not syncing None } diff --git a/rpc/src/v1/types/trace.rs b/rpc/src/v1/types/trace.rs index 4cd1ac408..6ea58543a 100644 --- a/rpc/src/v1/types/trace.rs +++ b/rpc/src/v1/types/trace.rs @@ -19,11 +19,16 @@ use ethcore::trace::trace; use ethcore::trace::LocalizedTrace; use v1::types::Bytes; +/// Create response #[derive(Debug, Serialize)] pub struct Create { + /// Sender from: Address, + /// Value value: U256, + /// Gas gas: U256, + /// Initialization code init: Bytes, } @@ -38,12 +43,18 @@ impl From for Create { } } +/// Call response #[derive(Debug, Serialize)] pub struct Call { + /// Sender from: Address, + /// Recipient to: Address, + /// Transfered Value value: U256, + /// Gas gas: U256, + /// Input data input: Bytes, } @@ -59,10 +70,13 @@ impl From for Call { } } +/// Action #[derive(Debug, Serialize)] pub enum Action { + /// Call #[serde(rename="call")] Call(Call), + /// Create #[serde(rename="create")] Create(Create), } @@ -76,10 +90,13 @@ impl From for Action { } } +/// Call Result #[derive(Debug, Serialize)] pub struct CallResult { + /// Gas used #[serde(rename="gasUsed")] gas_used: U256, + /// Output bytes output: Bytes, } @@ -92,11 +109,15 @@ impl From for CallResult { } } +/// Craete Result #[derive(Debug, Serialize)] pub struct CreateResult { + /// Gas used #[serde(rename="gasUsed")] gas_used: U256, + /// Code code: Bytes, + /// Assigned address address: Address, } @@ -110,14 +131,19 @@ impl From for CreateResult { } } +/// Response #[derive(Debug, Serialize)] pub enum Res { + /// Call #[serde(rename="call")] Call(CallResult), + /// Create #[serde(rename="create")] Create(CreateResult), + /// Call failure #[serde(rename="failedCall")] FailedCall, + /// Creation failure #[serde(rename="failedCreate")] FailedCreate, } @@ -133,19 +159,28 @@ impl From for Res { } } +/// Trace #[derive(Debug, Serialize)] pub struct Trace { + /// Action action: Action, + /// Result result: Res, + /// Trace address #[serde(rename="traceAddress")] trace_address: Vec, + /// Subtraces subtraces: U256, + /// Transaction position #[serde(rename="transactionPosition")] transaction_position: U256, + /// Transaction hash #[serde(rename="transactionHash")] transaction_hash: H256, + /// Block Number #[serde(rename="blockNumber")] block_number: U256, + /// Block Hash #[serde(rename="blockHash")] block_hash: H256, } diff --git a/rpc/src/v1/types/trace_filter.rs b/rpc/src/v1/types/trace_filter.rs index 3d45f4c79..ee2f231f0 100644 --- a/rpc/src/v1/types/trace_filter.rs +++ b/rpc/src/v1/types/trace_filter.rs @@ -21,14 +21,19 @@ use ethcore::client::BlockID; use ethcore::client; use super::BlockNumber; +/// Trace filter #[derive(Debug, PartialEq, Deserialize)] pub struct TraceFilter { + /// From block #[serde(rename="fromBlock")] pub from_block: Option, + /// To block #[serde(rename="toBlock")] pub to_block: Option, + /// From address #[serde(rename="fromAddress")] pub from_address: Option>, + /// To address #[serde(rename="toAddress")] pub to_address: Option>, } diff --git a/rpc/src/v1/types/transaction.rs b/rpc/src/v1/types/transaction.rs index 8a46d5e15..1c9a41084 100644 --- a/rpc/src/v1/types/transaction.rs +++ b/rpc/src/v1/types/transaction.rs @@ -18,22 +18,34 @@ use util::numbers::*; use ethcore::transaction::{LocalizedTransaction, Action, SignedTransaction}; use v1::types::{Bytes, OptionalValue}; +/// Transaction #[derive(Debug, Default, Serialize)] pub struct Transaction { + /// Hash pub hash: H256, + /// Nonce pub nonce: U256, + /// Block hash #[serde(rename="blockHash")] pub block_hash: OptionalValue, + /// Block number #[serde(rename="blockNumber")] pub block_number: OptionalValue, + /// Transaction Index #[serde(rename="transactionIndex")] pub transaction_index: OptionalValue, + /// Sender pub from: Address, + /// Recipient pub to: OptionalValue
, + /// Transfered value pub value: U256, + /// Gas Price #[serde(rename="gasPrice")] pub gas_price: U256, + /// Gas pub gas: U256, + /// Data pub input: Bytes } diff --git a/signer/src/types/transaction_request.rs b/rpc/src/v1/types/transaction_request.rs similarity index 98% rename from signer/src/types/transaction_request.rs rename to rpc/src/v1/types/transaction_request.rs index 83840515b..1b51e6b12 100644 --- a/signer/src/types/transaction_request.rs +++ b/rpc/src/v1/types/transaction_request.rs @@ -18,7 +18,7 @@ use util::hash::Address; use util::numbers::U256; -use types::bytes::Bytes; +use v1::types::bytes::Bytes; /// Transaction request coming from RPC #[derive(Debug, Clone, Default, Eq, PartialEq, Hash, Deserialize)] @@ -47,7 +47,7 @@ mod tests { use serde_json; use util::numbers::{U256}; use util::hash::Address; - use types::bytes::Bytes; + use v1::types::bytes::Bytes; use super::*; #[test] diff --git a/signer/Cargo.toml b/signer/Cargo.toml index be77a3fd9..59c7f90b2 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -16,10 +16,11 @@ syntex = "^0.32.0" serde = "0.7.0" serde_json = "0.7.0" rustc-serialize = "0.3" -ethcore-util = { path = "../util" } log = "0.3" env_logger = "0.3" ws = "0.4.7" +ethcore-util = { path = "../util" } +ethcore-rpc = { path = "../rpc" } serde_macros = { version = "0.7.0", optional = true } clippy = { version = "0.0.69", optional = true} diff --git a/signer/src/lib.rs b/signer/src/lib.rs index 13f8d2fa6..4317338e0 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -49,11 +49,11 @@ extern crate serde_json; extern crate rustc_serialize; extern crate ethcore_util as util; +extern crate ethcore_rpc as rpc; extern crate ws; mod signing_queue; mod ws_server; -pub mod types; pub use ws_server::*; diff --git a/signer/src/signing_queue.rs b/signer/src/signing_queue.rs index 7758f5df3..611d467c2 100644 --- a/signer/src/signing_queue.rs +++ b/signer/src/signing_queue.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use std::collections::HashSet; -use types::transaction_request::TransactionRequest; +use rpc::v1::types::TransactionRequest; pub trait SigningQueue { fn add_request(&mut self, transaction: TransactionRequest); @@ -45,7 +45,7 @@ mod test { use std::collections::HashSet; use util::hash::Address; use util::numbers::U256; - use types::transaction_request::TransactionRequest; + use rpc::v1::types::TransactionRequest; use super::*; #[test] diff --git a/signer/src/types/mod.rs.in b/signer/src/types/mod.rs.in index 986e3d6e2..a59f81ece 100644 --- a/signer/src/types/mod.rs.in +++ b/signer/src/types/mod.rs.in @@ -14,5 +14,12 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -pub mod transaction_request; -pub mod bytes; + + + + + + + + +// TODO [ToDr] Types are empty for now. But they are about to come. diff --git a/signer/src/ws_server.rs b/signer/src/ws_server.rs index 9be392e69..d2ab02d66 100644 --- a/signer/src/ws_server.rs +++ b/signer/src/ws_server.rs @@ -31,14 +31,14 @@ pub enum ServerError { /// Wrapped `std::io::Error` IoError(std::io::Error), /// Other `ws-rs` error - Other(ws::Error) + WebSocket(ws::Error) } impl From for ServerError { fn from(err: ws::Error) -> Self { match err.kind { ws::ErrorKind::Io(e) => ServerError::IoError(e), - _ => ServerError::Other(err), + _ => ServerError::WebSocket(err), } } }