-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamRandomizerLifecycle.t.sol
More file actions
1028 lines (891 loc) · 42.4 KB
/
Copy pathStreamRandomizerLifecycle.t.sol
File metadata and controls
1028 lines (891 loc) · 42.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "../smart-contracts/IXRandoms.sol";
import "../smart-contracts/IRandomizerLifecycle.sol";
import "../smart-contracts/RandomizerNXT.sol";
import "../smart-contracts/RandomizerRNG.sol";
import "../smart-contracts/RandomizerVRF.sol";
import "../smart-contracts/StreamRandomizerLifecycle.sol";
import "./helpers/Assertions.sol";
import "./helpers/CharacterizationTestBase.sol";
import "./helpers/StreamFixture.sol";
import "./mocks/MockRandomizer.sol";
import "./mocks/MockRandomizerCore.sol";
contract UnsupportedLifecycleRandomizer is IRandomizer {
function calculateTokenHash(uint256, uint256, uint256) external { }
function isRandomizerContract() external pure returns (bool) {
return true;
}
function supportsRandomizerLifecycle() external pure returns (bool) {
return false;
}
// Intentionally reverts because lifecycle support is false; a caller that
// still probes pending requests has a bug, not unfinished test scaffolding.
function pendingRandomnessRequests(uint256) external pure returns (uint256) {
revert("unsupported lifecycle pending probe");
}
}
contract RevertingPendingLifecycleRandomizer is IRandomizer {
function calculateTokenHash(uint256, uint256, uint256) external { }
function isRandomizerContract() external pure returns (bool) {
return true;
}
function supportsRandomizerLifecycle() external pure returns (bool) {
return true;
}
function pendingRandomnessRequests(uint256) external pure returns (uint256) {
revert("pending probe failed");
}
}
contract StreamRandomizerLifecycleTest is CharacterizationTestBase, StreamFixture {
using Assertions for address;
using Assertions for bool;
using Assertions for bytes32;
using Assertions for uint256;
address private constant PAYOUT = address(0x2002);
address private constant CURATORS_POOL = address(0x3003);
uint256 private constant TOKEN_ID = 1;
uint256 private constant SECOND_TOKEN_ID = TOKEN_ID + 1;
uint256 private constant COLLECTION_ID = 1;
bytes32 private constant PAUSE_REASON = keccak256("randomness-incident");
bytes32 private constant RANDOMNESS_SEED_TYPEHASH = keccak256(
"6529StreamRandomnessSeed(address provider,uint256 requestId,uint256 collectionId,uint256 tokenId,uint256 randomizerEpoch,bytes32 rawOutputHash)"
);
bytes32 private constant COLLECTION_RANDOMIZER_UPDATED_TOPIC =
keccak256("CollectionRandomizerUpdated(uint256,address,address,uint256)");
bytes32 private constant RANDOMNESS_FULFILLED_TOPIC =
keccak256("RandomnessFulfilled(uint256,uint256,uint256,address,uint256,bytes32,bytes32)");
bytes32 private constant PROVIDER_REQUEST_FULFILLED_TOPIC =
keccak256("RequestFulfilled(uint256,uint256[])");
bytes32 private constant RANDOMNESS_POST_PROCESSING_FAILED_TOPIC = keccak256(
"RandomnessPostProcessingFailed(uint256,uint256,uint256,address,uint256,bytes32,bytes32,bytes32)"
);
function testVrfRequestRecordsLifecycleAndFulfillmentSetsHashOnce() public {
(DeployedStream memory deployed, MockVrfCoordinator coordinator, NextGenRandomizerVRF vrf) =
_deployVrfRandomizer();
_mintToken(deployed);
StreamRandomizerLifecycle.RandomnessRequest memory request =
vrf.retrieveRandomnessRequest(1);
request.collectionId.assertEq(COLLECTION_ID, "collection");
request.tokenId.assertEq(TOKEN_ID, "token");
request.provider.assertEq(address(vrf), "provider");
request.providerRequestId.assertEq(1, "provider request");
request.randomizerEpoch.assertEq(2, "epoch");
uint256(request.state)
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Pending), "state");
vrf.tokenToRequest(TOKEN_ID).assertEq(1, "token request");
vrf.requestToToken(1).assertEq(TOKEN_ID, "request token");
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "pending collection");
vrf.totalPendingRandomnessRequests().assertEq(1, "pending total");
uint256[] memory words = _words(777);
bytes32 rawOutputHash = _rawOutputHash(words);
bytes32 expectedSeed =
_expectedSeed(address(vrf), uint256(1), COLLECTION_ID, TOKEN_ID, uint256(2), words);
vm.recordLogs();
coordinator.fulfill(vrf, 1, words);
Vm.Log[] memory logs = vm.getRecordedLogs();
_assertRandomnessFulfilled(
logs, address(vrf), 1, TOKEN_ID, address(vrf), uint256(2), expectedSeed, rawOutputHash
);
_assertProviderRequestFulfilled(logs, address(vrf), 1, words);
deployed.core.retrieveTokenHash(TOKEN_ID).assertEq(expectedSeed, "token hash");
request = vrf.retrieveRandomnessRequest(1);
uint256(request.state)
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.Fulfilled), "fulfilled"
);
request.derivedSeed.assertEq(expectedSeed, "stored seed");
request.rawOutputHash.assertEq(rawOutputHash, "raw output hash");
IRandomizerLifecycle lifecycle = IRandomizerLifecycle(address(vrf));
IRandomizerLifecycle.RandomnessRequest memory interfaceRequest =
lifecycle.retrieveRandomnessRequest(1);
uint256(interfaceRequest.state)
.assertEq(uint256(IRandomizerLifecycle.RandomnessRequestState.Fulfilled), "iface");
interfaceRequest.rawOutputHash.assertEq(rawOutputHash, "iface raw hash");
lifecycle.requestToToken(1).assertEq(TOKEN_ID, "iface request token");
lifecycle.tokenToRequest(TOKEN_ID).assertEq(1, "iface token request");
lifecycle.tokenIdToCollection(TOKEN_ID).assertEq(COLLECTION_ID, "iface token collection");
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "collection cleared");
vrf.totalPendingRandomnessRequests().assertEq(0, "total cleared");
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.Fulfilled
)
);
coordinator.fulfill(vrf, 1, words);
}
function testTokenLevelViewsExposeEmptyPendingAndFulfilledState() public {
(DeployedStream memory deployed, MockVrfCoordinator coordinator, NextGenRandomizerVRF vrf) =
_deployVrfRandomizer();
StreamRandomizerLifecycle.RandomnessRequest memory request =
vrf.retrieveRandomnessRequestForToken(TOKEN_ID);
uint256(request.state)
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.None), "empty state");
request.provider.assertEq(address(0), "empty provider");
uint256(vrf.randomnessRequestStateForToken(TOKEN_ID))
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.None), "empty token");
_mintToken(deployed);
request = vrf.retrieveRandomnessRequestForToken(TOKEN_ID);
request.providerRequestId.assertEq(1, "request id");
request.collectionId.assertEq(COLLECTION_ID, "pending collection");
request.tokenId.assertEq(TOKEN_ID, "pending token");
uint256(request.state)
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Pending), "pending");
uint256(vrf.randomnessRequestStateForToken(TOKEN_ID))
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.Pending), "token pending"
);
uint256[] memory words = _words(111);
bytes32 expectedSeed =
_expectedSeed(address(vrf), uint256(1), COLLECTION_ID, TOKEN_ID, uint256(2), words);
bytes32 rawOutputHash = _rawOutputHash(words);
coordinator.fulfill(vrf, 1, words);
request = vrf.retrieveRandomnessRequestForToken(TOKEN_ID);
uint256(request.state)
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Fulfilled), "done");
request.derivedSeed.assertEq(expectedSeed, "token seed");
request.rawOutputHash.assertEq(rawOutputHash, "token raw hash");
(request.fulfilledBlock > 0).assertTrue("fulfilled block");
(request.fulfilledTimestamp > 0).assertTrue("fulfilled timestamp");
uint256(vrf.randomnessRequestStateForToken(TOKEN_ID))
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.Fulfilled), "token done"
);
}
function testVrfSeedIgnoresMutableTokenDataChangedAfterRequest() public {
(DeployedStream memory deployed, MockVrfCoordinator coordinator, NextGenRandomizerVRF vrf) =
_deployVrfRandomizer();
_mintToken(deployed);
deployed.core.changeTokenData(TOKEN_ID, "changed-after-randomness-request");
uint256[] memory words = _words(222);
bytes32 rawOutputHash = _rawOutputHash(words);
bytes32 expectedSeed =
_expectedSeed(address(vrf), uint256(1), COLLECTION_ID, TOKEN_ID, uint256(2), words);
coordinator.fulfill(vrf, 1, words);
StreamRandomizerLifecycle.RandomnessRequest memory request =
vrf.retrieveRandomnessRequest(1);
request.derivedSeed.assertEq(expectedSeed, "seed");
request.rawOutputHash.assertEq(rawOutputHash, "raw output hash");
deployed.core.retrieveTokenHash(TOKEN_ID).assertEq(expectedSeed, "token hash");
}
function testVrfUnknownAndEmptyFulfillmentsFailClosed() public {
(DeployedStream memory deployed, MockVrfCoordinator coordinator, NextGenRandomizerVRF vrf) =
_deployVrfRandomizer();
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.UnknownRandomnessRequest.selector, uint256(999)
)
);
coordinator.fulfill(vrf, 999, _words(1));
_mintToken(deployed);
vm.expectRevert(
abi.encodeWithSelector(StreamRandomizerLifecycle.EmptyRandomWords.selector, uint256(1))
);
coordinator.fulfill(vrf, 1, new uint256[](0));
}
function testVrfPostProcessingFailureRecordsFailedState() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockRandomizerCore core = new MockRandomizerCore();
MockVrfCoordinator coordinator = new MockVrfCoordinator();
NextGenRandomizerVRF vrf = new NextGenRandomizerVRF(
1, address(coordinator), address(core), address(deployed.admins)
);
core.setRandomizer(COLLECTION_ID, address(vrf), 1);
core.setTokenCollection(TOKEN_ID, COLLECTION_ID);
core.setRejectTokenHash(true);
vm.prank(address(core));
vrf.calculateTokenHash(COLLECTION_ID, TOKEN_ID, 123);
uint256[] memory words = _words(777);
bytes32 rawOutputHash = _rawOutputHash(words);
bytes32 expectedSeed =
_expectedSeed(address(vrf), uint256(1), COLLECTION_ID, TOKEN_ID, uint256(1), words);
bytes32 failureDataHash =
keccak256(abi.encodeWithSelector(MockRandomizerCore.MockTokenHashRejected.selector));
vm.recordLogs();
coordinator.fulfill(vrf, 1, words);
_assertRandomnessPostProcessingFailed(
vm.getRecordedLogs(),
address(vrf),
1,
TOKEN_ID,
uint256(1),
expectedSeed,
rawOutputHash,
failureDataHash
);
StreamRandomizerLifecycle.RandomnessRequest memory request =
vrf.retrieveRandomnessRequest(1);
uint256(request.state)
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.FailedPostProcessing),
"failed state"
);
request.derivedSeed.assertEq(expectedSeed, "failed seed");
request.rawOutputHash.assertEq(rawOutputHash, "failed raw hash");
request.failureDataHash.assertEq(failureDataHash, "failure hash");
(request.fulfilledBlock > 0).assertTrue("failed block");
(request.fulfilledTimestamp > 0).assertTrue("failed timestamp");
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "pending collection");
vrf.totalPendingRandomnessRequests().assertEq(0, "pending total");
core.retrieveTokenHash(TOKEN_ID).assertEq(bytes32(0), "core hash");
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.FailedPostProcessing
)
);
coordinator.fulfill(vrf, 1, words);
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.FailedPostProcessing
)
);
vrf.markStaleRequest(1);
}
function testVrfStaleEpochOrProviderFulfillmentFails() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockRandomizerCore core = new MockRandomizerCore();
MockVrfCoordinator coordinator = new MockVrfCoordinator();
NextGenRandomizerVRF vrf = new NextGenRandomizerVRF(
1, address(coordinator), address(core), address(deployed.admins)
);
NoopRandomizer replacement = new NoopRandomizer();
core.setRandomizer(COLLECTION_ID, address(vrf), 1);
core.setTokenCollection(TOKEN_ID, COLLECTION_ID);
vm.prank(address(core));
vrf.calculateTokenHash(COLLECTION_ID, TOKEN_ID, 123);
core.setRandomizer(COLLECTION_ID, address(replacement), 2);
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.StaleRandomnessRequest.selector,
uint256(1),
uint256(1),
uint256(2),
address(vrf),
address(replacement)
)
);
coordinator.fulfill(vrf, 1, _words(777));
}
function testMarkedStaleRequestIsObservableAndCannotFulfill() public {
(DeployedStream memory deployed, MockVrfCoordinator coordinator, NextGenRandomizerVRF vrf) =
_deployVrfRandomizer();
_mintToken(deployed);
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "pending before stale");
vrf.markStaleRequest(1);
uint256(vrf.randomnessRequestState(1))
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Stale), "not stale");
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "pending after stale");
vrf.totalPendingRandomnessRequests().assertEq(0, "total after stale");
StreamRandomizerLifecycle.RandomnessRequest memory request =
vrf.retrieveRandomnessRequestForToken(TOKEN_ID);
uint256(request.state)
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.Stale), "token stale"
);
request.rawOutputHash.assertEq(bytes32(0), "stale raw hash");
uint256(vrf.randomnessRequestStateForToken(TOKEN_ID))
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Stale), "view stale");
IRandomizerLifecycle lifecycle = IRandomizerLifecycle(address(vrf));
IRandomizerLifecycle.RandomnessRequest memory interfaceRequest =
lifecycle.retrieveRandomnessRequestForToken(TOKEN_ID);
uint256(interfaceRequest.state)
.assertEq(uint256(IRandomizerLifecycle.RandomnessRequestState.Stale), "iface stale");
interfaceRequest.rawOutputHash.assertEq(bytes32(0), "iface stale raw hash");
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.Stale
)
);
coordinator.fulfill(vrf, 1, _words(777));
}
function testRandomizerMigrationWithNoPendingRequestsSucceedsAndEmitsEpoch() public {
(DeployedStream memory deployed,, NextGenRandomizerVRF vrf) = _deployVrfRandomizer();
NoopRandomizer replacement = new NoopRandomizer();
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "unexpected pending");
vm.recordLogs();
deployed.core.addRandomizer(COLLECTION_ID, address(replacement));
_assertCollectionRandomizerUpdated(
vm.getRecordedLogs(), address(deployed.core), address(vrf), address(replacement), 3
);
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(replacement), "replacement");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(3, "epoch");
}
function testUnsupportedLifecycleRandomizerDoesNotBlockMigration() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
UnsupportedLifecycleRandomizer unsupported = new UnsupportedLifecycleRandomizer();
NoopRandomizer replacement = new NoopRandomizer();
vm.recordLogs();
deployed.core.addRandomizer(COLLECTION_ID, address(unsupported));
_assertCollectionRandomizerUpdated(
vm.getRecordedLogs(),
address(deployed.core),
address(deployed.randomizer),
address(unsupported),
2
);
vm.recordLogs();
deployed.core.addRandomizer(COLLECTION_ID, address(replacement));
_assertCollectionRandomizerUpdated(
vm.getRecordedLogs(),
address(deployed.core),
address(unsupported),
address(replacement),
3
);
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(replacement), "replacement");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(3, "epoch");
}
function testSupportedLifecyclePendingProbeFailureBlocksMigration() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
RevertingPendingLifecycleRandomizer oldRandomizer =
new RevertingPendingLifecycleRandomizer();
NoopRandomizer replacement = new NoopRandomizer();
deployed.core.addRandomizer(COLLECTION_ID, address(oldRandomizer));
vm.expectRevert(abi.encodeWithSignature("Error(string)", "pending probe failed"));
deployed.core.addRandomizer(COLLECTION_ID, address(replacement));
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(oldRandomizer), "provider changed");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(2, "epoch changed");
}
function testRandomizerMigrationWithPendingRequestIsBlocked() public {
(DeployedStream memory deployed,, NextGenRandomizerVRF vrf) = _deployVrfRandomizer();
NoopRandomizer replacement = new NoopRandomizer();
_mintToken(deployed);
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "pending before migration");
vm.expectRevert(
abi.encodeWithSelector(
StreamCore.PendingRandomnessRequests.selector,
COLLECTION_ID,
address(vrf),
uint256(1)
)
);
deployed.core.addRandomizer(COLLECTION_ID, address(replacement));
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(vrf), "provider changed");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(2, "epoch changed");
vrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "pending changed");
}
function testFulfilledRequestUnblocksMigrationAndOldDuplicateCannotOverwrite() public {
(
DeployedStream memory deployed,
MockVrfCoordinator oldCoordinator,
NextGenRandomizerVRF oldVrf
) = _deployVrfRandomizer();
NoopRandomizer replacement = new NoopRandomizer();
_mintToken(deployed);
oldCoordinator.fulfill(oldVrf, 1, _words(777));
oldVrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "pending after fulfill");
deployed.core.addRandomizer(COLLECTION_ID, address(replacement));
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(replacement), "replacement");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(3, "epoch");
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.Fulfilled
)
);
oldCoordinator.fulfill(oldVrf, 1, _words(999));
}
function testMarkedStaleRequestUnblocksMigrationAndNewProviderFulfillment() public {
(
DeployedStream memory deployed,
MockVrfCoordinator oldCoordinator,
NextGenRandomizerVRF oldVrf
) = _deployVrfRandomizer();
MockVrfCoordinator newCoordinator = new MockVrfCoordinator();
NextGenRandomizerVRF newVrf = new NextGenRandomizerVRF(
1, address(newCoordinator), address(deployed.core), address(deployed.admins)
);
_mintToken(deployed);
oldVrf.markStaleRequest(1);
deployed.core.addRandomizer(COLLECTION_ID, address(newVrf));
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(newVrf), "new provider");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(3, "new epoch");
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.Stale
)
);
oldCoordinator.fulfill(oldVrf, 1, _words(777));
_mintToken(deployed, SECOND_TOKEN_ID);
StreamRandomizerLifecycle.RandomnessRequest memory request =
newVrf.retrieveRandomnessRequestForToken(SECOND_TOKEN_ID);
request.randomizerEpoch.assertEq(3, "request epoch");
uint256(request.state)
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Pending), "state");
newVrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "new pending");
uint256[] memory words = _words(888);
bytes32 expectedSeed = _expectedSeed(
address(newVrf), uint256(1), COLLECTION_ID, SECOND_TOKEN_ID, uint256(3), words
);
newCoordinator.fulfill(newVrf, 1, words);
deployed.core.retrieveTokenHash(SECOND_TOKEN_ID).assertEq(expectedSeed, "new hash");
newVrf.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "new pending cleared");
}
function testArrngPendingRequestBlocksMigration() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockArrngLifecycleController controller = new MockArrngLifecycleController();
NextGenRandomizerRNG rng = new NextGenRandomizerRNG(
address(deployed.core), address(deployed.admins), address(controller)
);
NoopRandomizer replacement = new NoopRandomizer();
deployed.core.addRandomizer(COLLECTION_ID, address(rng));
_mintToken(deployed);
rng.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "rng pending");
vm.expectRevert(
abi.encodeWithSelector(
StreamCore.PendingRandomnessRequests.selector,
COLLECTION_ID,
address(rng),
uint256(1)
)
);
deployed.core.addRandomizer(COLLECTION_ID, address(replacement));
deployed.core.viewCollectionRandomizerContract(COLLECTION_ID)
.assertEq(address(rng), "provider changed");
deployed.core.viewRandomizerEpoch(COLLECTION_ID).assertEq(2, "epoch changed");
}
function testRandomnessRequestPauseDoesNotBlockVrfFulfillment() public {
(DeployedStream memory deployed, MockVrfCoordinator coordinator, NextGenRandomizerVRF vrf) =
_deployVrfRandomizer();
_mintToken(deployed);
deployed.admins
.setPaused(deployed.admins.PAUSE_DOMAIN_RANDOMNESS_REQUEST(), true, PAUSE_REASON);
coordinator.fulfill(vrf, 1, _words(777));
(deployed.core.retrieveTokenHash(TOKEN_ID) == bytes32(0)).assertFalse("fulfillment paused");
}
function testArrngRequestAndFulfillmentUseSameLifecycle() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockArrngLifecycleController controller = new MockArrngLifecycleController();
NextGenRandomizerRNG rng = new NextGenRandomizerRNG(
address(deployed.core), address(deployed.admins), address(controller)
);
deployed.core.addRandomizer(COLLECTION_ID, address(rng));
_mintToken(deployed);
StreamRandomizerLifecycle.RandomnessRequest memory request =
rng.retrieveRandomnessRequest(1);
request.collectionId.assertEq(COLLECTION_ID, "collection");
request.tokenId.assertEq(TOKEN_ID, "token");
request.provider.assertEq(address(rng), "provider");
request.randomizerEpoch.assertEq(2, "epoch");
rng.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "pending");
uint256[] memory words = _words(999);
bytes32 rawOutputHash = _rawOutputHash(words);
bytes32 expectedSeed =
_expectedSeed(address(rng), uint256(1), COLLECTION_ID, TOKEN_ID, uint256(2), words);
vm.recordLogs();
controller.fulfill(rng, 1, words);
_assertProviderRequestFulfilled(vm.getRecordedLogs(), address(rng), 1, words);
deployed.core.retrieveTokenHash(TOKEN_ID).assertEq(expectedSeed, "token hash");
uint256(rng.randomnessRequestState(1))
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.Fulfilled), "fulfilled"
);
request = rng.retrieveRandomnessRequest(1);
request.rawOutputHash.assertEq(rawOutputHash, "raw output hash");
rng.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "pending cleared");
}
function testArrngPostProcessingFailureRecordsFailedState() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockRandomizerCore core = new MockRandomizerCore();
MockArrngLifecycleController controller = new MockArrngLifecycleController();
NextGenRandomizerRNG rng = new NextGenRandomizerRNG(
address(core), address(deployed.admins), address(controller)
);
core.setRandomizer(COLLECTION_ID, address(rng), 1);
core.setTokenCollection(TOKEN_ID, COLLECTION_ID);
core.setRejectTokenHash(true);
vm.prank(address(core));
rng.calculateTokenHash(COLLECTION_ID, TOKEN_ID, 123);
uint256[] memory words = _words(999);
bytes32 rawOutputHash = _rawOutputHash(words);
bytes32 expectedSeed =
_expectedSeed(address(rng), uint256(1), COLLECTION_ID, TOKEN_ID, uint256(1), words);
bytes32 failureDataHash =
keccak256(abi.encodeWithSelector(MockRandomizerCore.MockTokenHashRejected.selector));
vm.recordLogs();
controller.fulfill(rng, 1, words);
_assertRandomnessPostProcessingFailed(
vm.getRecordedLogs(),
address(rng),
1,
TOKEN_ID,
uint256(1),
expectedSeed,
rawOutputHash,
failureDataHash
);
StreamRandomizerLifecycle.RandomnessRequest memory request =
rng.retrieveRandomnessRequestForToken(TOKEN_ID);
uint256(request.state)
.assertEq(
uint256(StreamRandomizerLifecycle.RandomnessRequestState.FailedPostProcessing),
"failed state"
);
request.derivedSeed.assertEq(expectedSeed, "failed seed");
request.rawOutputHash.assertEq(rawOutputHash, "failed raw hash");
request.failureDataHash.assertEq(failureDataHash, "failure hash");
rng.pendingRandomnessRequests(COLLECTION_ID).assertEq(0, "pending collection");
rng.totalPendingRandomnessRequests().assertEq(0, "pending total");
core.retrieveTokenHash(TOKEN_ID).assertEq(bytes32(0), "core hash");
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.RandomnessRequestNotPending.selector,
uint256(1),
StreamRandomizerLifecycle.RandomnessRequestState.FailedPostProcessing
)
);
controller.fulfill(rng, 1, words);
}
function testArrngControllerCannotReenterFulfillmentDuringRequest() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
ReentrantArrngLifecycleController controller = new ReentrantArrngLifecycleController();
NextGenRandomizerRNG rng = new NextGenRandomizerRNG(
address(deployed.core), address(deployed.admins), address(controller)
);
deployed.core.addRandomizer(COLLECTION_ID, address(rng));
vm.expectRevert(
abi.encodeWithSelector(NextGenRandomizerRNG.RandomizerRequestReentrancy.selector)
);
_mintToken(deployed);
deployed.core.retrieveTokenHash(TOKEN_ID).assertEq(bytes32(0), "reentrant hash written");
}
function testArrngZeroRequestIdFailsBeforeRecordingLifecycle() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
ZeroRequestIdArrngLifecycleController controller =
new ZeroRequestIdArrngLifecycleController();
NextGenRandomizerRNG rng = new NextGenRandomizerRNG(
address(deployed.core), address(deployed.admins), address(controller)
);
deployed.core.addRandomizer(COLLECTION_ID, address(rng));
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.UnknownRandomnessRequest.selector, uint256(0)
)
);
_mintToken(deployed);
uint256(rng.randomnessRequestState(0))
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.None), "zero state");
rng.tokenToRequest(TOKEN_ID).assertEq(0, "token request recorded");
deployed.core.retrieveTokenHash(TOKEN_ID).assertEq(bytes32(0), "hash written");
}
function testNxtRandomizerCannotBeConfiguredForProductionCollections() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
NextGenRandomizerNXT nxt = new NextGenRandomizerNXT(
address(new MockXRandoms()), address(deployed.admins), address(deployed.core)
);
nxt.isRandomizerContract().assertFalse("nxt advertises production randomizer");
(bool success,) = address(deployed.core)
.call(
abi.encodeWithSelector(
deployed.core.addRandomizer.selector, COLLECTION_ID, address(nxt)
)
);
success.assertFalse("nxt configured as production randomizer");
}
function testVrfWrongCollectionBindingFailsClosed() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockRandomizerCore core = new MockRandomizerCore();
MockVrfCoordinator coordinator = new MockVrfCoordinator();
NextGenRandomizerVRF vrf = new NextGenRandomizerVRF(
1, address(coordinator), address(core), address(deployed.admins)
);
core.setRandomizer(COLLECTION_ID, address(vrf), 1);
core.setTokenCollection(TOKEN_ID, COLLECTION_ID + 1);
vm.prank(address(core));
vrf.calculateTokenHash(COLLECTION_ID, TOKEN_ID, 123);
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.WrongRandomnessTokenCollection.selector,
uint256(1),
TOKEN_ID,
COLLECTION_ID,
COLLECTION_ID + 1
)
);
coordinator.fulfill(vrf, 1, _words(777));
_assertWrongCollectionFulfillmentPreservedPending(core, vrf);
}
function testArrngWrongCollectionBindingFailsClosed() public {
DeployedStream memory deployed = deployStream(PAYOUT, CURATORS_POOL);
MockRandomizerCore core = new MockRandomizerCore();
MockArrngLifecycleController controller = new MockArrngLifecycleController();
NextGenRandomizerRNG rng =
new NextGenRandomizerRNG(address(core), address(deployed.admins), address(controller));
core.setRandomizer(COLLECTION_ID, address(rng), 1);
core.setTokenCollection(TOKEN_ID, COLLECTION_ID + 1);
vm.prank(address(core));
rng.calculateTokenHash(COLLECTION_ID, TOKEN_ID, 123);
vm.expectRevert(
abi.encodeWithSelector(
StreamRandomizerLifecycle.WrongRandomnessTokenCollection.selector,
uint256(1),
TOKEN_ID,
COLLECTION_ID,
COLLECTION_ID + 1
)
);
controller.fulfill(rng, 1, _words(888));
_assertWrongCollectionFulfillmentPreservedPending(core, rng);
}
function _deployVrfRandomizer()
private
returns (
DeployedStream memory deployed,
MockVrfCoordinator coordinator,
NextGenRandomizerVRF vrf
)
{
deployed = deployStream(PAYOUT, CURATORS_POOL);
coordinator = new MockVrfCoordinator();
vrf = new NextGenRandomizerVRF(
1, address(coordinator), address(deployed.core), address(deployed.admins)
);
deployed.core.addRandomizer(COLLECTION_ID, address(vrf));
}
function _mintToken(DeployedStream memory deployed) private {
_mintToken(deployed, TOKEN_ID);
}
function _mintToken(DeployedStream memory deployed, uint256 tokenId) private {
vm.prank(address(deployed.minter));
deployed.core.mint(tokenId, address(0xBEEF), "data", 123, COLLECTION_ID);
}
function _words(uint256 word) private pure returns (uint256[] memory words) {
words = new uint256[](1);
words[0] = word;
}
function _rawOutputHash(uint256[] memory words) private pure returns (bytes32) {
return keccak256(abi.encode(words));
}
function _expectedSeed(
address provider,
uint256 requestId,
uint256 collectionId,
uint256 tokenId,
uint256 randomizerEpoch,
uint256[] memory words
) private pure returns (bytes32) {
return keccak256(
abi.encode(
RANDOMNESS_SEED_TYPEHASH,
provider,
requestId,
collectionId,
tokenId,
randomizerEpoch,
_rawOutputHash(words)
)
);
}
function _assertRandomnessFulfilled(
Vm.Log[] memory logs,
address emitter,
uint256 requestId,
uint256 tokenId,
address expectedProvider,
uint256 expectedRandomizerEpoch,
bytes32 expectedSeed,
bytes32 expectedRawOutputHash
) private pure {
bool found = false;
for (uint256 i = 0; i < logs.length; i++) {
if (
logs[i].emitter == emitter && logs[i].topics.length == 4
&& logs[i].topics[0] == RANDOMNESS_FULFILLED_TOPIC
&& uint256(logs[i].topics[1]) == requestId
&& uint256(logs[i].topics[2]) == COLLECTION_ID
&& uint256(logs[i].topics[3]) == tokenId
) {
(
address actualProvider,
uint256 actualRandomizerEpoch,
bytes32 actualSeed,
bytes32 actualRawOutputHash
) = abi.decode(logs[i].data, (address, uint256, bytes32, bytes32));
bool matches = actualProvider == expectedProvider
&& actualRandomizerEpoch == expectedRandomizerEpoch
&& actualSeed == expectedSeed && actualRawOutputHash == expectedRawOutputHash;
found = found || matches;
}
}
found.assertTrue("fulfilled event");
}
function _assertProviderRequestFulfilled(
Vm.Log[] memory logs,
address emitter,
uint256 requestId,
uint256[] memory expectedWords
) private pure {
bool found = false;
for (uint256 i = 0; i < logs.length; i++) {
if (
logs[i].emitter == emitter && logs[i].topics.length == 1
&& logs[i].topics[0] == PROVIDER_REQUEST_FULFILLED_TOPIC
) {
(uint256 actualRequestId, uint256[] memory actualWords) =
abi.decode(logs[i].data, (uint256, uint256[]));
bool matches =
actualRequestId == requestId && _wordsMatch(actualWords, expectedWords);
found = found || matches;
}
}
found.assertTrue("provider fulfilled event");
}
function _wordsMatch(uint256[] memory actualWords, uint256[] memory expectedWords)
private
pure
returns (bool)
{
if (actualWords.length != expectedWords.length) {
return false;
}
for (uint256 i = 0; i < actualWords.length; i++) {
if (actualWords[i] != expectedWords[i]) {
return false;
}
}
return true;
}
function _assertCollectionRandomizerUpdated(
Vm.Log[] memory logs,
address emitter,
address oldRandomizer,
address newRandomizer,
uint256 epoch
) private pure {
bool found = false;
for (uint256 i = 0; i < logs.length; i++) {
if (
logs[i].emitter == emitter && logs[i].topics.length == 4
&& logs[i].topics[0] == COLLECTION_RANDOMIZER_UPDATED_TOPIC
&& uint256(logs[i].topics[1]) == COLLECTION_ID
&& address(uint160(uint256(logs[i].topics[2]))) == oldRandomizer
&& address(uint160(uint256(logs[i].topics[3]))) == newRandomizer
&& abi.decode(logs[i].data, (uint256)) == epoch
) {
found = true;
}
}
found.assertTrue("randomizer event");
}
function _assertRandomnessPostProcessingFailed(
Vm.Log[] memory logs,
address emitter,
uint256 requestId,
uint256 tokenId,
uint256 expectedEpoch,
bytes32 expectedSeed,
bytes32 expectedRawOutputHash,
bytes32 expectedFailureDataHash
) private pure {
bool found = false;
for (uint256 i = 0; i < logs.length; i++) {
if (
logs[i].emitter == emitter && logs[i].topics.length == 4
&& logs[i].topics[0] == RANDOMNESS_POST_PROCESSING_FAILED_TOPIC
&& uint256(logs[i].topics[1]) == requestId
&& uint256(logs[i].topics[2]) == COLLECTION_ID
&& uint256(logs[i].topics[3]) == tokenId
) {
(
address actualProvider,
uint256 actualEpoch,
bytes32 actualSeed,
bytes32 actualRawOutputHash,
bytes32 actualFailureDataHash
) = abi.decode(logs[i].data, (address, uint256, bytes32, bytes32, bytes32));
bool matches = actualProvider == emitter && actualEpoch == expectedEpoch
&& actualSeed == expectedSeed && actualRawOutputHash == expectedRawOutputHash
&& actualFailureDataHash == expectedFailureDataHash;
found = found || matches;
}
}
found.assertTrue("failed event");
}
function _assertWrongCollectionFulfillmentPreservedPending(
MockRandomizerCore core,
StreamRandomizerLifecycle randomizer
) private view {
StreamRandomizerLifecycle.RandomnessRequest memory request =
randomizer.retrieveRandomnessRequest(1);
request.collectionId.assertEq(COLLECTION_ID, "request collection");
request.tokenId.assertEq(TOKEN_ID, "request token");
request.provider.assertEq(address(randomizer), "request provider");
request.providerRequestId.assertEq(1, "provider request");
request.randomizerEpoch.assertEq(1, "request epoch");
uint256(request.state)
.assertEq(uint256(StreamRandomizerLifecycle.RandomnessRequestState.Pending), "state");
request.derivedSeed.assertEq(bytes32(0), "seed");
request.rawOutputHash.assertEq(bytes32(0), "raw output");
request.failureDataHash.assertEq(bytes32(0), "failure hash");
request.fulfilledBlock.assertEq(0, "fulfilled block");
request.fulfilledTimestamp.assertEq(0, "fulfilled timestamp");
randomizer.pendingRandomnessRequests(COLLECTION_ID).assertEq(1, "pending collection");
randomizer.totalPendingRandomnessRequests().assertEq(1, "pending total");
core.retrieveTokenHash(TOKEN_ID).assertEq(bytes32(0), "token hash");
}
}
contract MockVrfCoordinator {
uint256 public nextRequestId = 1;
function requestRandomWords(bytes32, uint64, uint16, uint32, uint32)
external
returns (uint256 requestId)
{
requestId = nextRequestId;
nextRequestId++;
}
function fulfill(NextGenRandomizerVRF randomizer, uint256 requestId, uint256[] memory words)
external
{
randomizer.rawFulfillRandomWords(requestId, words);
}
}
contract MockArrngLifecycleController {
uint256 public nextRequestId = 1;
function requestRandomWords(uint256, address) external returns (uint256 requestId) {
requestId = nextRequestId;
nextRequestId++;
}
function fulfill(NextGenRandomizerRNG randomizer, uint256 requestId, uint256[] memory words)
external
{