Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/eevm/system_contracts/block_hashes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,10 @@ defmodule EEVM.SystemContracts.BlockHashes do
should never happen with the canonical bytecode, but we surface it rather
than swallow it).
"""
@spec commit(Database.t(), Block.t(), non_neg_integer()) ::
@spec commit(Database.t(), Block.t(), <<_::256>>) ::
{:ok, Database.t()} | {:error, atom(), Database.t()}
def commit(%Database{} = db, %Block{} = block, parent_block_hash)
when is_integer(parent_block_hash) and parent_block_hash >= 0 do
calldata = <<parent_block_hash::unsigned-big-256>>

when is_binary(parent_block_hash) and byte_size(parent_block_hash) == 32 do
final_state =
@deployed_code
|> MachineState.new(
Expand All @@ -149,7 +147,7 @@ defmodule EEVM.SystemContracts.BlockHashes do
address: @address,
caller: @system_address,
callvalue: 0,
calldata: calldata
calldata: parent_block_hash
},
gas: @system_call_gas
)
Expand Down
8 changes: 1 addition & 7 deletions test/support/blockchain_test_runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,13 @@ defmodule EEVM.TestSupport.BlockchainTestRunner do
unwrap_system_call(BeaconRoots.commit(db, block_ctx, header.parent_beacon_block_root || 0))
end)
|> maybe_append(enabled_2935?, fn db, block_ctx ->
unwrap_system_call(
BlockHashes.commit(db, block_ctx, parent_hash_to_int(header.parent_hash))
)
unwrap_system_call(BlockHashes.commit(db, block_ctx, header.parent_hash))
end)
end

defp unwrap_system_call({:ok, %Database{} = db}), do: db
defp unwrap_system_call({:error, _reason, %Database{} = db}), do: db

defp parent_hash_to_int(nil), do: 0
defp parent_hash_to_int(<<value::unsigned-big-256>>), do: value
defp parent_hash_to_int(value) when is_integer(value), do: value

defp maybe_append(list, false, _fun), do: list
defp maybe_append(list, true, fun), do: list ++ [fun]

Expand Down
10 changes: 5 additions & 5 deletions test/system_contracts/block_hashes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule EEVM.SystemContracts.BlockHashesTest do
block = %Block{number: 1_000_001}
hash = 0xDEADBEEFCAFEBABE0000000000000000000000000000000000000000FACEFEED

{:ok, db} = BlockHashes.commit(db, block, hash)
{:ok, db} = BlockHashes.commit(db, block, <<hash::256>>)

slot = rem(1_000_000, @window)
assert Database.storage_load(db, BlockHashes.address(), slot) == hash
Expand All @@ -56,7 +56,7 @@ defmodule EEVM.SystemContracts.BlockHashesTest do
db = BlockHashes.install(InMemory.new())
hash = 0xFEED0001

{:ok, db} = BlockHashes.commit(db, %Block{number: 1_000_001}, hash)
{:ok, db} = BlockHashes.commit(db, %Block{number: 1_000_001}, <<hash::256>>)

assert BlockHashes.lookup(db, 1_000_000) == {:ok, hash}
end
Expand All @@ -70,8 +70,8 @@ defmodule EEVM.SystemContracts.BlockHashesTest do
db = BlockHashes.install(InMemory.new())

# parent block 1_000 then parent block 1_000 + 8191 — same ring slot.
{:ok, db} = BlockHashes.commit(db, %Block{number: 1_001}, 0xAAAA)
{:ok, db} = BlockHashes.commit(db, %Block{number: 1_001 + @window}, 0xBBBB)
{:ok, db} = BlockHashes.commit(db, %Block{number: 1_001}, <<0xAAAA::256>>)
{:ok, db} = BlockHashes.commit(db, %Block{number: 1_001 + @window}, <<0xBBBB::256>>)

assert BlockHashes.lookup(db, 1_000) == {:ok, 0xBBBB}
assert BlockHashes.lookup(db, 1_000 + @window) == {:ok, 0xBBBB}
Expand All @@ -85,7 +85,7 @@ defmodule EEVM.SystemContracts.BlockHashesTest do
hash = 0x1111111111111111111111111111111111111111111111111111111111111111

db = BlockHashes.install(InMemory.new())
{:ok, db} = BlockHashes.commit(db, %Block{number: block_number}, hash)
{:ok, db} = BlockHashes.commit(db, %Block{number: block_number}, <<hash::256>>)

final_state = call_contract(db, block_number, target_block)

Expand Down
Loading