Skip to content

Commit

Permalink
Adapt for Cancun HF
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Mar 11, 2024
1 parent 916b682 commit f328cb5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 11 deletions.
6 changes: 2 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ tokio = { version = "1.28.1" }

# zk-evm dependencies
plonky2 = "0.2.0"
mpt_trie = "0.1.1"
evm_arithmetization = "0.1.1"
mpt_trie = { git = "https://github.com/0xPolygonZero/zk_evm", branch = "feat/cancun" }
evm_arithmetization = { git = "https://github.com/0xPolygonZero/zk_evm", branch = "feat/cancun" }

[profile.release]
opt-level = 3
Expand Down
1 change: 1 addition & 0 deletions common/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl ParsedTestManifest {
gas_used_after: t_var.plonky2_metadata.block_metadata.block_gas_used,
withdrawals: t_var.plonky2_metadata.withdrawals,
block_hashes: BlockHashes::default(),
global_exit_roots: vec![], // not part of Ethereum tests
};

TestVariantRunInfo {
Expand Down
68 changes: 63 additions & 5 deletions eth_test_parser/src/deserialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use anyhow::Result;
use bytes::Bytes;
use ethereum_types::{Address, H160, H256, U256};
use evm_arithmetization::generation::mpt::transaction_testing::{
AccessListItemRlp, AccessListTransactionRlp, AddressOption, FeeMarketTransactionRlp,
LegacyTransactionRlp,
AccessListItemRlp, AccessListTransactionRlp, AddressOption, BlobTransactionRlp,
FeeMarketTransactionRlp, LegacyTransactionRlp,
};
use hex::FromHex;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
Expand Down Expand Up @@ -103,6 +103,9 @@ pub(crate) struct BlockHeader {
pub(crate) _nonce: Vec<u8>,
pub(crate) base_fee_per_gas: U256,
pub(crate) _withdrawals_root: FieldOption<H256>,
pub(crate) blob_gas_used: U256,
pub(crate) excess_blob_gas: U256,
pub(crate) parent_beacon_block_root: H256,
}

// Some tests store the access list in a way that doesn't respect the specs,
Expand Down Expand Up @@ -239,11 +242,57 @@ impl CustomFeeMarketTransactionRlp {
}
}

// A custom type-2 txn to handle some edge-cases with the access_list field.
#[derive(RlpDecodable, Debug, Clone)]
pub struct CustomBlobTransactionRlp {
pub chain_id: u64,
pub nonce: U256,
pub max_priority_fee_per_gas: U256,
pub max_fee_per_gas: U256,
pub gas: U256,
pub to: H160,
pub value: U256,
pub data: Bytes,
pub access_list: Vec<AccessItemRlp>,
pub max_fee_per_blob_gas: U256,
pub blob_versioned_hashes: Vec<H256>,
pub y_parity: U256,
pub r: U256,
pub s: U256,
}

impl CustomBlobTransactionRlp {
fn into_regular(self) -> BlobTransactionRlp {
BlobTransactionRlp {
chain_id: self.chain_id,
nonce: self.nonce,
max_priority_fee_per_gas: self.max_priority_fee_per_gas,
max_fee_per_gas: self.max_fee_per_gas,
gas: self.gas,
to: self.to,
value: self.value,
data: self.data.clone(),
access_list: self
.access_list
.clone()
.into_iter()
.map(|x| x.into_regular())
.collect(),
max_fee_per_blob_gas: self.max_fee_per_blob_gas,
blob_versioned_hashes: self.blob_versioned_hashes,
y_parity: self.y_parity,
r: self.r,
s: self.s,
}
}
}

#[derive(Clone, Debug)]
pub enum Transaction {
Legacy(LegacyTransactionRlp),
AccessList(AccessListTransactionRlp),
FeeMarket(FeeMarketTransactionRlp),
Blob(BlobTransactionRlp),
}

impl Transaction {
Expand All @@ -254,6 +303,8 @@ impl Transaction {
.map(|tx| Transaction::AccessList(tx.into_regular())),
2 => CustomFeeMarketTransactionRlp::decode(&Rlp::new(&bytes[1..]))
.map(|tx| Transaction::FeeMarket(tx.into_regular())),
3 => CustomBlobTransactionRlp::decode(&Rlp::new(&bytes[1..]))
.map(|tx| Transaction::Blob(tx.into_regular())),
_ => LegacyTransactionRlp::decode(&Rlp::new(bytes)).map(Transaction::Legacy),
}
}
Expand All @@ -271,6 +322,10 @@ impl Encodable for Transaction {
s.encoder().encode_value(&[0x02]);
s.append(tx)
}
Transaction::Blob(tx) => {
s.encoder().encode_value(&[0x03]);
s.append(tx)
}
};
}
}
Expand All @@ -283,6 +338,8 @@ impl Decodable for Transaction {
.map(|tx| Transaction::AccessList(tx.into_regular())),
2 => CustomFeeMarketTransactionRlp::decode(&Rlp::new(&rlp.as_raw()[1..]))
.map(|tx| Transaction::FeeMarket(tx.into_regular())),
3 => CustomBlobTransactionRlp::decode(&Rlp::new(&rlp.as_raw()[1..]))
.map(|tx| Transaction::Blob(tx.into_regular())),
_ => LegacyTransactionRlp::decode(rlp).map(Transaction::Legacy),
};

Expand Down Expand Up @@ -360,6 +417,7 @@ pub(crate) struct TestBody {

impl TestBody {
fn from_parsed_json(value: &ValueJson, variant_name: String) -> Self {
log::info!("Parsing {:?}", variant_name);
let block: Block = rlp::decode(&value.blocks[0].rlp.0).unwrap();
let genesis_block: GenesisBlock =
rlp::decode(&value.genesis_rlp.as_ref().unwrap().0).unwrap();
Expand Down Expand Up @@ -388,7 +446,7 @@ struct ValueJson {
}

// Wrapper around a regular `HashMap` used to conveniently skip
// non-Shanghai related tests when deserializing.
// non-Cancun related tests when deserializing.
#[derive(Default, Debug)]
pub(crate) struct TestFile(pub(crate) HashMap<String, TestBody>);

Expand Down Expand Up @@ -423,9 +481,9 @@ impl<'de> Deserialize<'de> for TestFile {
let mut map = TestFile(HashMap::with_capacity(access.size_hint().unwrap_or(0)));

// While we are parsing many values, we only care about the ones containing
// `Shanghai` in their key name.
// `Cancun` in their key name.
while let Some((key, value)) = access.next_entry::<String, ValueJson>()? {
if key.contains("Shanghai") {
if key.contains("_Cancun") {
if value.blocks[0].transaction_sequence.is_none() {
let test_body = TestBody::from_parsed_json(&value, key.clone());

Expand Down
5 changes: 5 additions & 0 deletions eth_test_parser/src/trie_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl Block {
block_base_fee: header.base_fee_per_gas,
block_random: header.mix_hash,
block_gas_used: header.gas_used,
// TODO: Should be removed from block header on `zk_evm` side.
block_blob_base_fee: 0.into(),
block_blob_gas_used: header.blob_gas_used,
block_excess_blob_gas: header.excess_blob_gas,
parent_beacon_block_root: header.parent_beacon_block_root,
block_bloom: header
.bloom
.chunks_exact(32)
Expand Down

0 comments on commit f328cb5

Please sign in to comment.