Skip to content

Commit

Permalink
Merge pull request #75 from gelatodigital/rpc-rate-limit2
Browse files Browse the repository at this point in the history
fix: Now respecting free plan rate limit
  • Loading branch information
Louis Bettens authored Sep 27, 2023
2 parents 09be070 + 7e0441f commit 26cfc80
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion test/cl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ 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}`),
adapter.address,
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ 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;
Expand Down
13 changes: 8 additions & 5 deletions web3-functions/vrf/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const CONSUMER_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
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;
Expand All @@ -30,9 +31,11 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {

const currentBlock = await provider.getBlockNumber();
const lastBlockStr = await storage.get("lastBlockNumber");
let lastBlock = lastBlockStr
? parseInt(lastBlockStr)
: currentBlock - MAX_DEPTH;
let lastBlock = lastBlockStr ? parseInt(lastBlockStr) : 0;

if (!lastBlockStr || currentBlock - lastBlock > MAX_DISTANCE) {
lastBlock = currentBlock - MAX_DEPTH;
}

const topics = [consumer.interface.getEventTopic("RequestedRandomness")];

Expand Down

0 comments on commit 26cfc80

Please sign in to comment.