Skip to content

Commit

Permalink
test: Adds execution hashes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmerkleplant committed Nov 1, 2024
1 parent e61755a commit a60e62f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/ExecutionHashes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ library ExecutionHashes {
address internal constant SYSTEM_CONTRACT =
0x000000000000000000000000000000000000cafE;

/// @dev Reads the execution hash of block `number`.
/// @dev Returns the execution hash of block `number`.
function tryGet(uint number) internal view returns (bytes32, bool) {
if (number >= block.number) {
return (0, false);
}

//if (number >= block.number - 256) {
// return (blockhash(number), true);
//}
if (number >= block.number - 1 - 256) {
return (blockhash(number), true);
}

bool ok;
bytes memory data;
Expand All @@ -48,7 +48,7 @@ library ExecutionHashes {
return (abi.decode(data, (bytes32)), true);
}

/// @dev Reads the execution hash of block `number`.
/// @dev Returns the execution hash of block `number`.
///
/// @dev Reverts if:
/// - No execution hash found for given number
Expand Down
11 changes: 7 additions & 4 deletions test/BeaconRoots.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import {BeaconRoots} from "../src/BeaconRoots.sol";
import {Geas} from "../script/Geas.sol";

contract BeaconRootsTest is Test {
uint constant RING_BUFFER_SIZE = 8191;
uint constant BUFFER_SIZE = 8191;

function setUp() public {
bytes memory code = Geas.compile("lib/sys-asm/src/beacon_root/main.eas");

vm.etch(BeaconRoots.SYSTEM_CONTRACT, code);
vm.etch(
BeaconRoots.SYSTEM_CONTRACT,
Geas.compile("lib/sys-asm/src/beacon_root/main.eas")
);
}

function testFuzz_tryGet() public {
Expand All @@ -24,4 +25,6 @@ contract BeaconRootsTest is Test {
function testFuzz_get() public {
vm.skip(true);
}

// -- Private Helpers --
}
8 changes: 4 additions & 4 deletions test/Consolidations.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ contract ConsolidationsTest is Test {
uint constant INHIBITOR = type(uint).max;

function setUp() public {
bytes memory code =
Geas.compile("lib/sys-asm/src/consolidations/main.eas");

vm.etch(Consolidations.SYSTEM_CONTRACT, code);
vm.etch(
Consolidations.SYSTEM_CONTRACT,
Geas.compile("lib/sys-asm/src/consolidations/main.eas")
);
}

function testFuzz_request(uint seed) public {
Expand Down
127 changes: 118 additions & 9 deletions test/ExecutionHashes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,134 @@ pragma solidity ^0.8.16;
import {Test} from "forge-std/Test.sol";
import {console2 as console} from "forge-std/console2.sol";

import {ExecutionHashes} from "../src/ExecutionHashes.sol";
import {ExecutionHashes, NoExecutionHashFound} from "../src/ExecutionHashes.sol";

import {Geas} from "../script/Geas.sol";

contract ExecutionHashesTest is Test {
uint constant RING_BUFFER_SIZE = 8191;
uint constant BUFFER_SIZE = 8191;

function setUp() public {
bytes memory code =
Geas.compile("lib/sys-asm/src/execution_hash/main.eas");
vm.etch(
ExecutionHashes.SYSTEM_CONTRACT,
Geas.compile("lib/sys-asm/src/execution_hash/main.eas")
);
}

// -- Test: tryGet --

function testFuzz_tryGet(uint number, uint seed) public {
vm.assume(number != 0);
vm.assume(number - 1 > BUFFER_SIZE);

// Create valid query from seed.
//
// A valid query is in [number - 1 - BUFFER_SIZE, number - 1].
uint query = _bound(seed, number - 1 - BUFFER_SIZE, number - 1);

// Roll to number.
vm.roll(number);

// Let system contract have random storage.
vm.setArbitraryStorage(ExecutionHashes.SYSTEM_CONTRACT);

// Let block hash of query be zero.
// This value is returned if the blockhash() optimization is utilized.
vm.setBlockhash(query, bytes32(0));

// Read block hash from system contract.
(bytes32 got, bool ok) = ExecutionHashes.tryGet(query);
assertTrue(ok);

// The expected block hash is either the result of blockhash() or the
// value at storage slot query % BUFFER_SIZE.
bytes32 want;
if (query >= number - 1 - 256) {
want = blockhash(query);
} else {
want = vm.load(ExecutionHashes.SYSTEM_CONTRACT, bytes32(query % BUFFER_SIZE));
}
assertEq(want, got);
}

function testFuzz_tryGet_Boundaries(uint number) public {
vm.assume(number > BUFFER_SIZE + 2);

// Roll to number.
vm.roll(number);

bool ok;

// Fails for number.
(, ok) = ExecutionHashes.tryGet(number);
assertFalse(ok);

vm.etch(ExecutionHashes.SYSTEM_CONTRACT, code);
// Does not fail for number - 1.
(, ok) = ExecutionHashes.tryGet(number - 1);
assertTrue(ok);

// Does not fail for number - 1 - BUFFER_SIZE.
(, ok) = ExecutionHashes.tryGet(number - 1 - BUFFER_SIZE);
assertTrue(ok);

// Fails for number - 1 - BUFFER_SIZE - 1.
(, ok) = ExecutionHashes.tryGet(number - 1 - BUFFER_SIZE - 1);
assertFalse(ok);
}

function testFuzz_tryGet() public {
vm.skip(true);
// -- Test: get --

function testFuzz_get(uint number, uint seed) public {
vm.assume(number != 0);
vm.assume(number - 1 > BUFFER_SIZE);

// Create valid query from seed.
//
// A valid query is in [number - 1 - BUFFER_SIZE, number - 1].
uint query = _bound(seed, number - 1 - BUFFER_SIZE, number - 1);

// Roll to number.
vm.roll(number);

// Let system contract have random storage.
vm.setArbitraryStorage(ExecutionHashes.SYSTEM_CONTRACT);

// Let block hash of query be zero.
// This value is returned if the blockhash() optimization is utilized.
vm.setBlockhash(query, bytes32(0));

// Read block hash from system contract.
bytes32 got = ExecutionHashes.get(query);

// The expected block hash is either the result of blockhash() or the
// value at storage slot query % BUFFER_SIZE.
bytes32 want;
if (query >= number - 1 - 256) {
want = blockhash(query);
} else {
want = vm.load(ExecutionHashes.SYSTEM_CONTRACT, bytes32(query % BUFFER_SIZE));
}
assertEq(want, got);
}

function testFuzz_get() public {
vm.skip(true);
function testFuzz_get_Boundaries(uint number) public {
vm.assume(number > BUFFER_SIZE - 2);

// Roll to number.
vm.roll(number);

// Revert for number.
vm.expectRevert(NoExecutionHashFound.selector);
ExecutionHashes.get(number);

// Does not revert for number - 1.
ExecutionHashes.get(number - 1);

// Does not revert for number - 1 - BUFFER_SIZE.
ExecutionHashes.get(number - 1 - BUFFER_SIZE);

// Revert for number - 1 - BUFFER_SIZE - 1.
vm.expectRevert(NoExecutionHashFound.selector);
ExecutionHashes.get(number - 1 - BUFFER_SIZE - 1);
}
}
7 changes: 4 additions & 3 deletions test/Withdrawals.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ contract WithdrawalsTest is Test {
uint constant INHIBITOR = type(uint).max;

function setUp() public {
bytes memory code = Geas.compile("lib/sys-asm/src/withdrawals/main.eas");

vm.etch(Withdrawals.SYSTEM_CONTRACT, code);
vm.etch(
Withdrawals.SYSTEM_CONTRACT,
Geas.compile("lib/sys-asm/src/withdrawals/main.eas")
);
}

function testFuzz_request(uint seed) public {
Expand Down

0 comments on commit a60e62f

Please sign in to comment.