fixed eth_getCode and added tests for it

This commit is contained in:
debris 2016-03-13 14:45:39 +01:00
parent c2b3ba533b
commit 00820c342a
4 changed files with 29 additions and 4 deletions

View File

@ -44,6 +44,8 @@ pub struct TestBlockChainClient {
pub balances: RwLock<HashMap<Address, U256>>, pub balances: RwLock<HashMap<Address, U256>>,
/// Storage. /// Storage.
pub storage: RwLock<HashMap<(Address, H256), H256>>, pub storage: RwLock<HashMap<(Address, H256), H256>>,
/// Code.
pub code: RwLock<HashMap<Address, Bytes>>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -77,16 +79,24 @@ impl TestBlockChainClient {
difficulty: RwLock::new(From::from(0)), difficulty: RwLock::new(From::from(0)),
balances: RwLock::new(HashMap::new()), balances: RwLock::new(HashMap::new()),
storage: RwLock::new(HashMap::new()), storage: RwLock::new(HashMap::new()),
code: RwLock::new(HashMap::new()),
}; };
client.add_blocks(1, EachBlockWith::Nothing); // add genesis block client.add_blocks(1, EachBlockWith::Nothing); // add genesis block
client.genesis_hash = client.last_hash.read().unwrap().clone(); client.genesis_hash = client.last_hash.read().unwrap().clone();
client 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) { pub fn set_balance(&mut self, address: Address, balance: U256) {
self.balances.write().unwrap().insert(address, balance); 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) { pub fn set_storage(&mut self, address: Address, position: H256, value: H256) {
self.storage.write().unwrap().insert((address, position), value); self.storage.write().unwrap().insert((address, position), value);
} }
@ -181,8 +191,8 @@ impl BlockChainClient for TestBlockChainClient {
U256::zero() U256::zero()
} }
fn code(&self, _address: &Address) -> Option<Bytes> { fn code(&self, address: &Address) -> Option<Bytes> {
unimplemented!(); self.code.read().unwrap().get(address).cloned()
} }
fn balance(&self, address: &Address) -> U256 { fn balance(&self, address: &Address) -> U256 {

View File

@ -221,7 +221,8 @@ impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S:
// TODO: do not ignore block number param // TODO: do not ignore block number param
fn code_at(&self, params: Params) -> Result<Value, Error> { fn code_at(&self, params: Params) -> Result<Value, Error> {
from_params::<(Address, BlockNumber)>(params) 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<Value, Error> { fn block_by_hash(&self, params: Params) -> Result<Value, Error> {

View File

@ -28,6 +28,7 @@ fn blockchain_client() -> Arc<TestBlockChainClient> {
client.add_blocks(10, EachBlockWith::Nothing); client.add_blocks(10, EachBlockWith::Nothing);
client.set_balance(Address::from(1), U256::from(5)); client.set_balance(Address::from(1), U256::from(5));
client.set_storage(Address::from(1), H256::from(4), H256::from(7)); client.set_storage(Address::from(1), H256::from(4), H256::from(7));
client.set_code(Address::from(1), vec![0xff, 0x21]);
Arc::new(client) 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())); 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()));
}

View File

@ -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_getBlockTransactionCountByNumber", Eth::block_transaction_count_by_number);
delegate.add_method("eth_getUncleCountByBlockHash", Eth::block_uncles_count_by_hash); 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_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_sendTransaction", Eth::send_transaction);
delegate.add_method("eth_call", Eth::call); delegate.add_method("eth_call", Eth::call);
delegate.add_method("eth_estimateGas", Eth::estimate_gas); delegate.add_method("eth_estimateGas", Eth::estimate_gas);