Repository navigation
Avoid refetching empty live RPC ranges #220
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
Merged
franceccon
merged 3 commits into
apibara:main
from
moodysalem:fix/live-empty-range-refetch
May 11, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@apibara-protocol-cefba257-a3ad-451e-b3d6-13613ecf7903.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "Avoid refetching empty live RPC ranges", | ||
| "packageName": "@apibara/protocol", | ||
| "email": "francesco@ceccon.me", | ||
| "dependentChangeType": "patch" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import type { Bytes, Cursor } from "../src/common"; | ||
| import { RpcClient } from "../src/rpc/client"; | ||
| import { | ||
| type BlockInfo, | ||
| type FetchBlockByHashArgs, | ||
| type FetchBlockByHashResult, | ||
| type FetchBlockRangeArgs, | ||
| type FetchBlockRangeResult, | ||
| type FetchCursorArgs, | ||
| type FetchCursorRangeArgs, | ||
| RpcStreamConfig, | ||
| } from "../src/rpc/config"; | ||
|
|
||
| type TestBlock = { | ||
| blockNumber: bigint; | ||
| }; | ||
|
|
||
| class EmptyLiveHeadConfig extends RpcStreamConfig<string, TestBlock> { | ||
| fetchBlockRangeCalls: FetchBlockRangeArgs<string>[] = []; | ||
| headBlock = 2n; | ||
|
|
||
| headRefreshIntervalMs(): number { | ||
| return 10; | ||
| } | ||
|
|
||
| finalizedRefreshIntervalMs(): number { | ||
| return 10_000; | ||
| } | ||
|
|
||
| validateFilter() { | ||
| return { valid: true as const }; | ||
| } | ||
|
|
||
| async fetchCursor(args: FetchCursorArgs): Promise<BlockInfo | null> { | ||
| if (args.blockTag === "latest") { | ||
| return blockInfo(this.headBlock); | ||
| } | ||
|
|
||
| if (args.blockTag === "finalized") { | ||
| return blockInfo(1n); | ||
| } | ||
|
|
||
| if (args.blockNumber !== undefined) { | ||
| return blockInfo(args.blockNumber); | ||
| } | ||
|
|
||
| return blockInfo(blockNumberFromHash(args.blockHash ?? "0x0")); | ||
| } | ||
|
|
||
| async fetchCursorRange({ | ||
| startBlockNumber, | ||
| endBlockNumber, | ||
| }: FetchCursorRangeArgs): Promise<BlockInfo[]> { | ||
| const blocks: BlockInfo[] = []; | ||
| for ( | ||
| let blockNumber = startBlockNumber; | ||
| blockNumber <= endBlockNumber; | ||
| blockNumber++ | ||
| ) { | ||
| blocks.push(blockInfo(blockNumber)); | ||
| } | ||
| return blocks; | ||
| } | ||
|
|
||
| async fetchBlockRange( | ||
| args: FetchBlockRangeArgs<string>, | ||
| ): Promise<FetchBlockRangeResult<TestBlock>> { | ||
| this.fetchBlockRangeCalls.push(args); | ||
| await sleep(1); | ||
|
|
||
| return { | ||
| startBlock: args.startBlock, | ||
| endBlock: args.maxBlock, | ||
| data: [], | ||
| }; | ||
| } | ||
|
|
||
| async fetchHeaderByHash({ | ||
| blockHash, | ||
| }: FetchBlockByHashArgs<string>): Promise<FetchBlockByHashResult<TestBlock>> { | ||
| const blockNumber = blockNumberFromHash(blockHash); | ||
| const info = blockInfo(blockNumber); | ||
|
|
||
| return { | ||
| blockInfo: info, | ||
| data: { | ||
| cursor: cursorForBlock(blockNumber - 1n), | ||
| endCursor: cursorForBlock(blockNumber)!, | ||
| block: { blockNumber }, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| describe("RpcDataStream", () => { | ||
| it("does not refetch empty accepted blocks as the live head advances", async () => { | ||
| const config = new EmptyLiveHeadConfig(); | ||
| const client = new RpcClient(config); | ||
| const stream = client.streamData({ | ||
| finality: "accepted", | ||
| filter: ["logs"], | ||
| startingCursor: { orderKey: 1n }, | ||
| }); | ||
| const iterator = stream[Symbol.asyncIterator](); | ||
|
|
||
| try { | ||
| const first = await iterator.next(); | ||
|
|
||
| expect(first.done).toBe(false); | ||
| expect(first.value).toMatchObject({ | ||
| _tag: "data", | ||
| data: { | ||
| endCursor: { orderKey: 2n }, | ||
| finality: "accepted", | ||
| production: "live", | ||
| }, | ||
| }); | ||
| expect(config.fetchBlockRangeCalls).toHaveLength(1); | ||
| expect(config.fetchBlockRangeCalls[0]).toMatchObject({ | ||
| startBlock: 2n, | ||
| maxBlock: 2n, | ||
| }); | ||
|
|
||
| config.headBlock = 3n; | ||
| const second = await withTimeout(iterator.next(), 1_000); | ||
|
|
||
| expect(second.done).toBe(false); | ||
| expect(second.value).toMatchObject({ | ||
| _tag: "data", | ||
| data: { | ||
| endCursor: { orderKey: 3n }, | ||
| finality: "accepted", | ||
| production: "live", | ||
| }, | ||
| }); | ||
| expect(config.fetchBlockRangeCalls).toHaveLength(2); | ||
| expect(config.fetchBlockRangeCalls[1]).toMatchObject({ | ||
| startBlock: 2n, | ||
| maxBlock: 3n, | ||
| }); | ||
| await iterator.return?.(); | ||
| } finally { | ||
| await iterator.return?.(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| function blockInfo(blockNumber: bigint): BlockInfo { | ||
| return { | ||
| blockNumber, | ||
| blockHash: blockHash(blockNumber), | ||
| parentBlockHash: blockHash(blockNumber - 1n), | ||
| }; | ||
| } | ||
|
|
||
| function cursorForBlock(blockNumber: bigint): Cursor | undefined { | ||
| if (blockNumber < 0n) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| orderKey: blockNumber, | ||
| uniqueKey: blockHash(blockNumber), | ||
| }; | ||
| } | ||
|
|
||
| function blockHash(blockNumber: bigint): Bytes { | ||
| const value = blockNumber < 0n ? 0n : blockNumber; | ||
| return `0x${value.toString(16).padStart(64, "0")}`; | ||
| } | ||
|
|
||
| function blockNumberFromHash(hash: Bytes): bigint { | ||
| return BigInt(hash); | ||
| } | ||
|
|
||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
|
|
||
| function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> { | ||
| return Promise.race([ | ||
| promise, | ||
| sleep(ms).then(() => { | ||
| throw new Error(`Timed out after ${ms}ms`); | ||
| }), | ||
| ]); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.