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(store-sync): Handle unsynced RPC nodes #2901

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion packages/block-logs-stream/src/fetchLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ export async function* fetchLogs<TAbiEvents extends readonly AbiEvent[]>({
try {
const toBlock = fromBlock + blockRange;
debug("getting logs", { fromBlock, toBlock });
const logs = await publicClient.getLogs({ ...getLogsOpts, fromBlock, toBlock, strict: true });

const [latestBlockNumber, logs] = await Promise.all([
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This only works if batch is enabled in the HTTP transport. This is decided by clients though, so maybe this is put into docs?

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should have an option to enable the "validate unsynchronized nodes" behavior, where we require batch to be enabled or, ideally, do the batch call here directly rather than relying on some client configuration.

Since this approach isn't useful unless the call is atomic (going to the same RPC) and I suspect not all RPC providers support batching, seems like we should put this behind a flag to avoid doubling RPC calls for syncing data from RPC. Could make this the default approach with a way to opt out of this new behavior.

publicClient.getBlockNumber({ cacheTime: 0 }),
publicClient.getLogs({ ...getLogsOpts, fromBlock, toBlock, strict: true }),
]);
if (latestBlockNumber < toBlock) {
const blockTimeInSeconds = 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what the best way to handle this is, ie should it be a deploy config? I dont think there's a way to detect from the chosen chain what the block time is?

Copy link
Member

@holic holic Jun 20, 2024

Choose a reason for hiding this comment

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

we could use the client's pollingInterval here?

either way, we should probably align this with the other retry logic below

const seconds = Number(toBlock - latestBlockNumber) * blockTimeInSeconds;
debug(`latest block number ${latestBlockNumber} is less than toBlock ${toBlock}, retrying in ${seconds}s`);
await wait(1000 * seconds);
continue;
}
yield { fromBlock, toBlock, logs };
fromBlock = toBlock + 1n;
blockRange = bigIntMin(maxBlockRange, getLogsOpts.toBlock - fromBlock);
Expand Down
24 changes: 16 additions & 8 deletions packages/store-sync/src/createStoreSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,35 +187,43 @@ export async function createStoreSync<config extends StoreConfig = StoreConfig>(
tap((startBlock) => debug("starting sync from block", startBlock)),
);

let startBlock: bigint | null = null;
let endBlock: bigint | null = null;
let lastBlockNumberProcessed: bigint | null = null;

const latestBlock$ = createBlockStream({ publicClient, blockTag: followBlockTag }).pipe(shareReplay(1));
const latestBlockNumber$ = latestBlock$.pipe(
map((block) => block.number),
tap((blockNumber) => {
debug("on block number", blockNumber, "for", followBlockTag, "block tag");
}),
filter((blockNumber) => {
return lastBlockNumberProcessed == null || blockNumber > lastBlockNumberProcessed;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

unsure if we want to add a debug log here

}),
shareReplay(1),
);

let startBlock: bigint | null = null;
let endBlock: bigint | null = null;
let lastBlockNumberProcessed: bigint | null = null;

const storedBlock$ = combineLatest([startBlock$, latestBlockNumber$]).pipe(
map(([startBlock, endBlock]) => ({ startBlock, endBlock })),
tap((range) => {
startBlock = range.startBlock;
endBlock = range.endBlock;
}),
concatMap((range) => {
const fromBlock = lastBlockNumberProcessed
? bigIntMax(range.startBlock, lastBlockNumberProcessed + 1n)
: range.startBlock;
const toBlock = range.endBlock;
if (toBlock < fromBlock) {
throw new Error(`toBlock ${toBlock} is less than fromBlock ${fromBlock}`);
}
const storedBlocks = fetchAndStoreLogs({
publicClient,
address,
events: storeEventsAbi,
maxBlockRange,
fromBlock: lastBlockNumberProcessed
? bigIntMax(range.startBlock, lastBlockNumberProcessed + 1n)
: range.startBlock,
toBlock: range.endBlock,
fromBlock: fromBlock,
toBlock: toBlock,
storageAdapter,
logFilter,
});
Expand Down
Loading