Skip to content

Commit

Permalink
evm: properly handle chain id in evmone APIv2
Browse files Browse the repository at this point in the history
Fixes #2690.
  • Loading branch information
chfast committed Feb 3, 2025
1 parent d977055 commit d7fa2e0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion silkworm/core/execution/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ void ExecutionProcessor::execute_transaction(const Transaction& txn, Receipt& re
.value = txn.value,
// access_list
// blob_hashes
.chain_id = static_cast<uint64_t>(txn.chain_id.value_or(0)),
// TODO: This should be corrected in the evmone APIv2,
// because it uses transaction's chain id for CHAINID instruction.
.chain_id = evm().config().chain_id,
.nonce = txn.nonce};
for (const auto& [account, storage_keys] : txn.access_list)
evm1_txn.access_list.emplace_back(account, storage_keys);
Expand Down
38 changes: 38 additions & 0 deletions silkworm/core/execution/processor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,42 @@ TEST_CASE("Empty suicide beneficiary") {
CHECK(!state.read_account(suicide_beneficiary));
}

TEST_CASE("CHAINID instruction") {
// Set chain_id to a value other than 1
ChainConfig config = kMainnetConfig;
config.chain_id = 42;

Block block{};
block.header.number = 20'000'000; // PUSH0 enabled
block.header.gas_limit = 50'000;
const auto caller = 0x5ed8cee6b63b1c6afce3ad7c92f4fd7e1b8fad9f_address;

const auto code = *from_hex("465955"); // SSTORE(0, CHAINID)

Transaction txn{};
txn.to = 0xc0de_address;
txn.gas_limit = block.header.gas_limit;
txn.set_sender(caller);

SECTION("protected") {
txn.chain_id = config.chain_id; // chain id matches in a valid transaction
}
SECTION("legacy") {
txn.chain_id = std::nullopt; // valid legacy transaction may not have the chain id
}

InMemoryState state;

ExecutionProcessor processor{block, *protocol::rule_set_factory(config), state, config, true};
processor.evm().state().add_to_balance(caller, kEther);
processor.evm().state().set_code(*txn.to, code);

Receipt receipt;
processor.execute_transaction(txn, receipt);
CHECK(receipt.success);

const auto v = processor.evm().state().get_current_storage(*txn.to, 0x00_bytes32);
CHECK(v == evmc::bytes32{config.chain_id});
}

} // namespace silkworm
2 changes: 1 addition & 1 deletion third_party/evmone/evmone
Submodule evmone updated 1 files
+14 −3 test/state/host.cpp

0 comments on commit d7fa2e0

Please sign in to comment.