Make BlockNumber optional.

This commit is contained in:
Gav Wood 2016-03-26 12:35:15 +01:00
parent b4d95089d4
commit 4d04668c48
6 changed files with 116 additions and 42 deletions

View File

@ -35,6 +35,7 @@ use v1::traits::{Eth, EthFilter};
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, SyncInfo, Transaction, TransactionRequest, CallRequest, OptionalValue, Index, Filter, Log, Receipt};
use v1::helpers::{PollFilter, PollManager, ExternalMinerService, ExternalMiner};
use util::keys::store::AccountProvider;
use serde;
fn default_gas() -> U256 {
U256::from(21_000)
@ -199,6 +200,27 @@ impl<C, S, A, M, EM> EthClient<C, S, A, M, EM>
const MAX_QUEUE_SIZE_TO_MINE_ON: usize = 4; // because uncles go back 6.
fn params_len(params: &Params) -> usize {
match params {
&Params::Array(ref vec) => vec.len(),
_ => 0,
}
}
fn from_params_discard_second<F>(params: Params) -> Result<(F,), Error> where F: serde::de::Deserialize {
match params_len(&params) {
1 => from_params::<(F, )>(params),
_ => from_params::<(F, BlockNumber)>(params).map(|(f, _block_number)| (f, )),
}
}
fn from_params_discard_third<F1, F2>(params: Params) -> Result<(F1, F2,), Error> where F1: serde::de::Deserialize, F2: serde::de::Deserialize {
match params_len(&params) {
2 => from_params::<(F1, F2, )>(params),
_ => from_params::<(F1, F2, BlockNumber)>(params).map(|(f1, f2, _block_number)| (f1, f2, )),
}
}
impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
where C: BlockChainClient + 'static,
S: SyncProvider + 'static,
@ -278,19 +300,18 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
}
fn balance(&self, params: Params) -> Result<Value, Error> {
from_params::<(Address, BlockNumber)>(params)
.and_then(|(address, _block_number)| to_value(&take_weak!(self.client).balance(&address)))
from_params_discard_second(params).and_then(|(address, )|
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)))))
from_params_discard_third::<Address, U256>(params).and_then(|(address, 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)))
from_params_discard_second(params).and_then(|(address, )|
to_value(&take_weak!(self.client).nonce(&address)))
}
fn block_transaction_count_by_hash(&self, params: Params) -> Result<Value, Error> {
@ -329,9 +350,8 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
// TODO: do not ignore block number param
fn code_at(&self, params: Params) -> Result<Value, Error> {
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)))
from_params_discard_second(params).and_then(|(address, )|
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> {
@ -511,31 +531,29 @@ impl<C, S, A, M, EM> Eth for EthClient<C, S, A, M, EM>
}
fn call(&self, params: Params) -> Result<Value, Error> {
from_params::<(CallRequest, BlockNumber)>(params)
.and_then(|(request, _block_number)| {
let client = take_weak!(self.client);
let accounts = take_weak!(self.accounts);
let signed = Self::sign_call(&client, &accounts, request);
let output = signed.map(|tx| client.call(&tx)
.map(|e| Bytes::new(e.output))
.unwrap_or(Bytes::default()));
from_params_discard_second(params).and_then(|(request, )| {
let client = take_weak!(self.client);
let accounts = take_weak!(self.accounts);
let signed = Self::sign_call(&client, &accounts, request);
let output = signed.map(|tx| client.call(&tx)
.map(|e| Bytes::new(e.output))
.unwrap_or(Bytes::default()));
to_value(&output)
})
to_value(&output)
})
}
fn estimate_gas(&self, params: Params) -> Result<Value, Error> {
from_params::<(CallRequest, BlockNumber)>(params)
.and_then(|(request, _block_number)| {
let client = take_weak!(self.client);
let accounts = take_weak!(self.accounts);
let signed = Self::sign_call(&client, &accounts, request);
let output = signed.map(|tx| client.call(&tx)
.map(|e| e.gas_used + e.refunded)
.unwrap_or(U256::zero()));
from_params_discard_second(params).and_then(|(request, )| {
let client = take_weak!(self.client);
let accounts = take_weak!(self.accounts);
let signed = Self::sign_call(&client, &accounts, request);
let output = signed.map(|tx| client.call(&tx)
.map(|e| e.gas_used + e.refunded)
.unwrap_or(U256::zero()));
to_value(&output)
})
to_value(&output)
})
}
}

