From 487ba9b08aacd81224324d3b374433de50a7d5da Mon Sep 17 00:00:00 2001 From: debris Date: Sun, 13 Mar 2016 12:09:30 +0100 Subject: [PATCH] implemented eth_storageAt rpc method, added more tests for rpc --- ethcore/src/client/client.rs | 4 ++ ethcore/src/client/mod.rs | 3 ++ ethcore/src/client/test_client.rs | 11 +++++ rpc/src/v1/impls/eth.rs | 8 +++- rpc/src/v1/tests/eth.rs | 76 ++++++++++++++++++++++++++++--- 5 files changed, 94 insertions(+), 8 deletions(-) diff --git a/ethcore/src/client/client.rs b/ethcore/src/client/client.rs index a860dd752..174142f7a 100644 --- a/ethcore/src/client/client.rs +++ b/ethcore/src/client/client.rs @@ -453,6 +453,10 @@ impl BlockChainClient for Client where V: Verifier { self.state().balance(address) } + fn storage_at(&self, address: &Address, position: &H256) -> H256 { + self.state().storage_at(address, position) + } + fn transaction(&self, id: TransactionId) -> Option { match id { TransactionId::Hash(ref hash) => self.chain.transaction_address(hash), diff --git a/ethcore/src/client/mod.rs b/ethcore/src/client/mod.rs index af2c6ac14..f07a1f7c3 100644 --- a/ethcore/src/client/mod.rs +++ b/ethcore/src/client/mod.rs @@ -69,6 +69,9 @@ pub trait BlockChainClient : Sync + Send { /// Get address balance. fn balance(&self, address: &Address) -> U256; + /// Get value of the storage at given position. + fn storage_at(&self, address: &Address, position: &H256) -> H256; + /// 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 997f159d2..0dd4359ce 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -42,6 +42,8 @@ pub struct TestBlockChainClient { pub difficulty: RwLock, /// Balances. pub balances: RwLock>, + /// Storage. + pub storage: RwLock>, } #[derive(Clone)] @@ -74,6 +76,7 @@ impl TestBlockChainClient { last_hash: RwLock::new(H256::new()), difficulty: RwLock::new(From::from(0)), balances: RwLock::new(HashMap::new()), + storage: RwLock::new(HashMap::new()), }; client.add_blocks(1, EachBlockWith::Nothing); // add genesis block client.genesis_hash = client.last_hash.read().unwrap().clone(); @@ -84,6 +87,10 @@ impl TestBlockChainClient { self.balances.write().unwrap().insert(address, balance); } + pub fn set_storage(&mut self, address: Address, position: H256, value: H256) { + self.storage.write().unwrap().insert((address, position), value); + } + /// Add blocks to test client. pub fn add_blocks(&mut self, count: usize, with: EachBlockWith) { let len = self.numbers.read().unwrap().len(); @@ -182,6 +189,10 @@ impl BlockChainClient for TestBlockChainClient { self.balances.read().unwrap().get(address).cloned().unwrap_or_else(U256::zero) } + 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 transaction(&self, _id: TransactionId) -> Option { unimplemented!(); } diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index f5159f55f..a5736b76b 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -101,7 +101,7 @@ impl EthClient where C: BlockChainClient, S: SyncProvider, A: impl Eth for EthClient where C: BlockChainClient + 'static, S: SyncProvider + 'static, A: AccountProvider + 'static { fn protocol_version(&self, params: Params) -> Result { match params { - Params::None => to_value(&U256::from(take_weak!(self.sync).status().protocol_version)), + Params::None => Ok(Value::String(format!("{}", take_weak!(self.sync).status().protocol_version).to_owned())), _ => Err(Error::invalid_params()) } } @@ -175,6 +175,12 @@ impl Eth for EthClient where C: BlockChainClient + 'static, S: .and_then(|(address, _block_number)| to_value(&take_weak!(self.client).balance(&address))) } + fn storage_at(&self, params: Params) -> Result { + from_params::<(Address, U256, BlockNumber)>(params) + .and_then(|(address, position, _block_number)| + to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position))))) + } + fn block_transaction_count_by_hash(&self, params: Params) -> Result { from_params::<(H256,)>(params) .and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) { diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 8c61c2ed9..47dc1dd89 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -17,7 +17,7 @@ use std::collections::HashMap; use std::sync::Arc; use jsonrpc_core::IoHandler; -use util::hash::Address; +use util::hash::{Address, H256}; use util::numbers::U256; use ethcore::client::{TestBlockChainClient, EachBlockWith}; use v1::{Eth, EthClient}; @@ -27,6 +27,7 @@ fn blockchain_client() -> Arc { let mut client = TestBlockChainClient::new(); client.add_blocks(10, EachBlockWith::Nothing); client.set_balance(Address::from(1), U256::from(5)); + client.set_storage(Address::from(1), H256::from(4), H256::from(7)); Arc::new(client) } @@ -45,9 +46,9 @@ fn sync_provider() -> Arc { } struct EthTester { - client: Arc, - sync: Arc, - accounts_provider: Arc, + _client: Arc, + _sync: Arc, + _accounts_provider: Arc, pub io: IoHandler, } @@ -60,14 +61,54 @@ impl Default for EthTester { let io = IoHandler::new(); io.add_delegate(eth); EthTester { - client: client, - sync: sync, - accounts_provider: ap, + _client: client, + _sync: sync, + _accounts_provider: ap, io: io } } } +#[test] +fn rpc_eth_protocol_version() { + let request = r#"{"jsonrpc": "2.0", "method": "eth_protocolVersion", "params": [], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"65","id":1}"#; + + assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); +} + +#[test] +#[ignore] +fn rpc_eth_syncing() { + unimplemented!() +} + +#[test] +#[ignore] +fn rpc_eth_hashrate() { + unimplemented!() +} + +#[test] +#[ignore] +fn rpc_eth_author() { + unimplemented!() +} + +#[test] +#[ignore] +fn rpc_eth_mining() { + unimplemented!() +} + +#[test] +fn rpc_eth_gas_price() { + let request = r#"{"jsonrpc": "2.0", "method": "eth_gasPrice", "params": [], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0ba43b7400","id":1}"#; + + assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); +} + #[test] fn rpc_eth_accounts() { let request = r#"{"jsonrpc": "2.0", "method": "eth_accounts", "params": [], "id": 1}"#; @@ -76,6 +117,14 @@ fn rpc_eth_accounts() { assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); } +#[test] +fn rpc_eth_block_number() { + let request = r#"{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1}"#; + let response = r#"{"jsonrpc":"2.0","result":"0x0a","id":1}"#; + + assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); +} + #[test] fn rpc_eth_balance() { let request = r#"{ @@ -88,3 +137,16 @@ fn rpc_eth_balance() { assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); } + +#[test] +fn rpc_eth_storage_at() { + let request = r#"{ + "jsonrpc": "2.0", + "method": "eth_getStorageAt", + "params": ["0x0000000000000000000000000000000000000001", "0x4", "latest"], + "id": 1 + }"#; + let response = r#"{"jsonrpc":"2.0","result":"0x07","id":1}"#; + + assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); +}