-
Notifications
You must be signed in to change notification settings - Fork 194
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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([ | ||
publicClient.getBlockNumber({ cacheTime: 0 }), | ||
publicClient.getLogs({ ...getLogsOpts, fromBlock, toBlock, strict: true }), | ||
]); | ||
if (latestBlockNumber < toBlock) { | ||
const blockTimeInSeconds = 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could use the client's 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}); | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.