Skip to content

Commit

Permalink
EIP-2935: Serve historical block hashes from state (#953)
Browse files Browse the repository at this point in the history
Implement [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935) "Serve
historical block hashes from state" required for the
[Prague](https://eips.ethereum.org/EIPS/eip-7600) revision.
This mostly adds new system contract.
  • Loading branch information
chfast authored Sep 10, 2024
2 parents a79218c + 645756f commit 652b4b1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,16 @@ jobs:
# Tests for in-development EVM revision currently passing.
working_directory: ~/spec-tests/fixtures/state_tests
command: >
~/build/bin/evmone-statetest
~/build/bin/evmone-statetest
prague/eip2537_bls_12_381_precompiles/bls12_precompiles_before_fork
prague/eip2537_bls_12_381_precompiles/bls12_g1add
- run:
name: "Execution spec tests (develop, blockchain_tests)"
# Tests for in-development EVM revision currently passing.
working_directory: ~/spec-tests/fixtures/blockchain_tests
command: >
~/build/bin/evmone-blockchaintest
prague/eip2935_historical_block_hashes_from_state
- collect_coverage_gcc
- upload_coverage:
flags: execution_spec_tests
Expand Down
4 changes: 4 additions & 0 deletions test/state/system_contracts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct SystemContract
constexpr std::array SYSTEM_CONTRACTS{
SystemContract{EVMC_CANCUN, BEACON_ROOTS_ADDRESS,
[](const BlockInfo& block) noexcept { return bytes_view{block.parent_beacon_block_root}; }},
SystemContract{EVMC_PRAGUE, HISTORY_STORAGE_ADDRESS,
[](const BlockInfo& block) noexcept {
return bytes_view{block.known_block_hashes.at(block.number - 1)};
}},
};

static_assert(std::ranges::is_sorted(SYSTEM_CONTRACTS,
Expand Down
3 changes: 3 additions & 0 deletions test/state/system_contracts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ constexpr auto SYSTEM_ADDRESS = 0xfffffffffffffffffffffffffffffffffffffffe_addre
/// The address of the system contract storing the root hashes of beacon chain blocks (EIP-4788).
constexpr auto BEACON_ROOTS_ADDRESS = 0x000F3df6D732807Ef1319fB7B8bB8522d0Beac02_address;

/// The address of the system contract storing historical block hashes (EIP-2935).
constexpr auto HISTORY_STORAGE_ADDRESS = 0x0aae40965e6800cd9b1f4b05ff21581047e3f91e_address;

struct BlockInfo;
class State;

Expand Down
20 changes: 20 additions & 0 deletions test/unittests/state_system_call_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ TEST_F(state_system_call, beacon_roots)
EXPECT_EQ(storage.at(0x01_bytes32).current, block.parent_beacon_block_root);
EXPECT_EQ(storage.at(0x00_bytes32).current, to_bytes32(SYSTEM_ADDRESS));
}

TEST_F(state_system_call, history_storage)
{
static constexpr auto NUMBER = 123456789;
static constexpr auto PREV_BLOCKHASH = 0xbbbb_bytes32;
const BlockInfo block{.number = NUMBER, .known_block_hashes = {{NUMBER - 1, PREV_BLOCKHASH}}};
state.insert(HISTORY_STORAGE_ADDRESS,
{.code = sstore(OP_NUMBER, calldataload(0)) + sstore(0, OP_CALLER)});

system_call(state, block, EVMC_PRAGUE, vm);

ASSERT_EQ(state.get_accounts().size(), 1);
EXPECT_EQ(state.find(SYSTEM_ADDRESS), nullptr);
EXPECT_EQ(state.get(HISTORY_STORAGE_ADDRESS).nonce, 0);
EXPECT_EQ(state.get(HISTORY_STORAGE_ADDRESS).balance, 0);
const auto& storage = state.get(HISTORY_STORAGE_ADDRESS).storage;
ASSERT_EQ(storage.size(), 2);
EXPECT_EQ(storage.at(bytes32{NUMBER}).current, PREV_BLOCKHASH);
EXPECT_EQ(storage.at(0x00_bytes32).current, to_bytes32(SYSTEM_ADDRESS));
}
2 changes: 2 additions & 0 deletions test/utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ RevisionSchedule to_rev_schedule(std::string_view s)
{
if (s == "ShanghaiToCancunAtTime15k")
return {EVMC_SHANGHAI, EVMC_CANCUN, 15'000};
if (s == "CancunToPragueAtTime15k")
return {EVMC_CANCUN, EVMC_PRAGUE, 15'000};

const auto single_rev = to_rev(s);
return {single_rev, single_rev, 0};
Expand Down

0 comments on commit 652b4b1

Please sign in to comment.