Commit 98bfbd81 authored by clabby's avatar clabby

Fix first `MIPS` tests

parent e4b9e3b8
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -123,10 +123,15 @@ contract MIPS {
switch _exited
case 1 {
switch _exitCode
case 0 { status_ := 0 } // VMStatusValid
case 1 { status_ := 1 } // VMStatusInvalid
default { status_ := 2 } // VMStatusPanic
} default { status_ := 3 } // VMStatusUnfinished
// VMStatusValid
case 0 { status_ := 0 }
// VMStatusInvalid
case 1 { status_ := 1 }
// VMStatusPanic
default { status_ := 2 }
}
// VMStatusUnfinished
default { status_ := 3 }
}
let status := vmStatus(exited, exitCode)
......
......@@ -91,10 +91,6 @@ contract DisputeGameFactory is OwnableUpgradeable, IDisputeGameFactory, Semver {
// If there is no implementation to clone for the given `GameType`, revert.
if (address(impl) == address(0)) revert NoImplementation(_gameType);
// The VMStatus must indicate (1) 'invalid', to argue that disputed thing is invalid.
// Games that agree with the existing outcome are not allowed.
if (uint8(Claim.unwrap(_rootClaim)[0]) != 1) revert UnexpectedRootClaim(_rootClaim);
// Clone the implementation contract and initialize it with the given parameters.
proxy_ = IDisputeGame(address(impl).clone(abi.encodePacked(_rootClaim, _extraData)));
proxy_.initialize();
......
......@@ -437,6 +437,10 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone, Semver {
// Implicit assumptions:
// - The `gameStatus` state variable defaults to 0, which is `GameStatus.IN_PROGRESS`
// The VMStatus must indicate (1) 'invalid', to argue that disputed thing is invalid.
// Games that agree with the existing outcome are not allowed.
if (uint8(Claim.unwrap(rootClaim())[0]) != 1) revert UnexpectedRootClaim(rootClaim());
// Set the game's starting timestamp
createdAt = Timestamp.wrap(uint64(block.timestamp));
......
......@@ -36,12 +36,6 @@ contract DisputeGameFactory_Init is L2OutputOracle_Initializer {
}
contract DisputeGameFactory_Create_Test is DisputeGameFactory_Init {
function changeClaimStatus(Claim claim, uint8 status) public pure returns (Claim _out) {
bytes32 hash = Claim.unwrap(claim);
hash = bytes32((uint256(hash) & (~(uint256(0xff) << 248))) | (uint256(status) << 248));
return Claim.wrap(hash);
}
/// @dev Tests that the `create` function succeeds when creating a new dispute game
/// with a `GameType` that has an implementation set.
function testFuzz_create_succeeds(uint8 gameType, Claim rootClaim, bytes calldata extraData) public {
......@@ -83,17 +77,6 @@ contract DisputeGameFactory_Create_Test is DisputeGameFactory_Init {
factory.create(gt, rootClaim, extraData);
}
/// @dev Tests that the `create` function reverts when the rootClaim does not disagree with the outcome.
function testFuzz_create_badRootStatus_reverts(uint8 gameType, Claim rootClaim, bytes calldata extraData) public {
// Ensure that the `gameType` is within the bounds of the `GameType` enum's possible values.
GameType gt = GameType.wrap(uint8(bound(gameType, 0, 2)));
// Ensure the root claim does not have the correct VM status
if (uint8(Claim.unwrap(rootClaim)[0]) == 1) rootClaim = changeClaimStatus(rootClaim, 0);
vm.expectRevert(abi.encodeWithSelector(UnexpectedRootClaim.selector, rootClaim));
factory.create(gt, rootClaim, extraData);
}
/// @dev Tests that the `create` function reverts when there exists a dispute game with the same UUID.
function testFuzz_create_sameUUID_reverts(uint8 gameType, Claim rootClaim, bytes calldata extraData) public {
// Ensure that the `gameType` is within the bounds of the `GameType` enum's possible values.
......@@ -122,6 +105,12 @@ contract DisputeGameFactory_Create_Test is DisputeGameFactory_Init {
);
factory.create(gt, rootClaim, extraData);
}
function changeClaimStatus(Claim claim, uint8 status) public pure returns (Claim _out) {
bytes32 hash = Claim.unwrap(claim);
hash = bytes32((uint256(hash) & (~(uint256(0xff) << 248))) | (uint256(status) << 248));
return Claim.wrap(hash);
}
}
contract DisputeGameFactory_SetImplementation_Test is DisputeGameFactory_Init {
......
......@@ -143,6 +143,16 @@ contract FaultDisputeGame_Test is FaultDisputeGame_Init {
factory.create(GAME_TYPE, ROOT_CLAIM, abi.encode(1800, block.number - 1));
}
/// @dev Tests that the `create` function reverts when the rootClaim does not disagree with the outcome.
function testFuzz_initialize_badRootStatus_reverts(Claim rootClaim, bytes calldata extraData) public {
// Ensure that the `gameType` is within the bounds of the `GameType` enum's possible values.
// Ensure the root claim does not have the correct VM status
if (uint8(Claim.unwrap(rootClaim)[0]) == 1) rootClaim = changeClaimStatus(rootClaim, 0);
vm.expectRevert(abi.encodeWithSelector(UnexpectedRootClaim.selector, rootClaim));
factory.create(GameTypes.FAULT, rootClaim, extraData);
}
/// @dev Tests that the game is initialized with the correct data.
function test_initialize_correctData_succeeds() public {
// Starting
......@@ -449,6 +459,12 @@ contract FaultDisputeGame_Test is FaultDisputeGame_Init {
bytes32 h = keccak256(abi.encode(_ident | (1 << 248), address(gameProxy)));
return bytes32((uint256(h) & ~uint256(0xFF << 248)) | (1 << 248));
}
function changeClaimStatus(Claim claim, uint8 status) public pure returns (Claim _out) {
bytes32 hash = Claim.unwrap(claim);
hash = bytes32((uint256(hash) & (~(uint256(0xff) << 248))) | (uint256(status) << 248));
return Claim.wrap(hash);
}
}
/// @notice A generic game player actor with a configurable trace.
......
......@@ -1553,10 +1553,31 @@ contract MIPS_Test is CommonTest {
);
}
/// @dev MIPS VM status codes:
/// 0. Exited with success (Valid)
/// 1. Exited with success (Invalid)
/// 2. Exited with failure (Panic)
/// 3. Unfinished
function vmStatus(MIPS.State memory state) internal pure returns (uint8 out_) {
if (state.exited) {
if (state.exitCode == 0) {
return 0;
} else if (state.exitCode == 1) {
return 1;
} else {
return 2;
}
} else {
return 3;
}
}
function outputState(MIPS.State memory state) internal pure returns (bytes32 out_) {
bytes memory enc = encodeState(state);
uint8 status = vmStatus(state);
assembly {
out_ := keccak256(add(enc, 0x20), 226)
out_ := or(and(not(shl(248, 0xFF)), out_), shl(248, status))
}
}
......
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