Skip to content

Fix firstEventBlockNumber bug in metadata - #124

Merged
JonoPrest merged 3 commits into
mainfrom
jp/fix-first-event-blocknumber
Aug 19, 2024
Merged

Fix firstEventBlockNumber bug in metadata#124
JonoPrest merged 3 commits into
mainfrom
jp/fix-first-event-blocknumber

Conversation

@JonoPrest

Copy link
Copy Markdown
Collaborator

There was a bug introduced with partitioned fetchState where the firstEventBlockNumber can be set to any partitions first block.

@JonoPrest
JonoPrest requested a review from DZakh August 19, 2024 08:59
Comment on lines -310 to -314
let firstEventBlockNumber = switch parsedQueueItems[0] {
| Some(item) if chainFetcher.firstEventBlockNumber->Option.isNone => item.blockNumber->Some
| _ => chainFetcher.firstEventBlockNumber
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Previously this number was just being set on chain fetcher here. Either it existed or it didn't. Now that there can be multiple partitions even when the indexer starts up, the first batch to come back would not necessarily contain the first event.

//of a rollback.
dynamicContracts: DynamicContractsMap.t,
isFetchingAtHead: bool,
firstEventBlockNumber: option<int>,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Solution is to add firstEventBlockNumber to each partition and evaluate the earliest across partitions.

Comment on lines +277 to +281
let getFirstEventBlockNumber = ({partitions}: t) => {
partitions->List.reduce(None, (accum, partition) => {
Utils.Math.minOptInt(accum, partition.firstEventBlockNumber)
})
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Evaluate each partition's firstEventBlockNumber

Comment on lines +378 to +382
let getFirstEventBlockNumber = (chainFetcher: t) =>
Utils.Math.minOptInt(
chainFetcher.dbFirstEventBlockNumber,
chainFetcher.fetchState->PartitionedFetchState.getFirstEventBlockNumber,
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Evaluate the DBs first event block number against the partions first event block number in the case of an indexer restart.

@DZakh DZakh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Besides the newFetchedEvents->Utils.List.getLast line everything looks good 👍

And I don't like the list usage, but it's not related to the PR

Comment on lines +92 to +96
let rec getLast = (list, ~head=?) =>
switch list {
| list{} => head
| list{head, ...tail} => tail->getLast(~head)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not for the PR, but I'd like to avoid the usage of list for batch events.

  1. Array is a flexible enough data structure
  2. List has an overhead in the runtime representation https://rescript-lang.org/try?version=v11.1.3&code=DYUwLgBAhhC8HAJYGcwG8CMAaCAmHAzDgCw4CsAvgFBA
  3. And it's more limiting in terms of some operations like this one

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Hey @DZakh, I agree array is generally better. Lets have a chat, some time there's trade off in both. Array concats are quite heavy for immutable operation and other types of traversal that currently happen.

Some places can probably moved back to array.

) => {
let firstEventBlockNumber = switch self.firstEventBlockNumber {
| Some(n) => Some(n)
| None => newFetchedEvents->Utils.List.getLast->Option.map(v => v.blockNumber)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do you get last here instead of the first?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah good catch sorry I had it backwards in my head!

@JonoPrest

Copy link
Copy Markdown
Collaborator Author

@DZakh I've updated the code for getting the list item. I had it backwards in my head (was writing for an ordering of latest to earliest event not earliest first which is the correct)

@DZakh

DZakh commented Aug 19, 2024

Copy link
Copy Markdown
Member

Looks good 👌

@JonoPrest
JonoPrest merged commit 73e16dc into main Aug 19, 2024
@JonoPrest
JonoPrest deleted the jp/fix-first-event-blocknumber branch August 19, 2024 13:02
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.

2 participants