Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenGround0 committed Dec 12, 2024
1 parent f50da74 commit c2de7f2
Showing 1 changed file with 102 additions and 5 deletions.
107 changes: 102 additions & 5 deletions test/SimplePDPService.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ contract SimplePDPServiceTest is Test {
SimplePDPService public pdpService;
address public pdpVerifierAddress;
bytes empty = new bytes(0);
uint256 public proofSetId;
uint256 public leafCount;
uint256 public seed;

function setUp() public {
pdpVerifierAddress = address(this);
SimplePDPService pdpServiceImpl = new SimplePDPService();
bytes memory initializeData = abi.encodeWithSelector(SimplePDPService.initialize.selector, address(pdpVerifierAddress));
MyERC1967Proxy pdpServiceProxy = new MyERC1967Proxy(address(pdpServiceImpl), initializeData);
pdpService = SimplePDPService(address(pdpServiceProxy));
proofSetId = 1;
leafCount = 100;
seed = 12345;

}

function testInitialState() public view {
Expand All @@ -27,7 +34,6 @@ contract SimplePDPServiceTest is Test {

function testAddRecord() public {
uint64 epoch = 100;
uint256 proofSetId = 1;

vm.roll(epoch);
pdpService.proofSetCreated(proofSetId, address(this), empty);
Expand All @@ -41,7 +47,6 @@ contract SimplePDPServiceTest is Test {
}

function testListEvents() public {
uint256 proofSetId = 1;
uint64 epoch1 = 100;
uint64 epoch2 = 200;

Expand All @@ -67,15 +72,12 @@ contract SimplePDPServiceTest is Test {
}

function testOnlyPDPVerifierCanAddRecord() public {
uint256 proofSetId = 1;

vm.prank(address(0xdead));
vm.expectRevert("Caller is not the PDP verifier");
pdpService.proofSetCreated(proofSetId, address(this), empty);
}

function testGetEventOutOfBounds() public {
uint256 proofSetId = 1;
vm.expectRevert("Event index out of bounds");
pdpService.getEvent(proofSetId, 0);
}
Expand All @@ -89,6 +91,59 @@ contract SimplePDPServiceTest is Test {
uint64 challenges = pdpService.getChallengesPerProof();
assertEq(challenges, 5, "Challenges per proof should be 5");
}

function testInitialProvingPeriodHappyPath() public {
pdpService.rootsAdded(proofSetId, 0, new PDPVerifier.RootData[](0), empty);
uint256 challengeEpoch = pdpService.thisChallengeWindowStart(proofSetId);

pdpService.nextProvingPeriod(proofSetId, challengeEpoch, leafCount, empty);

assertEq(
pdpService.provingDeadlines(proofSetId),
block.number + pdpService.getMaxProvingPeriod(),
"Deadline should be set to current block + max period"
);
assertFalse(pdpService.provenThisPeriod(proofSetId));
}

function testInitialProvingPeriodInvalidChallengeEpoch() public {
pdpService.rootsAdded(proofSetId, 0, new PDPVerifier.RootData[](0), empty);
uint256 firstDeadline = block.number + pdpService.getMaxProvingPeriod();

// Test too early
uint256 tooEarly = firstDeadline - pdpService.challengeWindow() - 1;
vm.expectRevert("Next challenge epoch must fall within the next challenge window");
pdpService.nextProvingPeriod(proofSetId, tooEarly, leafCount, empty);

// Test too late
uint256 tooLate = firstDeadline + 1;
vm.expectRevert("Next challenge epoch must fall within the next challenge window");
pdpService.nextProvingPeriod(proofSetId, tooLate, leafCount, empty);
}

function testInactivateProofSetHappyPath() public {
// Setup initial state
pdpService.rootsAdded(proofSetId, 0, new PDPVerifier.RootData[](0), empty);
pdpService.nextProvingPeriod(proofSetId, pdpService.thisChallengeWindowStart(proofSetId), leafCount, empty);

// Prove possession in first period
vm.roll(block.number + pdpService.getMaxProvingPeriod() - 100);
pdpService.possessionProven(proofSetId, leafCount, seed, 5);

// Inactivate the proof set
pdpService.nextProvingPeriod(proofSetId, pdpService.NO_CHALLENGE_SCHEDULED(), leafCount, empty);

assertEq(
pdpService.provingDeadlines(proofSetId),
pdpService.NO_PROVING_DEADLINE(),
"Proving deadline should be set to NO_PROVING_DEADLINE"
);
assertEq(
pdpService.provenThisPeriod(proofSetId),
false,
"Proven this period should now be false"
);
}
}

contract SimplePDPServiceFaultsTest is Test {
Expand Down Expand Up @@ -308,4 +363,46 @@ contract SimplePDPServiceFaultsTest is Test {
// Works right on the deadline
pdpService.nextProvingPeriod(proofSetId, pdpService.nextChallengeWindowStart(proofSetId)+pdpService.challengeWindow(), leafCount, empty);
}

function testInactivateWithCurrentPeriodFault() public {
// Setup initial state
pdpService.rootsAdded(proofSetId, 0, new PDPVerifier.RootData[](0), empty);
pdpService.nextProvingPeriod(proofSetId, pdpService.thisChallengeWindowStart(proofSetId), leafCount, empty);

// Move to end of period without proving
vm.roll(block.number + pdpService.getMaxProvingPeriod());

// Expect fault event for the unproven period
vm.expectEmit(true, true, true, true);
emit SimplePDPService.FaultRecord(1);

pdpService.nextProvingPeriod(proofSetId, pdpService.NO_CHALLENGE_SCHEDULED(), leafCount, empty);

assertEq(
pdpService.provingDeadlines(proofSetId),
pdpService.NO_PROVING_DEADLINE(),
"Proving deadline should be set to NO_PROVING_DEADLINE"
);
}

function testInactivateWithMultiplePeriodFaults() public {
// Setup initial state
pdpService.rootsAdded(proofSetId, 0, new PDPVerifier.RootData[](0), empty);
pdpService.nextProvingPeriod(proofSetId, pdpService.thisChallengeWindowStart(proofSetId), leafCount, empty);

// Skip 3 proving periods without proving
vm.roll(block.number + pdpService.getMaxProvingPeriod() * 3 + 1);

// Expect fault event for all missed periods
vm.expectEmit(true, true, true, true);
emit SimplePDPService.FaultRecord(3);

pdpService.nextProvingPeriod(proofSetId, pdpService.NO_CHALLENGE_SCHEDULED(), leafCount, empty);

assertEq(
pdpService.provingDeadlines(proofSetId),
pdpService.NO_PROVING_DEADLINE(),
"Proving deadline should be set to NO_PROVING_DEADLINE"
);
}
}

0 comments on commit c2de7f2

Please sign in to comment.