Skip to content

Commit

Permalink
Testing for invalid, inconclusive, and unexpected outcomes in Trusted…
Browse files Browse the repository at this point in the history
…Execution.
  • Loading branch information
Kellan Wampler committed Apr 22, 2024
1 parent ac050f1 commit 35b166b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/Fullcount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ contract Fullcount is EIP712 {
NFT memory batterNFT,
Pitch[] memory pitches,
Swing[] memory swings,
AtBatOutcome expectedOutcome
AtBatOutcome proposedOutcome
)
external
{
Expand All @@ -937,11 +937,6 @@ contract Fullcount is EIP712 {
"Fullcount.submitAtBat: sender is not an executor for batter."
);

require(
expectedOutcome != AtBatOutcome(0),
"Fullcount.submitAtBat: cannot submit an at-bat that is still in progress."
);

// Create at-bat
NumAtBats++;

Expand All @@ -951,6 +946,10 @@ contract Fullcount is EIP712 {
uint256[] storage sessionList = AtBatSessions[NumAtBats];

for (uint256 i = 0; i < pitches.length; i++) {
if (AtBatState[NumAtBats].outcome != AtBatOutcome.InProgress) {
revert("Fullcount.submitAtBat: invalid at-bat - invalid at-bat");
}

Outcome sessionOutcome = resolve(pitches[i], swings[i]);

NumSessions++;
Expand All @@ -973,5 +972,13 @@ contract Fullcount is EIP712 {

_progressAtBat(NumSessions, false);
}

if (AtBatState[NumAtBats].outcome == AtBatOutcome.InProgress) {
revert("Fullcount.submitAtBat: invalid at-bat - inconclusive");
}

if (AtBatState[NumAtBats].outcome != proposedOutcome) {
revert("Fullcount.submitAtBat: at-bat outcome does not match executor proposed outcome");
}
}
}
101 changes: 98 additions & 3 deletions test/TrustedExecution.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ contract TrustedExecutionTest_submitAtBat is TrustedExecutionTest {
(pitches[3], swings[3]) = _generateBall();

vm.prank(executor1);
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Strikeout);
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Walk);

uint256 atBatID = game.NumAtBats();

Expand Down Expand Up @@ -248,7 +248,7 @@ contract TrustedExecutionTest_submitAtBat is TrustedExecutionTest {
(pitches[3], swings[3]) = _generateDouble();

vm.prank(executor1);
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Strikeout);
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Double);

uint256 atBatID = game.NumAtBats();

Expand Down Expand Up @@ -331,7 +331,7 @@ contract TrustedExecutionTest_submitAtBat is TrustedExecutionTest {
address(otherCharacterNFTs),
BatterTokenID
);
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Strikeout);
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.HomeRun);

uint256 atBatID = game.NumAtBats();

Expand All @@ -353,4 +353,99 @@ contract TrustedExecutionTest_submitAtBat is TrustedExecutionTest {
}
}
}

function test_submit_with_inconclusive_at_bat() public {
vm.prank(player1);
game.setTrustedExecutor(executor1, true);

vm.prank(player2);
game.setTrustedExecutor(executor1, true);

uint256 initialNumAtBats = game.NumAtBats();
uint256 initialNumSessions = game.NumSessions();

uint256 atBatLength = 6;

NFT memory pitcher = NFT({ nftAddress: address(characterNFTs), tokenID: PitcherTokenID });
NFT memory batter = NFT({ nftAddress: address(otherCharacterNFTs), tokenID: BatterTokenID });
Pitch[] memory pitches = new Pitch[](atBatLength);
Swing[] memory swings = new Swing[](atBatLength);

(pitches[0], swings[0]) = _generateStrike();
(pitches[1], swings[1]) = _generateBall();
(pitches[2], swings[2]) = _generateStrike();
(pitches[3], swings[3]) = _generateBall();
(pitches[4], swings[4]) = _generateFoul();
(pitches[5], swings[5]) = _generateBall();

vm.prank(executor1);
vm.expectRevert("Fullcount.submitAtBat: invalid at-bat - inconclusive");
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Strikeout);

assertEq(game.NumAtBats(), initialNumAtBats);
assertEq(game.NumSessions(), initialNumSessions);
}

function test_submit_with_multiple_finalities() public {
vm.prank(player1);
game.setTrustedExecutor(executor1, true);

vm.prank(player2);
game.setTrustedExecutor(executor1, true);

uint256 initialNumAtBats = game.NumAtBats();
uint256 initialNumSessions = game.NumSessions();

uint256 atBatLength = 4;

NFT memory pitcher = NFT({ nftAddress: address(characterNFTs), tokenID: PitcherTokenID });
NFT memory batter = NFT({ nftAddress: address(otherCharacterNFTs), tokenID: BatterTokenID });
Pitch[] memory pitches = new Pitch[](atBatLength);
Swing[] memory swings = new Swing[](atBatLength);

(pitches[0], swings[0]) = _generateFoul();
(pitches[1], swings[1]) = _generateStrike();
(pitches[2], swings[2]) = _generateStrike();
(pitches[3], swings[3]) = _generateDouble();

vm.prank(executor1);
vm.expectRevert("Fullcount.submitAtBat: invalid at-bat - invalid at-bat");
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Double);

assertEq(game.NumAtBats(), initialNumAtBats);
assertEq(game.NumSessions(), initialNumSessions);
}

function test_at_bat_outcome_must_match_executor_proposed_outcome() public {
vm.prank(player1);
game.setTrustedExecutor(executor1, true);

vm.prank(player2);
game.setTrustedExecutor(executor1, true);

uint256 initialNumAtBats = game.NumAtBats();
uint256 initialNumSessions = game.NumSessions();

uint256 atBatLength = 7;

NFT memory pitcher = NFT({ nftAddress: address(characterNFTs), tokenID: PitcherTokenID });
NFT memory batter = NFT({ nftAddress: address(otherCharacterNFTs), tokenID: BatterTokenID });
Pitch[] memory pitches = new Pitch[](atBatLength);
Swing[] memory swings = new Swing[](atBatLength);

(pitches[0], swings[0]) = _generateFoul();
(pitches[1], swings[1]) = _generateFoul();
(pitches[2], swings[2]) = _generateFoul();
(pitches[3], swings[3]) = _generateBall();
(pitches[4], swings[4]) = _generateBall();
(pitches[5], swings[5]) = _generateBall();
(pitches[6], swings[6]) = _generateBall();

vm.prank(executor1);
vm.expectRevert("Fullcount.submitAtBat: at-bat outcome does not match executor proposed outcome");
game.submitAtBat(pitcher, batter, pitches, swings, AtBatOutcome.Strikeout);

assertEq(game.NumAtBats(), initialNumAtBats);
assertEq(game.NumSessions(), initialNumSessions);
}
}

0 comments on commit 35b166b

Please sign in to comment.