diff --git a/cartesi-rollups/contracts/test/DaveConsensus.t.sol b/cartesi-rollups/contracts/test/DaveConsensus.t.sol index da91e095..968f3b9f 100644 --- a/cartesi-rollups/contracts/test/DaveConsensus.t.sol +++ b/cartesi-rollups/contracts/test/DaveConsensus.t.sol @@ -179,6 +179,26 @@ contract MockTournament is ITournament { function timeFinished() external pure override returns (bool, Time.Instant) { revert NotImplemented(); } + + function getCommitmentJoinedCount() external pure override returns (uint256) { + return 0; + } + + function getMatchCreatedCount() external pure override returns (uint256) { + return 0; + } + + function getMatchAdvancedCount() external pure override returns (uint256) { + return 0; + } + + function getMatchDeletedCount() external pure override returns (uint256) { + return 0; + } + + function getNewInnerTournamentCount() external pure override returns (uint256) { + return 0; + } } contract MockTournamentFactory is ITournamentFactory { diff --git a/prt/contracts/src/ITournament.sol b/prt/contracts/src/ITournament.sol index 33c71ba5..dbf2d725 100644 --- a/prt/contracts/src/ITournament.sol +++ b/prt/contracts/src/ITournament.sol @@ -499,6 +499,26 @@ interface ITournament { view returns (uint64 maxLevel, uint64 level, uint64 log2step, uint64 height); + /// @notice Get the number of `CommitmentJoined` events + /// that have been emitted since the contract was deployed. + function getCommitmentJoinedCount() external view returns (uint256); + + /// @notice Get the number of `MatchCreated` events + /// that have been emitted since the contract was deployed. + function getMatchCreatedCount() external view returns (uint256); + + /// @notice Get the number of `MatchAdvanced` events + /// that have been emitted since the contract was deployed. + function getMatchAdvancedCount() external view returns (uint256); + + /// @notice Get the number of `MatchDeleted` events + /// that have been emitted since the contract was deployed. + function getMatchDeletedCount() external view returns (uint256); + + /// @notice Get the number of `NewInnerTournament` events + /// that have been emitted since the contract was deployed. + function getNewInnerTournamentCount() external view returns (uint256); + // // Time view functions // diff --git a/prt/contracts/src/tournament/Tournament.sol b/prt/contracts/src/tournament/Tournament.sol index fa0e4bb5..6e16c129 100644 --- a/prt/contracts/src/tournament/Tournament.sol +++ b/prt/contracts/src/tournament/Tournament.sol @@ -72,6 +72,11 @@ contract Tournament is ITournament { Tree.Node danglingCommitment; uint256 matchCount; Time.Instant lastMatchDeleted; + uint256 commitmentJoinedCount; + uint256 matchCreatedCount; + uint256 matchAdvancedCount; + uint256 matchDeletedCount; + uint256 newInnerTournamentCount; uint256 constant MAX_GAS_PRICE = 50 gwei; uint256 constant MESSAGE_SENDER_PROFIT = 10 gwei; @@ -237,9 +242,9 @@ contract Tournament is ITournament { _clock.requireNotInitialized(); _clock.setNewPaused(args.startInstant, args.allowance); + _emitCommitmentJoined(_commitmentRoot, _finalState, msg.sender); pairCommitment(_commitmentRoot, _clock, _leftNode, _rightNode); claimers[_commitmentRoot] = msg.sender; - emit CommitmentJoined(_commitmentRoot, _finalState, msg.sender); } /// @inheritdoc ITournament @@ -250,16 +255,21 @@ contract Tournament is ITournament { Tree.Node _newLeftNode, Tree.Node _newRightNode ) external override refundable(Gas.ADVANCE_MATCH) tournamentNotFinished { - Match.State storage _matchState = matches[_matchId.hashFromId()]; + Match.IdHash matchIdHash = _matchId.hashFromId(); + Match.State storage _matchState = matches[matchIdHash]; _matchState.requireExist(); _matchState.requireCanBeAdvanced(); _matchState.advanceMatch( - _matchId, _leftNode, _rightNode, _newLeftNode, _newRightNode + _leftNode, _rightNode, _newLeftNode, _newRightNode ); clocks[_matchId.commitmentOne].advanceClock(); clocks[_matchId.commitmentTwo].advanceClock(); + + _emitMatchAdvanced( + matchIdHash, _matchState.otherParent, _matchState.leftNode + ); } /// @notice Win a match by timeout at any level (root or inner). @@ -546,7 +556,7 @@ contract Tournament is ITournament { ); matchIdFromInnerTournaments[_inner] = _matchId; - emit NewInnerTournament(_matchId.hashFromId(), _inner); + _emitNewInnerTournament(_matchId.hashFromId(), _inner); } /// @inheritdoc ITournament @@ -874,7 +884,7 @@ contract Tournament is ITournament { clearDanglingCommitment(); matchCount++; - emit MatchCreated( + _emitMatchCreated( _matchId, _danglingCommitment, _rootHash, _leftNode ); } else { @@ -901,13 +911,7 @@ contract Tournament is ITournament { } Match.IdHash _matchIdHash = _matchId.hashFromId(); delete matches[_matchIdHash]; - emit MatchDeleted( - _matchIdHash, - _matchId.commitmentOne, - _matchId.commitmentTwo, - _reason, - _winnerCommitment - ); + _emitMatchDeleted(_matchIdHash, _matchId, _reason, _winnerCommitment); } function deleteClaimer(Tree.Node commitment) internal { @@ -1042,6 +1046,36 @@ contract Tournament is ITournament { } } + function getCommitmentJoinedCount() + external + view + override + returns (uint256) + { + return commitmentJoinedCount; + } + + function getMatchCreatedCount() external view override returns (uint256) { + return matchCreatedCount; + } + + function getMatchAdvancedCount() external view override returns (uint256) { + return matchAdvancedCount; + } + + function getMatchDeletedCount() external view override returns (uint256) { + return matchDeletedCount; + } + + function getNewInnerTournamentCount() + external + view + override + returns (uint256) + { + return newInnerTournamentCount; + } + function _ensureTournamentIsNotFinished() private view { require(!isFinished(), TournamentIsFinished()); } @@ -1049,4 +1083,56 @@ contract Tournament is ITournament { function _ensureTournamentIsOpen() private view { require(!isClosed(), TournamentIsClosed()); } + + function _emitCommitmentJoined( + Tree.Node commitment, + Machine.Hash finalStateHash, + address submitter + ) private { + emit CommitmentJoined(commitment, finalStateHash, submitter); + ++commitmentJoinedCount; + } + + function _emitMatchCreated( + Match.IdHash matchIdHash, + Tree.Node one, + Tree.Node two, + Tree.Node leftOfTwo + ) private { + emit MatchCreated(matchIdHash, one, two, leftOfTwo); + ++matchCreatedCount; + } + + function _emitMatchAdvanced( + Match.IdHash matchIdHash, + Tree.Node otherParent, + Tree.Node leftNode + ) private { + emit MatchAdvanced(matchIdHash, otherParent, leftNode); + ++matchAdvancedCount; + } + + function _emitMatchDeleted( + Match.IdHash matchIdHash, + Match.Id memory matchId, + MatchDeletionReason reason, + WinnerCommitment winnerCommitment + ) private { + emit MatchDeleted( + matchIdHash, + matchId.commitmentOne, + matchId.commitmentTwo, + reason, + winnerCommitment + ); + ++matchDeletedCount; + } + + function _emitNewInnerTournament( + Match.IdHash matchIdHash, + ITournament childTournament + ) private { + emit NewInnerTournament(matchIdHash, childTournament); + ++newInnerTournamentCount; + } } diff --git a/prt/contracts/src/tournament/libs/Match.sol b/prt/contracts/src/tournament/libs/Match.sol index 6a4defc4..d2b83dc1 100644 --- a/prt/contracts/src/tournament/libs/Match.sol +++ b/prt/contracts/src/tournament/libs/Match.sol @@ -97,7 +97,6 @@ library Match { function advanceMatch( State storage state, - Id calldata id, Tree.Node leftNode, Tree.Node rightNode, Tree.Node newLeftNode, @@ -114,10 +113,6 @@ library Match { rightNode.requireChildren(newLeftNode, newRightNode); state._goDownRightTree(newLeftNode, newRightNode); } - - emit ITournament.MatchAdvanced( - id.hashFromId(), state.otherParent, state.leftNode - ); } function sealMatch( diff --git a/prt/contracts/test/BottomTournament.t.sol b/prt/contracts/test/BottomTournament.t.sol index 000f1f44..d8f49eef 100644 --- a/prt/contracts/test/BottomTournament.t.sol +++ b/prt/contracts/test/BottomTournament.t.sol @@ -89,10 +89,7 @@ contract BottomTournamentTest is Util { Vm.Log[] memory _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -145,10 +142,7 @@ contract BottomTournamentTest is Util { _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -156,6 +150,8 @@ contract BottomTournamentTest is Util { bottomTournament = ITournament(address(uint160(uint256(_entries[0].topics[2])))); + Util.assertEventCountersEqualZero(bottomTournament); + Util.joinTournament(bottomTournament, 0); Util.joinTournament(bottomTournament, _opponent); @@ -272,10 +268,7 @@ contract BottomTournamentTest is Util { Vm.Log[] memory _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -315,10 +308,7 @@ contract BottomTournamentTest is Util { _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -393,10 +383,7 @@ contract BottomTournamentTest is Util { Vm.Log[] memory _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -431,10 +418,7 @@ contract BottomTournamentTest is Util { _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -545,7 +529,7 @@ contract BottomTournamentTest is Util { uint256 end2 = Time.Instant.unwrap(c2.startInstant.add(c2.allowance)); uint256 endMax = end1 > end2 ? end1 : end2; vm.roll(endMax); - bottomTournament.eliminateMatchByTimeout(_matchId); + Util.eliminateMatchByTimeout(bottomTournament, _matchId); _match = bottomTournament.getMatch(_matchId.hashFromId()); assertFalse( diff --git a/prt/contracts/test/Match.t.sol b/prt/contracts/test/Match.t.sol index 6fb123fa..021f2043 100644 --- a/prt/contracts/test/Match.t.sol +++ b/prt/contracts/test/Match.t.sol @@ -26,14 +26,13 @@ library ExternalMatch { function advanceMatch( Match.State storage state, - Match.Id calldata id, Tree.Node leftNode, Tree.Node rightNode, Tree.Node newLeftNode, Tree.Node newRightNode ) external { Match.advanceMatch( - state, id, leftNode, rightNode, newLeftNode, newRightNode + state, leftNode, rightNode, newLeftNode, newRightNode ); } @@ -131,7 +130,6 @@ contract MatchTest is Test { advanceMatchStateLeft.requireCanBeAdvanced(); ExternalMatch.advanceMatch( advanceMatchStateLeft, - id, leftDivergenceCommitment1, Tree.ZERO_NODE, Tree.ZERO_NODE, @@ -190,7 +188,6 @@ contract MatchTest is Test { advanceMatchStateRight.requireCanBeAdvanced(); ExternalMatch.advanceMatch( advanceMatchStateRight, - id, Tree.ZERO_NODE, rightDivergenceCommitment1, Tree.ZERO_NODE, @@ -257,7 +254,6 @@ contract MatchTest is Test { advanceMatchStateRight.requireCanBeAdvanced(); ExternalMatch.advanceMatch( advanceMatchStateRight, - id, Tree.ZERO_NODE, rightDivergenceCommitment3, Tree.ZERO_NODE, @@ -273,7 +269,6 @@ contract MatchTest is Test { advanceMatchStateRight.requireCanBeAdvanced(); ExternalMatch.advanceMatch( advanceMatchStateRight, - id, Tree.ZERO_NODE, rightDivergenceCommitment2, Tree.ZERO_NODE, diff --git a/prt/contracts/test/MiddleTournament.t.sol b/prt/contracts/test/MiddleTournament.t.sol index 7c53bf8f..e32679c5 100644 --- a/prt/contracts/test/MiddleTournament.t.sol +++ b/prt/contracts/test/MiddleTournament.t.sol @@ -74,6 +74,8 @@ contract MiddleTournamentTest is Util { middleTournament = ITournament(address(uint160(uint256(_entries[0].topics[2])))); + Util.assertEventCountersEqualZero(middleTournament); + // Only player 0 joins middle; let it finish by timeout (no matches occur) Util.joinTournament(middleTournament, 0); @@ -146,10 +148,7 @@ contract MiddleTournamentTest is Util { Vm.Log[] memory _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -194,7 +193,8 @@ contract MiddleTournamentTest is Util { vm.roll(_rootTournamentFinish); (_finished, _winner,,) = middleTournament.innerTournamentWinner(); - topTournament.winInnerTournament( + Util.winInnerTournament( + topTournament, middleTournament, playerNodes[0][ArbitrationConstants.height(0) - 1], playerNodes[0][ArbitrationConstants.height(0) - 1] @@ -253,10 +253,7 @@ contract MiddleTournamentTest is Util { _entries = vm.getRecordedLogs(); assertEq(_entries[0].topics.length, 3); - assertEq( - _entries[0].topics[0], - keccak256("NewInnerTournament(bytes32,address)") - ); + assertEq(_entries[0].topics[0], ITournament.NewInnerTournament.selector); assertEq( _entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId()) ); @@ -300,12 +297,14 @@ contract MiddleTournamentTest is Util { ); assertNoElimination(); - vm.prank(player1); - middleTournament.winMatchByTimeout( + vm.startPrank(player1); + Util.winMatchByTimeout( + middleTournament, _matchId, playerNodes[1][ArbitrationConstants.height(1) - 1], playerNodes[1][ArbitrationConstants.height(1) - 1] ); + vm.stopPrank(); _match = middleTournament.getMatch(_matchId.hashFromId()); assertFalse(_match.exists(), "match should be deleted"); @@ -315,7 +314,8 @@ contract MiddleTournamentTest is Util { assertNoElimination(); (_finished, _winner,,) = middleTournament.innerTournamentWinner(); - topTournament.winInnerTournament( + Util.winInnerTournament( + topTournament, middleTournament, playerNodes[1][ArbitrationConstants.height(0) - 1], playerNodes[1][ArbitrationConstants.height(0) - 1] @@ -384,7 +384,7 @@ contract MiddleTournamentTest is Util { vm.roll(_middleTournamentFinish); assertTrue(middleTournament.canBeEliminated(), "can't be eliminated"); - topTournament.eliminateInnerTournament(middleTournament); + Util.eliminateInnerTournament(topTournament, middleTournament); vm.expectRevert(); topTournament.arbitrationResult(); @@ -417,7 +417,7 @@ contract MiddleTournamentTest is Util { vm.roll(_middleTournamentFinish); assertTrue(middleTournament.canBeEliminated(), "can't be eliminated"); - topTournament.eliminateInnerTournament(middleTournament); + Util.eliminateInnerTournament(topTournament, middleTournament); (bool _finishedTop, Tree.Node _commitment, Machine.Hash _finalState) = topTournament.arbitrationResult(); @@ -465,7 +465,7 @@ contract MiddleTournamentTest is Util { vm.roll(vm.getBlockNumber() + 1); assertTrue(middleTournament.canBeEliminated(), "can't be eliminated"); - topTournament.eliminateInnerTournament(middleTournament); + Util.eliminateInnerTournament(topTournament, middleTournament); (bool _finishedTop, Tree.Node _commitment, Machine.Hash _finalState) = topTournament.arbitrationResult(); @@ -518,7 +518,8 @@ contract MiddleTournamentTest is Util { assertFalse(hasWinner); assertNoElimination(); - middleTournament.winMatchByTimeout( + Util.winMatchByTimeout( + middleTournament, Util.matchId(1, 1), playerNodes[0][ArbitrationConstants.height(1) - 1], playerNodes[0][ArbitrationConstants.height(1) - 1] @@ -537,7 +538,7 @@ contract MiddleTournamentTest is Util { uint256 callerBalanceBefore = address(this).balance; uint256 tournamentBalanceBefore = address(topTournament).balance; - topTournament.eliminateInnerTournament(middleTournament); + Util.eliminateInnerTournament(topTournament, middleTournament); uint256 callerBalanceAfter = address(this).balance; uint256 tournamentBalanceAfter = address(topTournament).balance; @@ -602,7 +603,8 @@ contract MiddleTournamentTest is Util { assertFalse(hasWinner); assertNoElimination(); - middleTournament.winMatchByTimeout( + Util.winMatchByTimeout( + middleTournament, Util.matchId(1, 1), playerNodes[1][ArbitrationConstants.height(1) - 1], playerNodes[1][ArbitrationConstants.height(1) - 1] @@ -620,7 +622,8 @@ contract MiddleTournamentTest is Util { uint256 tournamentBalanceBefore = address(topTournament).balance; // win at the last second - topTournament.winInnerTournament( + Util.winInnerTournament( + topTournament, middleTournament, playerNodes[1][ArbitrationConstants.height(0) - 1], playerNodes[1][ArbitrationConstants.height(0) - 1] @@ -666,7 +669,8 @@ contract MiddleTournamentTest is Util { ); vm.roll(vm.getBlockNumber() + 1); - topTournament.winMatchByTimeout( + Util.winMatchByTimeout( + topTournament, topMatch, playerNodes[0][ArbitrationConstants.height(0) - 1], playerNodes[2][ArbitrationConstants.height(0) - 1] diff --git a/prt/contracts/test/TopTournament.t.sol b/prt/contracts/test/TopTournament.t.sol index d88d5936..2c73ea9f 100644 --- a/prt/contracts/test/TopTournament.t.sol +++ b/prt/contracts/test/TopTournament.t.sol @@ -12,6 +12,7 @@ pragma solidity ^0.8.0; +import {IDataProvider} from "src/IDataProvider.sol"; import {ITournament} from "src/ITournament.sol"; import { ArbitrationConstants @@ -40,6 +41,14 @@ contract TopTournamentTest is Util { (FACTORY,) = Util.instantiateTournamentFactory(); } + function testEventCounters( + Machine.Hash initialState, + IDataProvider dataProvider + ) external { + topTournament = FACTORY.instantiate(initialState, dataProvider); + Util.assertEventCountersEqualZero(topTournament); + } + function testRootWinner() public { topTournament = Util.initializePlayer0Tournament(FACTORY); diff --git a/prt/contracts/test/Tournament.t.sol b/prt/contracts/test/Tournament.t.sol index ee49aec0..4bfe20db 100644 --- a/prt/contracts/test/Tournament.t.sol +++ b/prt/contracts/test/Tournament.t.sol @@ -42,13 +42,6 @@ contract TournamentTest is Util { address player0 = vm.addr(1); address player1 = vm.addr(2); - event MatchCreated( - Match.IdHash indexed matchIdHash, - Tree.Node indexed one, - Tree.Node indexed two, - Tree.Node leftOfTwo - ); - constructor() { (FACTORY,) = Util.instantiateTournamentFactory(); } @@ -69,8 +62,8 @@ contract TournamentTest is Util { // player 1 joins tournament uint256 _opponent = 1; // pair commitment, expect a match - vm.expectEmit(true, true, false, true, address(topTournament)); - emit MatchCreated( + vm.expectEmit(true, true, true, true, address(topTournament)); + emit ITournament.MatchCreated( Util.matchId(_opponent, 0).hashFromId(), playerNodes[0][ArbitrationConstants.height(0)], playerNodes[1][ArbitrationConstants.height(0)], @@ -78,13 +71,20 @@ contract TournamentTest is Util { ); uint256 player1BalanceBefore = player1.balance; + uint256 matchCreatedCountBefore = topTournament.getMatchCreatedCount(); Util.joinTournament(topTournament, _opponent); uint256 player1BalanceAfter = player1.balance; + uint256 matchCreatedCountAfter = topTournament.getMatchCreatedCount(); assertEq( player1BalanceBefore - bondAmount, player1BalanceAfter, "Player 1 should have paid bond" ); + assertEq( + matchCreatedCountAfter, + matchCreatedCountBefore + 1, + "MatchCreated count should increase by 1" + ); } function testJoinTournamentInsufficientBond(uint256 insufficientBond) @@ -152,12 +152,14 @@ contract TournamentTest is Util { uint256 tournamentBalanceBefore = address(topTournament).balance; uint256 callerBalanceBefore = player0.balance; vm.txGasPrice(2); - vm.prank(player0); - topTournament.winMatchByTimeout( + vm.startPrank(player0); + Util.winMatchByTimeout( + topTournament, _matchId, playerNodes[1][ArbitrationConstants.height(0) - 1], playerNodes[1][ArbitrationConstants.height(0) - 1] ); + vm.stopPrank(); uint256 tournamentBalanceAfter = address(topTournament).balance; uint256 callerBalanceAfter = player0.balance; @@ -244,7 +246,8 @@ contract TournamentTest is Util { "should be able to win match by timeout" ); - topTournament.winMatchByTimeout( + Util.winMatchByTimeout( + topTournament, _matchId, playerNodes[0][ArbitrationConstants.height(0) - 1], playerNodes[0][ArbitrationConstants.height(0) - 1] @@ -295,7 +298,7 @@ contract TournamentTest is Util { uint256 tournamentBalanceBefore = address(topTournament).balance; uint256 callerBalanceBefore = address(this).balance; - topTournament.eliminateMatchByTimeout(_matchId); + Util.eliminateMatchByTimeout(topTournament, _matchId); uint256 tournamentBalanceAfter = address(topTournament).balance; uint256 callerBalanceAfter = address(this).balance; @@ -340,7 +343,8 @@ contract TournamentTest is Util { ); // Correct children should succeed - topTournament.winMatchByTimeout( + Util.winMatchByTimeout( + topTournament, _matchId, playerNodes[1][ArbitrationConstants.height(0) - 1], playerNodes[1][ArbitrationConstants.height(0) - 1] diff --git a/prt/contracts/test/Util.sol b/prt/contracts/test/Util.sol index de6d17bf..4d927219 100644 --- a/prt/contracts/test/Util.sol +++ b/prt/contracts/test/Util.sol @@ -177,6 +177,8 @@ contract Util is Test { ) internal returns (uint256 _playerToSeal) { (,,, uint64 _current) = _tournament.tournamentLevelConstants(); for (_current; _current > 1; _current -= 1) { + uint256 matchAdvancedCountBefore = + _tournament.getMatchAdvancedCount(); if (_playerToSeal == 0) { // advance match alternately until it can be sealed // starts with player 0 @@ -208,6 +210,11 @@ contract Util is Test { } _playerToSeal = 0; } + assertEq( + _tournament.getMatchAdvancedCount(), + matchAdvancedCountBefore + 1, + "MatchAdvanced count should be increased by 1" + ); } } @@ -243,10 +250,22 @@ contract Util is Test { Tree.Node _right = playerNodes[_player][height - 1]; Machine.Hash _finalState = _player == 0 ? ONE_STATE : TWO_STATE; uint256 bondAmount = _tournament.bondValue(); + uint256 commitmentJoinedCountBefore = + _tournament.getCommitmentJoinedCount(); + uint256 matchCreatedCountBefore = _tournament.getMatchCreatedCount(); vm.prank(addrs[_player]); _tournament.joinTournament{value: bondAmount}( _finalState, generateFinalStateProof(_player, height), _left, _right ); + assertEq( + _tournament.getCommitmentJoinedCount(), + commitmentJoinedCountBefore + 1 + ); + assertGe( + _tournament.getMatchCreatedCount(), + matchCreatedCountBefore, + "MatchCreated count must be non-decreasing" + ); } function sealLeafMatch( @@ -278,7 +297,13 @@ contract Util is Test { : playerNodes[0][height - 1]; Tree.Node _right = playerNodes[_player][height - 1]; + uint256 matchDeletedCountBefore = _tournament.getMatchDeletedCount(); _tournament.winLeafMatch(_matchId, _left, _right, new bytes(0)); + assertEq( + _tournament.getMatchDeletedCount(), + matchDeletedCountBefore + 1, + "MatchDeleted count should be increased by 1" + ); } function sealInnerMatchAndCreateInnerTournament( @@ -290,6 +315,8 @@ contract Util is Test { Tree.Node _left = _player == 1 ? playerNodes[1][0] : playerNodes[0][0]; Tree.Node _right = playerNodes[_player][0]; + uint256 newInnerTournamentCountBefore = + _tournament.getNewInnerTournamentCount(); _tournament.sealInnerMatchAndCreateInnerTournament( _matchId, _left, @@ -297,6 +324,67 @@ contract Util is Test { ONE_STATE, generateDivergenceProof(_player, height) ); + assertEq( + _tournament.getNewInnerTournamentCount(), + newInnerTournamentCountBefore + 1, + "NewInnerTournament count should be increased by 1" + ); + } + + function winMatchByTimeout( + ITournament _tournament, + Match.Id memory _matchId, + Tree.Node _leftNode, + Tree.Node _rightNode + ) internal { + uint256 matchDeletedCountBefore = _tournament.getMatchDeletedCount(); + _tournament.winMatchByTimeout(_matchId, _leftNode, _rightNode); + assertEq( + _tournament.getMatchDeletedCount(), + matchDeletedCountBefore + 1, + "MatchDeleted count should be increased by 1" + ); + } + + function eliminateMatchByTimeout( + ITournament _tournament, + Match.Id memory _matchId + ) internal { + uint256 matchDeletedCountBefore = _tournament.getMatchDeletedCount(); + _tournament.eliminateMatchByTimeout(_matchId); + assertEq( + _tournament.getMatchDeletedCount(), + matchDeletedCountBefore + 1, + "MatchDeleted count should be increased by 1" + ); + } + + function winInnerTournament( + ITournament _tournament, + ITournament _childTournament, + Tree.Node _leftNode, + Tree.Node _rightNode + ) internal { + uint256 matchDeletedCountBefore = _tournament.getMatchDeletedCount(); + _tournament.winInnerTournament(_childTournament, _leftNode, _rightNode); + assertEq( + _tournament.getMatchDeletedCount(), + matchDeletedCountBefore + 1, + "MatchDeleted count should be increased by 1" + ); + } + + function eliminateInnerTournament( + ITournament _tournament, + ITournament _childTournament + ) internal { + uint256 matchDeletedCountBefore = _tournament.getMatchDeletedCount(); + _tournament.eliminateInnerTournament(_childTournament); + assertEq( + _tournament.getMatchDeletedCount(), + matchDeletedCountBefore + 1, + "MatchDeleted count should be increased by 1" + ); } // create match id for player 0 and _opponent at _level @@ -368,4 +456,15 @@ contract Util is Test { return (stateTransition, riscVStateTransition, cmioStateTransition); } + + function assertEventCountersEqualZero(ITournament tournament) + internal + view + { + assertEq(tournament.getCommitmentJoinedCount(), 0); + assertEq(tournament.getMatchCreatedCount(), 0); + assertEq(tournament.getMatchAdvancedCount(), 0); + assertEq(tournament.getMatchDeletedCount(), 0); + assertEq(tournament.getNewInnerTournamentCount(), 0); + } }