From 068c0f3782c73cc0de0d8d30452495aa1a718fb1 Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 21 Mar 2016 11:47:50 +0100 Subject: [PATCH] test for eth_getTransactionReceipt --- ethcore/src/client/ids.rs | 4 +- ethcore/src/client/test_client.rs | 12 +++++- ethcore/src/log_entry.rs | 2 +- ethcore/src/receipt.rs | 1 + rpc/src/v1/tests/eth.rs | 64 ++++++++++++++++++++++++++++++- rpc/src/v1/types/log.rs | 16 ++++---- rpc/src/v1/types/receipt.rs | 38 ++++++++++++++++++ 7 files changed, 122 insertions(+), 15 deletions(-) diff --git a/ethcore/src/client/ids.rs b/ethcore/src/client/ids.rs index 303657a76..9bd59b177 100644 --- a/ethcore/src/client/ids.rs +++ b/ethcore/src/client/ids.rs @@ -20,7 +20,7 @@ use util::hash::H256; use header::BlockNumber; /// Uniquely identifies block. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Hash, Eq)] pub enum BlockId { /// Block's sha3. /// Querying by hash is always faster. @@ -34,7 +34,7 @@ pub enum BlockId { } /// Uniquely identifies transaction. -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Hash, Eq)] pub enum TransactionId { /// Transaction's sha3. Hash(H256), diff --git a/ethcore/src/client/test_client.rs b/ethcore/src/client/test_client.rs index 0adaae7e5..2b9f1051a 100644 --- a/ethcore/src/client/test_client.rs +++ b/ethcore/src/client/test_client.rs @@ -52,6 +52,8 @@ pub struct TestBlockChainClient { pub code: RwLock>, /// Execution result. pub execution_result: RwLock>, + /// Transaction receipts. + pub receipts: RwLock>, } #[derive(Clone)] @@ -87,12 +89,18 @@ impl TestBlockChainClient { storage: RwLock::new(HashMap::new()), code: RwLock::new(HashMap::new()), execution_result: RwLock::new(None), + receipts: RwLock::new(HashMap::new()), }; client.add_blocks(1, EachBlockWith::Nothing); // add genesis block client.genesis_hash = client.last_hash.read().unwrap().clone(); client } + /// Set the transaction receipt result + pub fn set_transaction_receipt(&self, id: TransactionId, receipt: LocalizedReceipt) { + self.receipts.write().unwrap().insert(id, receipt); + } + /// Set the execution result. pub fn set_execution_result(&self, result: Executed) { *self.execution_result.write().unwrap() = Some(result); @@ -224,8 +232,8 @@ impl BlockChainClient for TestBlockChainClient { unimplemented!(); } - fn transaction_receipt(&self, _id: TransactionId) -> Option { - unimplemented!(); + fn transaction_receipt(&self, id: TransactionId) -> Option { + self.receipts.read().unwrap().get(&id).cloned() } fn blocks_with_bloom(&self, _bloom: &H2048, _from_block: BlockId, _to_block: BlockId) -> Option> { diff --git a/ethcore/src/log_entry.rs b/ethcore/src/log_entry.rs index cf74a6df9..63e07730e 100644 --- a/ethcore/src/log_entry.rs +++ b/ethcore/src/log_entry.rs @@ -78,7 +78,7 @@ impl FromJson for LogEntry { } /// Log localized in a blockchain. -#[derive(Default, Debug, PartialEq)] +#[derive(Default, Debug, PartialEq, Clone)] pub struct LocalizedLogEntry { /// Plain log entry. pub entry: LogEntry, diff --git a/ethcore/src/receipt.rs b/ethcore/src/receipt.rs index 7f0b0d8dd..2fbd3f14c 100644 --- a/ethcore/src/receipt.rs +++ b/ethcore/src/receipt.rs @@ -76,6 +76,7 @@ impl HeapSizeOf for Receipt { } /// Receipt with additional info. +#[derive(Debug, Clone, PartialEq)] pub struct LocalizedReceipt { /// Transaction hash. pub transaction_hash: H256, diff --git a/rpc/src/v1/tests/eth.rs b/rpc/src/v1/tests/eth.rs index 72235b390..4564076eb 100644 --- a/rpc/src/v1/tests/eth.rs +++ b/rpc/src/v1/tests/eth.rs @@ -14,12 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +use std::str::FromStr; use std::collections::HashMap; use std::sync::{Arc, RwLock}; use jsonrpc_core::IoHandler; -use util::hash::{Address, H256}; +use util::hash::{Address, H256, FixedHash}; use util::numbers::{Uint, U256}; -use ethcore::client::{TestBlockChainClient, EachBlockWith, Executed}; +use ethcore::client::{TestBlockChainClient, EachBlockWith, Executed, TransactionId}; +use ethcore::log_entry::{LocalizedLogEntry, LogEntry}; +use ethcore::receipt::LocalizedReceipt; use v1::{Eth, EthClient}; use v1::tests::helpers::{TestAccount, TestAccountProvider, TestSyncProvider, Config, TestMinerService, TestExternalMiner}; @@ -382,6 +385,63 @@ fn rpc_eth_sign() { unimplemented!() } +#[test] +fn rpc_eth_transaction_receipt() { + let receipt = LocalizedReceipt { + transaction_hash: H256::zero(), + transaction_index: 0, + block_hash: H256::from_str("ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5").unwrap(), + block_number: 0x4510c, + cumulative_gas_used: U256::from(0x20), + gas_used: U256::from(0x10), + contract_address: None, + logs: vec![LocalizedLogEntry { + entry: LogEntry { + address: Address::from_str("33990122638b9132ca29c723bdf037f1a891a70c").unwrap(), + topics: vec![ + H256::from_str("a6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc").unwrap(), + H256::from_str("4861736852656700000000000000000000000000000000000000000000000000").unwrap() + ], + data: vec![], + }, + block_hash: H256::from_str("ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5").unwrap(), + block_number: 0x4510c, + transaction_hash: H256::new(), + transaction_index: 0, + log_index: 1, + }] + }; + + let hash = H256::from_str("b903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238").unwrap(); + let tester = EthTester::default(); + tester.client.set_transaction_receipt(TransactionId::Hash(hash), receipt); + + let request = r#"{ + "jsonrpc": "2.0", + "method": "eth_getTransactionReceipt", + "params": ["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"], + "id": 1 + }"#; + let response = r#"{"jsonrpc":"2.0","result":{"blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","contractAddress":null,"cumulativeGasUsed":"0x20","gasUsed":"0x10","logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","data":"0x","logIndex":"0x01","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00"}],"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00"},"id":1}"#; + + assert_eq!(tester.io.handle_request(request), Some(response.to_owned())); +} + +#[test] +fn rpc_eth_transaction_receipt_null() { + let tester = EthTester::default(); + + let request = r#"{ + "jsonrpc": "2.0", + "method": "eth_getTransactionReceipt", + "params": ["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"], + "id": 1 + }"#; + let response = r#"{"jsonrpc":"2.0","result":null,"id":1}"#; + + assert_eq!(tester.io.handle_request(request), Some(response.to_owned())); +} + #[test] fn rpc_eth_compilers() { let request = r#"{"jsonrpc": "2.0", "method": "eth_getCompilers", "params": [], "id": 1}"#; diff --git a/rpc/src/v1/types/log.rs b/rpc/src/v1/types/log.rs index 0f5fdee15..c8dfb1aec 100644 --- a/rpc/src/v1/types/log.rs +++ b/rpc/src/v1/types/log.rs @@ -20,19 +20,19 @@ use v1::types::Bytes; #[derive(Debug, Serialize)] pub struct Log { - address: Address, - topics: Vec, - data: Bytes, + pub address: Address, + pub topics: Vec, + pub data: Bytes, #[serde(rename="blockHash")] - block_hash: H256, + pub block_hash: H256, #[serde(rename="blockNumber")] - block_number: U256, + pub block_number: U256, #[serde(rename="transactionHash")] - transaction_hash: H256, + pub transaction_hash: H256, #[serde(rename="transactionIndex")] - transaction_index: U256, + pub transaction_index: U256, #[serde(rename="logIndex")] - log_index: U256, + pub log_index: U256, } impl From for Log { diff --git a/rpc/src/v1/types/receipt.rs b/rpc/src/v1/types/receipt.rs index fa34d5df5..4bcfa3eb5 100644 --- a/rpc/src/v1/types/receipt.rs +++ b/rpc/src/v1/types/receipt.rs @@ -53,4 +53,42 @@ impl From for Receipt { } } +#[cfg(test)] +mod tests { + use serde_json; + use std::str::FromStr; + use util::numbers::*; + use v1::types::{Bytes, Log, Receipt}; + + #[test] + fn receipt_serialization() { + let s = r#"{"transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","cumulativeGasUsed":"0x20","gasUsed":"0x10","contractAddress":null,"logs":[{"address":"0x33990122638b9132ca29c723bdf037f1a891a70c","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","blockNumber":"0x04510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x00","logIndex":"0x01"}]}"#; + + let receipt = Receipt { + transaction_hash: H256::zero(), + transaction_index: U256::zero(), + block_hash: H256::from_str("ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5").unwrap(), + block_number: U256::from(0x4510c), + cumulative_gas_used: U256::from(0x20), + gas_used: U256::from(0x10), + contract_address: None, + logs: vec![Log { + address: Address::from_str("33990122638b9132ca29c723bdf037f1a891a70c").unwrap(), + topics: vec![ + H256::from_str("a6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc").unwrap(), + H256::from_str("4861736852656700000000000000000000000000000000000000000000000000").unwrap() + ], + data: Bytes::new(vec![]), + block_hash: H256::from_str("ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5").unwrap(), + block_number: U256::from(0x4510c), + transaction_hash: H256::new(), + transaction_index: U256::zero(), + log_index: U256::one() + }] + }; + + let serialized = serde_json::to_string(&receipt).unwrap(); + assert_eq!(serialized, s); + } +}