Skip to content

Commit

Permalink
feat: sync receipt trie with eip-2718
Browse files Browse the repository at this point in the history
  • Loading branch information
wenyuanhust committed Oct 20, 2023
1 parent ec26fab commit ca97190
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
11 changes: 11 additions & 0 deletions common/merkle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@ impl TrieMerkle {
let ret = self.0.get_proof(&key)?;
Ok(ret)
}

pub fn from_receipts(receipts: &Vec<Bytes>) -> Self {
let mut trie = Self::default();
for (i, receipt) in receipts.iter().enumerate() {
trie.0
.insert(rlp::encode(&i).to_vec(), receipt.to_vec())
.unwrap();
}

trie
}
}
97 changes: 91 additions & 6 deletions core/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub use crate::utils::{code_address, decode_revert_msg, DefaultFeeAllocator, Fee

use std::cell::RefCell;
use std::collections::BTreeMap;
use std::iter::FromIterator;

use arc_swap::ArcSwap;
use common_config_parser::types::spec::HardforkName;
Expand All @@ -28,9 +27,8 @@ use evm::CreateScheme;
use common_merkle::TrieMerkle;
use protocol::traits::{Backend, Executor, ExecutorAdapter};
use protocol::types::{
data_gas_cost, logs_bloom, Config, ExecResp, Hasher, SignedTransaction, TransactionAction,
TxResp, ValidatorExtend, GAS_CALL_TRANSACTION, GAS_CREATE_TRANSACTION, H160, H256, RLP_NULL,
U256,
data_gas_cost, logs_bloom, Config, ExecResp, SignedTransaction, TransactionAction, TxResp,
ValidatorExtend, GAS_CALL_TRANSACTION, GAS_CREATE_TRANSACTION, H160, H256, RLP_NULL, U256,
};

use crate::precompiles::build_precompile_set;
Expand All @@ -40,6 +38,9 @@ use crate::system_contract::{
METADATA_CONTRACT_ADDRESS, METADATA_ROOT_KEY, NATIVE_TOKEN_CONTRACT_ADDRESS,
};

#[cfg(test)]
use {protocol::types::Hasher, std::iter::FromIterator};

lazy_static::lazy_static! {
pub static ref FEE_ALLOCATOR: ArcSwap<Box<dyn FeeAllocate>> = ArcSwap::from_pointee(Box::new(DefaultFeeAllocator));
}
Expand Down Expand Up @@ -161,7 +162,7 @@ impl Executor for AxonExecutor {
let logs_bloom = logs_bloom(r.logs.iter());
let receipt = tx.encode_receipt(&r, logs_bloom);

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

Expand Down Expand Up @@ -192,7 +193,7 @@ impl Executor for AxonExecutor {
let receipt_root = if hashes.is_empty() {
RLP_NULL
} else {
TrieMerkle::from_iter(hashes.iter().enumerate())
TrieMerkle::from_receipts(&hashes)
.root_hash()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for receipts since {err}")
Expand All @@ -208,6 +209,90 @@ impl Executor for AxonExecutor {
}
}

#[cfg(test)]
#[test]
fn test_receipt() {
use evm::{ExitReason, ExitSucceed};
use protocol::types::{Eip1559Transaction, UnsignedTransaction, UnverifiedTransaction};

let eip1559_tx = Eip1559Transaction {
nonce: Default::default(),
max_priority_fee_per_gas: Default::default(),
gas_price: Default::default(),
gas_limit: Default::default(),
action: TransactionAction::Create,
value: Default::default(),
data: Default::default(),
access_list: Default::default(),
};
let unsigned_tx = UnsignedTransaction::Eip1559(eip1559_tx);
let unverified_tx = UnverifiedTransaction {
unsigned: unsigned_tx,
signature: Default::default(),
chain_id: Default::default(),
hash: Default::default(),
};
let tx = SignedTransaction {
transaction: unverified_tx,
sender: Default::default(),
public: Default::default(),
};

let exit_reason = ExitReason::Succeed(ExitSucceed::Stopped);
let log = evm::backend::Log {
address: Default::default(),
topics: Default::default(),
data: Default::default(),
};
let tx_resp = TxResp {
exit_reason,
ret: Default::default(),
gas_used: 10,
remain_gas: Default::default(),
fee_cost: Default::default(),
logs: vec![log],
code_address: Default::default(),
removed: Default::default(),
};

let logs_bloom = logs_bloom(tx_resp.logs.iter());
let receipt = tx.encode_receipt(&tx_resp, logs_bloom);

let reference_encode: Vec<u8> = [
2 as u8, 249, 1, 30, 1, 10, 185, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216,
215, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 128,
]
.to_vec();

assert_eq!(receipt.to_vec(), reference_encode);

let hashes = vec![receipt];

let receipt_root = if hashes.is_empty() {
RLP_NULL
} else {
TrieMerkle::from_receipts(&hashes)
.root_hash()
.unwrap_or_else(|err| {
panic!("failed to calculate trie root hash for receipts since {err}")
})
};

let reference_root = [
197 as u8, 180, 204, 76, 181, 157, 142, 152, 246, 237, 148, 126, 24, 207, 94, 119, 119,
205, 11, 16, 193, 17, 102, 157, 61, 7, 166, 133, 173, 208, 124, 6,
];
assert_eq!(receipt_root, H256::from(reference_root));
}

impl AxonExecutor {
pub fn evm_exec<Adapter: ExecutorAdapter>(
adapter: &mut Adapter,
Expand Down

0 comments on commit ca97190

Please sign in to comment.