Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(consensus): correct receipts root #1270

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ impl Executor for AxonExecutor {
gas += r.gas_used;
fee = fee.checked_add(r.fee_cost).unwrap_or(U256::max_value());

hashes.push(Hasher::digest(&r.ret));
let logs_bloom = logs_bloom(r.logs.iter());
let receipt = tx.encode_receipt(&r, logs_bloom);

hashes.push(Hasher::digest(&receipt));
res.push(r);
}

Expand Down Expand Up @@ -354,7 +357,10 @@ impl AxonExecutor {
gas += r.gas_used;
fee = fee.checked_add(r.fee_cost).unwrap_or(U256::max_value());

hashes.push(Hasher::digest(&r.ret));
let logs_bloom = logs_bloom(r.logs.iter());
let receipt = tx.encode_receipt(&r, logs_bloom);

hashes.push(Hasher::digest(&receipt));
res.push(r);
}

Expand Down
2 changes: 1 addition & 1 deletion core/run/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const GENESIS_HASH: &str = "0x69eca8420bb4b072d732db96260a656f0e10201c6841b215a8
const GENESIS_STATE_ROOT: &str =
"0x65f57a6a666e656de33ed68957e04b35b3fe1b35a90f6eafb6f283c907dc3d77";
const GENESIS_RECEIPTS_ROOT: &str =
"0x0abcdb8fd7acc8c71f28a16c3095fdafca0171f371076650152b1c356a1bccad";
"0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b";

#[test]
fn decode_genesis() {
Expand Down
48 changes: 47 additions & 1 deletion protocol/src/types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use serde::{Deserialize, Serialize};

use common_crypto::secp256k1_recover;

use crate::types::{Bytes, BytesMut, Hash, Hasher, Public, TypesError, H160, H256, H520, U256};
use crate::types::{
Bloom, Bytes, BytesMut, ExitReason, Hash, Hasher, Public, TxResp, TypesError, H160, H256, H520,
U256,
};
use crate::ProtocolResult;

pub const GAS_PER_ZERO_BYTE: u64 = 4;
Expand Down Expand Up @@ -468,6 +471,49 @@ impl SignedTransaction {
pub fn get_to(&self) -> Option<H160> {
self.transaction.unsigned.to()
}

/// Encode a transaction receipt into bytes.
///
/// According to [`EIP-2718`]:
/// - `Receipt` is either `TransactionType || ReceiptPayload` or
/// `LegacyReceipt`.
/// - `LegacyReceipt` is kept to be RLP encoded bytes; it is `rlp([status,
/// cumulativeGasUsed, logsBloom, logs])`.
/// - `ReceiptPayload` is an opaque byte array whose interpretation is
/// dependent on the `TransactionType` and defined in future EIPs.
/// - As [`EIP-2930`] defined: if `TransactionType` is `1`,
/// `ReceiptPayload` is `rlp([status, cumulativeGasUsed, logsBloom,
/// logs])`.
/// - As [`EIP-1559`] defined: if `TransactionType` is `2`,
/// `ReceiptPayload` is `rlp([status, cumulative_transaction_gas_used,
/// logs_bloom, logs])`.
///
/// [`EIP-2718`]: https://eips.ethereum.org/EIPS/eip-2718#receipts
/// [`EIP-2930`]: https://eips.ethereum.org/EIPS/eip-2930#parameters
/// [`EIP-1559`]: https://eips.ethereum.org/EIPS/eip-1559#specification
pub fn encode_receipt(&self, r: &TxResp, logs_bloom: Bloom) -> Bytes {
// Status: either 1 (success) or 0 (failure).
// Only present after activation of [EIP-658](https://eips.ethereum.org/EIPS/eip-658)
let status: u64 = if matches!(r.exit_reason, ExitReason::Succeed(_)) {
1
} else {
0
};
let used_gas = U256::from(r.gas_used);
let legacy_receipt = {
let mut rlp = RlpStream::new();
rlp.begin_list(4);
rlp.append(&status);
rlp.append(&used_gas);
rlp.append(&logs_bloom);
rlp.append_list(&r.logs);
rlp.out().freeze()
};
match self.type_() {
x if x == 0x01 || x == 0x02 => [&x.to_be_bytes()[7..], &legacy_receipt].concat().into(),
_ => legacy_receipt, // legacy (0x00) or undefined type
}
}
}

pub fn public_to_address(public: &Public) -> H160 {
Expand Down