@@ -7,8 +7,11 @@ import "./type.sol";
7
7
import "./interface.sol " ;
8
8
9
9
contract PDP is Initializable , IPDP , IFsEvent {
10
- using IterableMapping for ProofsPool;
10
+ using ProofsMapping for ProofsPool;
11
+ using ChallengeMapping for ChallengePool;
12
+
11
13
ProofsPool proofsPool;
14
+ mapping (bytes => ChallengePool) challengeMap; // key => map(chKey => Challenge)
12
15
13
16
function initialize () public initializer {}
14
17
@@ -116,12 +119,11 @@ contract PDP is Initializable, IPDP, IFsEvent {
116
119
return pReturns;
117
120
}
118
121
119
- function VerifyProof (ProofRecord memory vParams )
120
- public
121
- payable
122
- virtual
123
- override
124
- {
122
+ function VerifyProof (
123
+ ProofRecord memory vParams ,
124
+ Challenge[] memory chgs ,
125
+ MerklePath[] memory mps
126
+ ) public payable virtual override {
125
127
ProofRecord memory pr;
126
128
pr.Proof.Version = vParams.Proof.Version;
127
129
pr.Proof.Proofs = vParams.Proof.Proofs;
@@ -133,7 +135,12 @@ contract PDP is Initializable, IPDP, IFsEvent {
133
135
pr.Proof.RootHashes = vParams.Proof.RootHashes;
134
136
pr.State = vParams.State;
135
137
pr.LastUpdateHeight = block .number ;
136
- bytes memory key = GetKeyByProofParams (vParams.Proof);
138
+ bytes memory key = GetKeyByProofParams (vParams.Proof, chgs, mps);
139
+ for (uint32 i = 0 ; i < chgs.length ; i++ ) {
140
+ bytes memory chKey = GetChallengeKey (chgs[i]);
141
+ ChallengePool storage chMap = challengeMap[key];
142
+ chMap.insert (chKey, chgs[i]);
143
+ }
137
144
proofsPool.insert (key, pr);
138
145
}
139
146
@@ -146,9 +153,9 @@ contract PDP is Initializable, IPDP, IFsEvent {
146
153
{
147
154
// TODO
148
155
return true ;
149
- bytes memory key = GetKeyByProofParams (vParams);
150
- ProofRecord memory pr = proofsPool.data[key].value;
151
- return pr.State;
156
+ // bytes memory key = GetKeyByProofParams(vParams);
157
+ // ProofRecord memory pr = proofsPool.data[key].value;
158
+ // return pr.State;
152
159
}
153
160
154
161
function VerifyPlotData (VerifyPlotDataParams memory vParams )
@@ -163,11 +170,16 @@ contract PDP is Initializable, IPDP, IFsEvent {
163
170
return true ;
164
171
}
165
172
166
- function GetKeyByProofParams (ProofParams memory vParams )
167
- public
168
- pure
169
- returns (bytes memory )
170
- {
173
+ function GetChallengeKey (Challenge memory chg ) public pure returns (bytes memory ) {
174
+ bytes memory key = abi.encodePacked (chg.Index, chg.Rand);
175
+ return key;
176
+ }
177
+
178
+ function GetKeyByProofParams (
179
+ ProofParams memory vParams ,
180
+ Challenge[] memory chgs ,
181
+ MerklePath[] memory mps
182
+ ) public pure returns (bytes memory ) {
171
183
bytes memory ids;
172
184
for (uint32 i = 0 ; i < vParams.FileIds.length ; i++ ) {
173
185
ids = abi.encodePacked (ids, vParams.FileIds[i]);
@@ -176,22 +188,18 @@ contract PDP is Initializable, IPDP, IFsEvent {
176
188
for (uint32 i = 0 ; i < vParams.Tags.length ; i++ ) {
177
189
tags = abi.encodePacked (tags, vParams.Tags[i]);
178
190
}
179
- // TODO
180
191
bytes memory challenges;
181
- // for (uint32 i = 0; i < vParams.Challenges .length; i++) {
182
- // challenges = abi.encodePacked(
183
- // challenges,
184
- // vParams.Challenges [i].Index,
185
- // vParams.Challenges [i].Rand
186
- // );
187
- // }
192
+ for (uint32 i = 0 ; i < chgs .length ; i++ ) {
193
+ challenges = abi.encodePacked (
194
+ challenges,
195
+ chgs [i].Index,
196
+ chgs [i].Rand
197
+ );
198
+ }
188
199
bytes memory merklePath;
189
- // for (uint32 i = 0; i < vParams.MerklePath_.length; i++) {
190
- // merklePath = abi.encodePacked(
191
- // merklePath,
192
- // vParams.MerklePath_[i].PathLen
193
- // );
194
- // }
200
+ for (uint32 i = 0 ; i < mps.length ; i++ ) {
201
+ merklePath = abi.encodePacked (merklePath, mps[i].PathLen);
202
+ }
195
203
string memory keyStr = string (
196
204
abi.encodePacked (
197
205
vParams.Version,
@@ -225,7 +233,7 @@ struct ProofsPool {
225
233
uint256 size;
226
234
}
227
235
228
- library IterableMapping {
236
+ library ProofsMapping {
229
237
function insert (
230
238
ProofsPool storage self ,
231
239
bytes memory key ,
@@ -300,3 +308,94 @@ library IterableMapping {
300
308
value = self.data[key].value;
301
309
}
302
310
}
311
+
312
+ // challenge map
313
+ struct IndexValueCH {
314
+ uint256 keyIndex;
315
+ Challenge value;
316
+ }
317
+ struct KeyFlagCh {
318
+ bytes key;
319
+ bool deleted;
320
+ }
321
+ struct ChallengePool {
322
+ mapping (bytes => IndexValueCH) data;
323
+ KeyFlagCh[] keys;
324
+ uint256 size;
325
+ }
326
+
327
+ library ChallengeMapping {
328
+ function insert (
329
+ ChallengePool storage self ,
330
+ bytes memory key ,
331
+ Challenge memory value
332
+ ) internal returns (bool replaced ) {
333
+ uint256 keyIndex = self.data[key].keyIndex;
334
+ self.data[key].value = value;
335
+ if (keyIndex > 0 ) return true ;
336
+ else {
337
+ keyIndex = self.keys.length ;
338
+ self.keys.push ();
339
+ self.data[key].keyIndex = keyIndex + 1 ;
340
+ self.keys[keyIndex].key = key;
341
+ self.size++ ;
342
+ return false ;
343
+ }
344
+ }
345
+
346
+ function remove (ChallengePool storage self , bytes memory key )
347
+ internal
348
+ returns (bool success )
349
+ {
350
+ uint256 keyIndex = self.data[key].keyIndex;
351
+ if (keyIndex == 0 ) return false ;
352
+ delete self.data[key];
353
+ self.keys[keyIndex - 1 ].deleted = true ;
354
+ self.size-- ;
355
+ }
356
+
357
+ function contains (ChallengePool storage self , bytes memory key )
358
+ internal
359
+ view
360
+ returns (bool )
361
+ {
362
+ return self.data[key].keyIndex > 0 ;
363
+ }
364
+
365
+ function iterate_start (ChallengePool storage self )
366
+ internal
367
+ view
368
+ returns (uint256 keyIndex )
369
+ {
370
+ uint256 index = iterate_next (self, type (uint256 ).min);
371
+ return index - 1 ;
372
+ }
373
+
374
+ function iterate_valid (ChallengePool storage self , uint256 keyIndex )
375
+ internal
376
+ view
377
+ returns (bool )
378
+ {
379
+ return keyIndex < self.keys.length ;
380
+ }
381
+
382
+ function iterate_next (ChallengePool storage self , uint256 keyIndex )
383
+ internal
384
+ view
385
+ returns (uint256 r_keyIndex )
386
+ {
387
+ keyIndex++ ;
388
+ while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
389
+ keyIndex++ ;
390
+ return keyIndex;
391
+ }
392
+
393
+ function iterate_get (ChallengePool storage self , uint256 keyIndex )
394
+ internal
395
+ view
396
+ returns (bytes memory key , Challenge memory value )
397
+ {
398
+ key = self.keys[keyIndex].key;
399
+ value = self.data[key].value;
400
+ }
401
+ }
0 commit comments