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

Contribution Count Shows Different Numbers - fixed #443 #460

Closed
wants to merge 4 commits into from

Conversation

aj-maz
Copy link
Member

@aj-maz aj-maz commented Oct 29, 2021

The root of the bug was the "first" parameter in the GraphQL query which has a default number of 100 and was not handled:
{ grantDonations(where: {lastUpdatedBlockNumber_gte: ${fromBlock}, lastUpdatedBlockNumber_lte: ${blockNumber}}) { grantId tokenIn donationAmount from hash rounds lastUpdatedBlockNumber } }

so whenever the length of the contributions would go more than 100, the system would act inconsistently. I created a recursive function that will continue to fetch contributions until there is no more left.

It only works if the cached data is properly set before "fromBlock" which is not unless there are less than 100 contributions So do the hard refresh and remove the cached data

@aj-maz aj-maz changed the title fixed #443 Contribution Count Shows Different Numbers - fixed #443 Oct 29, 2021
const json = await res.json();

if (json.data.grantDonations.length) {
return await fetchUntilAll(SUBGRAPH_URL, before, skip + 1);
Copy link
Collaborator

@apbendi apbendi Oct 29, 2021

Choose a reason for hiding this comment

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

Fixes needed:

  • merge json response with before before recursive call
  • increment indexdb number to bust cache

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's where to increment the version

// we can bump this version number to bust the users cache
version: 1,

@aj-maz
Copy link
Member Author

aj-maz commented Oct 29, 2021

I contribute to 6 grants on my own wallet and for other users everything was fine, but the contributor user only fetched 2 contributions. This PR is still an improvement and will fix the problem for most users, but for fixing #443 completely I need to find the root of this problem as well. Maybe we should create a new issue for it.

@@ -41,7 +41,7 @@ export const DefaultForageConfig: LocalForageConfig = {
// this store should be used for any on-chain/off-chain data but never user data (as we might clear it without notice)
name: 'dGrants',
// we can bump this version number to bust the users cache
version: 1,
version: 3,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why 3 and not 2?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wasn't sure what should I put, thought maybe some other PR is going to use 2.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are no other open PRs changing this, so we should use 2 🙂

Suggested change
version: 3,
version: 2,

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@@ -56,7 +56,7 @@ export async function getContributions(

const limit = 100;

const fetchUntilAll = async (SUBGRAPH_URL: string, before = [], skip = 0): Promise<any[]> => {
const fetchUntilAll = async (SUBGRAPH_URL: string, before: any[] = [], skip = 0): Promise<any[]> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we should have stronger typing than any, but we can save that for the PR that handles #463

Copy link
Member Author

Choose a reason for hiding this comment

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

Now we use type ContributionSubgraph instead of any.

@mds1
Copy link
Contributor

mds1 commented Oct 29, 2021

I contribute to 6 grants on my own wallet and for other users everything was fine, but the contributor user only fetched 2 contributions. This PR is still an improvement and will fix the problem for most users, but for fixing #443 completely I need to find the root of this problem as well. Maybe we should create a new issue for it.

@Ajand code-wise this looks good, but I'm not sure what exactly you mean here? Which users is this working for and which is it only fetching 2 contributions for?

@aj-maz aj-maz closed this Oct 29, 2021
@aj-maz aj-maz reopened this Oct 29, 2021
@aj-maz
Copy link
Member Author

aj-maz commented Oct 29, 2021

I contribute to 6 grants on my own wallet and for other users everything was fine, but the contributor user only fetched 2 contributions. This PR is still an improvement and will fix the problem for most users, but for fixing #443 completely I need to find the root of this problem as well. Maybe we should create a new issue for it.

@Ajand code-wise this looks good, but I'm not sure what exactly you mean here? Which users is this working for and which is it only fetching 2 contributions for?

Let's assume there are 3 users. user 1 will contribute to 10 grants. for user2 and user3, everything is fine, but for user1 we gonna get inconsistent results.

@metafraction
Copy link
Member

metafraction commented Oct 29, 2021

agree with @mds1 comments, so once those are resolved (and approved), I'll approve
good work!

@aj-maz
Copy link
Member Author

aj-maz commented Oct 31, 2021

Wrong commit message. There is nothing here for #469. I meant #460.

@aj-maz
Copy link
Member Author

aj-maz commented Nov 1, 2021

There was two problems with #470:

  1. The grant donation function would start a race with itself and would override the data it wanted to save so if in a block there would be more than one contribution some of them would not be saved even though they were fetched. I solved that issue by creating an async function call wait(timeout) and using a closure called timoutManager to put a timeout so the next call would wait so the last call could save its data.

  2. For contributor users for some reason the listener processor would be called 2 times simultaneously so even the in-memory closure wouldn't be able to stop the race. So I added a random timeout in addition to a normal timeout and used a set to prevent processing the same event multiple times.

This approach works fine for now but if we're going to scale ( for example if we have 50 contributions in each block ) it doesn't work. We need to create a queue while processing the events and saving all contributions at once after processing.

Also, it would be nice to find the root of double processing for contributor users so could stop it. Since I'm using a random number there is still a slim chance for some contributors to have some issues.

@mds1 mds1 requested a review from gdixon November 1, 2021 13:20
@phutchins
Copy link
Contributor

Whats left here @Ajand ? Is this read for final review? @mk1r thoughts?

@aj-maz
Copy link
Member Author

aj-maz commented Nov 5, 2021

Hi @phutchins .
I think it's finished. It's not the ultimate best solution but it's a pretty good solution for now and I believe it works unless we get to Facebook level scale. I guess we just need @gdixon approval as @mds1 requested.

@metafraction
Copy link
Member

@phutchins yes looks good to me, are all of @mds1 concerns resolved? @Ajand

Copy link
Member

@metafraction metafraction left a comment

Choose a reason for hiding this comment

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

good job!

@phutchins phutchins closed this Nov 8, 2021
@gdixon gdixon mentioned this pull request Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistency when user contribute to more than one grant Contribution Count Shows Different Numbers
5 participants