Skip to content

Commit

Permalink
De-stringify Receipt and Log
Browse files Browse the repository at this point in the history
Fix #2
  • Loading branch information
sorpaas committed Oct 2, 2017
1 parent a778fd7 commit 5d01397
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
34 changes: 17 additions & 17 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,42 @@ pub enum Either<T, U> {
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum RPCTopicFilter {
Single(String),
Or(Vec<String>)
Single(Hex<H256>),
Or(Vec<Hex<H256>>)
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RPCLogFilter {
pub from_block: Option<String>,
pub to_block: Option<String>,
pub address: Option<String>,
pub address: Option<Hex<Address>>,
pub topics: Option<Vec<Option<RPCTopicFilter>>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[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<String>,
pub log_index: Hex<usize>,
pub transaction_index: Hex<usize>,
pub transaction_hash: Hex<H256>,
pub block_hash: Hex<H256>,
pub block_number: Hex<U256>,
pub data: Bytes,
pub topics: Vec<Hex<H256>>,
}

#[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<String>,
pub transaction_hash: Hex<H256>,
pub transaction_index: Hex<usize>,
pub block_hash: Hex<H256>,
pub block_number: Hex<U256>,
pub cumulative_gas_used: Hex<Gas>,
pub gas_used: Hex<Gas>,
pub contract_address: Option<Hex<Address>>,
pub logs: Vec<RPCLog>,
pub root: Hex<H256>,
pub status: usize,
Expand Down
38 changes: 17 additions & 21 deletions src/rpc/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -306,14 +306,10 @@ pub fn from_topic_filter(filter: Option<RPCTopicFilter>) -> Result<TopicFilter,
Ok(match filter {
None => 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())
},
})
}
Expand All @@ -323,7 +319,7 @@ pub fn from_log_filter(state: &MinerState, filter: RPCLogFilter) -> Result<LogFi
from_block: from_block_number(state, filter.from_block)?,
to_block: from_block_number(state, filter.to_block)?,
address: match filter.address {
Some(val) => Some(Address::from_str(&val)?),
Some(val) => Some(val.0),
None => None,
},
topics: match filter.topics {
Expand Down

0 comments on commit 5d01397

Please sign in to comment.