Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Fixed receipt serialization and RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Sep 20, 2017
1 parent 8a21cde commit 732d50e
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 84 deletions.
3 changes: 1 addition & 2 deletions ethcore/light/src/on_demand/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,7 @@ mod tests {
#[test]
fn check_receipts() {
let receipts = (0..5).map(|_| Receipt {
state_root: Some(H256::random()),
status_code: None,
outcome: TransactionOutcome::StateRoot(H256::random()),
gas_used: 21_000u64.into(),
log_bloom: Default::default(),
logs: Vec::new(),
Expand Down
7 changes: 5 additions & 2 deletions ethcore/light/src/types/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,13 +1739,15 @@ mod tests {

#[test]
fn receipts_roundtrip() {
use ethcore::receipt::{Receipt, TransactionOutcome};
let req = IncompleteReceiptsRequest {
hash: Field::Scalar(Default::default()),
};

let full_req = Request::Receipts(req.clone());
let receipt = Receipt::new(TransactionOutcome::Unknown, Default::default(), Vec::new());
let res = ReceiptsResponse {
receipts: vec![Default::default(), Default::default()],
receipts: vec![receipt.clone(), receipt],
};
let full_res = Response::Receipts(res.clone());

Expand Down Expand Up @@ -1900,14 +1902,15 @@ mod tests {

#[test]
fn responses_vec() {
use ethcore::receipt::{Receipt, TransactionOutcome};
let mut stream = RlpStream::new_list(2);
stream.begin_list(0).begin_list(0);

let body = ::ethcore::encoded::Body::new(stream.out());
let reqs = vec![
Response::Headers(HeadersResponse { headers: vec![] }),
Response::HeaderProof(HeaderProofResponse { proof: vec![], hash: Default::default(), td: 100.into()}),
Response::Receipts(ReceiptsResponse { receipts: vec![Default::default()] }),
Response::Receipts(ReceiptsResponse { receipts: vec![Receipt::new(TransactionOutcome::Unknown, Default::default(), Vec::new())] }),
Response::Body(BodyResponse { body: body }),
Response::Account(AccountResponse {
proof: vec![],
Expand Down
4 changes: 2 additions & 2 deletions ethcore/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use engines::Engine;
use error::{Error, BlockError, TransactionError};
use factory::Factories;
use header::Header;
use receipt::Receipt;
use receipt::{Receipt, TransactionOutcome};
use state::State;
use state_db::StateDB;
use trace::FlatTrace;
Expand Down Expand Up @@ -533,7 +533,7 @@ impl LockedBlock {
pub fn strip_receipts(self) -> LockedBlock {
let mut block = self;
for receipt in &mut block.block.receipts {
receipt.state_root = None;
receipt.outcome = TransactionOutcome::Unknown;
}
block.block.header.set_receipts_root(ordered_trie_root(block.block.receipts.iter().map(|r| r.rlp_bytes().into_vec())));
block
Expand Down
11 changes: 4 additions & 7 deletions ethcore/src/blockchain/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ mod tests {
use hash::keccak;
use util::kvdb::KeyValueDB;
use bigint::hash::*;
use receipt::Receipt;
use receipt::{Receipt, TransactionOutcome};
use blockchain::{BlockProvider, BlockChain, Config, ImportRoute};
use tests::helpers::*;
use blockchain::generator::{ChainGenerator, ChainIterator, BlockFinalizer};
Expand Down Expand Up @@ -2048,8 +2048,7 @@ mod tests {
let db = new_db();
let bc = new_chain(&genesis, db.clone());
insert_block(&db, &bc, &b1, vec![Receipt {
state_root: Some(H256::default()),
status_code: None,
outcome: TransactionOutcome::StateRoot(H256::default()),
gas_used: 10_000.into(),
log_bloom: Default::default(),
logs: vec![
Expand All @@ -2058,8 +2057,7 @@ mod tests {
],
},
Receipt {
state_root: Some(H256::default()),
status_code: None,
outcome: TransactionOutcome::StateRoot(H256::default()),
gas_used: 10_000.into(),
log_bloom: Default::default(),
logs: vec![
Expand All @@ -2068,8 +2066,7 @@ mod tests {
}]);
insert_block(&db, &bc, &b2, vec![
Receipt {
state_root: Some(H256::default()),
status_code: None,
outcome: TransactionOutcome::StateRoot(H256::default()),
gas_used: 10_000.into(),
log_bloom: Default::default(),
logs: vec![
Expand Down
14 changes: 6 additions & 8 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,7 @@ fn transaction_receipt(engine: &Engine, mut tx: LocalizedTransaction, mut receip
log_index: no_of_logs + i,
}).collect(),
log_bloom: receipt.log_bloom,
state_root: receipt.state_root,
outcome: receipt.outcome,
}
}

Expand Down Expand Up @@ -2100,7 +2100,7 @@ mod tests {
use super::transaction_receipt;
use ethkey::KeyPair;
use log_entry::{LogEntry, LocalizedLogEntry};
use receipt::{Receipt, LocalizedReceipt};
use receipt::{Receipt, LocalizedReceipt, TransactionOutcome};
use transaction::{Transaction, LocalizedTransaction, Action};
use tests::helpers::TestEngine;

Expand All @@ -2111,7 +2111,7 @@ mod tests {

let block_number = 1;
let block_hash = 5.into();
let state_root = Some(99.into());
let state_root = 99.into();
let gas_used = 10.into();
let raw_tx = Transaction {
nonce: 0.into(),
Expand Down Expand Up @@ -2139,14 +2139,12 @@ mod tests {
data: vec![],
}];
let receipts = vec![Receipt {
state_root: state_root,
status_code: None,
outcome: TransactionOutcome::StateRoot(state_root),
gas_used: 5.into(),
log_bloom: Default::default(),
logs: vec![logs[0].clone()],
}, Receipt {
state_root: state_root,
status_code: None,
outcome: TransactionOutcome::StateRoot(state_root),
gas_used: gas_used,
log_bloom: Default::default(),
logs: logs.clone(),
Expand Down Expand Up @@ -2182,7 +2180,7 @@ mod tests {
log_index: 2,
}],
log_bloom: Default::default(),
state_root: state_root,
outcome: TransactionOutcome::StateRoot(state_root),
});
}
}
5 changes: 2 additions & 3 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use db::{NUM_COLUMNS, COL_STATE};
use header::{Header as BlockHeader, BlockNumber};
use filter::Filter;
use log_entry::LocalizedLogEntry;
use receipt::{Receipt, LocalizedReceipt};
use receipt::{Receipt, LocalizedReceipt, TransactionOutcome};
use blockchain::extras::BlockReceipts;
use error::{ImportResult, Error as EthcoreError};
use evm::{Factory as EvmFactory, VMType};
Expand Down Expand Up @@ -618,8 +618,7 @@ impl BlockChainClient for TestBlockChainClient {
// starts with 'f' ?
if *hash > H256::from("f000000000000000000000000000000000000000000000000000000000000000") {
let receipt = BlockReceipts::new(vec![Receipt::new(
Some(H256::zero()),
None,
TransactionOutcome::StateRoot(H256::zero()),
U256::zero(),
vec![])]);
let mut rlp = RlpStream::new();
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ impl MinerService for Miner {
},
logs: receipt.logs.clone(),
log_bloom: receipt.log_bloom,
state_root: receipt.state_root,
outcome: receipt.outcome.clone(),
}
})
}
Expand Down
20 changes: 9 additions & 11 deletions ethcore/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::fmt;
use std::sync::Arc;
use hash::{KECCAK_NULL_RLP, KECCAK_EMPTY};

use receipt::Receipt;
use receipt::{Receipt, TransactionOutcome};
use engines::Engine;
use vm::EnvInfo;
use error::Error;
Expand Down Expand Up @@ -699,21 +699,19 @@ impl<B: Backend> State<B> {
eip658 ||
(env_info.number >= engine.params().eip98_transition && env_info.number >= engine.params().validate_receipts_transition);

let state_root = if no_intermediate_commits {
None
let outcome = if no_intermediate_commits {
if eip658 {
TransactionOutcome::StatusCode(if e.exception.is_some() { 0 } else { 1 })
} else {
TransactionOutcome::Unknown
}
} else {
self.commit()?;
Some(self.root().clone())
};

let status_byte = if eip658 {
Some(if e.exception.is_some() { 0 } else { 1 })
} else {
None
TransactionOutcome::StateRoot(self.root().clone())
};

let output = e.output;
let receipt = Receipt::new(state_root, status_byte, e.cumulative_gas_used, e.logs);
let receipt = Receipt::new(outcome, e.cumulative_gas_used, e.logs);
trace!(target: "state", "Transaction receipt: {:?}", receipt);

Ok(ApplyOutcome {
Expand Down
103 changes: 64 additions & 39 deletions ethcore/types/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,56 @@ use rlp::*;
use {BlockNumber};
use log_entry::{LogBloom, LogEntry, LocalizedLogEntry};

/// Transaction outcome store in the receipt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransactionOutcome {
/// Status and state root are unknown under EIP-98 rules.
Unknown,
/// State root is known. Pre EIP-98 and EIP-658 rules.
StateRoot(H256),
/// Status code is known. EIP-658 rules.
StatusCode(u8),
}

/// Information describing execution of a transaction.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Receipt {
/// The state root after executing the transaction. Optional since EIP98
pub state_root: Option<H256>,
/// The total gas used in the block following execution of the transaction.
pub gas_used: U256,
/// The OR-wide combination of all logs' blooms for this transaction.
pub log_bloom: LogBloom,
/// The logs stemming from this transaction.
pub logs: Vec<LogEntry>,
/// Status byte. Optional before EIP-658.
pub status_code: Option<u8>,
/// Transaction outcome.
pub outcome: TransactionOutcome,
}

impl Receipt {
/// Create a new receipt.
pub fn new(state_root: Option<H256>, status_code: Option<u8>, gas_used: U256, logs: Vec<LogEntry>) -> Receipt {
pub fn new(outcome: TransactionOutcome, gas_used: U256, logs: Vec<LogEntry>) -> Receipt {
Receipt {
state_root: state_root,
gas_used: gas_used,
log_bloom: logs.iter().fold(LogBloom::default(), |mut b, l| { b = &b | &l.bloom(); b }), //TODO: use |= operator
logs: logs,
status_code: status_code,
outcome: outcome,
}
}
}

impl Encodable for Receipt {
fn rlp_append(&self, s: &mut RlpStream) {
if let Some(ref status) = self.status_code {
s.begin_list(4);
s.append(status);
} else if let Some(ref root) = self.state_root {
s.begin_list(4);
s.append(root);
} else {
s.begin_list(3);
match self.outcome {
TransactionOutcome::Unknown => {
s.begin_list(3);
},
TransactionOutcome::StateRoot(ref root) => {
s.begin_list(4);
s.append(root);
},
TransactionOutcome::StatusCode(ref status_code) => {
s.begin_list(4);
s.append(status_code);
},
}
s.append(&self.gas_used);
s.append(&self.log_bloom);
Expand All @@ -74,28 +86,25 @@ impl Decodable for Receipt {
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
if rlp.item_count()? == 3 {
Ok(Receipt {
state_root: None,
status_code: None,
outcome: TransactionOutcome::Unknown,
gas_used: rlp.val_at(0)?,
log_bloom: rlp.val_at(1)?,
logs: rlp.list_at(2)?,
})
} else {
let mut receipt = Receipt {
Ok(Receipt {
gas_used: rlp.val_at(1)?,
log_bloom: rlp.val_at(2)?,
logs: rlp.list_at(3)?,
state_root: None,
status_code: None,
};

let first = rlp.at(0)?;
if first.is_data() && first.data()?.len() == 1 {
receipt.status_code = Some(first.as_val()?);
} else {
receipt.state_root = Some(first.as_val()?);
}
Ok(receipt)
outcome: {
let first = rlp.at(0)?;
if first.is_data() && first.data()?.len() <= 1 {
TransactionOutcome::StatusCode(first.as_val()?)
} else {
TransactionOutcome::StateRoot(first.as_val()?)
}
}
})
}
}
}
Expand Down Expand Up @@ -123,8 +132,8 @@ pub struct RichReceipt {
pub logs: Vec<LogEntry>,
/// Logs bloom
pub log_bloom: LogBloom,
/// State root
pub state_root: Option<H256>,
/// Transaction outcome.
pub outcome: TransactionOutcome,
}

/// Receipt with additional info.
Expand All @@ -148,21 +157,20 @@ pub struct LocalizedReceipt {
pub logs: Vec<LocalizedLogEntry>,
/// Logs bloom
pub log_bloom: LogBloom,
/// State root
pub state_root: Option<H256>,
/// Transaction outcome.
pub outcome: TransactionOutcome,
}

#[cfg(test)]
mod tests {
use super::Receipt;
use super::{Receipt, TransactionOutcome};
use log_entry::LogEntry;

#[test]
fn test_no_state_root() {
let expected = ::rustc_hex::FromHex::from_hex("f9014183040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let r = Receipt::new(
None,
None,
TransactionOutcome::Unknown,
0x40cae.into(),
vec![LogEntry {
address: "dcf421d093428b096ca501a7cd1a740855a7976f".into(),
Expand All @@ -177,8 +185,25 @@ mod tests {
fn test_basic() {
let expected = ::rustc_hex::FromHex::from_hex("f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let r = Receipt::new(
Some("2f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee".into()),
None,
TransactionOutcome::StateRoot("2f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee".into()),
0x40cae.into(),
vec![LogEntry {
address: "dcf421d093428b096ca501a7cd1a740855a7976f".into(),
topics: vec![],
data: vec![0u8; 32]
}]
);
let encoded = ::rlp::encode(&r);
assert_eq!(&encoded[..], &expected[..]);
let decoded: Receipt = ::rlp::decode(&encoded);
assert_eq!(decoded, r);
}

#[test]
fn test_status_code() {
let expected = ::rustc_hex::FromHex::from_hex("f901428083040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let r = Receipt::new(
TransactionOutcome::StatusCode(0),
0x40cae.into(),
vec![LogEntry {
address: "dcf421d093428b096ca501a7cd1a740855a7976f".into(),
Expand Down
Loading

0 comments on commit 732d50e

Please sign in to comment.