Skip to content

Commit

Permalink
fetch before cached events in case that we only cached a later range
Browse files Browse the repository at this point in the history
  • Loading branch information
boudra committed Feb 6, 2024
1 parent 8bd5116 commit f7d24fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/cache/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ export function createSqliteCache(dbPath: string): Cache {

// Remove old overlapping ranges
db.prepare(
`DELETE FROM logRanges WHERE chainId = ? AND address = ?`
).run(chainId, address);
`DELETE FROM logRanges WHERE chainId = ? AND address = ? AND fromBlock >= ? AND toBlock <= ?`
).run(chainId, address, newFrom, newTo);

// Insert the new merged range
db.prepare(
Expand Down
25 changes: 15 additions & 10 deletions src/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,26 @@ export async function getSubscriptionEvents(args: {
pushEvent(event);
}

const cachedToBlock = result.toBlock + 1n;
const fetchRanges = [];

// we got all events from the cache
if (cachedToBlock >= to) {
return [];
if (result.fromBlock > from) {
fetchRanges.push({
from: from,
to: result.fromBlock - 1n,
subscription: subscription,
});
}

// fetch the remaining events
return [
{
from: cachedToBlock,
if (result.toBlock < to) {
fetchRanges.push({
from: result.toBlock + 1n,
to: to,
subscription: subscription,
},
];
});
}

// fetch the remaining events
return fetchRanges;
}

// no cache at all, fetch all events
Expand Down

0 comments on commit f7d24fc

Please sign in to comment.