Skip to content

Commit

Permalink
Fix encoding of addresses for contract creation
Browse files Browse the repository at this point in the history
Co-authored-by: Hamy Ratoanina <[email protected]>
  • Loading branch information
Nashtare and hratoanina committed Sep 26, 2023
1 parent 50e174a commit 3f03d88
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
36 changes: 32 additions & 4 deletions evm/src/generation/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
use ethereum_types::{Address, BigEndianHash, H256, U256, U512};
use keccak_hash::keccak;
use rlp::PayloadInfo;
use rlp::{Decodable, DecoderError, Encodable, PayloadInfo, Rlp, RlpStream};
use rlp_derive::{RlpDecodable, RlpEncodable};

use crate::cpu::kernel::constants::trie_type::PartialTrieType;
Expand Down Expand Up @@ -39,12 +39,40 @@ pub struct AccessListItemRlp {
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: Address,
pub to: AddressOption,
pub value: U256,
pub data: Bytes,
pub v: U256,
Expand All @@ -58,7 +86,7 @@ pub struct AccessListTransactionRlp {
pub nonce: U256,
pub gas_price: U256,
pub gas: U256,
pub to: Address,
pub to: AddressOption,
pub value: U256,
pub data: Bytes,
pub access_list: Vec<AccessListItemRlp>,
Expand All @@ -74,7 +102,7 @@ pub struct FeeMarketTransactionRlp {
pub max_priority_fee_per_gas: U256,
pub max_fee_per_gas: U256,
pub gas: U256,
pub to: Address,
pub to: AddressOption,
pub value: U256,
pub data: Bytes,
pub access_list: Vec<AccessListItemRlp>,
Expand Down
8 changes: 5 additions & 3 deletions evm/tests/log_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ 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, LegacyReceiptRlp, LegacyTransactionRlp, LogRlp};
use plonky2_evm::generation::mpt::{
AccountRlp, AddressOption, LegacyReceiptRlp, LegacyTransactionRlp, LogRlp,
};
use plonky2_evm::generation::{GenerationInputs, TrieInputs};
use plonky2_evm::proof::{BlockHashes, BlockMetadata, ExtraBlockData, PublicValues, TrieRoots};
use plonky2_evm::prover::prove;
Expand Down Expand Up @@ -631,7 +633,7 @@ fn test_txn_and_receipt_trie_hash() -> anyhow::Result<()> {
nonce: 157823u64.into(),
gas_price: 1000000000u64.into(),
gas: 250000u64.into(),
to: hex!("7ef66b77759e12Caf3dDB3E4AFF524E577C59D8D").into(),
to: AddressOption(Some(hex!("7ef66b77759e12Caf3dDB3E4AFF524E577C59D8D").into())),
value: 0u64.into(),
data: hex!("e9c6c176000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000bd9fe6f7af1cc94b1aef2e0fa15f1b4baefa86eb60e78fa4bd082372a0a446d197fb58")
.to_vec()
Expand All @@ -651,7 +653,7 @@ fn test_txn_and_receipt_trie_hash() -> anyhow::Result<()> {
nonce: 157824u64.into(),
gas_price: 1000000000u64.into(),
gas: 250000u64.into(),
to: hex!("7ef66b77759e12Caf3dDB3E4AFF524E577C59D8D").into(),
to: AddressOption(Some(hex!("7ef66b77759e12Caf3dDB3E4AFF524E577C59D8D").into())),
value: 0u64.into(),
data: hex!("e9c6c176000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000004920eaa814f7df6a2203dc0e472e8828be95957c6b329fee8e2b1bb6f044c1eb4fc243")
.to_vec()
Expand Down

0 comments on commit 3f03d88

Please sign in to comment.