Skip to content

Commit 19993af

Browse files
authored
Merge pull request #67 from FilOzone/fix/pdp-service-deadline-update
Simplify fault design
2 parents b88ef1e + f3e854f commit 19993af

File tree

4 files changed

+159
-91
lines changed

4 files changed

+159
-91
lines changed

src/PDPVerifier.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ contract PDPVerifier is Initializable, UUPSUpgradeable, OwnableUpgradeable {
429429
removeRoots(setId, removalsToProcess);
430430
// Bring added roots into proving set
431431
challengeRange[setId] = proofSetLeafCount[setId];
432-
433432
nextChallengeEpoch[setId] = block.number + challengeFinality;
434433

435434
// Clear next challenge epoch if the set is now empty.

src/SimplePDPService.sol

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,12 @@ contract PDPRecordKeeper {
6767
}
6868
}
6969

70-
// SimplePDPServiceApplication is a default implementation of a PDP Application.
70+
// SimplePDPServiceApplication is a default implementation of a PDP Listener
7171
// It maintains a record of all events that have occurred in the PDP service,
7272
// and provides a way to query these events.
7373
// This contract only supports one PDP service caller, set in the constructor.
7474
contract SimplePDPService is PDPListener, PDPRecordKeeper, Initializable, UUPSUpgradeable, OwnableUpgradeable {
75-
76-
enum FaultType {
77-
NONE,
78-
LATE,
79-
SKIPPED
80-
}
81-
82-
event Debug(string message, uint256 value);
83-
event FaultRecord(FaultType faultType, uint256 periodsFaulted);
75+
event FaultRecord(uint256 periodsFaulted);
8476

8577
// The address of the PDP verifier contract that is allowed to call this contract
8678
address public pdpVerifierAddress;
@@ -142,37 +134,48 @@ contract SimplePDPService is PDPListener, PDPRecordKeeper, Initializable, UUPSUp
142134
// it also checks that proofs are not late and emits a fault record if so
143135
function posessionProven(uint256 proofSetId, uint256 challengedLeafCount, uint256 seed, uint256 challengeCount) external onlyPDPVerifier {
144136
receiveProofSetEvent(proofSetId, OperationType.PROVE_POSSESSION, abi.encode(challengedLeafCount, seed, challengeCount));
145-
emit Debug("Here we go", 0);
146137
if (provenThisPeriod[proofSetId]) {
147-
// return immediately, we've already witnessed a proof for this proof set this period
148-
return;
138+
revert("Only one proof of possession allowed per proving period. Open a new proving period.");
149139
}
150-
151140
if (challengeCount < getChallengesPerProof()) {
152141
revert("Invalid challenge count < 5");
153142
}
154-
emit Debug("deadline", provingDeadlines[proofSetId]);
155-
emit Debug("block number", block.number);
156-
// check for late proof
143+
// check for proof outside of proving period
157144
if (provingDeadlines[proofSetId] < block.number) {
158-
uint256 periodsLate = 1 + ((block.number - provingDeadlines[proofSetId]) / getMaxProvingPeriod());
159-
emit Debug("we're late", periodsLate);
160-
emit FaultRecord(FaultType.LATE, periodsLate);
145+
revert("Current proving period passed. Open a new proving period.");
146+
}
147+
if (provingDeadlines[proofSetId] - getMaxProvingPeriod() >= block.number) {
148+
revert("Too early. Wait for proving period to open");
161149
}
162150
provenThisPeriod[proofSetId] = true;
163151
}
164152

165-
// nextProvingPeriod checks for unsubmitted proof and emits a fault record if so
153+
// nextProvingPeriod checks for unsubmitted proof and emits a fault if so
166154
function nextProvingPeriod(uint256 proofSetId, uint256 leafCount) external onlyPDPVerifier {
167155
receiveProofSetEvent(proofSetId, OperationType.NEXT_PROVING_PERIOD, abi.encode(leafCount));
168-
// check for unsubmitted proof
169-
if (!provenThisPeriod[proofSetId]) {
170-
uint256 periodsSkipped = 1;
171-
if (provingDeadlines[proofSetId] < block.number) {
172-
periodsSkipped = 1 + ((block.number - provingDeadlines[proofSetId]) / getMaxProvingPeriod());
173-
}
174-
emit FaultRecord(FaultType.SKIPPED, periodsSkipped);
156+
// Noop when proving period not yet open
157+
// Can only get here if calling nextProvingPeriod multiple times within the same proving period
158+
uint256 prevDeadline = provingDeadlines[proofSetId] - getMaxProvingPeriod();
159+
if (block.number <= prevDeadline) {
160+
revert("One call to nextProvingPeriod allowed per proving period");
175161
}
162+
163+
uint256 periodsSkipped;
164+
// Proving period is open 0 skipped periods
165+
if (block.number <= provingDeadlines[proofSetId]) {
166+
periodsSkipped = 0;
167+
} else { // Proving period has closed possible some skipped periods
168+
periodsSkipped = (block.number - (provingDeadlines[proofSetId] + 1)) / getMaxProvingPeriod();
169+
}
170+
uint256 faultPeriods = periodsSkipped;
171+
if (!provenThisPeriod[proofSetId]) {
172+
// include previous unproven period
173+
faultPeriods += 1;
174+
}
175+
if (faultPeriods > 0) {
176+
emit FaultRecord(faultPeriods);
177+
}
178+
provingDeadlines[proofSetId] = provingDeadlines[proofSetId] + getMaxProvingPeriod()*(periodsSkipped+1);
176179
provenThisPeriod[proofSetId] = false;
177180
}
178-
}
181+
}

