Skip to content

Commit

Permalink
Fix self_balance_gas_cost and basic_smart_contract.
Browse files Browse the repository at this point in the history
  • Loading branch information
LindaGuiga committed Sep 11, 2023
1 parent 3571f09 commit 5eb6da0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
28 changes: 22 additions & 6 deletions evm/tests/basic_smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ fn test_basic_smart_contract() -> anyhow::Result<()> {
let code_gas = 3 + 3 + 3;
let code_hash = keccak(code);

let beneficiary_account_before = AccountRlp::default();
let beneficiary_account_before = AccountRlp {
nonce: 1.into(),
..AccountRlp::default()
};
let sender_account_before = AccountRlp {
nonce: 5.into(),
balance: eth_to_wei(100_000.into()),
Expand All @@ -66,6 +69,11 @@ fn test_basic_smart_contract() -> anyhow::Result<()> {

let state_trie_before = {
let mut children = core::array::from_fn(|_| Node::Empty.into());
children[beneficiary_nibbles.get_nibble(0) as usize] = Node::Leaf {
nibbles: beneficiary_nibbles.truncate_n_nibbles_front(1),
value: rlp::encode(&beneficiary_account_before).to_vec(),
}
.into();
children[sender_nibbles.get_nibble(0) as usize] = Node::Leaf {
nibbles: sender_nibbles.truncate_n_nibbles_front(1),
value: rlp::encode(&sender_account_before).to_vec(),
Expand All @@ -90,25 +98,33 @@ fn test_basic_smart_contract() -> anyhow::Result<()> {
storage_tries: vec![],
};

let txdata_gas = 2 * 16;
let gas_used = 21_000 + code_gas + txdata_gas;

// Generated using a little py-evm script.
let txn = hex!("f861050a8255f094a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0648242421ba02c89eb757d9deeb1f5b3859a9d4d679951ef610ac47ad4608dc142beb1b7e313a05af7e9fbab825455d36c36c7f4cfcafbeafa9a77bdff936b52afb36d4fe4bcdd");
let value = U256::from(100u32);

let block_metadata = BlockMetadata {
block_beneficiary: Address::from(beneficiary),
..BlockMetadata::default()
block_difficulty: 0x20000.into(),
block_number: 1.into(),
block_chain_id: 1.into(),
block_timestamp: 0x03e8.into(),
block_gaslimit: 0xff112233u32.into(),
block_gas_used: gas_used.into(),
block_bloom: [0.into(); 8],
block_base_fee: 0xa.into(),
};

let mut contract_code = HashMap::new();
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 beneficiary_account_after = AccountRlp {
balance: beneficiary_account_before.balance + gas_used * 10,
..beneficiary_account_before
nonce: 1.into(),
..AccountRlp::default()
};
let sender_account_after = AccountRlp {
balance: sender_account_before.balance - value - gas_used * 10,
Expand Down
24 changes: 19 additions & 5 deletions evm/tests/self_balance_gas_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;
use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV};
use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
use ethereum_types::{Address, H256};
use ethereum_types::{Address, H256, U256};
use hex_literal::hex;
use keccak_hash::keccak;
use plonky2::field::goldilocks_field::GoldilocksField;
Expand Down Expand Up @@ -62,7 +62,10 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
+ 22100; // SSTORE
let code_hash = keccak(code);

let beneficiary_account_before = AccountRlp::default();
let beneficiary_account_before = AccountRlp {
nonce: 1.into(),
..AccountRlp::default()
};
let sender_account_before = AccountRlp {
balance: 0x3635c9adc5dea00000u128.into(),
..AccountRlp::default()
Expand All @@ -89,8 +92,17 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {

let txn = hex!("f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509b");

let gas_used = 21_000 + code_gas;

let block_metadata = BlockMetadata {
block_beneficiary: Address::from(beneficiary),
block_difficulty: 0x20000.into(),
block_number: 1.into(),
block_chain_id: 1.into(),
block_timestamp: 0x03e8.into(),
block_gaslimit: 0xff112233u32.into(),
block_gas_used: gas_used.into(),
block_bloom: [0.into(); 8],
block_base_fee: 0xa.into(),
..BlockMetadata::default()

Check failure on line 107 in evm/tests/self_balance_gas_cost.rs

View workflow job for this annotation

GitHub Actions / Formatting and Clippy

struct update has no effect, all the fields in the struct have already been specified
};
Expand All @@ -100,9 +112,12 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
contract_code.insert(code_hash, code.to_vec());

let expected_state_trie_after = {
let beneficiary_account_after = AccountRlp::default();
let beneficiary_account_after = AccountRlp {
nonce: 1.into(),
..AccountRlp::default()
};
let sender_account_after = AccountRlp {
balance: 999999999999999568680u128.into(),
balance: sender_account_before.balance - U256::from(gas_used) * U256::from(10),
nonce: 1.into(),
..AccountRlp::default()
};
Expand Down Expand Up @@ -132,7 +147,6 @@ 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(),
Expand Down

0 comments on commit 5eb6da0

Please sign in to comment.