add eth_blockNumber, eth_TransactionCount integration tests.

also adds an EthTester struct for more test flexibility.
This commit is contained in:
Robert Habermeier 2016-05-25 13:27:03 +02:00
parent 1de7ea090c
commit 4c55e4968e

View File

@ -17,17 +17,19 @@
//! rpc integration tests. //! rpc integration tests.
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::str::FromStr;
use ethcore::client::{BlockChainClient, Client, ClientConfig}; use ethcore::client::{BlockChainClient, Client, ClientConfig};
use ethcore::spec::Genesis; use ethcore::spec::Genesis;
use ethcore::block::Block; use ethcore::block::Block;
use ethcore::ethereum; use ethcore::ethereum;
use ethminer::ExternalMiner; use ethcore::transaction::{Transaction, Action};
use ethminer::{MinerService, ExternalMiner};
use devtools::RandomTempPath; use devtools::RandomTempPath;
use util::io::IoChannel; use util::io::IoChannel;
use util::hash::{Address, FixedHash}; use util::hash::{Address, FixedHash};
use util::numbers::U256; use util::numbers::{Uint, U256};
use util::keys::{TestAccount, TestAccountProvider}; use util::keys::{AccountProvider, TestAccount, TestAccountProvider};
use jsonrpc_core::IoHandler; use jsonrpc_core::IoHandler;
use ethjson::blockchain::BlockChain; use ethjson::blockchain::BlockChain;
@ -35,6 +37,13 @@ use v1::traits::eth::Eth;
use v1::impls::EthClient; use v1::impls::EthClient;
use v1::tests::helpers::{TestSyncProvider, Config, TestMinerService}; use v1::tests::helpers::{TestSyncProvider, Config, TestMinerService};
struct EthTester {
_client: Arc<BlockChainClient>,
_miner: Arc<MinerService>,
accounts: Arc<TestAccountProvider>,
handler: IoHandler,
}
#[test] #[test]
fn harness_works() { fn harness_works() {
let chain: BlockChain = extract_chain!("BlockchainTests/bcUncleTest"); let chain: BlockChain = extract_chain!("BlockchainTests/bcUncleTest");
@ -44,7 +53,7 @@ fn harness_works() {
#[test] #[test]
fn eth_get_balance() { fn eth_get_balance() {
let chain = extract_chain!("BlockchainTests/bcWalletTest", "wallet2outOf3txs"); let chain = extract_chain!("BlockchainTests/bcWalletTest", "wallet2outOf3txs");
chain_harness(chain, |handler| { chain_harness(chain, |tester| {
// final account state // final account state
let req_latest = r#"{ let req_latest = r#"{
"jsonrpc": "2.0", "jsonrpc": "2.0",
@ -52,8 +61,8 @@ fn eth_get_balance() {
"params": ["0xaaaf5374fce5edbc8e2a8697c15331677e6ebaaa", "latest"], "params": ["0xaaaf5374fce5edbc8e2a8697c15331677e6ebaaa", "latest"],
"id": 1 "id": 1
}"#; }"#;
let res_latest = r#"{"jsonrpc":"2.0","result":"0x09","id":1}"#; let res_latest = r#"{"jsonrpc":"2.0","result":"0x09","id":1}"#.to_owned();
assert_eq!(&handler.handle_request(req_latest).unwrap(), res_latest); assert_eq!(tester.handler.handle_request(req_latest).unwrap(), res_latest);
// non-existant account // non-existant account
let req_new_acc = r#"{ let req_new_acc = r#"{
@ -63,8 +72,94 @@ fn eth_get_balance() {
"id": 3 "id": 3
}"#; }"#;
let res_new_acc = r#"{"jsonrpc":"2.0","result":"0x00","id":3}"#; let res_new_acc = r#"{"jsonrpc":"2.0","result":"0x00","id":3}"#.to_owned();
assert_eq!(&handler.handle_request(req_new_acc).unwrap(), res_new_acc); assert_eq!(tester.handler.handle_request(req_new_acc).unwrap(), res_new_acc);
});
}
#[test]
fn eth_block_number() {
let chain = extract_chain!("BlockchainTests/bcRPC_API_Test");
chain_harness(chain, |tester| {
let req_number = r#"{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1
}"#;
let res_number = r#"{"jsonrpc":"2.0","result":"0x20","id":1}"#.to_owned();
assert_eq!(tester.handler.handle_request(req_number).unwrap(), res_number);
});
}
#[cfg(test)]
#[test]
fn eth_transaction_count() {
let chain = extract_chain!("BlockchainTests/bcRPC_API_Test");
chain_harness(chain, |tester| {
let address = tester.accounts.new_account("123").unwrap();
let req_before = r#"{
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", "latest"],
"id": 15
}"#;
let res_before = r#"{"jsonrpc":"2.0","result":"0x00","id":15}"#;
assert_eq!(tester.handler.handle_request(&req_before).unwrap(), res_before);
let t = Transaction {
nonce: U256::zero(),
gas_price: U256::from(0x9184e72a000u64),
gas: U256::from(0x76c0),
action: Action::Call(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap()),
value: U256::from(0x9184e72au64),
data: vec![]
}.fake_sign(address.clone());
let req_send_trans = r#"{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [{
"from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a"
}],
"id": 16
}"#;
// dispatch the transaction.
tester.handler.handle_request(&req_send_trans).unwrap();
// we have submitted the transaction -- but this shouldn't be reflected in a "latest" query.
let req_after_latest = r#"{
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", "latest"],
"id": 17
}"#;
let res_after_latest = r#"{"jsonrpc":"2.0","result":"0x00","id":17}"#;
assert_eq!(&tester.handler.handle_request(&req_after_latest).unwrap(), res_after_latest);
// the pending transactions should have been updated.
let req_after_pending = r#"{
"jsonrpc": "2.0",
"method": "eth_getTransactionCount",
"params": [""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"", "pending"],
"id": 18
}"#;
let res_after_pending = r#"{"jsonrpc":"2.0","result":"0x01","id":18}"#;
assert_eq!(&tester.handler.handle_request(&req_after_pending).unwrap(), res_after_pending);
}); });
} }
@ -89,7 +184,7 @@ fn miner_service() -> Arc<TestMinerService> {
// given a blockchain, this harness will create an EthClient wrapping it // given a blockchain, this harness will create an EthClient wrapping it
// which tests can pass specially crafted requests to. // which tests can pass specially crafted requests to.
fn chain_harness<F, U>(chain: BlockChain, mut cb: F) -> U fn chain_harness<F, U>(chain: BlockChain, mut cb: F) -> U
where F: FnMut(&IoHandler) -> U { where F: FnMut(&EthTester) -> U {
let genesis = Genesis::from(chain.genesis()); let genesis = Genesis::from(chain.genesis());
let mut spec = ethereum::new_frontier_test(); let mut spec = ethereum::new_frontier_test();
let state = chain.pre_state.clone().into(); let state = chain.pre_state.clone().into();
@ -120,5 +215,13 @@ fn chain_harness<F, U>(chain: BlockChain, mut cb: F) -> U
let handler = IoHandler::new(); let handler = IoHandler::new();
let delegate = eth_client.to_delegate(); let delegate = eth_client.to_delegate();
handler.add_delegate(delegate); handler.add_delegate(delegate);
cb(&handler)
let tester = EthTester {
_miner: miner_service,
_client: client,
accounts: account_provider,
handler: handler,
};
cb(&tester)
} }