Skip to content

Commit

Permalink
Add a route to refresh packets cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkvi committed Mar 11, 2024
1 parent 28ca480 commit 64d444f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
API_URL=http://localhost:26657
DISPATCHER_ADDRESS_OPTIMISM=0x58f1863F75c9Db1c7266dC3d7b43832b58f35e83
DISPATCHER_ADDRESS_OPTIMISM_SIMCLIENT=0x6C9427E8d770Ad9e5a493D201280Cc178125CEc0
DISPATCHER_ADDRESS_BASE=0xfC1d3E02e00e0077628e8Cc9edb6812F95Db05dC
DISPATCHER_ADDRESS_BASE_SIMCLIENT=0x0dE926fE2001B2c96e9cA6b79089CEB276325E9F
TOKEN=8N+om8QfLROAmecoV9L+FO/f72yO3KubuPC82ehh0/0=
21 changes: 21 additions & 0 deletions app/api/cache/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextRequest, NextResponse } from 'next/server';
import { getCacheTTL, SimpleCache } from '@/api/utils/cosmos';
import { getPackets } from '@/api/packets/route';

export async function GET(request: NextRequest) {
const TOKEN = process.env.TOKEN;

if (request.headers.get('Authorization') !== `Bearer ${TOKEN}`) {
console.error('Unauthorized request');
return NextResponse.error();
}

const cache = SimpleCache.getInstance();
try {
const packets = await getPackets('1', null);
cache.set('allPackets', packets, getCacheTTL());
return NextResponse.json(packets);
} catch (e) {
return NextResponse.error();
}
}
66 changes: 42 additions & 24 deletions app/api/packets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IdentifiedChannel } from 'cosmjs-types/ibc/core/channel/v1/channel';
import { QueryChannelsResponse } from 'cosmjs-types/ibc/core/channel/v1/query';
import Abi from 'utils/dispatcher.json';

export const dynamic = 'force-dynamic' // defaults to auto
export const dynamic = 'force-dynamic'; // defaults to auto

async function getChannels() {
const tmClient = await GetTmClient();
Expand All @@ -19,40 +19,52 @@ async function getChannels() {
export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const from = searchParams.get('from');
const to = searchParams.get('to')
const to = searchParams.get('to');

if (!from) {
return NextResponse.error();
}

const cache = SimpleCache.getInstance()
const allPackets = cache.get("allPackets");
const cache = SimpleCache.getInstance();
const allPackets = cache.get('allPackets');
if (allPackets) {
return NextResponse.json(allPackets);
}

try {
const packets = await getPackets(from, to);
cache.set('allPackets', packets, getCacheTTL());
return NextResponse.json(packets);
} catch (e) {
return NextResponse.error();
}
}

export async function getPackets(from: string, to: string | null) {
const channelsResponse = await getChannels();
if (!channelsResponse) {
return NextResponse.error();
console.error('No channels found');
throw new Error('No channels found');
}

const channels = channelsResponse as Array<IdentifiedChannel>;
const openChannels = channels.filter((channel) => {
return (
channel.portId.startsWith(`polyibc.`) &&
channel.counterparty.portId.startsWith(`polyibc.`)
);
})
});
if (openChannels.length === 0) {
return NextResponse.json([]);
return [];
}

const validChannelIds = new Set<string>();
openChannels.forEach((channel) => {
validChannelIds.add(channel.channelId);
})
});

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

// Collect send logs from all chains
let sendLogs: Array<[ethers.EventLog, CHAIN]> = [];
Expand All @@ -62,11 +74,15 @@ export async function GET(request: NextRequest) {
const chainId = chain as CHAIN;
const dispatcherAddresses = CHAIN_CONFIGS[chainId].dispatchers;
for (const dispatcherAddress of dispatcherAddresses) {
console.log('dispatcherAddress: ', dispatcherAddress);
console.log(`rpc: ${CHAIN_CONFIGS[chainId].rpc}, id: ${CHAIN_CONFIGS[chainId].id}`)
const provider = new CachingJsonRpcProvider(CHAIN_CONFIGS[chainId].rpc, CHAIN_CONFIGS[chainId].id);
srcChainProviders[chainId] = provider;
const contract = new ethers.Contract(dispatcherAddress, Abi.abi, provider);
srcChainContracts.push([contract, chainId]);
console.log('fromBlock: ', fromBlock, 'toBlock: ', toBlock)
const newSendLogs = (await contract.queryFilter('SendPacket', fromBlock, toBlock)) as Array<ethers.EventLog>;
console.log('newSendLogs: ', newSendLogs);
sendLogs = sendLogs.concat(newSendLogs.map((eventLog) => [eventLog, chainId]));
}
}
Expand All @@ -81,7 +97,7 @@ export async function GET(request: NextRequest) {
srcChannelId = ethers.decodeBytes32String(srcChannelId);

if (!validChannelIds.has(srcChannelId)) {
console.log("Skipping packet for channel: ", srcChannelId);
console.log('Skipping packet for channel: ', srcChannelId);
return;
}

Expand All @@ -91,9 +107,10 @@ export async function GET(request: NextRequest) {
channel.portId.endsWith(srcPortAddress.slice(2))
));
if (!channel) {
console.warn("No channel found for packet: ", srcChannelId, srcPortAddress);
console.warn('No channel found for packet: ', srcChannelId, srcPortAddress);
return;
}
1;

