From aaf04e793d65244775ff63271ada1dc896de4650 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sat, 26 Mar 2016 12:35:15 +0100 Subject: [PATCH] Make BlockNumber optional. --- rpc/src/v1/impls/eth.rs | 78 +++++++++++++++---------- rpc/src/v1/tests/eth.rs | 64 +++++++++++++++++++- rpc/src/v1/tests/web3.rs | 4 -- rpc/src/v1/types/call_request.rs | 3 +- rpc/src/v1/types/transaction_request.rs | 3 +- 5 files changed, 113 insertions(+), 39 deletions(-) diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index 99f6eda3e..13cc08996 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -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 EthClient 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(params: Params) -> Result<(F,), Error> where F: serde::de::Deserialize { + match params_len(¶ms) { + 1 => from_params::<(F, )>(params), + _ => from_params::<(F, BlockNumber)>(params).map(|(f, _block_number)| (f, )), + } +} + +fn from_params_discard_third(params: Params) -> Result<(F1, F2,), Error> where F1: serde::de::Deserialize, F2: serde::de::Deserialize { + match params_len(¶ms) { + 2 => from_params::<(F1, F2, )>(params), + _ => from_params::<(F1, F2, BlockNumber)>(params).map(|(f1, f2, _block_number)| (f1, f2, )), + } +} + impl Eth for EthClient where C: BlockChainClient + 'static, S: SyncProvider + 'static, @@ -278,19 +300,18 @@ impl Eth for EthClient } fn balance(&self, params: Params) -> Result { - 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 { - 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::(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 { - 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 { @@ -329,9 +350,8 @@ impl Eth for EthClient // TODO: do not ignore block number param fn code_at(&self, params: Params) -> Result { - 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 { @@ -511,31 +531,29 @@ impl Eth for EthClient } fn call(&self, params: Params) -> Result { - 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 { - 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) + }) } } diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 182f6c368..5c94320e3 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -334,6 +334,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(); @@ -367,6 +398,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() { @@ -476,7 +538,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}"#; diff --git a/rpc/src/v1/tests/web3.rs b/rpc/src/v1/tests/web3.rs index 178dddfd7..bb77b24a2 100644 --- a/rpc/src/v1/tests/web3.rs +++ b/rpc/src/v1/tests/web3.rs @@ -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}"#; diff --git a/rpc/src/v1/types/call_request.rs b/rpc/src/v1/types/call_request.rs index a47d40eb3..045b1cd9b 100644 --- a/rpc/src/v1/types/call_request.rs +++ b/rpc/src/v1/types/call_request.rs @@ -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::*; diff --git a/rpc/src/v1/types/transaction_request.rs b/rpc/src/v1/types/transaction_request.rs index a9ed8a3f4..2623fd0ea 100644 --- a/rpc/src/v1/types/transaction_request.rs +++ b/rpc/src/v1/types/transaction_request.rs @@ -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::*;