Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhoonbang committed Mar 13, 2024
1 parent 91717d9 commit 7f34448
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 33 deletions.
10 changes: 5 additions & 5 deletions contracts/src/v0.8/vrf/dev/SubscriptionAPI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,17 @@ abstract contract SubscriptionAPI is ConfirmedOwner, IERC677Receiver, IVRFSubscr
* @inheritdoc IVRFSubscriptionV2Plus
*/
function addConsumer(uint256 subId, address consumer) external override onlySubOwner(subId) nonReentrant {
// Already maxed, cannot add any more consumers.
address[] storage consumers = s_subscriptionConfigs[subId].consumers;
if (consumers.length == MAX_CONSUMERS) {
revert TooManyConsumers();
}
ConsumerConfig storage consumerConfig = s_consumers[consumer][subId];
if (consumerConfig.active) {
// Idempotence - do nothing if already added.
// Ensures uniqueness in s_subscriptions[subId].consumers.
return;
}
// Already maxed, cannot add any more consumers.
address[] storage consumers = s_subscriptionConfigs[subId].consumers;
if (consumers.length == MAX_CONSUMERS) {
revert TooManyConsumers();
}
// consumerConfig.nonce is 0 if the consumer had never sent a request to this subscription
// otherwise, consumerConfig.nonce is non-zero
// in both cases, use consumerConfig.nonce as is and set active status to true
Expand Down
79 changes: 72 additions & 7 deletions contracts/test/v0.8/foundry/vrf/VRFV2Plus.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ contract VRFV2Plus is BaseTest {
assertTrue(exists);
assertEq(GAS_LANE_MAX_GAS, maxGas);
assertEq(s_testCoordinator.s_provingKeyHashes(0), keyHash);
assertEq(keyHash, vrfKeyHash);
}

function testDeregisterProvingKey() public {
Expand Down Expand Up @@ -785,14 +786,21 @@ contract VRFV2Plus is BaseTest {
}

function testRequestRandomWords_ReAddConsumer_AssertRequestID() public {
// 1. setup consumer and subscription
setConfig();
registerProvingKey();
address subOwner = makeAddr("subOwner");
changePrank(subOwner);
uint256 subId = s_testCoordinator.createSubscription();
VRFV2PlusLoadTestWithMetrics consumer = new VRFV2PlusLoadTestWithMetrics(address(s_testCoordinator));
s_testCoordinator.addConsumer(subId, address(consumer));
uint32 requestBlock = 10;
vm.roll(requestBlock);
changePrank(LINK_WHALE);
s_testCoordinator.fundSubscriptionWithNative{value: 10 ether}(subId);

// Request random words.
// 2. Request random words.
changePrank(subOwner);
vm.expectEmit(true, true, false, true);
uint256 requestId;
uint256 preSeed;
Expand All @@ -805,20 +813,77 @@ contract VRFV2Plus is BaseTest {
MIN_CONFIRMATIONS,
CALLBACK_GAS_LIMIT,
NUM_WORDS,
VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: false})), // nativePayment, // nativePayment
address(s_testConsumer) // requester
VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: true})),
address(consumer) // requester
);
consumer.requestRandomWords(
subId,
MIN_CONFIRMATIONS,
vrfKeyHash,
CALLBACK_GAS_LIMIT,
false /* nativePayment */,
true /* nativePayment */,
NUM_WORDS,
1 /* requestCount */
);
assertTrue(s_testCoordinator.pendingRequestExists(subId));

// 3. Fulfill the request above
//console.log("requestId: ", requestId);
//console.log("preSeed: ", preSeed);
//console.log("sender: ", address(consumer));

// Move on to the next block.
// Store the previous block's blockhash, and assert that it is as expected.
vm.roll(requestBlock + 1);
s_bhs.store(requestBlock);
assertEq(hex"000000000000000000000000000000000000000000000000000000000000000a", s_bhs.getBlockhash(requestBlock));

// Fulfill the request.
// Proof generated via the generate-proof-v2-plus script command. Example usage:
/*
go run . generate-proof-v2-plus \
-key-hash 0x9f2353bde94264dbc3d554a94cceba2d7d2b4fdce4304d3e09a1fea9fbeb1528 \
-pre-seed 94043941380654896554739370173616551044559721638888689173752661912204412136884 \
-block-hash 0x000000000000000000000000000000000000000000000000000000000000000a \
-block-num 10 \
-sender 0x44CAfC03154A0708F9DCf988681821f648dA74aF \
-native-payment true
*/
VRF.Proof memory proof = VRF.Proof({
pk: [
72488970228380509287422715226575535698893157273063074627791787432852706183111,
62070622898698443831883535403436258712770888294397026493185421712108624767191
],
gamma: [
18593555375562408458806406536059989757338587469093035962641476877033456068708,
55675218112764789548330682504442195066741636758414578491295297591596761905475
],
c: 56595337384472359782910435918403237878894172750128610188222417200315739516270,
s: 60666722370046279064490737533582002977678558769715798604164042022636022215663,
seed: 94043941380654896554739370173616551044559721638888689173752661912204412136884,
uWitness: 0xEdbE15fd105cfEFb9CCcbBD84403d1F62719E50d,
cGammaWitness: [
11752391553651713021860307604522059957920042356542944931263270793211985356642,
14713353048309058367510422609936133400473710094544154206129568172815229277104
],
sHashWitness: [
109716108880570827107616596438987062129934448629902940427517663799192095060206,
79378277044196229730810703755304140279837983575681427317104232794580059801930
],
zInv: 18898957977631212231148068121702167284572066246731769473720131179584458697812
});
VRFCoordinatorV2_5.RequestCommitment memory rc = VRFCoordinatorV2_5.RequestCommitment({
blockNum: requestBlock,
subId: subId,
callbackGasLimit: CALLBACK_GAS_LIMIT,
numWords: NUM_WORDS,
sender: address(consumer),
extraArgs: VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: true}))
});
s_testCoordinator.fulfillRandomWords(proof, rc, true /* onlyPremium */);
assertFalse(s_testCoordinator.pendingRequestExists(subId));

// remove consumer and verify request random words doesn't work
// 4. remove consumer and verify request random words doesn't work
s_testCoordinator.removeConsumer(subId, address(consumer));
vm.expectRevert(abi.encodeWithSelector(SubscriptionAPI.InvalidConsumer.selector, subId, address(consumer)));
consumer.requestRandomWords(
Expand All @@ -831,7 +896,7 @@ contract VRFV2Plus is BaseTest {
1 /* requestCount */
);

// re-add consumer
// 5. re-add consumer and assert requestID nonce starts from 2 (nonce 1 was used before consumer removal)
s_testCoordinator.addConsumer(subId, address(consumer));
vm.expectEmit(true, true, false, true);
uint256 requestId2;
Expand All @@ -846,7 +911,7 @@ contract VRFV2Plus is BaseTest {
CALLBACK_GAS_LIMIT,
NUM_WORDS,
VRFV2PlusClient._argsToBytes(VRFV2PlusClient.ExtraArgsV1({nativePayment: false})), // nativePayment, // nativePayment
address(s_testConsumer) // requester
address(consumer) // requester
);
consumer.requestRandomWords(
subId,
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ vrf_consumer_v2_upgradeable_example: ../../contracts/solc/v0.8.6/VRFConsumerV2Up
vrf_coordinator_mock: ../../contracts/solc/v0.8.6/VRFCoordinatorMock/VRFCoordinatorMock.abi ../../contracts/solc/v0.8.6/VRFCoordinatorMock/VRFCoordinatorMock.bin 5c495cf8df1f46d8736b9150cdf174cce358cb8352f60f0d5bb9581e23920501
vrf_coordinator_test_v2: ../../contracts/solc/v0.8.6/VRFCoordinatorTestV2/VRFCoordinatorTestV2.abi ../../contracts/solc/v0.8.6/VRFCoordinatorTestV2/VRFCoordinatorTestV2.bin eaefd785c38bac67fb11a7fc2737ab2da68c988ca170e7db8ff235c80893e01c
vrf_coordinator_v2: ../../contracts/solc/v0.8.6/VRFCoordinatorV2/VRFCoordinatorV2.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2/VRFCoordinatorV2.bin 295f35ce282060317dfd01f45959f5a2b05ba26913e422fbd4fb6bf90b107006
<<<<<<< HEAD
vrf_coordinator_v2_5: ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.bin fc549a5c3a7ad2cc4a79342f3df500bdf797e9e75527a36ac790d39c705fd43d
=======
vrf_coordinator_v2_5: ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.bin 459b97ff4b2e1df90b2e6984afb7fbdf9a683904a0a09c16dddf0aa5d970ebfb
>>>>>>> develop
vrf_coordinator_v2_5: ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2_5/VRFCoordinatorV2_5.bin 6b4c50e8c8bbe877e5450d679e968dbde896f7c9043d29f3ecf79aefc28a0ef3
vrf_coordinator_v2_plus_v2_example: ../../contracts/solc/v0.8.6/VRFCoordinatorV2Plus_V2Example/VRFCoordinatorV2Plus_V2Example.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2Plus_V2Example/VRFCoordinatorV2Plus_V2Example.bin 4a5b86701983b1b65f0a8dfa116b3f6d75f8f706fa274004b57bdf5992e4cec3
vrf_coordinator_v2plus_interface: ../../contracts/solc/v0.8.6/IVRFCoordinatorV2PlusInternal/IVRFCoordinatorV2PlusInternal.abi ../../contracts/solc/v0.8.6/IVRFCoordinatorV2PlusInternal/IVRFCoordinatorV2PlusInternal.bin 86b8e23aab28c5b98e3d2384dc4f702b093e382dc985c88101278e6e4bf6f7b8
vrf_external_sub_owner_example: ../../contracts/solc/v0.8.6/VRFExternalSubOwnerExample/VRFExternalSubOwnerExample.abi ../../contracts/solc/v0.8.6/VRFExternalSubOwnerExample/VRFExternalSubOwnerExample.bin 14f888eb313930b50233a6f01ea31eba0206b7f41a41f6311670da8bb8a26963
Expand All @@ -113,17 +109,10 @@ vrf_owner_test_consumer: ../../contracts/solc/v0.8.6/VRFV2OwnerTestConsumer/VRFV
vrf_ownerless_consumer_example: ../../contracts/solc/v0.8.6/VRFOwnerlessConsumerExample/VRFOwnerlessConsumerExample.abi ../../contracts/solc/v0.8.6/VRFOwnerlessConsumerExample/VRFOwnerlessConsumerExample.bin 9893b3805863273917fb282eed32274e32aa3d5c2a67a911510133e1218132be
vrf_single_consumer_example: ../../contracts/solc/v0.8.6/VRFSingleConsumerExample/VRFSingleConsumerExample.abi ../../contracts/solc/v0.8.6/VRFSingleConsumerExample/VRFSingleConsumerExample.bin 892a5ed35da2e933f7fd7835cd6f7f70ef3aa63a9c03a22c5b1fd026711b0ece
vrf_v2_consumer_wrapper: ../../contracts/solc/v0.8.6/VRFv2Consumer/VRFv2Consumer.abi ../../contracts/solc/v0.8.6/VRFv2Consumer/VRFv2Consumer.bin 12368b3b5e06392440143a13b94c0ea2f79c4c897becc3b060982559e10ace40
<<<<<<< HEAD
vrf_v2plus_load_test_with_metrics: ../../contracts/solc/v0.8.6/VRFV2PlusLoadTestWithMetrics/VRFV2PlusLoadTestWithMetrics.abi ../../contracts/solc/v0.8.6/VRFV2PlusLoadTestWithMetrics/VRFV2PlusLoadTestWithMetrics.bin dfb5ca62b8017ae5e3f03221bc8acb567fcce426b31b40d57590a76a97d267a2
vrf_v2plus_single_consumer: ../../contracts/solc/v0.8.6/VRFV2PlusSingleConsumerExample/VRFV2PlusSingleConsumerExample.abi ../../contracts/solc/v0.8.6/VRFV2PlusSingleConsumerExample/VRFV2PlusSingleConsumerExample.bin 6226d05afa1664033b182bfbdde11d5dfb1d4c8e3eb0bd0448c8bfb76f5b96e4
vrf_v2plus_sub_owner: ../../contracts/solc/v0.8.6/VRFV2PlusExternalSubOwnerExample/VRFV2PlusExternalSubOwnerExample.abi ../../contracts/solc/v0.8.6/VRFV2PlusExternalSubOwnerExample/VRFV2PlusExternalSubOwnerExample.bin 7541f986571b8a5671a256edc27ae9b8df9bcdff45ac3b96e5609bbfcc320e4e
vrf_v2plus_upgraded_version: ../../contracts/solc/v0.8.6/VRFCoordinatorV2PlusUpgradedVersion/VRFCoordinatorV2PlusUpgradedVersion.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2PlusUpgradedVersion/VRFCoordinatorV2PlusUpgradedVersion.bin 3262273f6d78cea2240e8846734d5dba42488cdc2603b2aa92d6f8dc98284bc9
=======
vrf_v2plus_load_test_with_metrics: ../../contracts/solc/v0.8.6/VRFV2PlusLoadTestWithMetrics/VRFV2PlusLoadTestWithMetrics.abi ../../contracts/solc/v0.8.6/VRFV2PlusLoadTestWithMetrics/VRFV2PlusLoadTestWithMetrics.bin e8c6888df57e63e8b9a835b68e9e575631a2385feeb08c02c59732f699cc1f58
vrf_v2plus_single_consumer: ../../contracts/solc/v0.8.6/VRFV2PlusSingleConsumerExample/VRFV2PlusSingleConsumerExample.abi ../../contracts/solc/v0.8.6/VRFV2PlusSingleConsumerExample/VRFV2PlusSingleConsumerExample.bin 12b5d322db7dbf8af71955699e411109a4cc40811b606273ea0b5ecc8dbc639d
vrf_v2plus_sub_owner: ../../contracts/solc/v0.8.6/VRFV2PlusExternalSubOwnerExample/VRFV2PlusExternalSubOwnerExample.abi ../../contracts/solc/v0.8.6/VRFV2PlusExternalSubOwnerExample/VRFV2PlusExternalSubOwnerExample.bin 7b4f5ffe8fc293d2f4294d3d8348ed8dd480e909cef0743393095e5b20dc9c34
vrf_v2plus_upgraded_version: ../../contracts/solc/v0.8.6/VRFCoordinatorV2PlusUpgradedVersion/VRFCoordinatorV2PlusUpgradedVersion.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2PlusUpgradedVersion/VRFCoordinatorV2PlusUpgradedVersion.bin 09e4186c64cdaf1e5d36405467fb86996d7e4177cb08ecec425a4352d4246140
>>>>>>> develop
vrf_v2plus_upgraded_version: ../../contracts/solc/v0.8.6/VRFCoordinatorV2PlusUpgradedVersion/VRFCoordinatorV2PlusUpgradedVersion.abi ../../contracts/solc/v0.8.6/VRFCoordinatorV2PlusUpgradedVersion/VRFCoordinatorV2PlusUpgradedVersion.bin 1695b5f9990dfe1c7d71c6f47f4be3488be15d09def9d984ffbc1db0f207a08a
vrfv2_proxy_admin: ../../contracts/solc/v0.8.6/VRFV2ProxyAdmin/VRFV2ProxyAdmin.abi ../../contracts/solc/v0.8.6/VRFV2ProxyAdmin/VRFV2ProxyAdmin.bin 402b1103087ffe1aa598854a8f8b38f8cd3de2e3aaa86369e28017a9157f4980
vrfv2_reverting_example: ../../contracts/solc/v0.8.6/VRFV2RevertingExample/VRFV2RevertingExample.abi ../../contracts/solc/v0.8.6/VRFV2RevertingExample/VRFV2RevertingExample.bin 1ae46f80351d428bd85ba58b9041b2a608a1845300d79a8fed83edf96606de87
vrfv2_transparent_upgradeable_proxy: ../../contracts/solc/v0.8.6/VRFV2TransparentUpgradeableProxy/VRFV2TransparentUpgradeableProxy.abi ../../contracts/solc/v0.8.6/VRFV2TransparentUpgradeableProxy/VRFV2TransparentUpgradeableProxy.bin fe1a8e6852fbd06d91f64315c5cede86d340891f5b5cc981fb5b86563f7eac3f
Expand Down
2 changes: 1 addition & 1 deletion core/scripts/vrfv2plus/testnet/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var rcTemplate = `{
callbackGasLimit: %d,
numWords: %d,
sender: %s,
nativePayment: %s
extraArgs: %s
}
`

Expand Down

0 comments on commit 7f34448

Please sign in to comment.