From 5b962f3c06945aca5b537354bcb78a25a9cc3e54 Mon Sep 17 00:00:00 2001 From: Linda Guiga Date: Thu, 17 Aug 2023 22:32:35 +0100 Subject: [PATCH] Change receipts_trie in basic_smart_contract and self_balance_gas_cost --- evm/tests/basic_smart_contract.rs | 23 ++++++++++++++++++----- evm/tests/self_balance_gas_cost.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/evm/tests/basic_smart_contract.rs b/evm/tests/basic_smart_contract.rs index 60cb9878..89aacd2c 100644 --- a/evm/tests/basic_smart_contract.rs +++ b/evm/tests/basic_smart_contract.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::str::FromStr; use std::time::Duration; use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV}; @@ -13,7 +14,7 @@ use plonky2::util::timing::TimingTree; use plonky2_evm::all_stark::AllStark; use plonky2_evm::config::StarkConfig; use plonky2_evm::cpu::kernel::opcodes::{get_opcode, get_push_opcode}; -use plonky2_evm::generation::mpt::AccountRlp; +use plonky2_evm::generation::mpt::{AccountRlp, LegacyReceiptRlp}; use plonky2_evm::generation::{GenerationInputs, TrieInputs}; use plonky2_evm::proof::{BlockMetadata, TrieRoots}; use plonky2_evm::prover::prove; @@ -102,10 +103,9 @@ fn test_basic_smart_contract() -> anyhow::Result<()> { contract_code.insert(keccak(vec![]), vec![]); contract_code.insert(code_hash, code.to_vec()); + let txdata_gas = 2 * 16; + let gas_used = 21_000 + code_gas + txdata_gas; let expected_state_trie_after: HashedPartialTrie = { - let txdata_gas = 2 * 16; - let gas_used = 21_000 + code_gas + txdata_gas; - let beneficiary_account_after = AccountRlp { balance: beneficiary_account_before.balance + gas_used * 10, ..beneficiary_account_before @@ -142,10 +142,23 @@ fn test_basic_smart_contract() -> anyhow::Result<()> { } } .into(); + + let receipt_0 = LegacyReceiptRlp { + status: true, + cum_gas_used: gas_used.into(), + bloom: vec![0; 256].into(), + logs: vec![], + }; + let mut receipts_trie = HashedPartialTrie::from(Node::Empty); + receipts_trie.insert( + Nibbles::from_str("0x80").unwrap(), + rlp::encode(&receipt_0).to_vec(), + ); + let trie_roots_after = TrieRoots { state_root: expected_state_trie_after.hash(), transactions_root: tries_before.transactions_trie.hash(), // TODO: Fix this when we have transactions trie. - receipts_root: tries_before.receipts_trie.hash(), // TODO: Fix this when we have receipts trie. + receipts_root: receipts_trie.hash(), }; let inputs = GenerationInputs { signed_txns: vec![txn.to_vec()], diff --git a/evm/tests/self_balance_gas_cost.rs b/evm/tests/self_balance_gas_cost.rs index ddf9a2a4..19a7fcf2 100644 --- a/evm/tests/self_balance_gas_cost.rs +++ b/evm/tests/self_balance_gas_cost.rs @@ -13,7 +13,7 @@ use plonky2::plonk::config::KeccakGoldilocksConfig; use plonky2::util::timing::TimingTree; use plonky2_evm::all_stark::AllStark; use plonky2_evm::config::StarkConfig; -use plonky2_evm::generation::mpt::AccountRlp; +use plonky2_evm::generation::mpt::{AccountRlp, LegacyReceiptRlp}; use plonky2_evm::generation::{GenerationInputs, TrieInputs}; use plonky2_evm::proof::{BlockMetadata, TrieRoots}; use plonky2_evm::prover::prove; @@ -48,6 +48,18 @@ fn self_balance_gas_cost() -> anyhow::Result<()> { let code = [ 0x5a, 0x47, 0x5a, 0x90, 0x50, 0x90, 0x03, 0x60, 0x02, 0x90, 0x03, 0x60, 0x01, 0x55, 0x00, ]; + let code_gas = 2 // GAS + + 5 // SELFBALANCE + + 2 // GAS + + 3 // SWAP1 + + 2 // POP + + 3 // SWAP1 + + 3 // SUB + + 3 // PUSH1 + + 3 // SWAP1 + + 3 // SUB + + 3 // PUSH1 + + 22100; // SSTORE let code_hash = keccak(code); let beneficiary_account_before = AccountRlp::default(); @@ -120,10 +132,22 @@ fn self_balance_gas_cost() -> anyhow::Result<()> { expected_state_trie_after }; + let gas_used = 21_000 + code_gas; + let receipt_0 = LegacyReceiptRlp { + status: true, + cum_gas_used: gas_used.into(), + bloom: vec![0; 256].into(), + logs: vec![], + }; + let mut receipts_trie = HashedPartialTrie::from(Node::Empty); + receipts_trie.insert( + Nibbles::from_str("0x80").unwrap(), + rlp::encode(&receipt_0).to_vec(), + ); let trie_roots_after = TrieRoots { state_root: expected_state_trie_after.hash(), transactions_root: tries_before.transactions_trie.hash(), // TODO: Fix this when we have transactions trie. - receipts_root: tries_before.receipts_trie.hash(), // TODO: Fix this when we have receipts trie. + receipts_root: receipts_trie.hash(), }; let inputs = GenerationInputs { signed_txns: vec![txn.to_vec()],