Skip to content

Commit

Permalink
Record latency and gas during packet lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkvi committed Apr 9, 2024
1 parent f030b6d commit c72878b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 76 deletions.
77 changes: 34 additions & 43 deletions ponder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
CloseIbcChannel: p.createTable({
Expand All @@ -27,9 +27,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
ConnectIbcChannel: p.createTable({
Expand All @@ -42,9 +42,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
OpenIbcChannel: p.createTable({
Expand All @@ -62,9 +62,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
OwnershipTransferred: p.createTable({
Expand All @@ -77,9 +77,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
RecvPacket: p.createTable({
Expand All @@ -93,9 +93,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
SendPacket: p.createTable({
Expand All @@ -111,9 +111,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
Timeout: p.createTable({
Expand All @@ -127,9 +127,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
WriteAckPacket: p.createTable({
Expand All @@ -145,9 +145,9 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
WriteTimeoutPacket: p.createTable({
Expand All @@ -164,40 +164,31 @@ export default createSchema((p) => ({
blockTimestamp: p.bigint(),
transactionHash: p.string(),
chainId: p.int(),
gas: p.bigint(),
maxFeePerGas: p.bigint().optional(),
maxPriorityFeePerGas: p.bigint().optional(),
gas: p.int(),
maxFeePerGas: p.int().optional(),
maxPriorityFeePerGas: p.int().optional(),
from: p.string(),
}),
PacketStates: p.createEnum(["SENT", "RECV", "WRITE_ACK", "ACK", "TIMEOUT", "WRITE_TIMEOUT"]),
Packet: p.createTable({
id: p.string(),
state: p.enum('PacketStates'),
// sequence: p.bigint(),
// sourcePortAddress: p.string().optional(),
// sourceChannelId: p.string().optional(),
// destPortAddress: p.string().optional(),
// destChannelId: p.string().optional(),
// timeoutTimestamp: p.bigint(),
// createTime: p.bigint().optional(),
// endTime: p.bigint().optional(),
// sendTx: p.string().optional(),
// rcvTx: p.string().optional(),
// ackTx: p.string().optional(),
// srcChainId: p.int().optional(),
// destChainId: p.int().optional(),
sendPacketId: p.string().optional().references('SendPacket.id'),
sendPacket: p.one('sendPacketId'),
sendTx: p.string().optional(),
recvPacketId: p.string().optional().references('RecvPacket.id'),
recvPacket: p.one('recvPacketId'),
recvTx: p.string().optional(),
sendToRecvTime: p.int().optional(),
sendToRecvGas: p.int().optional(),
writeAckPacketId: p.string().optional().references('WriteAckPacket.id'),
writeAckPacket: p.one('writeAckPacketId'),
writeAckTx: p.string().optional(),
ackPacketId: p.string().optional().references('Acknowledgement.id'),
ackPacket: p.one('ackPacketId'),
ackTx: p.string().optional(),
sendToAckTime: p.int().optional(),
sendToAckGas: p.int().optional(),
}),
ChannelStates: p.createEnum(["INIT", "TRY", "OPEN", "CLOSED"]),
Channel: p.createTable({
Expand Down
121 changes: 88 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ async function openIbcChannel<name extends Virtual.EventNames<config>>(event: Vi
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand Down Expand Up @@ -118,9 +118,9 @@ async function connectIbcChannel<name extends Virtual.EventNames<config>>(event:
blockNumber: event.block.number,
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand All @@ -136,7 +136,7 @@ async function connectIbcChannel<name extends Virtual.EventNames<config>>(event:
const channel = await tmClient.ibc.channel.channel(portId, channelId);

if (!channel.channel) {
logger.warn('No channel found for write ack: ', portId, channelId );
logger.warn('No channel found for write ack: ', portId, channelId);
// Use bail to immediately stop retrying under certain conditions
bail(new Error('No channel found, giving up'));
} else {
Expand Down Expand Up @@ -214,9 +214,9 @@ async function closeIbcChannel<name extends Virtual.EventNames<config>>(event: V
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand All @@ -238,9 +238,9 @@ async function ownershipTransferred<name extends Virtual.EventNames<config>>(eve
blockTimestamp: event.block.timestamp,
transactionHash: event.transaction.hash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand Down Expand Up @@ -271,9 +271,9 @@ async function sendPacket<name extends Virtual.EventNames<config>>(event: Virtua
blockTimestamp: event.block.timestamp,
transactionHash: transactionHash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand All @@ -295,6 +295,7 @@ async function sendPacket<name extends Virtual.EventNames<config>>(event: Virtua
}
});

await updatePacket(context, key)
await updateStats(context.db.Stat, StatName.SendPackets)
}

Expand Down Expand Up @@ -323,9 +324,9 @@ async function writeAckPacket<name extends Virtual.EventNames<config>>(event: Vi
blockTimestamp: event.block.timestamp,
transactionHash: transactionHash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand Down Expand Up @@ -370,6 +371,59 @@ async function writeAckPacket<name extends Virtual.EventNames<config>>(event: Vi
await updateStats(context.db.Stat, StatName.WriteAckPacket);
}

async function updatePacket<name extends Virtual.EventNames<config>>(context: Virtual.Context<config, schema, name>, id: string) {
let packet = await context.db.Packet.findUnique({id})
if (!packet) {
console.warn('No packet found for updatePacket', id)
return;
}

if (packet.sendPacketId && packet.recvPacketId && !packet.sendToRecvTime) {
const sendPacket = await context.db.SendPacket.findUnique({id: packet.sendPacketId});
const recvPacket = await context.db.RecvPacket.findUnique({id: packet.recvPacketId});
if (sendPacket && recvPacket) {
packet.sendToRecvTime = Number(recvPacket.blockTimestamp - sendPacket.blockTimestamp);
packet.sendToRecvGas = Number(recvPacket.gas + sendPacket.gas);
await context.db.Packet.update({
id,
data: {
sendToRecvTime: packet.sendToRecvTime,
sendToRecvGas: packet.sendToRecvGas,
}
});
}
}

if (packet.sendPacketId && packet.ackPacketId && !packet.sendToAckTime) {
const sendPacket = await context.db.SendPacket.findUnique({id: packet.sendPacketId});
const ackPacket = await context.db.Acknowledgement.findUnique({id: packet.ackPacketId});
if (sendPacket && ackPacket) {
packet.sendToAckTime = Number(ackPacket.blockTimestamp - sendPacket.blockTimestamp);
await context.db.Packet.update({
id,
data: {
sendToAckTime: packet.sendToAckTime,
}
});
}
}

if (packet.sendPacketId && packet.recvPacketId && packet.ackPacketId && !packet.sendToAckGas) {
const sendPacket = await context.db.SendPacket.findUnique({id: packet.sendPacketId});
const recvPacket = await context.db.RecvPacket.findUnique({id: packet.recvPacketId});
const ackPacket = await context.db.Acknowledgement.findUnique({id: packet.ackPacketId});
if (sendPacket && recvPacket && ackPacket) {
packet.sendToAckGas = Number(ackPacket.gas + recvPacket.gas + sendPacket.gas);
await context.db.Packet.update({
id,
data: {
sendToAckGas: packet.sendToAckGas,
}
});
}
}
}

async function recvPacket<name extends Virtual.EventNames<config>>(event: Virtual.Event<config, "DispatcherSim:RecvPacket" | "DispatcherProof:RecvPacket">, context: Virtual.Context<config, schema, name>, contractName: Virtual.ExtractContractName<name>) {
const {address, dispatcherType} = getAddressAndDispatcherType<name>(contractName, context);
const chainId = context.network.chainId as number;
Expand All @@ -394,9 +448,9 @@ async function recvPacket<name extends Virtual.EventNames<config>>(event: Virtua
blockTimestamp: event.block.timestamp,
transactionHash: recvTx,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand All @@ -408,7 +462,7 @@ async function recvPacket<name extends Virtual.EventNames<config>>(event: Virtua
try {
channel = await tmClient.ibc.channel.channel(destPortId, destChannelId);
} catch (e) {
logger.info('Skipping packet for channel in recvPacket: ', destPortId, destChannelId);
logger.info('Skipping packet for channel in recvPacket');
return;
}

Expand Down Expand Up @@ -438,7 +492,7 @@ async function recvPacket<name extends Virtual.EventNames<config>>(event: Virtua
},
});


await updatePacket(context, key)
await updateStats(context.db.Stat, StatName.RecvPackets)
}

Expand All @@ -465,9 +519,9 @@ async function acknowledgement<name extends Virtual.EventNames<config>>(event: V
blockTimestamp: event.block.timestamp,
transactionHash: transactionHash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand All @@ -490,6 +544,7 @@ async function acknowledgement<name extends Virtual.EventNames<config>>(event: V
}
});

await updatePacket(context, key)
await updateStats(context.db.Stat, StatName.AckPackets)
}

Expand All @@ -515,9 +570,9 @@ async function timeout<name extends Virtual.EventNames<config>>(event: Virtual.E
blockTimestamp: event.block.timestamp,
transactionHash: transactionHash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand Down Expand Up @@ -550,9 +605,9 @@ async function writeTimeoutPacket<name extends Virtual.EventNames<config>>(event
blockTimestamp: event.block.timestamp,
transactionHash: transactionHash,
chainId: chainId,
gas: event.transaction.gas,
maxFeePerGas: event.transaction.maxFeePerGas,
maxPriorityFeePerGas: event.transaction.maxPriorityFeePerGas,
gas: Number(event.transaction.gas),
maxFeePerGas: Number(event.transaction.maxFeePerGas),
maxPriorityFeePerGas: Number(event.transaction.maxPriorityFeePerGas),
from: event.transaction.from.toString(),
},
});
Expand Down

0 comments on commit c72878b

Please sign in to comment.