From 00820c342a7a04f02886c2f920d29a5c0e419c61 Mon Sep 17 00:00:00 2001 From: debris Date: Sun, 13 Mar 2016 14:45:39 +0100 Subject: [PATCH] fixed eth_getCode and added tests for it --- ethcore/src/client/test_client.rs | 14 ++++++++++++-- rpc/src/v1/impls/eth.rs | 3 ++- rpc/src/v1/tests/eth.rs | 14 ++++++++++++++ rpc/src/v1/traits/eth.rs | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 0dd4359ce..d85540858 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -44,6 +44,8 @@ pub struct TestBlockChainClient { pub balances: RwLock>, /// Storage. pub storage: RwLock>, + /// Code. + pub code: RwLock>, } #[derive(Clone)] @@ -77,16 +79,24 @@ impl TestBlockChainClient { difficulty: RwLock::new(From::from(0)), balances: RwLock::new(HashMap::new()), storage: RwLock::new(HashMap::new()), + code: RwLock::new(HashMap::new()), }; client.add_blocks(1, EachBlockWith::Nothing); // add genesis block client.genesis_hash = client.last_hash.read().unwrap().clone(); client } + /// Set code at given address. + pub fn set_code(&mut self, address: Address, code: Bytes) { + self.code.write().unwrap().insert(address, code); + } + + /// Set balance at given address. pub fn set_balance(&mut self, address: Address, balance: U256) { self.balances.write().unwrap().insert(address, balance); } + /// Set storage at given address and position. pub fn set_storage(&mut self, address: Address, position: H256, value: H256) { self.storage.write().unwrap().insert((address, position), value); } @@ -181,8 +191,8 @@ impl BlockChainClient for TestBlockChainClient { U256::zero() } - fn code(&self, _address: &Address) -> Option { - unimplemented!(); + fn code(&self, address: &Address) -> Option { + self.code.read().unwrap().get(address).cloned() } fn balance(&self, address: &Address) -> U256 { diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 6d28ccc75..2c7e16af5 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -221,7 +221,8 @@ impl Eth for EthClient where C: BlockChainClient + 'static, S: // TODO: do not ignore block number param fn code_at(&self, params: Params) -> Result { from_params::<(Address, BlockNumber)>(params) - .and_then(|(address, _block_number)| to_value(&take_weak!(self.client).code(&address).map_or_else(Bytes::default, Bytes::new))) + .and_then(|(address, _block_number)| + to_value(&take_weak!(self.client).code(&address).map_or_else(Bytes::default, Bytes::new))) } fn block_by_hash(&self, params: Params) -> Result { diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index bde84491b..a636ba278 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -28,6 +28,7 @@ fn blockchain_client() -> Arc { 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)); + client.set_code(Address::from(1), vec![0xff, 0x21]); Arc::new(client) } @@ -216,3 +217,16 @@ fn rpc_eth_uncle_count_by_block_number() { assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); } +#[test] +fn rpc_eth_code() { + let request = r#"{ + "jsonrpc": "2.0", + "method": "eth_getCode", + "params": ["0x0000000000000000000000000000000000000001", "latest"], + "id": 1 + }"#; + let response = r#"{"jsonrpc":"2.0","result":"0xff21","id":1}"#; + + assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned())); +} + diff --git a/rpc/src/v1/traits/eth.rs b/rpc/src/v1/traits/eth.rs index 738f561b2..0b09012ab 100644 --- a/rpc/src/v1/traits/eth.rs +++ b/rpc/src/v1/traits/eth.rs @@ -140,7 +140,7 @@ pub trait Eth: Sized + Send + Sync + 'static { delegate.add_method("eth_getBlockTransactionCountByNumber", Eth::block_transaction_count_by_number); delegate.add_method("eth_getUncleCountByBlockHash", Eth::block_uncles_count_by_hash); delegate.add_method("eth_getUncleCountByBlockNumber", Eth::block_uncles_count_by_number); - delegate.add_method("eth_code", Eth::code_at); + delegate.add_method("eth_getCode", Eth::code_at); delegate.add_method("eth_sendTransaction", Eth::send_transaction); delegate.add_method("eth_call", Eth::call); delegate.add_method("eth_estimateGas", Eth::estimate_gas);