Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

implemented eth_geStorageAt rpc method, added more tests for rpc #695

Merged
merged 1 commit into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ impl<V> BlockChainClient for Client<V> 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<LocalizedTransaction> {
match id {
TransactionId::Hash(ref hash) => self.chain.transaction_address(hash),
Expand Down
3 changes: 3 additions & 0 deletions ethcore/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalizedTransaction>;

Expand Down
11 changes: 11 additions & 0 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct TestBlockChainClient {
pub difficulty: RwLock<U256>,
/// Balances.
pub balances: RwLock<HashMap<Address, U256>>,
/// Storage.
pub storage: RwLock<HashMap<(Address, H256), H256>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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<LocalizedTransaction> {
unimplemented!();
}
Expand Down
8 changes: 7 additions & 1 deletion rpc/src/v1/impls/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<C, S, A> EthClient<C, S, A> where C: BlockChainClient, S: SyncProvider, A:
impl<C, S, A> Eth for EthClient<C, S, A> where C: BlockChainClient + 'static, S: SyncProvider + 'static, A: AccountProvider + 'static {
fn protocol_version(&self, params: Params) -> Result<Value, Error> {
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())
}
}
Expand Down Expand Up @@ -175,6 +175,12 @@ impl<C, S, A> Eth for EthClient<C, S, A> 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<Value, Error> {
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<Value, Error> {
from_params::<(H256,)>(params)
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) {
Expand Down
76 changes: 69 additions & 7 deletions rpc/src/v1/tests/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -27,6 +27,7 @@ fn blockchain_client() -> Arc<TestBlockChainClient> {
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)
}

Expand All @@ -45,9 +46,9 @@ fn sync_provider() -> Arc<TestSyncProvider> {
}

struct EthTester {
client: Arc<TestBlockChainClient>,
sync: Arc<TestSyncProvider>,
accounts_provider: Arc<TestAccountProvider>,
_client: Arc<TestBlockChainClient>,
_sync: Arc<TestSyncProvider>,
_accounts_provider: Arc<TestAccountProvider>,
pub io: IoHandler,
}

Expand All @@ -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}"#;
Expand All @@ -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#"{
Expand All @@ -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()));
}