Skip to content

Commit

Permalink
Limit batch size to 10 (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebrianchen authored Apr 26, 2022
1 parent f0df9fb commit 7711415
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/subscriptions/subscriptionBackfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export type Backfiller = ReturnType<typeof makeBackfiller>;
*/
const MAX_BACKFILL_BLOCKS = 120;

/**
* The maximum number of requests that can be included in a single batch request.
* This value is enforced by the backend.
*/
const MAX_BATCH_SIZE = 10;

export function makeBackfiller(jsonRpcSenders: JsonRpcSenders) {
return { getNewHeadsBackfill, getLogsBackfill };

Expand Down Expand Up @@ -129,15 +135,29 @@ export function makeBackfiller(jsonRpcSenders: JsonRpcSenders) {
if (fromBlockInclusive >= toBlockExclusive) {
return [];
}
const batchParts: BatchPart[] = [];
let batchParts: BatchPart[] = [];
const headEventBatches: Array<Promise<BlockHead[]>> = [];
for (let i = fromBlockInclusive; i < toBlockExclusive; i++) {
batchParts.push({
method: "eth_getBlockByNumber",
params: [toHex(i), false],
});
if (batchParts.length % MAX_BATCH_SIZE === 0) {
headEventBatches.push(jsonRpcSenders.sendBatch(batchParts));
batchParts = [];
}
}
const heads = await jsonRpcSenders.sendBatch(batchParts);
return heads.map(toNewHeadsEvent);

if (batchParts.length > 0) {
headEventBatches.push(jsonRpcSenders.sendBatch(batchParts));
}

const batchedBlockHeads = await Promise.all(headEventBatches);
const blockHeads = batchedBlockHeads.reduce(
(acc, batch) => acc.concat(batch),
[],
);
return blockHeads.map(toNewHeadsEvent);
}

async function getBlockByNumber(blockNumber: number): Promise<BlockHead> {
Expand Down

0 comments on commit 7711415

Please sign in to comment.