Commit 5e344ac5 authored by clabby's avatar clabby

Clock / lint

parent 507ddd2c
......@@ -55,12 +55,12 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
Timestamp public gameStart;
/**
* @notice The current status of the game.
* @inheritdoc IDisputeGame
*/
GameStatus public status;
/**
* @notice The DisputeGame's bond manager.
* @inheritdoc IDisputeGame
*/
IBondManager public bondManager;
......@@ -96,11 +96,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
}
/**
* @notice Performs a VM step via an on-chain fault proof processor
* @dev This function should point to a fault proof processor in order to execute
* a step in the fault proof program on-chain. The interface of the fault proof processor
* contract should be generic enough such that we can use different fault proof VMs (MIPS, RiscV5, etc.)
* @param disagreement The GindexClaim of the disagreement
* @inheritdoc IFaultDisputeGame
*/
function step(ClaimHash disagreement) public {
// TODO - Call the VM to perform the execution step.
......@@ -153,21 +149,36 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
? LibPosition.attack(parent.position)
: LibPosition.defend(parent.position);
// Compute the clock for the next claim. The clock's duration is computed by taking the
// difference between the current block timestamp and the parent's clock timestamp.
Clock nextClock = LibClock.wrap(
Duration.wrap(
uint64(block.timestamp - Timestamp.unwrap(LibClock.timestamp(parent.clock)))
),
Timestamp.wrap(uint64(block.timestamp))
// Fetch the grandparent clock, if it exists.
// The grandparent clock should always exist unless the parent is the root claim.
Clock grandparentClock;
if (parent.parentIndex != type(uint32).max) {
grandparentClock = claimData[parent.parentIndex].clock;
}
// Compute the duration of the next clock. This is done by adding the duration of the
// grandparent claim to the difference between the current block timestamp and the
// parent's clock timestamp.
Duration nextDuration = Duration.wrap(
uint64(
// First, fetch the duration of the grandparent claim.
Duration.unwrap(LibClock.duration(grandparentClock)) +
// Second, add the difference between the current block timestamp and the
// parent's clock timestamp.
block.timestamp -
Timestamp.unwrap(LibClock.timestamp(parent.clock))
)
);
// Enforce the clock time. If the new clock duration is greater than half of the game
// duration, then the move is invalid and cannot be made.
if (Duration.unwrap(LibClock.duration(nextClock)) > Duration.unwrap(GAME_DURATION) >> 1) {
if (Duration.unwrap(nextDuration) > Duration.unwrap(GAME_DURATION) >> 1) {
revert ClockTimeExceeded();
}
// Construct the next clock with the new duration and the current block timestamp.
Clock nextClock = LibClock.wrap(nextDuration, Timestamp.wrap(uint64(block.timestamp)));
// Do not allow for a duplicate claim to be made.
// TODO.
// Maybe map the claimHash? There's no efficient way to check for this with the flat DAG.
......@@ -184,6 +195,7 @@ contract FaultDisputeGame is IFaultDisputeGame, Clone {
})
);
// Emit the appropriate event for the attack or defense.
if (isAttack) {
emit Attack(challengeIndex, pivot, msg.sender);
} else {
......
......@@ -97,8 +97,7 @@ contract FaultDisputeGame_Test is Test {
* @dev Tests that the game's data is set correctly.
*/
function test_gameData_succeeds() public {
(GameType gameType, Claim rootClaim, bytes memory extraData) =
gameProxy.gameData();
(GameType gameType, Claim rootClaim, bytes memory extraData) = gameProxy.gameData();
assertEq(uint256(gameType), uint256(GAME_TYPE));
assertEq(Claim.unwrap(rootClaim), Claim.unwrap(ROOT_CLAIM));
......@@ -127,7 +126,10 @@ contract FaultDisputeGame_Test is Test {
assertEq(countered, false);
assertEq(Claim.unwrap(claim), Claim.unwrap(ROOT_CLAIM));
assertEq(Position.unwrap(position), 0);
assertEq(Clock.unwrap(clock), Clock.unwrap(LibClock.wrap(Duration.wrap(0), Timestamp.wrap(uint64(block.timestamp)))));
assertEq(
Clock.unwrap(clock),
Clock.unwrap(LibClock.wrap(Duration.wrap(0), Timestamp.wrap(uint64(block.timestamp))))
);
}
/**
......@@ -138,7 +140,7 @@ contract FaultDisputeGame_Test is Test {
vm.warp(block.timestamp + 5);
// Perform the attack.
gameProxy.attack(0, Claim.wrap(bytes32(uint(5))));
gameProxy.attack(0, Claim.wrap(bytes32(uint256(5))));
// Grab the claim data of the attack.
(
......@@ -154,19 +156,15 @@ contract FaultDisputeGame_Test is Test {
assertEq(parentIndex, 0);
assertEq(rc, 0);
assertEq(countered, false);
assertEq(Claim.unwrap(claim), Claim.unwrap(Claim.wrap(bytes32(uint(5)))));
assertEq(Claim.unwrap(claim), Claim.unwrap(Claim.wrap(bytes32(uint256(5)))));
assertEq(Position.unwrap(position), Position.unwrap(LibPosition.attack(Position.wrap(0))));
assertEq(Clock.unwrap(clock), Clock.unwrap(LibClock.wrap(Duration.wrap(5), Timestamp.wrap(uint64(block.timestamp)))));
assertEq(
Clock.unwrap(clock),
Clock.unwrap(LibClock.wrap(Duration.wrap(5), Timestamp.wrap(uint64(block.timestamp))))
);
// Grab the claim data of the parent.
(
parentIndex,
rc,
countered,
claim,
position,
clock
) = gameProxy.claimData(0);
(parentIndex, rc, countered, claim, position, clock) = gameProxy.claimData(0);
// Assert correctness of the parent claim's data.
assertEq(parentIndex, type(uint32).max);
......@@ -174,7 +172,12 @@ contract FaultDisputeGame_Test is Test {
assertEq(countered, true);
assertEq(Claim.unwrap(claim), Claim.unwrap(ROOT_CLAIM));
assertEq(Position.unwrap(position), 0);
assertEq(Clock.unwrap(clock), Clock.unwrap(LibClock.wrap(Duration.wrap(0), Timestamp.wrap(uint64(block.timestamp - 5)))));
assertEq(
Clock.unwrap(clock),
Clock.unwrap(
LibClock.wrap(Duration.wrap(0), Timestamp.wrap(uint64(block.timestamp - 5)))
)
);
// Resolve the game.
assertEq(uint256(gameProxy.resolve()), uint256(GameStatus.CHALLENGER_WINS));
......
......@@ -12,7 +12,7 @@ contract LibClock_Test is Test {
/**
* @notice Tests that the `duration` function correctly shifts out the `Duration` from a packed `Clock` type.
*/
function testFuzz_duration_correctness(uint64 _duration, uint64 _timestamp) public {
function testFuzz_duration_succeeds(uint64 _duration, uint64 _timestamp) public {
Clock clock = LibClock.wrap(Duration.wrap(_duration), Timestamp.wrap(_timestamp));
assertEq(Duration.unwrap(LibClock.duration(clock)), _duration);
}
......@@ -20,7 +20,7 @@ contract LibClock_Test is Test {
/**
* @notice Tests that the `timestamp` function correctly shifts out the `Timestamp` from a packed `Clock` type.
*/
function testFuzz_timestamp_correctness(uint64 _duration, uint64 _timestamp) public {
function testFuzz_timestamp_succeeds(uint64 _duration, uint64 _timestamp) public {
Clock clock = LibClock.wrap(Duration.wrap(_duration), Timestamp.wrap(_timestamp));
assertEq(Timestamp.unwrap(LibClock.timestamp(clock)), _timestamp);
}
......
......@@ -39,7 +39,7 @@ contract LibPosition_Test is Test {
// Depth bound: [0, 127]
_depth = uint8(bound(_depth, 0, MAX_DEPTH));
// Index at depth bound: [0, 2 ** _depth]
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2 ** _depth));
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2**_depth));
Position position = LibPosition.wrap(_depth, _indexAtDepth);
Position left = LibPosition.left(position);
......@@ -55,7 +55,7 @@ contract LibPosition_Test is Test {
// Depth bound: [0, 127]
_depth = uint8(bound(_depth, 0, MAX_DEPTH));
// Index at depth bound: [0, 2 ** _depth]
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2 ** _depth));
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2**_depth));
Position position = LibPosition.wrap(_depth, _indexAtDepth);
Position right = LibPosition.right(position);
......@@ -71,7 +71,7 @@ contract LibPosition_Test is Test {
// Depth bound: [1, 127]
_depth = uint8(bound(_depth, 1, MAX_DEPTH));
// Index at depth bound: [0, 2 ** _depth]
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2 ** _depth));
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2**_depth));
Position position = LibPosition.wrap(_depth, _indexAtDepth);
Position parent = LibPosition.parent(position);
......@@ -84,14 +84,18 @@ contract LibPosition_Test is Test {
* @notice Tests that the `rightIndex` function correctly computes the deepest, right most index relative
* to a given position.
*/
function testFuzz_rightIndex_correctness(uint8 _maxDepth, uint8 _depth, uint128 _indexAtDepth) public {
function testFuzz_rightIndex_correctness(
uint8 _maxDepth,
uint8 _depth,
uint128 _indexAtDepth
) public {
// Max depth bound: [1, 127]
// The max game depth MUST be at least 1.
_maxDepth = uint8(bound(_maxDepth, 1, MAX_DEPTH));
// Depth bound: [0, _maxDepth]
_depth = uint8(bound(_depth, 0, _maxDepth));
// Index at depth bound: [0, 2 ** _depth]
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2 ** _depth));
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2**_depth));
Position position = LibPosition.wrap(_depth, _indexAtDepth);
uint128 rightIndex = LibPosition.rightIndex(position, _maxDepth);
......@@ -114,7 +118,7 @@ contract LibPosition_Test is Test {
// Depth bound: [0, 127]
_depth = uint8(bound(_depth, 0, MAX_DEPTH));
// Index at depth bound: [0, 2 ** _depth]
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2 ** _depth));
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2**_depth));
Position position = LibPosition.wrap(_depth, _indexAtDepth);
Position attack = LibPosition.attack(position);
......@@ -133,7 +137,7 @@ contract LibPosition_Test is Test {
// Depth bound: [1, 127]
_depth = uint8(bound(_depth, 1, MAX_DEPTH));
// Index at depth bound: [0, 2 ** _depth]
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2 ** _depth));
_indexAtDepth = uint128(bound(_indexAtDepth, 0, 2**_depth));
Position position = LibPosition.wrap(_depth, _indexAtDepth);
Position defend = LibPosition.defend(position);
......
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