From cd36e96cb844f3042aa7acf9c03ea8e66735c904 Mon Sep 17 00:00:00 2001 From: Robin Salen <30937548+Nashtare@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:59:45 -0400 Subject: [PATCH] Derive clone for txn RLP structs (#1264) * Derive Clone for txn rlp structs * Put txn rlp related structs behind testing module * Move module to end of file --- evm/src/generation/mpt.rs | 164 +++++++++++++++++++------------------- evm/tests/log_opcode.rs | 5 +- 2 files changed, 86 insertions(+), 83 deletions(-) diff --git a/evm/src/generation/mpt.rs b/evm/src/generation/mpt.rs index 1f1be2b9..f829c4e2 100644 --- a/evm/src/generation/mpt.rs +++ b/evm/src/generation/mpt.rs @@ -33,92 +33,14 @@ impl Default for AccountRlp { } } -#[derive(RlpEncodable, RlpDecodable, Debug)] -pub struct AccessListItemRlp { - pub address: Address, - pub storage_keys: Vec, -} - -#[derive(Debug)] -pub struct AddressOption(pub Option
); - -impl Encodable for AddressOption { - fn rlp_append(&self, s: &mut RlpStream) { - match self.0 { - None => { - s.append_empty_data(); - } - Some(value) => { - s.encoder().encode_value(&value.to_fixed_bytes()); - } - } - } -} - -impl Decodable for AddressOption { - fn decode(rlp: &Rlp) -> Result { - if rlp.is_int() && rlp.is_empty() { - return Ok(AddressOption(None)); - } - if rlp.is_data() && rlp.size() == 20 { - return Ok(AddressOption(Some(Address::decode(rlp)?))); - } - Err(DecoderError::RlpExpectedToBeData) - } -} - -#[derive(RlpEncodable, RlpDecodable, Debug)] -pub struct LegacyTransactionRlp { - pub nonce: U256, - pub gas_price: U256, - pub gas: U256, - pub to: AddressOption, - pub value: U256, - pub data: Bytes, - pub v: U256, - pub r: U256, - pub s: U256, -} - -#[derive(RlpEncodable, RlpDecodable, Debug)] -pub struct AccessListTransactionRlp { - pub chain_id: u64, - pub nonce: U256, - pub gas_price: U256, - pub gas: U256, - pub to: AddressOption, - pub value: U256, - pub data: Bytes, - pub access_list: Vec, - pub y_parity: U256, - pub r: U256, - pub s: U256, -} - -#[derive(RlpEncodable, RlpDecodable, Debug)] -pub struct FeeMarketTransactionRlp { - 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: AddressOption, - pub value: U256, - pub data: Bytes, - pub access_list: Vec, - pub y_parity: U256, - pub r: U256, - pub s: U256, -} - -#[derive(RlpEncodable, RlpDecodable, Debug)] +#[derive(RlpEncodable, RlpDecodable, Debug, Clone)] pub struct LogRlp { pub address: Address, pub topics: Vec, pub data: Bytes, } -#[derive(RlpEncodable, RlpDecodable, Debug)] +#[derive(RlpEncodable, RlpDecodable, Debug, Clone)] pub struct LegacyReceiptRlp { pub status: bool, pub cum_gas_used: U256, @@ -356,3 +278,85 @@ fn empty_nibbles() -> Nibbles { packed: U512::zero(), } } + +pub mod transaction_testing { + use super::*; + + #[derive(RlpEncodable, RlpDecodable, Debug, Clone)] + pub struct AccessListItemRlp { + pub address: Address, + pub storage_keys: Vec, + } + + #[derive(Debug, Clone)] + pub struct AddressOption(pub Option
); + + impl Encodable for AddressOption { + fn rlp_append(&self, s: &mut RlpStream) { + match self.0 { + None => { + s.append_empty_data(); + } + Some(value) => { + s.encoder().encode_value(&value.to_fixed_bytes()); + } + } + } + } + + impl Decodable for AddressOption { + fn decode(rlp: &Rlp) -> Result { + if rlp.is_int() && rlp.is_empty() { + return Ok(AddressOption(None)); + } + if rlp.is_data() && rlp.size() == 20 { + return Ok(AddressOption(Some(Address::decode(rlp)?))); + } + Err(DecoderError::RlpExpectedToBeData) + } + } + + #[derive(RlpEncodable, RlpDecodable, Debug, Clone)] + pub struct LegacyTransactionRlp { + pub nonce: U256, + pub gas_price: U256, + pub gas: U256, + pub to: AddressOption, + pub value: U256, + pub data: Bytes, + pub v: U256, + pub r: U256, + pub s: U256, + } + + #[derive(RlpEncodable, RlpDecodable, Debug, Clone)] + pub struct AccessListTransactionRlp { + pub chain_id: u64, + pub nonce: U256, + pub gas_price: U256, + pub gas: U256, + pub to: AddressOption, + pub value: U256, + pub data: Bytes, + pub access_list: Vec, + pub y_parity: U256, + pub r: U256, + pub s: U256, + } + + #[derive(RlpEncodable, RlpDecodable, Debug, Clone)] + pub struct FeeMarketTransactionRlp { + 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: AddressOption, + pub value: U256, + pub data: Bytes, + pub access_list: Vec, + pub y_parity: U256, + pub r: U256, + pub s: U256, + } +} diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index b9d587b3..3a0e5abf 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -17,9 +17,8 @@ use plonky2::util::timing::TimingTree; use plonky2_evm::all_stark::AllStark; use plonky2_evm::config::StarkConfig; use plonky2_evm::fixed_recursive_verifier::AllRecursiveCircuits; -use plonky2_evm::generation::mpt::{ - AccountRlp, AddressOption, LegacyReceiptRlp, LegacyTransactionRlp, LogRlp, -}; +use plonky2_evm::generation::mpt::transaction_testing::{AddressOption, LegacyTransactionRlp}; +use plonky2_evm::generation::mpt::{AccountRlp, LegacyReceiptRlp, LogRlp}; use plonky2_evm::generation::{GenerationInputs, TrieInputs}; use plonky2_evm::proof::{BlockHashes, BlockMetadata, ExtraBlockData, PublicValues, TrieRoots}; use plonky2_evm::prover::prove;