Skip to content

Commit

Permalink
test(prt-contracts): improve LeafTournament coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Feb 11, 2025
1 parent 2fbee90 commit 7badf23
Show file tree
Hide file tree
Showing 2 changed files with 408 additions and 2 deletions.
359 changes: 357 additions & 2 deletions prt/contracts/test/BottomTournament.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract BottomTournamentTest is Util, Test {

function setUp() public {}

function testBottom1() public {
function testComputeStep() public {
topTournament = Util.initializePlayer0Tournament(factory);

// pair commitment, expect a match
Expand Down Expand Up @@ -148,7 +148,7 @@ contract BottomTournamentTest is Util, Test {
Util.winLeafMatch(bottomTournament, _matchId, _playerToSeal);
}

function testBottom2() public {
function testComputeReset() public {
topTournament = Util.initializePlayer0Tournament(factory);

// pair commitment, expect a match
Expand Down Expand Up @@ -278,4 +278,359 @@ contract BottomTournamentTest is Util, Test {
// win match, expect revert
Util.winLeafMatch(bottomTournament, _matchId, _playerToSeal);
}

function testRollupsCmio() public {
topTournament = Util.initializePlayer0RollupsTournament(factory);

// pair commitment, expect a match
// player 1 joins tournament
uint256 _opponent = 1;
uint64 _height = 0;
Util.joinTournament(topTournament, _opponent);

Match.Id memory _matchId = Util.matchId(_opponent, _height);
Match.State memory _match =
topTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to left tree
uint256 _playerToSeal =
Util.advanceMatch(topTournament, _matchId, _opponent);

// expect new inner created
vm.recordLogs();

// seal match
Util.sealInnerMatchAndCreateInnerTournament(
topTournament, _matchId, _playerToSeal
);
_height += 1;

assertEq(
topTournament.getMatchCycle(_matchId.hashFromId()),
0,
"agree cycle should be zero"
);

Vm.Log[] memory _entries = vm.getRecordedLogs();
assertEq(_entries[0].topics.length, 2);
assertEq(
_entries[0].topics[0],
keccak256("newInnerTournament(bytes32,address)")
);
assertEq(
_entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId())
);

middleTournament = MiddleTournament(
address(bytes20(bytes32(_entries[0].data) << (12 * 8)))
);

Util.joinTournament(middleTournament, 0);
Util.joinTournament(middleTournament, _opponent);

_matchId = Util.matchId(_opponent, _height);
_match = middleTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to left tree
_playerToSeal = Util.advanceMatch(middleTournament, _matchId, _opponent);

// expect new inner created (middle 2)
vm.recordLogs();

// seal match
Util.sealInnerMatchAndCreateInnerTournament(
middleTournament, _matchId, _playerToSeal
);
_height += 1;

assertEq(
middleTournament.getMatchCycle(_matchId.hashFromId()),
0,
"agree cycle should be zero"
);

_entries = vm.getRecordedLogs();
assertEq(_entries[0].topics.length, 2);
assertEq(
_entries[0].topics[0],
keccak256("newInnerTournament(bytes32,address)")
);
assertEq(
_entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId())
);

bottomTournament = BottomTournament(
address(bytes20(bytes32(_entries[0].data) << (12 * 8)))
);

Util.joinTournament(bottomTournament, 0);
Util.joinTournament(bottomTournament, _opponent);

_matchId = Util.matchId(_opponent, _height);
_match = bottomTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to left tree
_playerToSeal = Util.advanceMatch(bottomTournament, _matchId, _opponent);

// seal match
Util.sealLeafMatch(bottomTournament, _matchId, _playerToSeal);

assertEq(
bottomTournament.getMatchCycle(_matchId.hashFromId()),
0,
"agree cycle should be zero"
);

vm.expectRevert();
// win match, expect revert
Util.winLeafMatchRollupsWithInput(
bottomTournament, _matchId, _playerToSeal
);
}

function testRollupsStep() public {
topTournament = Util.initializePlayer0RollupsTournament(factory);

// pair commitment, expect a match
// player 1 joins tournament
uint256 _opponent = 1;
uint64 _height = 0;
Util.joinTournament(topTournament, _opponent);

Match.Id memory _matchId = Util.matchId(_opponent, _height);
Match.State memory _match =
topTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to left tree
uint256 _playerToSeal =
Util.advanceMatch(topTournament, _matchId, _opponent);

// expect new inner created
vm.recordLogs();

// seal match
Util.sealInnerMatchAndCreateInnerTournament(
topTournament, _matchId, _playerToSeal
);
_height += 1;

assertEq(
topTournament.getMatchCycle(_matchId.hashFromId()),
0,
"agree cycle should be zero"
);

Vm.Log[] memory _entries = vm.getRecordedLogs();
assertEq(_entries[0].topics.length, 2);
assertEq(
_entries[0].topics[0],
keccak256("newInnerTournament(bytes32,address)")
);
assertEq(
_entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId())
);

middleTournament = MiddleTournament(
address(bytes20(bytes32(_entries[0].data) << (12 * 8)))
);

Util.joinTournament(middleTournament, 0);
Util.joinTournament(middleTournament, _opponent);

_matchId = Util.matchId(_opponent, _height);
_match = middleTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to left tree
_playerToSeal = Util.advanceMatch(middleTournament, _matchId, _opponent);

// expect new inner created (middle 2)
vm.recordLogs();

// seal match
Util.sealInnerMatchAndCreateInnerTournament(
middleTournament, _matchId, _playerToSeal
);
_height += 1;

assertEq(
middleTournament.getMatchCycle(_matchId.hashFromId()),
0,
"agree cycle should be zero"
);

_entries = vm.getRecordedLogs();
assertEq(_entries[0].topics.length, 2);
assertEq(
_entries[0].topics[0],
keccak256("newInnerTournament(bytes32,address)")
);
assertEq(
_entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId())
);

bottomTournament = BottomTournament(
address(bytes20(bytes32(_entries[0].data) << (12 * 8)))
);

Util.joinTournament(bottomTournament, 0);
Util.joinTournament(bottomTournament, _opponent);

_matchId = Util.matchId(_opponent, _height);
_match = bottomTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to left tree
_playerToSeal = Util.advanceMatch(bottomTournament, _matchId, _opponent);

// seal match
Util.sealLeafMatch(bottomTournament, _matchId, _playerToSeal);

assertEq(
bottomTournament.getMatchCycle(_matchId.hashFromId()),
0,
"agree cycle should be zero"
);

vm.expectRevert();
// win match, expect revert
Util.winLeafMatchRollupsWithoutInput(
bottomTournament, _matchId, _playerToSeal
);
}

function testRollupsReset() public {
topTournament = Util.initializePlayer0RollupsTournament(factory);

// pair commitment, expect a match
// player 2 joins tournament
uint256 _opponent = 2;
uint64 _height = 0;
Util.joinTournament(topTournament, _opponent);

Match.Id memory _matchId = Util.matchId(_opponent, _height);
Match.State memory _match =
topTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to right tree
uint256 _playerToSeal =
Util.advanceMatch(topTournament, _matchId, _opponent);

// expect new inner created
vm.recordLogs();

// seal match
Util.sealInnerMatchAndCreateInnerTournament(
topTournament, _matchId, _playerToSeal
);
_height += 1;

uint256 cycle = (
1
<< (
ArbitrationConstants.height(0)
+ ArbitrationConstants.log2step(0)
)
) - (1 << ArbitrationConstants.log2step(0));
assertEq(
topTournament.getMatchCycle(_matchId.hashFromId()),
cycle,
"agree cycle should be 4951760157141503507410452480"
);

Vm.Log[] memory _entries = vm.getRecordedLogs();
assertEq(_entries[0].topics.length, 2);
assertEq(
_entries[0].topics[0],
keccak256("newInnerTournament(bytes32,address)")
);
assertEq(
_entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId())
);

middleTournament = MiddleTournament(
address(bytes20(bytes32(_entries[0].data) << (12 * 8)))
);

Util.joinTournament(middleTournament, 0);
Util.joinTournament(middleTournament, _opponent);

_matchId = Util.matchId(_opponent, _height);
_match = middleTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to right tree
_playerToSeal = Util.advanceMatch(middleTournament, _matchId, _opponent);

// expect new inner created (middle 2)
vm.recordLogs();

// seal match
Util.sealInnerMatchAndCreateInnerTournament(
middleTournament, _matchId, _playerToSeal
);
_height += 1;

cycle = (
1
<< (
ArbitrationConstants.height(0)
+ ArbitrationConstants.log2step(0)
)
) - (1 << ArbitrationConstants.log2step(1));
assertEq(
middleTournament.getMatchCycle(_matchId.hashFromId()),
cycle,
"agree cycle should be 4951760157141521099462279168"
);

_entries = vm.getRecordedLogs();
assertEq(_entries[0].topics.length, 2);
assertEq(
_entries[0].topics[0],
keccak256("newInnerTournament(bytes32,address)")
);
assertEq(
_entries[0].topics[1], Match.IdHash.unwrap(_matchId.hashFromId())
);

bottomTournament = BottomTournament(
address(bytes20(bytes32(_entries[0].data) << (12 * 8)))
);

Util.joinTournament(bottomTournament, 0);
Util.joinTournament(bottomTournament, _opponent);

_matchId = Util.matchId(_opponent, _height);
_match = bottomTournament.getMatch(_matchId.hashFromId());
assertTrue(_match.exists(), "match should exist");

// advance match to end, this match will always advance to right tree
_playerToSeal = Util.advanceMatch(bottomTournament, _matchId, _opponent);

// seal match
Util.sealLeafMatch(bottomTournament, _matchId, _playerToSeal);

cycle = (
1
<< (
ArbitrationConstants.height(0)
+ ArbitrationConstants.log2step(0)
)
) - (1 << ArbitrationConstants.log2step(2));
assertEq(
bottomTournament.getMatchCycle(_matchId.hashFromId()),
cycle,
"agree cycle should be 4951760157141521099596496895"
);

vm.expectRevert();
// win match, expect revert
Util.winLeafMatch(bottomTournament, _matchId, _playerToSeal);
}
}
Loading

0 comments on commit 7badf23

Please sign in to comment.