Skip to content

Commit

Permalink
Merge pull request #48 from ethstorage/bill/unittest_for_resolve
Browse files Browse the repository at this point in the history
fix the unit test of resolve
  • Loading branch information
JustXxx authored Aug 4, 2024
2 parents 910e922 + d021cec commit 6afc90f
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions packages/contracts-bedrock/test/dispute/FaultDisputeGameN.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1450,10 +1450,10 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
function test_resolve_multiPart_succeeds() public {
vm.deal(address(this), 10_000 ether);

uint256 bond = _getRequiredBond(0);
uint256 bond = _getRequiredBondV2(0, 0);
for (uint256 i = 0; i < 2048; i++) {
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: bond }(disputed, 0, Claim.wrap(bytes32(i)));
gameProxy.attackV2{ value: bond }(disputed, 0, Claim.wrap(bytes32(i)), 0);
}

// Warp past the clock period.
Expand All @@ -1472,7 +1472,7 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
gameProxy.resolutionCheckpoints(0);
assertTrue(initCheckpoint);
assertEq(subgameIndex, 1024);
assertEq(leftmostPosition.raw(), Position.wrap(1).move(true).raw());
assertEq(leftmostPosition.raw(), Position.wrap(1).moveN(N_BITS, 0).raw());
assertEq(counteredBy, address(this));

// The root subgame should not be resolved.
Expand All @@ -1489,7 +1489,7 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
(initCheckpoint, subgameIndex, leftmostPosition, counteredBy) = gameProxy.resolutionCheckpoints(0);
assertTrue(initCheckpoint);
assertEq(subgameIndex, 2048);
assertEq(leftmostPosition.raw(), Position.wrap(1).move(true).raw());
assertEq(leftmostPosition.raw(), Position.wrap(1).moveN(N_BITS, 0).raw());
assertEq(counteredBy, address(this));

// The root subgame should now be resolved
Expand Down Expand Up @@ -1525,7 +1525,7 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
/// @dev Static unit test for the correctness of resolving a single attack game state.
function test_resolve_rootContested_succeeds() public {
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);

vm.warp(block.timestamp + 3 days + 12 hours);

Expand All @@ -1537,9 +1537,9 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
/// @dev Static unit test for the correctness of resolving a game with a contested challenge claim.
function test_resolve_challengeContested_succeeds() public {
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);
(,,,, disputed,,) = gameProxy.claimData(1);
gameProxy.defend{ value: _getRequiredBond(1) }(disputed, 1, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(1, 0) }(disputed, 1, _dummyClaim(), uint64(MAX_ATTACK_BRANCH));

vm.warp(block.timestamp + 3 days + 12 hours);

Expand All @@ -1552,11 +1552,11 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
/// @dev Static unit test for the correctness of resolving a game with multiplayer moves.
function test_resolve_teamDeathmatch_succeeds() public {
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);
(,,,, disputed,,) = gameProxy.claimData(1);
gameProxy.defend{ value: _getRequiredBond(1) }(disputed, 1, _dummyClaim());
gameProxy.defend{ value: _getRequiredBond(1) }(disputed, 1, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(1, uint64(MAX_ATTACK_BRANCH)) }(disputed, 1, _dummyClaim(), uint64(MAX_ATTACK_BRANCH));
gameProxy.attackV2{ value: _getRequiredBondV2(1, uint64(MAX_ATTACK_BRANCH)) }(disputed, 1, _dummyClaim(), uint64(MAX_ATTACK_BRANCH));

vm.warp(block.timestamp + 3 days + 12 hours);

