Merge remote-tracking branch 'origin/master' into merge_ethminer

This commit is contained in:
Gav Wood 2016-03-13 16:35:52 +01:00
commit b4f7f220f1
5 changed files with 191 additions and 25 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,6 +79,7 @@ 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();
@ -88,6 +91,11 @@ impl TestBlockChainClient {
self.balances.write().unwrap().insert(address, balance); self.balances.write().unwrap().insert(address, balance);
} }
/// Set `code` at `address`.
pub fn set_code(&mut self, address: Address, code: Bytes) {
self.code.write().unwrap().insert(address, code);
}
/// Set storage `position` to `value` for account `address`. /// Set storage `position` to `value` for account `address`.
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);
@ -183,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

@ -198,37 +198,48 @@ impl<C, S, A, M> Eth for EthClient<C, S, A, M>
to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position))))) to_value(&U256::from(take_weak!(self.client).storage_at(&address, &H256::from(position)))))
} }
fn transaction_count(&self, params: Params) -> Result<Value, Error> {
from_params::<(Address, BlockNumber)>(params)
.and_then(|(address, _block_number)| to_value(&take_weak!(self.client).nonce(&address)))
}
fn block_transaction_count_by_hash(&self, params: Params) -> Result<Value, Error> { fn block_transaction_count_by_hash(&self, params: Params) -> Result<Value, Error> {
from_params::<(H256,)>(params) from_params::<(H256,)>(params)
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) { .and_then(|(hash,)| // match
Some(bytes) => to_value(&BlockView::new(&bytes).transactions_count()), to_value(&take_weak!(self.client).block(BlockId::Hash(hash))
None => Ok(Value::Null) .map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).transactions_count()))))
})
} }
fn block_transaction_count_by_number(&self, params: Params) -> Result<Value, Error> { fn block_transaction_count_by_number(&self, params: Params) -> Result<Value, Error> {
from_params::<(BlockNumber,)>(params) from_params::<(BlockNumber,)>(params)
.and_then(|(block_number,)| match block_number { .and_then(|(block_number,)| match block_number {
BlockNumber::Pending => to_value(&take_weak!(self.miner).status().transaction_queue_pending), BlockNumber::Pending => to_value(&U256::from(take_weak!(self.miner).status().transaction_queue_pending)),
_ => match take_weak!(self.client).block(block_number.into()) { _ => to_value(&take_weak!(self.client).block(block_number.into())
Some(bytes) => to_value(&BlockView::new(&bytes).transactions_count()), .map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).transactions_count())))
None => Ok(Value::Null)
}
}) })
} }
fn block_uncles_count(&self, params: Params) -> Result<Value, Error> { fn block_uncles_count_by_hash(&self, params: Params) -> Result<Value, Error> {
from_params::<(H256,)>(params) from_params::<(H256,)>(params)
.and_then(|(hash,)| match take_weak!(self.client).block(BlockId::Hash(hash)) { .and_then(|(hash,)|
Some(bytes) => to_value(&BlockView::new(&bytes).uncles_count()), to_value(&take_weak!(self.client).block(BlockId::Hash(hash))
None => Ok(Value::Null) .map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).uncles_count()))))
}
fn block_uncles_count_by_number(&self, params: Params) -> Result<Value, Error> {
from_params::<(BlockNumber,)>(params)
.and_then(|(block_number,)| match block_number {
BlockNumber::Pending => to_value(&U256::from(0)),
_ => to_value(&take_weak!(self.client).block(block_number.into())
.map_or_else(U256::zero, |bytes| U256::from(BlockView::new(&bytes).uncles_count())))
}) })
} }
// 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> {
@ -256,6 +267,13 @@ impl<C, S, A, M> Eth for EthClient<C, S, A, M>
.and_then(|(number, index)| self.transaction(TransactionId::Location(number.into(), index.value()))) .and_then(|(number, index)| self.transaction(TransactionId::Location(number.into(), index.value())))
} }
fn compilers(&self, params: Params) -> Result<Value, Error> {
match params {
Params::None => to_value(&vec![] as &Vec<String>),
_ => Err(Error::invalid_params())
}
}
fn logs(&self, params: Params) -> Result<Value, Error> { fn logs(&self, params: Params) -> Result<Value, Error> {
from_params::<(Filter,)>(params) from_params::<(Filter,)>(params)
.and_then(|(filter,)| { .and_then(|(filter,)| {

View File

@ -21,13 +21,14 @@ use util::hash::{Address, H256};
use util::numbers::U256; use util::numbers::U256;
use ethcore::client::{TestBlockChainClient, EachBlockWith}; use ethcore::client::{TestBlockChainClient, EachBlockWith};
use v1::{Eth, EthClient}; use v1::{Eth, EthClient};
use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config}; use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config, TestMinerService};
fn blockchain_client() -> Arc<TestBlockChainClient> { fn blockchain_client() -> Arc<TestBlockChainClient> {
let mut client = TestBlockChainClient::new(); let mut client = TestBlockChainClient::new();
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)
} }
@ -45,10 +46,15 @@ fn sync_provider() -> Arc<TestSyncProvider> {
})) }))
} }
fn miner_service() -> Arc<TestMinerService> {
Arc::new(TestMinerService)
}
struct EthTester { struct EthTester {
_client: Arc<TestBlockChainClient>, _client: Arc<TestBlockChainClient>,
_sync: Arc<TestSyncProvider>, _sync: Arc<TestSyncProvider>,
_accounts_provider: Arc<TestAccountProvider>, _accounts_provider: Arc<TestAccountProvider>,
_miner: Arc<TestMinerService>,
pub io: IoHandler, pub io: IoHandler,
} }
@ -57,13 +63,15 @@ impl Default for EthTester {
let client = blockchain_client(); let client = blockchain_client();
let sync = sync_provider(); let sync = sync_provider();
let ap = accounts_provider(); let ap = accounts_provider();
let eth = EthClient::new(&client, &sync, &ap).to_delegate(); let miner = miner_service();
let eth = EthClient::new(&client, &sync, &ap, &miner).to_delegate();
let io = IoHandler::new(); let io = IoHandler::new();
io.add_delegate(eth); io.add_delegate(eth);
EthTester { EthTester {
_client: client, _client: client,
_sync: sync, _sync: sync,
_accounts_provider: ap, _accounts_provider: ap,
_miner: miner,
io: io io: io
} }
} }
@ -150,3 +158,122 @@ fn rpc_eth_storage_at() {
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_transaction_count() {
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": ["0x0000000000000000000000000000000000000001", "latest"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#;
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_block_transaction_count_by_hash() {
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_getBlockTransactionCountByHash",
"params": ["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#;
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_transaction_count_by_number() {
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_getBlockTransactionCountByNumber",
"params": ["latest"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#;
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_uncle_count_by_block_hash() {
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_getUncleCountByBlockHash",
"params": ["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#;
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_uncle_count_by_block_number() {
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_getUncleCountByBlockNumber",
"params": ["latest"],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x00","id":1}"#;
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()));
}
#[test]
#[ignore]
fn rpc_eth_call() {
unimplemented!()
}
#[test]
#[ignore]
fn rpc_eth_send_transaction() {
unimplemented!()
}
#[test]
#[ignore]
fn rpc_eth_send_raw_transaction() {
unimplemented!()
}
#[test]
#[ignore]
fn rpc_eth_sign() {
unimplemented!()
}
#[test]
#[ignore]
fn rpc_eth_estimate_gas() {
unimplemented!()
}
#[test]
fn rpc_eth_compilers() {
let request = r#"{"jsonrpc": "2.0", "method": "eth_getCompilers", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":[],"id":1}"#;
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned()));
}

View File

@ -16,6 +16,8 @@
mod account_provider; mod account_provider;
mod sync_provider; mod sync_provider;
mod miner_service;
pub use self::account_provider::{TestAccount, TestAccountProvider}; pub use self::account_provider::{TestAccount, TestAccountProvider};
pub use self::sync_provider::{Config, TestSyncProvider}; pub use self::sync_provider::{Config, TestSyncProvider};
pub use self::miner_service::{TestMinerService};

View File

@ -59,21 +59,30 @@ pub trait Eth: Sized + Send + Sync + 'static {
/// Returns the number of transactions sent from given address at given time (block number). /// Returns the number of transactions sent from given address at given time (block number).
fn transaction_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn transaction_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of transactions in a block given block hash. /// Returns the number of transactions in a block with given hash.
fn block_transaction_count_by_hash(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn block_transaction_count_by_hash(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of transactions in a block given block number. /// Returns the number of transactions in a block with given block number.
fn block_transaction_count_by_number(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn block_transaction_count_by_number(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of uncles in a given block. /// Returns the number of uncles in a block with given hash.
fn block_uncles_count(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn block_uncles_count_by_hash(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the number of uncles in a block with given block number.
fn block_uncles_count_by_number(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns the code at given address at given time (block number). /// Returns the code at given address at given time (block number).
fn code_at(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn code_at(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Signs the data with given address signature.
fn sign(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Sends transaction. /// Sends transaction.
fn send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn send_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Sends signed transaction.
fn send_raw_transaction(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Call contract. /// Call contract.
fn call(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() } fn call(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
@ -135,10 +144,12 @@ pub trait Eth: Sized + Send + Sync + 'static {
delegate.add_method("eth_getTransactionCount", Eth::transaction_count); delegate.add_method("eth_getTransactionCount", Eth::transaction_count);
delegate.add_method("eth_getBlockTransactionCountByHash", Eth::block_transaction_count_by_hash); delegate.add_method("eth_getBlockTransactionCountByHash", Eth::block_transaction_count_by_hash);
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); delegate.add_method("eth_getUncleCountByBlockHash", Eth::block_uncles_count_by_hash);
delegate.add_method("eth_getUncleCountByBlockNumber", Eth::block_uncles_count); 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_sign", Eth::sign);
delegate.add_method("eth_sendTransaction", Eth::send_transaction); delegate.add_method("eth_sendTransaction", Eth::send_transaction);
delegate.add_method("eth_sendRawTransaction", Eth::send_raw_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);
delegate.add_method("eth_getBlockByHash", Eth::block_by_hash); delegate.add_method("eth_getBlockByHash", Eth::block_by_hash);