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

feat: add custom queryFilter #57

Merged
merged 7 commits into from
Apr 27, 2023
Merged
Changes from 5 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
90 changes: 86 additions & 4 deletions src/rln_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export class RLNContract {
rlnInstance: RLNInstance,
fromBlock?: number
): Promise<void> {
const registeredMemberEvents = await this.contract.queryFilter(
this.membersFilter,
fromBlock
);
const registeredMemberEvents = await queryFilter(this.contract, {
fromBlock,
membersFilter: this.membersFilter,
});

for (const event of registeredMemberEvents) {
this.addMemberFromEvent(rlnInstance, event);
Expand Down Expand Up @@ -109,3 +109,85 @@ export class RLNContract {
return txRegisterReceipt?.events?.[0];
}
}

type CustomQueryOptions = {
fromBlock?: number;
membersFilter: ethers.EventFilter;
};

// these value should be tested on other networks
weboko marked this conversation as resolved.
Show resolved Hide resolved
const FETCH_CHUNK = 5;
Copy link
Contributor

Choose a reason for hiding this comment

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

All the issue we are encountering are going to be dependent on the Wallet and RPC Provider.

MetaMask using infura is the one with an issue. But If a user used MetaMask with another provider, fetch chunk may need to be lower or higher.

Hence, a few things:

  1. It should probably be configurable so webapp developer can fine tune the experience as it mays also depends on other calls they do to the blockchain
  2. May be good to do further research and understand how other application do this (going through all logs of a contracts).
  3. I believe we already discussed that with previous researcher, but maybe using event log as the only way to get information of existing membership is not a good smart contract design? I am not familiar with smart contract design patterns but a function call on the smart contract to get a list of memberships may be a more efficient approach, only using events to listen to new members. WDYT @rymnc ?

Copy link

Choose a reason for hiding this comment

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

re 3, we made the gas efficient choice to store all idCommitments in a mapping. While this makes the contract more gas efficient, it definitely increases complexity client side. If clients would prefer the tree being stored onchain, we can create a variant of the RLN contracts that do that. Let me know 🫡

Copy link

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

re 3, we made the gas efficient choice to store all idCommitments in a mapping. While this makes the contract more gas efficient, it definitely increases complexity client side. If clients would prefer the tree being stored onchain, we can create a variant of the RLN contracts that do that. Let me know 🫡

since idCommitments are present on a contract that means there is no reason to query events, no?

Copy link
Contributor Author

@weboko weboko Apr 27, 2023

Choose a reason for hiding this comment

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

It should probably be configurable so webapp developer can fine tune the experience as it mays also depends on other calls they do to the blockchain

Exposed configuration to outside

May be good to do further research and understand how other application do this (going through all logs of a contracts).

I did some research and I see when someone needs to query many blocks then there are two solutions - query in chunks (or have a gateway that allows huge query to run) or store data somewhere on a contract

are there any other solutions, @rymnc , @richard-ramos ?

Copy link
Member

Choose a reason for hiding this comment

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

either that or use thegraph to query the events.

The problem with storing the data in the smart contract is that it will probably increase the cost of registering a new membership. Retrieving the query in chunks seem to be the usual workaround (here's an implementation i found in TS that we could use to retrieve the logs: https://github.com/MarkusSprunck/ethereum-event-scan/blob/master/src/app/services/reader.service.ts#L279-L358). I'm also thinking that to speedup loading the events next time the example is accessed, perhaps we should store them in localstorage or indexeddb, but that is something that could be implemented in future PRs

const BLOCK_RANGE = 3000;

async function queryFilter(
contract: ethers.Contract,
options: CustomQueryOptions
): Promise<ethers.Event[]> {
const { fromBlock, membersFilter } = options;

if (!fromBlock) {
return contract.queryFilter(membersFilter);
}

if (!contract.signer.provider) {
throw Error("No provider found on the contract's signer.");
}

const toBlock = await contract.signer.provider.getBlockNumber();

if (toBlock - fromBlock < BLOCK_RANGE) {
return contract.queryFilter(membersFilter);
}

const events: ethers.Event[][] = [];
const chunks = splitToChunks(fromBlock, toBlock, BLOCK_RANGE);

for (const portion of takeN<[number, number]>(chunks, FETCH_CHUNK)) {
const promises = portion.map(([left, right]) =>
ignoreErrors(contract.queryFilter(membersFilter, left, right), [])
);
const fetchedEvents = await Promise.all(promises);
events.push(fetchedEvents.flatMap((v) => v));
}

return events.flatMap((v) => v);
}

function splitToChunks(
from: number,
to: number,
step: number
): Array<[number, number]> {
const chunks = [];

let left = from;
while (left < to) {
const right = left + step < to ? left + step : to;

chunks.push([left, right] as [number, number]);

left = right;
}

return chunks;
}

function* takeN<T>(array: T[], size: number): Iterable<T[]> {
let start = 0;
let skip = size;

while (skip < array.length) {
const portion = array.slice(start, skip);

yield portion;

start = skip;
skip += size;
}
}

function ignoreErrors<T>(promise: Promise<T>, defaultValue: T): Promise<T> {
return promise.catch((_) => {
return defaultValue;
weboko marked this conversation as resolved.
Show resolved Hide resolved
});
}