@@ -219,13 +219,13 @@ contract ProtocolAdapterMockVerifierTest is Test {
219
219
_mockPa.execute (txn);
220
220
}
221
221
222
- uint256 public constant MAX_ACTION_PARAMS = 4 ;
222
+ uint256 public constant MAX_ACTIONS = 4 ;
223
223
uint256 public constant MAX_RESOURCES = 4 ;
224
224
225
225
struct ActionParams {
226
226
Resource[2 ][MAX_RESOURCES] resources;
227
- uint256 [MAX_RESOURCES] bijections ;
228
- uint256 len ;
227
+ uint256 [MAX_RESOURCES] bijection ;
228
+ uint256 targetResourcesLen ;
229
229
}
230
230
231
231
function commitment (Resource memory resource ) internal pure returns (bytes32 hash ) {
@@ -244,15 +244,15 @@ contract ProtocolAdapterMockVerifierTest is Test {
244
244
root = SHA256.EMPTY_HASH;
245
245
}
246
246
247
- function truncate_resources (Resource[2 ][MAX_RESOURCES] memory resources , uint256 len ) public returns (Resource[2 ][] memory truncatedResources ) {
247
+ function truncateResources (Resource[2 ][MAX_RESOURCES] memory resources , uint256 len ) public pure returns (Resource[2 ][] memory truncatedResources ) {
248
248
truncatedResources = new Resource [2 ][](len);
249
249
for (uint256 i = 0 ; i < len; i++ ) {
250
250
truncatedResources[i][0 ] = resources[i][0 ];
251
251
truncatedResources[i][1 ] = resources[i][1 ];
252
252
}
253
253
}
254
254
255
- function generateBijection (uint256 [MAX_RESOURCES] memory input , uint256 len ) public returns (uint256 [] memory output ) {
255
+ function generateBijection (uint256 [MAX_RESOURCES] memory input , uint256 len ) public pure returns (uint256 [] memory output ) {
256
256
output = new uint256 [](len);
257
257
uint256 [] memory duplicates = new uint256 [](len);
258
258
uint256 duplicateCount = 0 ;
@@ -275,23 +275,25 @@ contract ProtocolAdapterMockVerifierTest is Test {
275
275
}
276
276
}
277
277
278
- function generate_action (ActionParams memory params ) public returns (Action memory action , uint256 totalValueCommitmentRandomness ) {
279
- Resource[2 ][] memory truncatedResources = truncate_resources (params.resources, params.len % MAX_RESOURCES);
278
+ function generateAction (ActionParams memory params ) public returns (Action memory action , uint256 totalValueCommitmentRandomness ) {
279
+ Resource[2 ][] memory truncatedResources = truncateResources (params.resources, params.targetResourcesLen % MAX_RESOURCES);
280
280
action.logicVerifierInputs = new Logic.VerifierInput [](truncatedResources.length * 2 );
281
281
action.complianceVerifierInputs = new Compliance.VerifierInput [](truncatedResources.length );
282
+ // Created empty app data for all the resources
282
283
Logic.AppData memory appData = Logic.AppData ({
283
284
resourcePayload: new Logic.ExpirableBlob [](0 ),
284
285
discoveryPayload: new Logic.ExpirableBlob [](0 ),
285
286
externalPayload: new Logic.ExpirableBlob [](0 ),
286
287
applicationPayload: new Logic.ExpirableBlob [](0 )
287
288
});
288
-
289
- uint256 [] memory bijections = generateBijection (params.bijections , truncatedResources.length );
289
+ // Match the created and consumed resources
290
+ uint256 [] memory bijection = generateBijection (params.bijection , truncatedResources.length );
290
291
for (uint256 i = 0 ; i < truncatedResources.length ; ++ i) {
291
- truncatedResources[bijections [i]][1 ].quantity = truncatedResources[i][0 ].quantity;
292
- truncatedResources[bijections [i]][1 ].logicRef = truncatedResources[i][0 ].logicRef;
293
- truncatedResources[bijections [i]][1 ].labelRef = truncatedResources[i][0 ].labelRef;
292
+ truncatedResources[bijection [i]][1 ].quantity = truncatedResources[i][0 ].quantity;
293
+ truncatedResources[bijection [i]][1 ].logicRef = truncatedResources[i][0 ].logicRef;
294
+ truncatedResources[bijection [i]][1 ].labelRef = truncatedResources[i][0 ].labelRef;
294
295
}
296
+ // Compute action tree tags and action tree root
295
297
bytes32 [] memory actionTreeTags = new bytes32 [](2 * truncatedResources.length );
296
298
for (uint256 i = 0 ; i < truncatedResources.length ; ++ i) {
297
299
uint256 index = (i * 2 );
@@ -300,67 +302,72 @@ contract ProtocolAdapterMockVerifierTest is Test {
300
302
actionTreeTags[index + 1 ] = commitment (truncatedResources[i][1 ]);
301
303
}
302
304
bytes32 actionTreeRoot = actionTreeTags.computeRoot ();
305
+ // Create logic and compliance verifier inputs
303
306
totalValueCommitmentRandomness = 0 ;
304
-
305
307
for (uint256 i = 0 ; i < truncatedResources.length ; i++ ) {
306
- bytes32 nullifier = nullifier (truncatedResources[i][0 ], 0 );
307
- bytes32 commitment = commitment (truncatedResources[i][1 ]);
308
-
308
+ Resource memory consumedResource = truncatedResources[i][0 ];
309
+ Resource memory createdResource = truncatedResources[i][1 ];
310
+ bytes32 nf = nullifier (consumedResource, 0 );
311
+ bytes32 cm = commitment (createdResource);
312
+
313
+ // Created logic verifier input for a consumed resource
309
314
action.logicVerifierInputs[2 * i] = Logic.VerifierInput ({
310
- tag: nullifier ,
311
- verifyingKey: truncatedResources[i][ 0 ] .logicRef,
315
+ tag: nf ,
316
+ verifyingKey: consumedResource .logicRef,
312
317
proof: "" ,
313
318
appData: appData
314
319
});
315
320
action.logicVerifierInputs[2 * i].proof = _mockVerifier.mockProve ({
316
- imageId: truncatedResources[i][ 0 ] .logicRef,
321
+ imageId: consumedResource .logicRef,
317
322
journalDigest: sha256 (action.logicVerifierInputs[2 * i].toJournal (actionTreeRoot, true ))
318
323
}).seal;
319
-
324
+ // Create logic verifier input for a created resource
320
325
action.logicVerifierInputs[2 * i + 1 ] = Logic.VerifierInput ({
321
- tag: commitment ,
322
- verifyingKey: truncatedResources[i][ 1 ] .logicRef,
326
+ tag: cm ,
327
+ verifyingKey: createdResource .logicRef,
323
328
proof: "" ,
324
329
appData: appData
325
330
});
326
331
action.logicVerifierInputs[2 * i + 1 ].proof = _mockVerifier.mockProve ({
327
- imageId: truncatedResources[i][ 1 ] .logicRef,
332
+ imageId: createdResource .logicRef,
328
333
journalDigest: sha256 (action.logicVerifierInputs[2 * i + 1 ].toJournal (actionTreeRoot, false ))
329
334
}).seal;
330
-
335
+ // Create the delta for the consumed resource
331
336
Delta.CurvePoint memory unitDelta = DeltaGen.generateInstance (
332
337
vm,
333
338
DeltaGen.InstanceInputs ({
334
- kind: kind (truncatedResources[i][ 0 ] ),
335
- quantity: truncatedResources[i][ 0 ] .quantity,
339
+ kind: kind (consumedResource ),
340
+ quantity: consumedResource .quantity,
336
341
consumed: true ,
337
342
valueCommitmentRandomness: 1
338
343
})
339
344
);
345
+ // Add the delta for the created resource
340
346
unitDelta = Delta.add (
341
347
unitDelta,
342
348
DeltaGen.generateInstance (
343
349
vm,
344
350
DeltaGen.InstanceInputs ({
345
- kind: kind (truncatedResources[i][ 1 ] ),
346
- quantity: truncatedResources[i][ 1 ] .quantity,
351
+ kind: kind (createdResource ),
352
+ quantity: createdResource .quantity,
347
353
consumed: false ,
348
354
valueCommitmentRandomness: 1
349
355
})
350
356
)
351
357
);
352
358
totalValueCommitmentRandomness += 2 ;
359
+ // Create the compliance verifier input
353
360
Compliance.Instance memory instance = Compliance.Instance ({
354
361
unitDeltaX: bytes32 (unitDelta.x),
355
362
unitDeltaY: bytes32 (unitDelta.y),
356
363
consumed: Compliance.ConsumedRefs ({
357
- nullifier: nullifier ,
358
- logicRef: truncatedResources[i][ 0 ] .logicRef,
364
+ nullifier: nf ,
365
+ logicRef: consumedResource .logicRef,
359
366
commitmentTreeRoot: initialRoot ()
360
367
}),
361
368
created: Compliance.CreatedRefs ({
362
- commitment: commitment ,
363
- logicRef: truncatedResources[i][ 1 ] .logicRef
369
+ commitment: cm ,
370
+ logicRef: createdResource .logicRef
364
371
})
365
372
});
366
373
action.complianceVerifierInputs[i] = Compliance.VerifierInput ({
@@ -370,22 +377,21 @@ contract ProtocolAdapterMockVerifierTest is Test {
370
377
}
371
378
}
372
379
373
- function truncate_action_params (ActionParams[MAX_ACTION_PARAMS] memory params , uint256 len ) public returns (ActionParams[] memory truncatedParams ) {
374
- truncatedParams = new ActionParams [](len);
375
- for (uint256 i = 0 ; i < len; i++ ) {
376
- truncatedParams[i] = params[i];
377
- }
380
+ struct TransactionParams {
381
+ ActionParams[MAX_ACTIONS] actionParams;
382
+ uint256 targetActionsLen;
378
383
}
379
384
380
- function test_random_transactions (ActionParams[MAX_ACTION_PARAMS] memory actionParams , uint256 actionParamsLen ) public {
381
- ActionParams[] memory truncatedActionParams = truncate_action_params (actionParams, actionParamsLen % MAX_ACTION_PARAMS);
382
- Action[] memory actions = new Action [](truncatedActionParams. length );
385
+ function generateTransaction (TransactionParams memory params ) public returns (Transaction memory txn ) {
386
+ // Generate actions
387
+ Action[] memory actions = new Action [](params.targetActionsLen % MAX_ACTIONS );
383
388
uint256 totalValueCommitmentRandomness = 0 ;
384
389
for (uint256 i = 0 ; i < actions.length ; i++ ) {
385
390
uint256 valueCommitmentRandomness;
386
- (actions[i], valueCommitmentRandomness) = generate_action (truncatedActionParams [i]);
391
+ (actions[i], valueCommitmentRandomness) = generateAction (params.actionParams [i]);
387
392
totalValueCommitmentRandomness += valueCommitmentRandomness;
388
393
}
394
+ // Generate delta proof
389
395
bytes memory proof = "" ;
390
396
bytes32 [] memory tags = TxGen.collectTags (actions);
391
397
if (tags.length != 0 ) {
@@ -397,12 +403,16 @@ contract ProtocolAdapterMockVerifierTest is Test {
397
403
})
398
404
);
399
405
}
400
- Transaction memory txn = Transaction ({
406
+ // Generate transaction
407
+ txn = Transaction ({
401
408
actions: actions,
402
409
deltaProof: proof,
403
410
aggregationProof: ""
404
411
});
405
- _mockPa.execute (txn);
412
+ }
413
+
414
+ function test_random_transactions_execute (TransactionParams memory params ) public {
415
+ _mockPa.execute (generateTransaction (params));
406
416
}
407
417
408
418
function test_execute_reverts_on_pre_existing_nullifier () public {
0 commit comments