Skip to content

Commit

Permalink
Merge branch 'accounts' of https://github.com/0xOlias/ponder into acc…
Browse files Browse the repository at this point in the history
…ounts
  • Loading branch information
kyscott18 committed Nov 10, 2024
2 parents 438c9f6 + 34172a6 commit 8237db7
Show file tree
Hide file tree
Showing 6 changed files with 557 additions and 210 deletions.
32 changes: 24 additions & 8 deletions packages/core/src/sync-historical/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export const createHistoricalSync = async (
* Note: All entries are deleted at the end of each call to `sync()`.
*/
const transactionsCache = new Set<Hash>();
/**
* Traces that have already been fetched.
* Note: All entries are deleted at the end of each call to `sync()`.
*/
const traceCache = new Map<bigint, Promise<SyncTrace[]>>();
/**
* Data about the range passed to "eth_getLogs" for all log
* filters and log factories.
Expand Down Expand Up @@ -304,11 +309,21 @@ export const createHistoricalSync = async (

const syncTrace = async (
filter: TransactionFilter | TransferFilter,
blockNumber: number,
blockNumber: bigint,
) => {
const syncTraces = await _debug_traceBlockByNumber(args.requestQueue, {
blockNumber: toHex(blockNumber),
});
let syncTraces: SyncTrace[] = [];

if (traceCache.has(blockNumber)) {
syncTraces = await traceCache.get(blockNumber)!;
} else {
const _syncTraces = _debug_traceBlockByNumber(args.requestQueue, {
blockNumber: toHex(blockNumber),
});

traceCache.set(blockNumber, _syncTraces);

syncTraces = await _syncTraces;
}

const traces: {
trace: SyncTrace["result"];
Expand Down Expand Up @@ -537,10 +552,10 @@ export const createHistoricalSync = async (

case "transaction":
case "transfer": {
const blocks = Array.from(
{ length: interval[1] - interval[0] + 1 },
(_, i) => interval[0] + i,
);
const blocks = [];
for (let i = interval[0]; i <= interval[1]; i++) {
blocks.push(BigInt(i));
}
await Promise.all(
blocks.map((blockNumber) =>
syncTrace(filter, blockNumber),
Expand Down Expand Up @@ -604,6 +619,7 @@ export const createHistoricalSync = async (

blockCache.clear();
transactionsCache.clear();
traceCache.clear();

return latestBlock;
},
Expand Down
Loading

0 comments on commit 8237db7

Please sign in to comment.