View File

@ -40,7 +40,7 @@ fn accounts_provider() -> Arc<TestAccountProvider> {
fn sync_provider() -> Arc<TestSyncProvider> {
Arc::new(TestSyncProvider::new(Config {
protocol_version: 65,
network_id: From::from(3),
num_peers: 120,
}))
}
@ -83,7 +83,7 @@ impl Default for EthTester {
#[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}"#;
let response = r#"{"jsonrpc":"2.0","result":"63","id":1}"#;
assert_eq!(EthTester::default().io.handle_request(request), Some(response.to_owned()));
}
@ -333,6 +333,37 @@ fn rpc_eth_call() {
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_call_default_block() {
let tester = EthTester::default();
tester.client.set_execution_result(Executed {
gas: U256::zero(),
gas_used: U256::from(0xff30),
refunded: U256::from(0x5),
cumulative_gas_used: U256::zero(),
logs: vec![],
contracts_created: vec![],
output: vec![0x12, 0x34, 0xff],
});
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_call",
"params": [{
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a",
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
}],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x1234ff","id":1}"#;
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_estimate_gas() {
let tester = EthTester::default();
@ -365,6 +396,37 @@ fn rpc_eth_estimate_gas() {
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_eth_estimate_gas_default_block() {
let tester = EthTester::default();
tester.client.set_execution_result(Executed {
gas: U256::zero(),
gas_used: U256::from(0xff30),
refunded: U256::from(0x5),
cumulative_gas_used: U256::zero(),
logs: vec![],
contracts_created: vec![],
output: vec![0x12, 0x34, 0xff],
});
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [{
"from": "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a",
"data": "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
}],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","result":"0xff35","id":1}"#;
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
}
#[test]
#[ignore]
fn rpc_eth_send_transaction() {
@ -474,7 +536,7 @@ fn rpc_eth_compile_serpent() {
#[test]
fn returns_no_work_if_cant_mine() {
let mut eth_tester = EthTester::default();
let eth_tester = EthTester::default();
eth_tester.client.set_queue_size(10);
let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#;

View File

@ -21,7 +21,7 @@ use v1::tests::helpers::{Config, TestSyncProvider};
fn sync_provider() -> Arc<TestSyncProvider> {
Arc::new(TestSyncProvider::new(Config {
network_id: 3,
network_id: From::from(3),
num_peers: 120,
}))
}

View File

@ -38,8 +38,6 @@ fn rpc_web3_sha3() {
let io = IoHandler::new();
io.add_delegate(web3);
let v = version().to_owned().replace("Parity/", "Parity//");
let request = r#"{"jsonrpc": "2.0", "method": "web3_sha3", "params": ["0x00"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0xbc36789e7a1e281436464229828f817d6612f7b477d66591ff96a9e064bcc98a","id":1}"#;
@ -52,8 +50,6 @@ fn rpc_web3_sha3_wiki() {
let io = IoHandler::new();
io.add_delegate(web3);
let v = version().to_owned().replace("Parity/", "Parity//");
let request = r#"{"jsonrpc": "2.0", "method": "web3_sha3", "params": ["0x68656c6c6f20776f726c64"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":"0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad","id":1}"#;

View File

@ -35,9 +35,8 @@ mod tests {
use std::str::FromStr;
use rustc_serialize::hex::FromHex;
use serde_json;
use util::numbers::{Uint, U256};
use util::numbers::{U256};
use util::hash::Address;
use ethcore::transaction::{Transaction, Action};
use v1::types::Bytes;
use super::*;

View File

@ -35,9 +35,8 @@ mod tests {
use std::str::FromStr;
use rustc_serialize::hex::FromHex;
use serde_json;
use util::numbers::{Uint, U256};
use util::numbers::{U256};
use util::hash::Address;
use ethcore::transaction::{Transaction, Action};
use v1::types::Bytes;
use super::*;