From 5d01397028e77f943f5d4a47c3dd6d605022cfc9 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Mon, 2 Oct 2017 19:11:16 +0800 Subject: [PATCH] De-stringify Receipt and Log Fix #2 --- src/rpc/mod.rs | 34 +++++++++++++++++----------------- src/rpc/util.rs | 38 +++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index 8d749d7..7343ea2 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -31,8 +31,8 @@ pub enum Either { #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] pub enum RPCTopicFilter { - Single(String), - Or(Vec) + Single(Hex), + Or(Vec>) } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -40,7 +40,7 @@ pub enum RPCTopicFilter { pub struct RPCLogFilter { pub from_block: Option, pub to_block: Option, - pub address: Option, + pub address: Option>, pub topics: Option>>, } @@ -48,25 +48,25 @@ pub struct RPCLogFilter { #[serde(rename_all = "camelCase")] pub struct RPCLog { pub removed: bool, - pub log_index: String, - pub transaction_index: String, - pub transaction_hash: String, - pub block_hash: String, - pub block_number: String, - pub data: String, - pub topics: Vec, + pub log_index: Hex, + pub transaction_index: Hex, + pub transaction_hash: Hex, + pub block_hash: Hex, + pub block_number: Hex, + pub data: Bytes, + pub topics: Vec>, } #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] pub struct RPCReceipt { - pub transaction_hash: String, - pub transaction_index: String, - pub block_hash: String, - pub block_number: String, - pub cumulative_gas_used: String, - pub gas_used: String, - pub contract_address: Option, + pub transaction_hash: Hex, + pub transaction_index: Hex, + pub block_hash: Hex, + pub block_number: Hex, + pub cumulative_gas_used: Hex, + pub gas_used: Hex, + pub contract_address: Option>, pub logs: Vec, pub root: Hex, pub status: usize, diff --git a/src/rpc/util.rs b/src/rpc/util.rs index 02bae68..95faf57 100644 --- a/src/rpc/util.rs +++ b/src/rpc/util.rs @@ -55,13 +55,13 @@ pub fn to_rpc_log(receipt: &Receipt, index: usize, transaction: &Transaction, bl RPCLog { removed: false, - log_index: format!("0x{:x}", index), - transaction_index: format!("0x{:x}", transaction_index), - transaction_hash: format!("0x{:x}", transaction_hash), - block_hash: format!("0x{:x}", block.header.header_hash()), - block_number: format!("0x{:x}", block.header.number), - data: to_hex(&receipt.logs[index].data), - topics: receipt.logs[index].topics.iter().map(|t| format!("0x{:x}", t)).collect(), + log_index: Hex(index), + transaction_index: Hex(transaction_index), + transaction_hash: Hex(transaction_hash), + block_hash: Hex(block.header.header_hash()), + block_number: Hex(block.header.number), + data: Bytes(receipt.logs[index].data.clone()), + topics: receipt.logs[index].topics.iter().map(|t| Hex(*t)).collect(), } } @@ -103,13 +103,13 @@ pub fn to_rpc_receipt(state: &MinerState, receipt: Receipt, transaction: &Transa }; Ok(RPCReceipt { - transaction_hash: format!("0x{:x}", transaction_hash), - transaction_index: format!("0x{:x}", transaction_index), - block_hash: format!("0x{:x}", block.header.header_hash()), - block_number: format!("0x{:x}", block.header.number), - cumulative_gas_used: format!("0x{:x}", cumulative_gas_used), - gas_used: format!("0x{:x}", receipt.used_gas), - contract_address: contract_address.map(|v| format!("0x{:x}", v)), + transaction_hash: Hex(transaction_hash), + transaction_index: Hex(transaction_index), + block_hash: Hex(block.header.header_hash()), + block_number: Hex(block.header.number), + cumulative_gas_used: Hex(cumulative_gas_used), + gas_used: Hex(receipt.used_gas), + contract_address: contract_address.map(|v| Hex(v)), logs: { let mut ret = Vec::new(); for i in 0..receipt.logs.len() { @@ -306,14 +306,10 @@ pub fn from_topic_filter(filter: Option) -> Result TopicFilter::All, Some(RPCTopicFilter::Single(s)) => TopicFilter::Or(vec![ - H256::from_str(&s)? + s.0 ]), Some(RPCTopicFilter::Or(ss)) => { - let mut ret = Vec::new(); - for s in ss { - ret.push(H256::from_str(&s)?) - } - TopicFilter::Or(ret) + TopicFilter::Or(ss.into_iter().map(|v| v.0).collect()) }, }) } @@ -323,7 +319,7 @@ pub fn from_log_filter(state: &MinerState, filter: RPCLogFilter) -> Result Some(Address::from_str(&val)?), + Some(val) => Some(val.0), None => None, }, topics: match filter.topics {