Skip to content

Commit

Permalink
Add stats table
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkvi committed Mar 21, 2024
1 parent 7e541c4 commit 124d09d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 30 deletions.
4 changes: 4 additions & 0 deletions ponder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,8 @@ export default createSchema((p) => ({
transactionHash: p.string(),
state: p.enum('ChannelStates'),
}),
Stats: p.createTable({
id: p.string(),
val: p.int(),
}),
}));
80 changes: 74 additions & 6 deletions src/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ponder } from "@/generated";
import { ethers } from "ethers";
import { DISPATCHER_CLIENT, TmClient } from "./client";
import logger from './logger';
import { StatName, updateStats } from "./stats";

ponder.on("DispatcherProof:ConnectIbcChannel", async ({event, context}) => {
const {address} = context.contracts.DispatcherProof;
Expand Down Expand Up @@ -69,17 +70,19 @@ ponder.on("DispatcherProof:OwnershipTransferred", async ({event, context}) => {
});
});

ponder.on("DispatcherProof:Acknowledgement", async ({event, context}) => {
ponder.on("DispatcherProof:SendPacket", async ({event, context}) => {
const {address} = context.contracts.DispatcherProof;
const chainId = context.network.chainId;
await context.db.Acknowledgement.create({
await context.db.SendPacket.create({
id: event.log.id,
data: {
dispatcherAddress: address || "0x",
dispatcherType: "proof",
sourcePortAddress: event.args.sourcePortAddress,
sourceChannelId: ethers.decodeBytes32String(event.args.sourceChannelId),
packet: event.args.packet,
sequence: event.args.sequence,
timeoutTimestamp: event.args.timeoutTimestamp,
blockNumber: event.block.number,
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
Expand All @@ -90,6 +93,7 @@ ponder.on("DispatcherProof:Acknowledgement", async ({event, context}) => {
from: event.transaction.from.toString(),
},
});
await updateStats(context.db.Stats, StatName.SendPackets)
});

ponder.on("DispatcherProof:RecvPacket", async ({event, context}) => {
Expand Down Expand Up @@ -147,21 +151,84 @@ ponder.on("DispatcherProof:RecvPacket", async ({event, context}) => {
};
},
});
await updateStats(context.db.Stats, StatName.RecvPackets)
});

ponder.on("DispatcherProof:SendPacket", async ({event, context}) => {
ponder.on("DispatcherProof:WriteAckPacket", async ({event, context}) => {
const {address} = context.contracts.DispatcherProof;
const chainId = context.network.chainId;
await context.db.SendPacket.create({
let writerPortAddress = event.args.writerPortAddress;
let writerChannelId = ethers.decodeBytes32String(event.args.writerChannelId);
let sequence = event.args.sequence;
await context.db.WriteAckPacket.create({
id: event.log.id,
data: {
dispatcherAddress: address || "0x",
dispatcherType: "proof",
writerPortAddress: writerPortAddress,
writerChannelId: writerChannelId,
sequence: sequence,
ackPacketSuccess: event.args.ackPacket.success,
ackPacketData: event.args.ackPacket.data,
blockNumber: event.block.number,
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
from: event.transaction.from.toString(),
},
});

let client = DISPATCHER_CLIENT[address!];
const destPortId = `polyibc.${client}.${writerPortAddress.slice(2)}`;
const tmClient = await TmClient.getInstance();
let channel;
try {
channel = await tmClient.ibc.channel.channel(destPortId, writerChannelId);
} catch (e) {
logger.info('Skipping packet for channel: ', writerChannelId);
return;
}
if (!channel.channel) {
logger.warn('No channel found for write ack: ', writerChannelId, writerPortAddress);
return;
}
const key = `${channel.channel.counterparty.portId}-${channel.channel.counterparty.channelId}-${sequence}`;
await context.db.Packet.upsert({
id: key,
create: {
state: "WRITE_ACK",
writeAckPacketId: event.log.id,
},
update: ({current}) => {
let state = current.state;
if (current.state == "SENT") {
state = "WRITE_ACK"
}
return {
writeAckPacketId: event.log.id,
state: state,
};
},
});

await updateStats(context.db.Stats, StatName.WriteAckPacket);
});


ponder.on("DispatcherProof:Acknowledgement", async ({event, context}) => {
const {address} = context.contracts.DispatcherProof;
const chainId = context.network.chainId;
await context.db.Acknowledgement.create({
id: event.log.id,
data: {
dispatcherAddress: address || "0x",
dispatcherType: "proof",
sourcePortAddress: event.args.sourcePortAddress,
sourceChannelId: ethers.decodeBytes32String(event.args.sourceChannelId),
packet: event.args.packet,
sequence: event.args.sequence,
timeoutTimestamp: event.args.timeoutTimestamp,
blockNumber: event.block.number,
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
Expand All @@ -172,6 +239,7 @@ ponder.on("DispatcherProof:SendPacket", async ({event, context}) => {
from: event.transaction.from.toString(),
},
});
await updateStats(context.db.Stats, StatName.AckPackets)
});

ponder.on("DispatcherProof:Timeout", async ({event, context}) => {
Expand Down
63 changes: 39 additions & 24 deletions src/sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ponder } from "@/generated";
import { ethers } from "ethers";
import { DISPATCHER_CLIENT, TmClient } from "./client";
import logger from './logger';
import { StatName, updateStats } from "./stats";

ponder.on("DispatcherSim:OpenIbcChannel", async ({event, context}) => {
const {address} = context.contracts.DispatcherSim;
Expand Down Expand Up @@ -114,7 +115,11 @@ ponder.on("DispatcherSim:ConnectIbcChannel", async ({event, context}) => {
}

// update earliest INIT state record that have incomplete id
let channels = await context.db.Channel.findMany({where: {portId: portId, channelId: ""}, orderBy: {blockTimestamp: "asc"}, limit: 1});
let channels = await context.db.Channel.findMany({
where: {portId: portId, channelId: ""},
orderBy: {blockTimestamp: "asc"},
limit: 1
});
for (let channel of channels.items) {
await context.db.Channel.update({
id: channel.id,
Expand All @@ -130,7 +135,11 @@ ponder.on("DispatcherSim:ConnectIbcChannel", async ({event, context}) => {
})
}

channels = await context.db.Channel.findMany({where: {portId: portId, channelId: channelId}, orderBy: {blockTimestamp: "asc"}, limit: 1});
channels = await context.db.Channel.findMany({
where: {portId: portId, channelId: channelId},
orderBy: {blockTimestamp: "asc"},
limit: 1
});
for (let channel of channels.items) {
await context.db.Channel.update({
id: channel.id,
Expand Down Expand Up @@ -227,29 +236,8 @@ ponder.on("DispatcherSim:SendPacket", async ({event, context}) => {
sendPacketId: event.log.id,
}
});
});

ponder.on("DispatcherSim:RecvPacket", async ({event, context}) => {
const {address} = context.contracts.DispatcherSim;
const chainId = context.network.chainId;
await context.db.RecvPacket.create({
id: event.log.id,
data: {
dispatcherAddress: address || "0x",
dispatcherType: "sim",
destPortAddress: event.args.destPortAddress,
destChannelId: ethers.decodeBytes32String(event.args.destChannelId),
sequence: event.args.sequence,
blockNumber: event.block.number,
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
from: event.transaction.from.toString(),
},
});
await updateStats(context.db.Stats, StatName.SendPackets)
});

ponder.on("DispatcherSim:WriteAckPacket", async ({event, context}) => {
Expand Down Expand Up @@ -310,6 +298,32 @@ ponder.on("DispatcherSim:WriteAckPacket", async ({event, context}) => {
};
},
});

await updateStats(context.db.Stats, StatName.WriteAckPacket);
});

ponder.on("DispatcherSim:RecvPacket", async ({event, context}) => {
const {address} = context.contracts.DispatcherSim;
const chainId = context.network.chainId;
await context.db.RecvPacket.create({
id: event.log.id,
data: {
dispatcherAddress: address || "0x",
dispatcherType: "sim",
destPortAddress: event.args.destPortAddress,
destChannelId: ethers.decodeBytes32String(event.args.destChannelId),
sequence: event.args.sequence,
blockNumber: event.block.number,
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
from: event.transaction.from.toString(),
},
});
await updateStats(context.db.Stats, StatName.RecvPackets)
});

ponder.on("DispatcherSim:Acknowledgement", async ({event, context}) => {
Expand Down Expand Up @@ -350,6 +364,7 @@ ponder.on("DispatcherSim:Acknowledgement", async ({event, context}) => {
state: "ACK",
}
});
await updateStats(context.db.Stats, StatName.AckPackets)
});

ponder.on("DispatcherSim:Timeout", async ({event, context}) => {
Expand Down
25 changes: 25 additions & 0 deletions src/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { DatabaseModel } from "@ponder/core/src/types/model";
import { Infer } from "@ponder/core/src/schema/types";
import { schema } from "@/generated";

export enum StatName {
SendPackets = 'SendPackets',
RecvPackets = 'RecvPackets',
AckPackets = 'AckPackets',
WriteAckPacket = 'WriteAckPacket',
}

export async function updateStats<T extends DatabaseModel<Infer<schema>["Stats"]>>(Stats: T, id: StatName) {
await Stats.upsert({
id: id,
create: {
val: 1,
},
update: ({current}: T) => {
return {
val: current.val + 1
}
}
});

}

0 comments on commit 124d09d

Please sign in to comment.