test/PDPVerifier.t.sol

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {PDPFees} from "../src/Fees.sol";
1010
import {SimplePDPService, PDPRecordKeeper} from "../src/SimplePDPService.sol";
1111

1212
contract PDPVerifierProofSetCreateDeleteTest is Test {
13-
SimplePDPService listener;
13+
TestingRecordKeeperService listener;
1414
ListenerHelper listenerAssert;
1515
PDPVerifier pdpVerifier;
1616

@@ -23,10 +23,7 @@ contract PDPVerifierProofSetCreateDeleteTest is Test {
2323
);
2424
MyERC1967Proxy proxy = new MyERC1967Proxy(address(pdpVerifierImpl), initializeData);
2525
pdpVerifier = PDPVerifier(address(proxy));
26-
SimplePDPService listenerImpl = new SimplePDPService();
27-
initializeData = abi.encodeWithSelector(SimplePDPService.initialize.selector, address(pdpVerifier));
28-
MyERC1967Proxy listenerProxy = new MyERC1967Proxy(address(listenerImpl), initializeData);
29-
listener = SimplePDPService(address(listenerProxy));
26+
listener = new TestingRecordKeeperService();
3027
listenerAssert = new ListenerHelper(address(listener));
3128
}
3229
function tearDown() public view {
@@ -147,7 +144,7 @@ contract PDPVerifierProofSetCreateDeleteTest is Test {
147144

148145
contract PDPVerifierOwnershipTest is Test {
149146
PDPVerifier pdpVerifier;
150-
SimplePDPService listener;
147+
TestingRecordKeeperService listener;
151148
address public owner;
152149
address public nextOwner;
153150
address public nonOwner;
@@ -160,10 +157,7 @@ contract PDPVerifierOwnershipTest is Test {
160157
);
161158
MyERC1967Proxy proxy = new MyERC1967Proxy(address(pdpVerifierImpl), initializeData);
162159
pdpVerifier = PDPVerifier(address(proxy));
163-
SimplePDPService listenerImpl = new SimplePDPService();
164-
initializeData = abi.encodeWithSelector(SimplePDPService.initialize.selector, address(pdpVerifier));
165-
MyERC1967Proxy listenerProxy = new MyERC1967Proxy(address(listenerImpl), initializeData);
166-
listener = SimplePDPService(address(listenerProxy));
160+
listener = new TestingRecordKeeperService();
167161

168162
owner = address(this);
169163
nextOwner = address(0x1234);
@@ -231,7 +225,7 @@ contract PDPVerifierProofSetMutateTest is Test {
231225
uint256 constant challengeFinalityDelay = 2;
232226

233227
PDPVerifier pdpVerifier;
234-
SimplePDPService listener;
228+
TestingRecordKeeperService listener;
235229
ListenerHelper listenerAssert;
236230

237231
function setUp() public {
@@ -242,10 +236,7 @@ contract PDPVerifierProofSetMutateTest is Test {
242236
);
243237
MyERC1967Proxy proxy = new MyERC1967Proxy(address(pdpVerifierImpl), initializeData);
244238
pdpVerifier = PDPVerifier(address(proxy));
245-
SimplePDPService listenerImpl = new SimplePDPService();
246-
initializeData = abi.encodeWithSelector(SimplePDPService.initialize.selector, address(pdpVerifier));
247-
MyERC1967Proxy listenerProxy = new MyERC1967Proxy(address(listenerImpl), initializeData);
248-
listener = SimplePDPService(address(listenerProxy));
239+
listener = new TestingRecordKeeperService();
249240
listenerAssert = new ListenerHelper(address(listener));
250241
}
251242

@@ -896,7 +887,7 @@ import "../src/PDPVerifier.sol";
896887

897888
contract SumTreeAddTest is Test {
898889
SumTreeInternalTestPDPVerifier pdpVerifier;
899-
SimplePDPService listener;
890+
TestingRecordKeeperService listener;
900891
uint256 testSetId;
901892

902893
function setUp() public {
@@ -907,10 +898,7 @@ contract SumTreeAddTest is Test {
907898
);
908899
MyERC1967Proxy proxy = new MyERC1967Proxy(address(pdpVerifierImpl), initializeData);
909900
pdpVerifier = SumTreeInternalTestPDPVerifier(address(proxy));
910-
SimplePDPService listenerImpl = new SimplePDPService();
911-
initializeData = abi.encodeWithSelector(SimplePDPService.initialize.selector, address(pdpVerifier));
912-
MyERC1967Proxy listenerProxy = new MyERC1967Proxy(address(listenerImpl), initializeData);
913-
listener = SimplePDPService(address(listenerProxy));
901+
listener = new TestingRecordKeeperService();
914902
testSetId = pdpVerifier.createProofSet{value: PDPFees.sybilFee()}(address(listener));
915903
}
916904

@@ -1272,7 +1260,7 @@ contract PDPListenerIntegrationTest is Test {
12721260

12731261
contract PDPVerifierE2ETest is Test, ProofBuilderHelper {
12741262
PDPVerifier pdpVerifier;
1275-
SimplePDPService listener;
1263+
TestingRecordKeeperService listener;
12761264
uint256 constant challengeFinalityDelay = 2;
12771265

12781266
function setUp() public {
@@ -1283,10 +1271,7 @@ contract PDPVerifierE2ETest is Test, ProofBuilderHelper {
12831271
);
12841272
MyERC1967Proxy proxy = new MyERC1967Proxy(address(pdpVerifierImpl), initializeData);
12851273
pdpVerifier = PDPVerifier(address(proxy));
1286-
SimplePDPService listenerImpl = new SimplePDPService();
1287-
initializeData = abi.encodeWithSelector(SimplePDPService.initialize.selector, address(pdpVerifier));
1288-
MyERC1967Proxy listenerProxy = new MyERC1967Proxy(address(listenerImpl), initializeData);
1289-
listener = SimplePDPService(address(listenerProxy));
1274+
listener = new TestingRecordKeeperService();
12901275
}
12911276

12921277
function testCompleteProvingPeriodE2E() public {

0 commit comments

Comments
 (0)