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

Derive clone for txn RLP structs #1264

Merged
merged 3 commits into from
Oct 2, 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
164 changes: 84 additions & 80 deletions evm/src/generation/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,92 +33,14 @@ impl Default for AccountRlp {
}
}

#[derive(RlpEncodable, RlpDecodable, Debug)]
pub struct AccessListItemRlp {
pub address: Address,
pub storage_keys: Vec<U256>,
}

#[derive(Debug)]
pub struct AddressOption(pub Option<Address>);

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<Self, DecoderError> {
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<AccessListItemRlp>,
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<AccessListItemRlp>,
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<H256>,
pub data: Bytes,
}

#[derive(RlpEncodable, RlpDecodable, Debug)]
#[derive(RlpEncodable, RlpDecodable, Debug, Clone)]
pub struct LegacyReceiptRlp {
pub status: bool,
pub cum_gas_used: U256,
Expand Down Expand Up @@ -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<U256>,
}

#[derive(Debug, Clone)]
pub struct AddressOption(pub Option<Address>);

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<Self, DecoderError> {
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<AccessListItemRlp>,
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<AccessListItemRlp>,
pub y_parity: U256,
pub r: U256,
pub s: U256,
}
}
5 changes: 2 additions & 3 deletions evm/tests/log_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading