From 68148ddfc03fcce08128976c49ebf7dfc49d4b06 Mon Sep 17 00:00:00 2001 From: Alberto Date: Mon, 18 Sep 2023 22:36:53 +0200 Subject: [PATCH 1/6] fix: Now respecting free plan rate limit --- web3-functions/vrf/index.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/web3-functions/vrf/index.ts b/web3-functions/vrf/index.ts index 93afed5..6b46041 100644 --- a/web3-functions/vrf/index.ts +++ b/web3-functions/vrf/index.ts @@ -20,9 +20,8 @@ const CALLBACK_ABI = [ ]; // w3f constants -const MAX_DEPTH = 700; const MAX_RANGE = 100; // limit range of events to comply with rpc providers -const MAX_REQUESTS = 100; // limit number of requests on every execution to avoid hitting timeout +const MAX_REQUESTS = 5; // limit number of requests on every execution to avoid hitting timeout Web3Function.onRun(async (context: Web3FunctionContext) => { const { userArgs, storage, multiChainProvider } = context; @@ -34,10 +33,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => { const inbox = new Contract(inboxAddress, INBOX_ABI, provider); const currentBlock = await provider.getBlockNumber(); - const lastBlockStr = await storage.get("lastBlockNumber"); - let lastBlock = lastBlockStr - ? parseInt(lastBlockStr) - : currentBlock - MAX_DEPTH; + const lastBlock = parseInt(await storage.get("lastBlockNumber") as string); const topics = [ inbox.interface.getEventTopic("RequestedRandomness"), From 2775ba504d2f02a870473253da28bc7a7c425fa4 Mon Sep 17 00:00:00 2001 From: Louis Bettens Date: Wed, 20 Sep 2023 23:08:15 +0200 Subject: [PATCH 2/6] fixup! fix: Now respecting free plan rate limit --- test/cl.test.ts | 6 +++--- web3-functions/vrf/index.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/cl.test.ts b/test/cl.test.ts index 8d7191d..6e92654 100644 --- a/test/cl.test.ts +++ b/test/cl.test.ts @@ -101,11 +101,11 @@ describe("Chainlink Adapter Test Suite", function () { const abi = ethers.utils.defaultAbiCoder; const seed = ethers.utils.keccak256( abi.encode( - ["uint256", "address", "uint256", "uint32"], + ["uint256", "address", "uint256", "uint256"], [ ethers.BigNumber.from(`0x${randomness}`), - mockConsumer.address, - ethers.provider.network.chainId, + adapter.address, + (await ethers.provider.getNetwork()).chainId, requestId, ] ) diff --git a/web3-functions/vrf/index.ts b/web3-functions/vrf/index.ts index 6b46041..6f181a1 100644 --- a/web3-functions/vrf/index.ts +++ b/web3-functions/vrf/index.ts @@ -33,7 +33,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => { const inbox = new Contract(inboxAddress, INBOX_ABI, provider); const currentBlock = await provider.getBlockNumber(); - const lastBlock = parseInt(await storage.get("lastBlockNumber") as string); + let lastBlock = parseInt(await storage.get("lastBlockNumber") as string); const topics = [ inbox.interface.getEventTopic("RequestedRandomness"), From 8b43a1dd776ef13e46a2a3ccbb122abdbfd602de Mon Sep 17 00:00:00 2001 From: Louis Bettens Date: Wed, 20 Sep 2023 23:11:12 +0200 Subject: [PATCH 3/6] fixup! fix: Now respecting free plan rate limit --- web3-functions/vrf/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web3-functions/vrf/index.ts b/web3-functions/vrf/index.ts index 6f181a1..d5e819a 100644 --- a/web3-functions/vrf/index.ts +++ b/web3-functions/vrf/index.ts @@ -33,7 +33,7 @@ Web3Function.onRun(async (context: Web3FunctionContext) => { const inbox = new Contract(inboxAddress, INBOX_ABI, provider); const currentBlock = await provider.getBlockNumber(); - let lastBlock = parseInt(await storage.get("lastBlockNumber") as string); + let lastBlock = parseInt((await storage.get("lastBlockNumber")) as string); const topics = [ inbox.interface.getEventTopic("RequestedRandomness"), From bc6b10981be2b84d66ea499b7e27d523d6df6572 Mon Sep 17 00:00:00 2001 From: Alberto Date: Thu, 21 Sep 2023 11:04:55 +0200 Subject: [PATCH 4/6] feat: Handling VRF pause + restored depth limits --- web3-functions/vrf/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/web3-functions/vrf/index.ts b/web3-functions/vrf/index.ts index d5e819a..a051cd4 100644 --- a/web3-functions/vrf/index.ts +++ b/web3-functions/vrf/index.ts @@ -22,6 +22,8 @@ const CALLBACK_ABI = [ // w3f constants const MAX_RANGE = 100; // limit range of events to comply with rpc providers const MAX_REQUESTS = 5; // limit number of requests on every execution to avoid hitting timeout +const MAX_DEPTH = MAX_RANGE * MAX_REQUESTS; // How far the VRF should catch up with blocks +const MAX_DISTANCE = 1000; // Helpful to detect if the VRF has been paused not to recover too many blocks Web3Function.onRun(async (context: Web3FunctionContext) => { const { userArgs, storage, multiChainProvider } = context; @@ -33,7 +35,12 @@ Web3Function.onRun(async (context: Web3FunctionContext) => { const inbox = new Contract(inboxAddress, INBOX_ABI, provider); const currentBlock = await provider.getBlockNumber(); - let lastBlock = parseInt((await storage.get("lastBlockNumber")) as string); + const lastBlockStr = await storage.get("lastBlockNumber"); + let lastBlock = lastBlockStr ? parseInt(lastBlockStr) : 0; + + if (!lastBlockStr || currentBlock - lastBlock > MAX_DISTANCE) { + lastBlock = currentBlock - MAX_DEPTH; + } const topics = [ inbox.interface.getEventTopic("RequestedRandomness"), From a8ba6087b9fb6b0b38796392c8f2df4f41087db0 Mon Sep 17 00:00:00 2001 From: Alberto Date: Wed, 27 Sep 2023 11:49:41 +0200 Subject: [PATCH 5/6] test: Pause works correctly --- test/index.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/index.test.ts b/test/index.test.ts index 864c1b3..29f4139 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -100,6 +100,29 @@ describe("VRF Test Suite", function () { ); }); + it("When paused it catches up the specified amount of blocks", async () => { + (userArgs.allowedSenders as string[]).push(user.address); + + // Simulates request to ignore + await inbox.connect(user).requestRandomness(mockConsumer.address, data); + + // Triggers pause condition + for (let i = 0; i < 1001; ++i) ethers.provider.send("evm_mine", []); + + + // Spams requests, only the last MAX_RANGE * MAX_REQUESTS should be picked up + for (let i = 0; i < 1000; ++i) { + await inbox.connect(user).requestRandomness(mockConsumer.address, data); + } + + const exec = await vrf.run({ userArgs }); + const res = exec.result as Web3FunctionResultV2; + + if (!res.canExec) assert.fail(res.message); + + expect(res.callData).to.have.lengthOf(500); + }); + it("Doesn't execute if no event was emitted", async () => { const exec = await vrf.run({ userArgs }); const res = exec.result as Web3FunctionResultV2; From 7e0441fc9c6f3f7b9171dd0358d495ecbcf167c1 Mon Sep 17 00:00:00 2001 From: Louis Bettens Date: Wed, 27 Sep 2023 18:53:59 +0200 Subject: [PATCH 6/6] format --- test/index.test.ts | 3 +-- web3-functions/vrf/index.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index 29f4139..e57bfd5 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -109,9 +109,8 @@ describe("VRF Test Suite", function () { // Triggers pause condition for (let i = 0; i < 1001; ++i) ethers.provider.send("evm_mine", []); - // Spams requests, only the last MAX_RANGE * MAX_REQUESTS should be picked up - for (let i = 0; i < 1000; ++i) { + for (let i = 0; i < 1000; ++i) { await inbox.connect(user).requestRandomness(mockConsumer.address, data); } diff --git a/web3-functions/vrf/index.ts b/web3-functions/vrf/index.ts index a051cd4..3799646 100644 --- a/web3-functions/vrf/index.ts +++ b/web3-functions/vrf/index.ts @@ -23,7 +23,7 @@ const CALLBACK_ABI = [ const MAX_RANGE = 100; // limit range of events to comply with rpc providers const MAX_REQUESTS = 5; // limit number of requests on every execution to avoid hitting timeout const MAX_DEPTH = MAX_RANGE * MAX_REQUESTS; // How far the VRF should catch up with blocks -const MAX_DISTANCE = 1000; // Helpful to detect if the VRF has been paused not to recover too many blocks +const MAX_DISTANCE = 1000; // Helpful to detect if the VRF has been paused not to recover too many blocks Web3Function.onRun(async (context: Web3FunctionContext) => { const { userArgs, storage, multiChainProvider } = context;