Expand All @@ -1571,20 +1571,20 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
/// @dev Static unit test for the correctness of resolving a game that reaches max game depth.
function test_resolve_stepReached_succeeds() public {
Claim claim = _dummyClaim();
for (uint256 i; i < gameProxy.splitDepth(); i++) {
for (uint256 i; i * N_BITS < gameProxy.splitDepth(); i++) {
(,,,, Claim disputed,,) = gameProxy.claimData(i);
gameProxy.attack{ value: _getRequiredBond(i) }(disputed, i, claim);
gameProxy.attackV2{ value: _getRequiredBondV2(i, 0) }(disputed, i, claim, 0);
}

claim = _changeClaimStatus(claim, VMStatuses.PANIC);
for (uint256 i = gameProxy.claimDataLen() - 1; i < gameProxy.maxGameDepth(); i++) {
for (uint256 i = gameProxy.claimDataLen() - 1; i * N_BITS < gameProxy.maxGameDepth(); i++) {
(,,,, Claim disputed,,) = gameProxy.claimData(i);
gameProxy.attack{ value: _getRequiredBond(i) }(disputed, i, claim);
gameProxy.attackV2{ value: _getRequiredBondV2(i, 0) }(disputed, i, claim, 0);
}

vm.warp(block.timestamp + 3 days + 12 hours);

for (uint256 i = 9; i > 0; i--) {
for (uint256 i = 5; i > 0; i--) {
gameProxy.resolveClaim(i - 1, 0);
}
assertEq(uint8(gameProxy.resolve()), uint8(GameStatus.DEFENDER_WINS));
Expand All @@ -1593,14 +1593,14 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
/// @dev Static unit test asserting that resolve reverts when attempting to resolve a subgame multiple times
function test_resolve_claimAlreadyResolved_reverts() public {
Claim claim = _dummyClaim();
uint256 firstBond = _getRequiredBond(0);
uint256 firstBond = _getRequiredBondV2(0, 0);
vm.deal(address(this), firstBond);
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: firstBond }(disputed, 0, claim);
uint256 secondBond = _getRequiredBond(1);
gameProxy.attackV2{ value: firstBond }(disputed, 0, claim, 0);
uint256 secondBond = _getRequiredBondV2(1, 0);
vm.deal(address(this), secondBond);
(,,,, disputed,,) = gameProxy.claimData(1);
gameProxy.attack{ value: secondBond }(disputed, 1, claim);
gameProxy.attackV2{ value: secondBond }(disputed, 1, claim, 0);

vm.warp(block.timestamp + 3 days + 12 hours);

Expand All @@ -1622,40 +1622,40 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
/// @dev Static unit test asserting that resolve reverts when attempting to resolve a subgame at max depth
function test_resolve_claimAtMaxDepthAlreadyResolved_reverts() public {
Claim claim = _dummyClaim();
for (uint256 i; i < gameProxy.splitDepth(); i++) {
for (uint256 i; i * N_BITS < gameProxy.splitDepth(); i++) {
(,,,, Claim disputed,,) = gameProxy.claimData(i);
gameProxy.attack{ value: _getRequiredBond(i) }(disputed, i, claim);
gameProxy.attackV2{ value: _getRequiredBondV2(i, 0) }(disputed, i, claim, 0);
}

vm.deal(address(this), 10000 ether);
claim = _changeClaimStatus(claim, VMStatuses.PANIC);
for (uint256 i = gameProxy.claimDataLen() - 1; i < gameProxy.maxGameDepth(); i++) {
for (uint256 i = gameProxy.claimDataLen() - 1; i * N_BITS < gameProxy.maxGameDepth(); i++) {
(,,,, Claim disputed,,) = gameProxy.claimData(i);
gameProxy.attack{ value: _getRequiredBond(i) }(disputed, i, claim);
gameProxy.attackV2{ value: _getRequiredBondV2(i, 0) }(disputed, i, claim, 0);
}

vm.warp(block.timestamp + 3 days + 12 hours);

// Resolve to claim bond
uint256 balanceBefore = address(this).balance;
gameProxy.resolveClaim(8, 0);
gameProxy.resolveClaim(4, 0);

// Wait for the withdrawal delay.
vm.warp(block.timestamp + delayedWeth.delay() + 1 seconds);

gameProxy.claimCredit(address(this));
assertEq(address(this).balance, balanceBefore + _getRequiredBond(7));
assertEq(address(this).balance, balanceBefore + _getRequiredBondV2(3, 0));

vm.expectRevert(ClaimAlreadyResolved.selector);
gameProxy.resolveClaim(8, 0);
gameProxy.resolveClaim(4, 0);
}

/// @dev Static unit test asserting that resolve reverts when attempting to resolve subgames out of order
function test_resolve_outOfOrderResolution_reverts() public {
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);
(,,,, disputed,,) = gameProxy.claimData(1);
gameProxy.attack{ value: _getRequiredBond(1) }(disputed, 1, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(1, 0) }(disputed, 1, _dummyClaim(), 0);

vm.warp(block.timestamp + 3 days + 12 hours);

Expand Down Expand Up @@ -1965,7 +1965,7 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {

// Challenge the claim and resolve it.
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);
vm.warp(block.timestamp + 3 days + 12 hours);
gameProxy.resolveClaim(1, 0);
gameProxy.resolveClaim(0, 0);
Expand Down Expand Up @@ -2207,17 +2207,17 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
// Defender's turn
vm.warp(block.timestamp + 3.5 days - 1 seconds);
(,,,, Claim disputed,,) = gameProxy.claimData(0);
gameProxy.attack{ value: _getRequiredBond(0) }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(0, 0) }(disputed, 0, _dummyClaim(), 0);
// Chess clock time accumulated:
assertEq(gameProxy.getChallengerDuration(0).raw(), 3.5 days - 1 seconds);
assertEq(gameProxy.getChallengerDuration(1).raw(), 0);

// Advance time by 1 second, so that the root claim challenger clock is expired.
vm.warp(block.timestamp + 1 seconds);
// Attempt a second attack against the root claim. This should revert since the challenger clock is expired.
uint256 expectedBond = _getRequiredBond(0);
uint256 expectedBond = _getRequiredBondV2(0, 0);
vm.expectRevert(ClockTimeExceeded.selector);
gameProxy.attack{ value: expectedBond }(disputed, 0, _dummyClaim());
gameProxy.attackV2{ value: expectedBond }(disputed, 0, _dummyClaim(), 0);
// Chess clock time accumulated:
assertEq(gameProxy.getChallengerDuration(0).raw(), 3.5 days);
assertEq(gameProxy.getChallengerDuration(1).raw(), 1 seconds);
Expand All @@ -2232,11 +2232,12 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
vm.warp(block.timestamp + 3.5 days - 2 seconds);
// Attack the challenge to the root claim. This should succeed, since the defender clock is not expired.
(,,,, disputed,,) = gameProxy.claimData(1);
gameProxy.attack{ value: _getRequiredBond(1) }(disputed, 1, _dummyClaim());
gameProxy.attackV2{ value: _getRequiredBondV2(1, 0) }(disputed, 1, _dummyClaim(), 0);
// Chess clock time accumulated:
assertEq(gameProxy.getChallengerDuration(0).raw(), 3.5 days);
assertEq(gameProxy.getChallengerDuration(1).raw(), 3.5 days - 1 seconds);
assertEq(gameProxy.getChallengerDuration(2).raw(), 3.5 days - gameProxy.clockExtension().raw());
// todo, bill modify :2024-07-31 09:43:35
assertEq(gameProxy.getChallengerDuration(2).raw(), 3.5 days - (gameProxy.clockExtension().raw() * 2));

// Should not be able to resolve any claims yet.
vm.expectRevert(ClockNotExpired.selector);
Expand All @@ -2259,17 +2260,17 @@ contract FaultDisputeGameN_Test is FaultDisputeGame_Init {
// Chess clock time accumulated:
assertEq(gameProxy.getChallengerDuration(0).raw(), 3.5 days);
assertEq(gameProxy.getChallengerDuration(1).raw(), 3.5 days);
assertEq(gameProxy.getChallengerDuration(2).raw(), 3.5 days - 1 seconds);
assertEq(gameProxy.getChallengerDuration(2).raw(), 3.5 days - 1 seconds - gameProxy.clockExtension().raw());

// Warp past the challenge period for the root claim defender. Defending the root claim should now revert.
vm.warp(block.timestamp + 1 seconds);
expectedBond = _getRequiredBond(1);
vm.warp(block.timestamp + 1 seconds + gameProxy.clockExtension().raw());
expectedBond = _getRequiredBondV2(1, 0);
vm.expectRevert(ClockTimeExceeded.selector); // no further move can be made
gameProxy.attack{ value: expectedBond }(disputed, 1, _dummyClaim());
expectedBond = _getRequiredBond(2);
gameProxy.attackV2{ value: expectedBond }(disputed, 1, _dummyClaim(), 0);
expectedBond = _getRequiredBondV2(2, 0);
(,,,, disputed,,) = gameProxy.claimData(2);
vm.expectRevert(ClockTimeExceeded.selector); // no further move can be made
gameProxy.attack{ value: expectedBond }(disputed, 2, _dummyClaim());
gameProxy.attackV2{ value: expectedBond }(disputed, 2, _changeClaimStatus(_dummyClaim(), VMStatuses.PANIC), 0);
// Chess clock time accumulated:
assertEq(gameProxy.getChallengerDuration(0).raw(), 3.5 days);
assertEq(gameProxy.getChallengerDuration(1).raw(), 3.5 days);
Expand Down

0 comments on commit 6afc90f

Please sign in to comment.