implemented eth_storageAt rpc method, added more tests for rpc

This commit is contained in:
debris
2016-03-13 12:09:30 +01:00
parent 70624f816a
commit 487ba9b08a
5 changed files with 94 additions and 8 deletions

View File

@@ -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())
}
}
@@ -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)) {

View File

@@ -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<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)
}
@@ -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,
}
@@ -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()));
}