Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore requests that will potential fall #104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions web3-functions/vrf-fallback/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
} catch (error) {
return {
canExec: false,
message: `Fail to getLogs ${fromBlock}-${toBlock}: ${
(error as Error).message
}.`,
message: `Fail to getLogs ${fromBlock}-${toBlock}: ${(error as Error).message

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format

}.`,
};
}
}

// h: blockHash, t: timestamp, i: index, r: requestId
let requests: { h: string; t: number; i: number; r: string }[] = JSON.parse(
// h: blockHash, t: timestamp, i: index, r: requestId, o: original keccak256 hash
let requests: { h: string; t: number; i: number; r: string; o: string }[] = JSON.parse(
(await storage.get("requests")) ?? "[]"
);

Expand All @@ -96,12 +95,14 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
const requestId: BigNumber = decoded[0];

const timestamp = Math.floor(getRoundTime(round.toNumber()) / 1000);
const originalHash = utils.keccak256(utils.defaultAbiCoder.encode(["uint256", "bytes"], [round, consumerData]));

requests.push({
h: log.blockHash,
t: timestamp,
i: log.logIndex,
r: requestId.toString(),
o: originalHash,
});
}

Expand All @@ -121,10 +122,29 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
requests = requests.filter((_, index) => {
if (index >= MAX_MULTICALL_REQUESTS) return true; // Keep requests that were not included in multicall.
// Converting returnData to boolean. returnData is in bytes32 hexadecimal form (0x..00 or 0x..01).
const isRequestPending = !!parseInt(returnData[index]);
const isRequestPending = !!Number.parseInt(returnData[index]);
return isRequestPending;
});

// filter out invalid requests
const zeroHashRequests = requests.slice(0, MAX_MULTICALL_REQUESTS)
const zeroHashRequestsData = zeroHashRequests.map(({ r }) => {
return {
target: consumer.address,
callData: consumer.interface.encodeFunctionData("requestedHash", [r]),
};
});

const { returnData: zeroHashReturnData } = (await multicall.callStatic.aggregate(
zeroHashRequestsData
)) as { blockNumber: BigNumber; returnData: string[] };
requests = requests.filter((_, index) => {
if (index >= MAX_MULTICALL_REQUESTS) return true;
const isValidHash = zeroHashReturnData[index] === requests[index].o;
return isValidHash;
});


await storage.set("requests", JSON.stringify(requests));
await storage.set("lastBlock", lastBlock.toString());

Expand All @@ -150,14 +170,13 @@ Web3Function.onRun(async (context: Web3FunctionContext) => {
Math.random() * Math.min(MAX_MULTICALL_REQUESTS, requests.length)
);
const requestToFulfill = requests[randomRequestIndex];

const logsToProcess = await provider.getLogs({
address: consumerAddress,
blockHash: requestToFulfill.h,
});

const logToProcess = logsToProcess.find(
(l) => l.logIndex == requestToFulfill.i
(l) => l.logIndex === requestToFulfill.i
);

if (logsToProcess.length === 0 || !logToProcess) {
Expand Down