Commit 0781c705 authored by clabby's avatar clabby

Add tests for the `addLocalData` function in the `FaultDisputeGame`

parent d0d40100
......@@ -8,6 +8,8 @@ import { DisputeGameFactory } from "src/dispute/DisputeGameFactory.sol";
import { FaultDisputeGame } from "src/dispute/FaultDisputeGame.sol";
import { L2OutputOracle } from "src/L1/L2OutputOracle.sol";
import { BlockOracle } from "src/dispute/BlockOracle.sol";
import { PreimageOracle } from "src/cannon/PreimageOracle.sol";
import { PreimageKeyLib } from "src/cannon/PreimageKeyLib.sol";
import "src/libraries/DisputeTypes.sol";
import "src/libraries/DisputeErrors.sol";
......@@ -38,12 +40,7 @@ contract FaultDisputeGame_Init is DisputeGameFactory_Init {
// Propose 2 mock outputs
vm.startPrank(oracle.PROPOSER());
for (uint256 i; i < 2; i++) {
oracle.proposeL2Output(
bytes32(i + 1),
oracle.nextBlockNumber(),
blockhash(i),
i
);
oracle.proposeL2Output(bytes32(i + 1), oracle.nextBlockNumber(), blockhash(i), i);
// Advance 1 block
vm.roll(block.number + 1);
......@@ -129,7 +126,11 @@ contract FaultDisputeGame_Test is FaultDisputeGame_Init {
/// contain the disputed L2 output root.
function test_initialize_l1HeadTooOld_reverts() public {
// Store a mock block hash for the genesis block. The timestamp will default to 0.
vm.store(address(gameImpl.BLOCK_ORACLE()), keccak256(abi.encode(0, 0)), bytes32(uint256(1)));
vm.store(
address(gameImpl.BLOCK_ORACLE()),
keccak256(abi.encode(0, 0)),
bytes32(uint256(1))
);
bytes memory _extraData = abi.encode(oracle.SUBMISSION_INTERVAL() * 2, 0);
vm.expectRevert(L1HeadTooOld.selector);
......@@ -149,7 +150,10 @@ contract FaultDisputeGame_Test is FaultDisputeGame_Init {
/// @dev Tests that the game is initialized with the correct data.
function test_initialize_correctData_succeeds() public {
// Starting
(FaultDisputeGame.OutputProposal memory startingProp, FaultDisputeGame.OutputProposal memory disputedProp) = gameProxy.proposals();
(
FaultDisputeGame.OutputProposal memory startingProp,
FaultDisputeGame.OutputProposal memory disputedProp
) = gameProxy.proposals();
Types.OutputProposal memory starting = oracle.getL2Output(startingProp.index);
assertEq(startingProp.index, 0);
assertEq(startingProp.l2BlockNumber, starting.l2BlockNumber);
......@@ -421,6 +425,65 @@ contract FaultDisputeGame_Test is FaultDisputeGame_Init {
assertEq(uint8(status), uint8(GameStatus.CHALLENGER_WINS));
assertEq(uint8(gameProxy.status()), uint8(GameStatus.CHALLENGER_WINS));
}
/// @dev Tests that adding local data with an out of bounds identifier reverts.
function testFuzz_addLocalData_oob_reverts(uint256 _ident) public {
// [1, 5] are valid local data identifiers.
if (_ident <= 5) _ident = 0;
vm.expectRevert(InvalidLocalIdent.selector);
gameProxy.addLocalData(_ident, 0);
}
/// @dev Tests that local data is loaded into the preimage oracle correctly.
function test_addLocalData_static_succeeds() public {
IPreimageOracle oracle = IPreimageOracle(address(gameProxy.VM().oracle()));
(
FaultDisputeGame.OutputProposal memory starting,
FaultDisputeGame.OutputProposal memory disputed
) = gameProxy.proposals();
// Load the L1 head hash
gameProxy.addLocalData(1, 8);
bytes32 key = _getKey(1);
(bytes32 dat, uint256 datLen) = oracle.readPreimage(key, 8);
assertEq(dat, Hash.unwrap(gameProxy.l1Head()));
assertEq(datLen, 32);
// Load the starting output root
gameProxy.addLocalData(2, 8);
key = _getKey(2);
(dat, datLen) = oracle.readPreimage(key, 8);
assertEq(dat, Hash.unwrap(starting.outputRoot));
assertEq(datLen, 32);
// Load the disputed output root
gameProxy.addLocalData(3, 8);
key = _getKey(3);
(dat, datLen) = oracle.readPreimage(key, 8);
assertEq(dat, Hash.unwrap(disputed.outputRoot));
assertEq(datLen, 32);
// Load the starting L2 block number
gameProxy.addLocalData(4, 8);
key = _getKey(4);
(dat, datLen) = oracle.readPreimage(key, 8);
assertEq(dat, bytes32(uint256(starting.l2BlockNumber) << 0xC0));
assertEq(datLen, 8);
// Load the chain ID
gameProxy.addLocalData(5, 8);
key = _getKey(5);
(dat, datLen) = oracle.readPreimage(key, 8);
assertEq(dat, bytes32(block.chainid << 0xC0));
assertEq(datLen, 8);
}
/// @dev Helper to get the localized key for an identifier in the context of the game proxy.
function _getKey(uint256 _ident) internal view returns (bytes32) {
bytes32 h = keccak256(abi.encode(_ident | (1 << 248), address(gameProxy)));
return bytes32((uint256(h) & ~uint256(0xFF << 248)) | (1 << 248));
}
}
/// @notice A generic game player actor with a configurable trace.
......@@ -963,7 +1026,7 @@ contract AlphabetVM is IBigStepper {
constructor(Claim _absolutePrestate) {
ABSOLUTE_PRESTATE = _absolutePrestate;
oracle = IPreimageOracle(deployNoop());
oracle = new PreimageOracle();
}
/// @inheritdoc IBigStepper
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment