Skip to content

Commit

Permalink
cache last 100k block queries quantized by blockStep
Browse files Browse the repository at this point in the history
  • Loading branch information
dshiell committed Mar 10, 2024
1 parent 6aad336 commit 17db74f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
22 changes: 15 additions & 7 deletions app/api/packets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,27 @@ export async function GET(request: NextRequest) {
validChannelIds.add(channel.channelId);
});

const fromBlock = Number(from);
const toBlock = to ? Number(to) : 'latest';
const blockStep = 30;

// Collect send logs from all chains
let sendLogs: Array<[ethers.EventLog, CHAIN]> = [];
let srcChainProviders: Record<CHAIN, CachingJsonRpcProvider> = {} as Record<
CHAIN,
CachingJsonRpcProvider
>;
let srcChainHeights: Record<CHAIN, number> = {} as Record<CHAIN, number>;
let srcChainContracts: Array<[ethers.Contract, CHAIN]> = [];
for (const chain in CHAIN_CONFIGS) {
const chainId = chain as CHAIN;
const dispatcherAddresses = CHAIN_CONFIGS[chainId].dispatchers;
const provider = new CachingJsonRpcProvider(
CHAIN_CONFIGS[chainId].rpc,
CHAIN_CONFIGS[chainId].id
CHAIN_CONFIGS[chainId].id,
CHAIN_CONFIGS[chainId].cache
);
srcChainProviders[chainId] = provider;
let latestBlock = await provider.getBlockNumber();
const latestBlock = await provider.getBlockNumber();
srcChainHeights[chainId] = latestBlock - (latestBlock % blockStep);

for (const dispatcherAddress of dispatcherAddresses) {
const contract = new ethers.Contract(
Expand All @@ -76,8 +78,9 @@ export async function GET(request: NextRequest) {
srcChainContracts.push([contract, chainId]);

// query the last 100000 blocks ~2.5 days
let fromBlock = latestBlock - 100000;
let toBlock = latestBlock;
const toBlock = srcChainHeights[chainId];
const fromBlock = toBlock > 100000 ? toBlock - 100000 : 1;
console.log(`SendPacket fromBlock: ${fromBlock}, toBlock: ${toBlock}`);

const newSendLogs = (await contract.queryFilter(
'SendPacket',
Expand Down Expand Up @@ -157,6 +160,10 @@ export async function GET(request: NextRequest) {

// Start by searching for ack events on the source chains
const ackLogsPromises = srcChainContracts.map(async ([contract, chain]) => {
const toBlock = srcChainHeights[chain];
const fromBlock = toBlock > 100000 ? toBlock - 100000 : 1;

console.log(`Ack fromBlock: ${fromBlock}, toBlock: ${toBlock}`);
const newAckLogs = (await contract.queryFilter(
'Acknowledgement',
fromBlock,
Expand Down Expand Up @@ -215,7 +222,8 @@ export async function GET(request: NextRequest) {
for (const dispatcherAddress of dispatcherAddresses) {
const provider = new CachingJsonRpcProvider(
CHAIN_CONFIGS[chainId].rpc,
CHAIN_CONFIGS[chainId].id
CHAIN_CONFIGS[chainId].id,
CHAIN_CONFIGS[chainId].cache
);
destChainProviders[chainId] = provider;
const contract = new ethers.Contract(
Expand Down
13 changes: 9 additions & 4 deletions app/api/utils/provider-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import * as flatCache from 'flat-cache';
export class CachingJsonRpcProvider extends ethers.JsonRpcProvider {
private cache;

constructor(url: string, network?: ethers.Networkish) {
constructor(
url: string,
network?: ethers.Networkish,
cache: string = '/tmp'
) {
super(url, network);
this.cache = flatCache.load('ethCache', '/tmp');
this.cache = flatCache.load('ethCache', cache);
}

private async fetchDataWithCache<T>(
Expand All @@ -16,7 +20,6 @@ export class CachingJsonRpcProvider extends ethers.JsonRpcProvider {
// Check if the data is already in the cache
const cachedData = this.cache.getKey(cacheKey);
if (cachedData) {
console.log('CACHE HIT: ', cacheKey);
return cachedData;
}

Expand Down Expand Up @@ -53,7 +56,9 @@ export class CachingJsonRpcProvider extends ethers.JsonRpcProvider {

async getLogs(filter: ethers.Filter): Promise<Array<ethers.Log>> {
return await this.fetchDataWithCache(
`${this._network.chainId}_logs_${filter.address}_${filter.fromBlock}_${filter.toBlock}`,
`${this._network.chainId}_logs_${filter.topics![0]!.toString()}_${
filter.address
}_${filter.fromBlock}_${filter.toBlock}`,
async () => await super.getLogs(filter)
);
}
Expand Down
4 changes: 3 additions & 1 deletion app/utils/chains/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let opDispatcher = process.env.DISPATCHER_ADDRESS_OPTIMISM!;
let baseDispatcher = process.env.DISPATCHER_ADDRESS_BASE!;
let opDispatcherSimClient = process.env.DISPATCHER_ADDRESS_OPTIMISM_SIMCLIENT!;
let baseDispatcherSimClient = process.env.DISPATCHER_ADDRESS_BASE_SIMCLIENT!;

let cachePath = process.env.CACHE_PATH || '/cache';
let optimismRPC =
process.env.OPTIMISM_RPC ||
'https://opt-sepolia.g.alchemy.com/v2/iMhzwCPw18v9Byeh59cedtUKbul_jFF3'; // "https://opt-sepolia.g.alchemy.com/v2/jKvLhhXvtnWdNeZrKst0demxnwJcYH1o"
Expand All @@ -25,6 +25,7 @@ export const CHAIN_CONFIGS: {
dispatchers: [opDispatcher, opDispatcherSimClient],
blockTime: 2,
icon: OptimismIcon,
cache: cachePath,
},
base: {
id: 84532,
Expand All @@ -33,5 +34,6 @@ export const CHAIN_CONFIGS: {
dispatchers: [baseDispatcher, baseDispatcherSimClient],
blockTime: 2,
icon: BaseIcon,
cache: cachePath,
},
};
1 change: 1 addition & 0 deletions app/utils/types/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Chain {
dispatchers: string[];
blockTime: number;
icon: () => JSX.Element;
cache: string;
}

0 comments on commit 17db74f

Please sign in to comment.