const key = `${srcPortAddress}-${srcChannelId}-${sequence}`;
const srcProvider = srcChainProviders[srcChain];
Expand All @@ -111,13 +128,15 @@ export async function GET(request: NextRequest) {
state: PacketStates.SENT,
createTime: srcBlock!.timestamp,
sendTx: sendEvent.transactionHash,
sourceChain: channel.portId.split(".")[1],
destChain: channel.counterparty.portId.split(".")[1],
sourceChain: channel.portId.split('.')[1],
destChain: channel.counterparty.portId.split('.')[1]
};
unprocessedPacketKeys.add(key);
}
await Promise.allSettled(sendLogs.map(processSendLog))

await Promise.allSettled(sendLogs.map(processSendLog));

console.log('Fetched send logs');
/*
** Find the state of each packet
** States could be like:
Expand Down Expand Up @@ -152,7 +171,7 @@ export async function GET(request: NextRequest) {
const key = `${srcPortAddress}-${ethers.decodeBytes32String(srcChannelId)}-${sequence}`;

if (!packets[key]) {
console.log("No packet found for ack: ", key);
console.log('No packet found for ack: ', key);
return;
}

Expand Down Expand Up @@ -206,7 +225,7 @@ export async function GET(request: NextRequest) {
// It seems that due to short circuiting POLY_ACK_RECV can't be distinguished as a separate state so this state is skipped

const writeAckLogsPromises = destChainContracts.map(async ([destContract, destChain]) => {
const newWriteAckLogs = await destContract.queryFilter('WriteAckPacket', 1, "latest");
const newWriteAckLogs = await destContract.queryFilter('WriteAckPacket', 1, 'latest');
return newWriteAckLogs.map((eventLog) => [eventLog, destChain] as [ethers.EventLog, CHAIN]);
});

Expand All @@ -223,14 +242,14 @@ export async function GET(request: NextRequest) {

const channel = openChannels.find((channel) => {
return (
channel.counterparty.channelId === ethers.decodeBytes32String(destChannelId) &&
channel.counterparty.channelId === ethers.decodeBytes32String(destChannelId) &&
channel.counterparty.portId.startsWith(`polyibc.${destChain}`) &&
channel.counterparty.portId.endsWith(receiver.slice(2))
);
})
});

if (!channel) {
console.log("Unable to find channel for write ack: ", destChannelId, "receiver: ", receiver);
console.log('Unable to find channel for write ack: ', destChannelId, 'receiver: ', receiver);
continue;
}

Expand All @@ -244,7 +263,7 @@ export async function GET(request: NextRequest) {
// Match any recv packet events on destination chains to packets
const recvPacketPromises = destChainContracts.map(async (destChainContract) => {
const [destContract, destChain] = destChainContract;
const newRecvPacketLogs = await destContract.queryFilter('RecvPacket', 1, "latest");
const newRecvPacketLogs = await destContract.queryFilter('RecvPacket', 1, 'latest');
return newRecvPacketLogs.map((eventLog) => [eventLog, destChain] as [ethers.EventLog, CHAIN]);
});

Expand All @@ -268,11 +287,11 @@ export async function GET(request: NextRequest) {
});

if (!channel) {
console.log("Unable to find channel for recv packet: ", destChannelId, "receiver: ", destPortAddress);
console.log('Unable to find channel for recv packet: ', destChannelId, 'receiver: ', destPortAddress);
return;
}

const key = `0x${channel.portId.split(".")[2]}-${channel.channelId}-${sequence}`;
const key = `0x${channel.portId.split('.')[2]}-${channel.channelId}-${sequence}`;
if (packets[key]) {
const recvBlock = await destChainProviders[destChain].getBlock(recvPacketEvent.blockNumber);

Expand Down Expand Up @@ -330,6 +349,5 @@ export async function GET(request: NextRequest) {
response.push(packets[key]);
});

cache.set("allPackets", response, getCacheTTL());
return NextResponse.json(response);
return response;
}

0 comments on commit 64d444f

Please sign in to comment.