Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect fee entities with their relevant sources (send packet and open channel) #18

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
with:
image-ref: ${{ steps.meta.outputs.tags }}
image-ref: ${{ env.IMAGE_NAME }}:latest
format: 'table'
exit-code: '1'
ignore-unfixed: true
Expand Down
17 changes: 17 additions & 0 deletions db/migrations/1721920194282-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = class Data1721920194282 {
name = 'Data1721920194282'

async up(db) {
await db.query(`ALTER TABLE "send_packet" ADD "fee_deposited_id" character varying`)
await db.query(`ALTER TABLE "send_packet" ADD CONSTRAINT "UQ_0dcda86a7cfb4b2c7869d1ae9ae" UNIQUE ("fee_deposited_id")`)
await db.query(`CREATE UNIQUE INDEX "IDX_0dcda86a7cfb4b2c7869d1ae9a" ON "send_packet" ("fee_deposited_id") `)
await db.query(`ALTER TABLE "send_packet" ADD CONSTRAINT "FK_0dcda86a7cfb4b2c7869d1ae9ae" FOREIGN KEY ("fee_deposited_id") REFERENCES "send_packet_fee_deposited"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`ALTER TABLE "send_packet" DROP COLUMN "fee_deposited_id"`)
await db.query(`ALTER TABLE "send_packet" DROP CONSTRAINT "UQ_0dcda86a7cfb4b2c7869d1ae9ae"`)
await db.query(`DROP INDEX "public"."IDX_0dcda86a7cfb4b2c7869d1ae9a"`)
await db.query(`ALTER TABLE "send_packet" DROP CONSTRAINT "FK_0dcda86a7cfb4b2c7869d1ae9ae"`)
}
}
17 changes: 17 additions & 0 deletions db/migrations/1721920657518-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = class Data1721920657518 {
name = 'Data1721920657518'

async up(db) {
await db.query(`CREATE INDEX "IDX_36524a30939bb522f1eda95267" ON "send_packet_fee_deposited" ("send_gas_limit") `)
await db.query(`CREATE INDEX "IDX_b05770c7b9f6fba9a0b12814e9" ON "send_packet_fee_deposited" ("send_gas_price") `)
await db.query(`CREATE INDEX "IDX_8847ac7451965338a765734545" ON "send_packet_fee_deposited" ("ack_gas_limit") `)
await db.query(`CREATE INDEX "IDX_bea86541f9bbed391be346275d" ON "send_packet_fee_deposited" ("ack_gas_price") `)
}

async down(db) {
await db.query(`DROP INDEX "public"."IDX_36524a30939bb522f1eda95267"`)
await db.query(`DROP INDEX "public"."IDX_b05770c7b9f6fba9a0b12814e9"`)
await db.query(`DROP INDEX "public"."IDX_8847ac7451965338a765734545"`)
await db.query(`DROP INDEX "public"."IDX_bea86541f9bbed391be346275d"`)
}
}
25 changes: 25 additions & 0 deletions db/migrations/1721923283239-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = class Data1721923283239 {
name = 'Data1721923283239'

async up(db) {
await db.query(`ALTER TABLE "send_packet" DROP CONSTRAINT "FK_0dcda86a7cfb4b2c7869d1ae9ae"`)
await db.query(`DROP INDEX "public"."IDX_0dcda86a7cfb4b2c7869d1ae9a"`)
await db.query(`ALTER TABLE "send_packet" DROP CONSTRAINT "UQ_0dcda86a7cfb4b2c7869d1ae9ae"`)
await db.query(`ALTER TABLE "send_packet" DROP COLUMN "fee_deposited_id"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "send_packet_id" character varying`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "UQ_9415c52ccc157e1a37c9f3f6235" UNIQUE ("send_packet_id")`)
await db.query(`CREATE UNIQUE INDEX "IDX_9415c52ccc157e1a37c9f3f623" ON "send_packet_fee_deposited" ("send_packet_id") `)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235" FOREIGN KEY ("send_packet_id") REFERENCES "send_packet"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`ALTER TABLE "send_packet" ADD CONSTRAINT "FK_0dcda86a7cfb4b2c7869d1ae9ae" FOREIGN KEY ("fee_deposited_id") REFERENCES "send_packet_fee_deposited"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`CREATE UNIQUE INDEX "IDX_0dcda86a7cfb4b2c7869d1ae9a" ON "send_packet" ("fee_deposited_id") `)
await db.query(`ALTER TABLE "send_packet" ADD CONSTRAINT "UQ_0dcda86a7cfb4b2c7869d1ae9ae" UNIQUE ("fee_deposited_id")`)
await db.query(`ALTER TABLE "send_packet" ADD "fee_deposited_id" character varying`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "send_packet_id"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "UQ_9415c52ccc157e1a37c9f3f6235"`)
await db.query(`DROP INDEX "public"."IDX_9415c52ccc157e1a37c9f3f623"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235"`)
}
}
11 changes: 11 additions & 0 deletions db/migrations/1721924207047-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = class Data1721924207047 {
name = 'Data1721924207047'

async up(db) {
await db.query(`CREATE INDEX "IDX_f702d8ab77e40da621977ffe11" ON "send_packet" ("sequence") `)
}

async down(db) {
await db.query(`DROP INDEX "public"."IDX_f702d8ab77e40da621977ffe11"`)
}
}
19 changes: 19 additions & 0 deletions db/migrations/1721926496341-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = class Data1721926496341 {
name = 'Data1721926496341'

async up(db) {
await db.query(`DROP INDEX "public"."IDX_9415c52ccc157e1a37c9f3f623"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "UQ_9415c52ccc157e1a37c9f3f6235"`)
await db.query(`CREATE INDEX "IDX_9415c52ccc157e1a37c9f3f623" ON "send_packet_fee_deposited" ("send_packet_id") `)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235" FOREIGN KEY ("send_packet_id") REFERENCES "send_packet"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`CREATE UNIQUE INDEX "IDX_9415c52ccc157e1a37c9f3f623" ON "send_packet_fee_deposited" ("send_packet_id") `)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235" FOREIGN KEY ("send_packet_id") REFERENCES "send_packet"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "UQ_9415c52ccc157e1a37c9f3f6235" UNIQUE ("send_packet_id")`)
await db.query(`DROP INDEX "public"."IDX_9415c52ccc157e1a37c9f3f623"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235"`)
}
}
15 changes: 15 additions & 0 deletions db/migrations/1721926766448-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class Data1721926766448 {
name = 'Data1721926766448'

async up(db) {
await db.query(`ALTER TABLE "open_channel_fee_deposited" ADD "open_channel_id" character varying`)
await db.query(`CREATE INDEX "IDX_a3c03e6c9e7053f39ef10aaab7" ON "open_channel_fee_deposited" ("open_channel_id") `)
await db.query(`ALTER TABLE "open_channel_fee_deposited" ADD CONSTRAINT "FK_a3c03e6c9e7053f39ef10aaab73" FOREIGN KEY ("open_channel_id") REFERENCES "channel_open_init"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`ALTER TABLE "open_channel_fee_deposited" DROP COLUMN "open_channel_id"`)
await db.query(`DROP INDEX "public"."IDX_a3c03e6c9e7053f39ef10aaab7"`)
await db.query(`ALTER TABLE "open_channel_fee_deposited" DROP CONSTRAINT "FK_a3c03e6c9e7053f39ef10aaab73"`)
}
}
32 changes: 19 additions & 13 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type SendPacket @entity {
srcChannelId: String! @index
sourceChannel: Channel
packet: String! @index
sequence: BigInt!
sequence: BigInt! @index
timeoutTimestamp: BigInt!
blockNumber: BigInt!
blockTimestamp: BigInt! @index
Expand All @@ -52,6 +52,8 @@ type SendPacket @entity {
polymerTxHash: String @index
polymerGas: Int
polymerBlockNumber: BigInt

feesDeposited: [SendPacketFeeDeposited!] @derivedFrom(field: "sendPacket")
}

type RecvPacket @entity {
Expand Down Expand Up @@ -228,6 +230,8 @@ type ChannelOpenInit @entity {
polymerTxHash: String @index
polymerGas: Int
polymerBlockNumber: BigInt

feesDeposited: [OpenChannelFeeDeposited!] @derivedFrom(field: "openChannel")
}

type ChannelOpenTry @entity {
Expand Down Expand Up @@ -345,18 +349,19 @@ type ChannelCatchUpError @entity {
}

type SendPacketFeeDeposited @entity {
id: ID!
channelId: String! @index
sequence: BigInt! @index
sendGasLimit: BigInt!
sendGasPrice: BigInt!
ackGasLimit: BigInt!
ackGasPrice: BigInt!
blockNumber: BigInt!
blockTimestamp: BigInt! @index
transactionHash: String! @index
chainId: Int! @index
from: String! @index
id: ID!
channelId: String! @index
sequence: BigInt! @index
sendGasLimit: BigInt! @index
sendGasPrice: BigInt! @index
ackGasLimit: BigInt! @index
ackGasPrice: BigInt! @index
blockNumber: BigInt!
blockTimestamp: BigInt! @index
transactionHash: String! @index
chainId: Int! @index
from: String! @index
sendPacket: SendPacket @index
}

type OpenChannelFeeDeposited @entity {
Expand All @@ -372,6 +377,7 @@ type OpenChannelFeeDeposited @entity {
transactionHash: String! @index
chainId: Int! @index
from: String! @index
openChannel: ChannelOpenInit @index
}

type Transaction @entity {
Expand Down
83 changes: 82 additions & 1 deletion src/handlers/backfill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Context } from "../utils/types";
import { BACKFILL_CONCURRENCY, CATCHUP_BATCH_SIZE, CATCHUP_ERROR_LIMIT, ENABLE_CATCHUP } from "../chains/constants";
import { Channel, Packet, PacketCatchUpError } from "../model";
import {
Channel, ChannelOpenInit,
OpenChannelFeeDeposited,
Packet,
PacketCatchUpError,
SendPacket,
SendPacketFeeDeposited
} from "../model";
import { And, IsNull, LessThan, MoreThan, Not } from "typeorm";
import { channelMetrics } from "./channels";
import { packetMetrics } from "./packets";
Expand All @@ -11,6 +18,9 @@ export async function handler(ctx: Context) {
await updateMissingPacketMetrics(ctx);
await updateMissingChannelMetrics(ctx);
}

await updateMissingSendPacketFees(ctx)
await updateMissingOpenChannelFeeDeposited(ctx)
}

export function getMissingChannelMetricsClauses() {
Expand Down Expand Up @@ -131,3 +141,74 @@ async function updateMissingPacketMetrics(ctx: Context) {

await packetMetrics(Array.from(uniquePacketIds), ctx, BACKFILL_CONCURRENCY);
}

async function updateMissingSendPacketFees(ctx: Context) {
let sendPacketFees = await ctx.store.find(SendPacketFeeDeposited, {
take: CATCHUP_BATCH_SIZE,
where: {
sendPacket: IsNull()
}
})

let updatedSendPackets: SendPacket[] = [];
let updatedSendPacketFees: SendPacketFeeDeposited[] = [];

for (let sendPacketFee of sendPacketFees) {
let sendPacket = await ctx.store.findOne(SendPacket,
{
where: {
chainId: sendPacketFee.chainId,
srcChannelId: sendPacketFee.channelId,
sequence: sendPacketFee.sequence
}
});
if (sendPacket) {
sendPacket.feesDeposited = [sendPacketFee];
sendPacketFee.sendPacket = sendPacket;

updatedSendPackets.push(sendPacket);
updatedSendPacketFees.push(sendPacketFee);
} else {
console.log(`Could not find send packet for send packet fee ${sendPacketFee.id}`);
}
}

await ctx.store.upsert(updatedSendPackets);
await ctx.store.upsert(updatedSendPacketFees);
}


async function updateMissingOpenChannelFeeDeposited(ctx: Context) {
let openChannelFees = await ctx.store.find(OpenChannelFeeDeposited, {
take: CATCHUP_BATCH_SIZE,
where: {
openChannel: IsNull()
}
})

let updatedOpenChannels: ChannelOpenInit[] = [];
let updatedOpenChannelFees: OpenChannelFeeDeposited[] = [];

for (let openChannelFee of openChannelFees) {
let openChannel = await ctx.store.findOne(ChannelOpenInit,
{
where: {
chainId: openChannelFee.chainId,
counterpartyPortId: openChannelFee.counterpartyPortId,
portAddress: openChannelFee.sourceAddress,
}
});
if (openChannel) {
openChannel.feesDeposited = [openChannelFee];
openChannelFee.openChannel = openChannel;

updatedOpenChannels.push(openChannel);
updatedOpenChannelFees.push(openChannelFee);
} else {
console.log(`Could not find open channel for open channel fee ${openChannelFee.id}`);
}
}

await ctx.store.upsert(updatedOpenChannels);
await ctx.store.upsert(updatedOpenChannelFees);
}
6 changes: 5 additions & 1 deletion src/model/generated/channelOpenInit.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, IntColumn as IntColumn_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_, Index as Index_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, IntColumn as IntColumn_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_, Index as Index_, OneToMany as OneToMany_} from "@subsquid/typeorm-store"
import {OpenChannelFeeDeposited} from "./openChannelFeeDeposited.model"

@Entity_()
export class ChannelOpenInit {
Expand Down Expand Up @@ -84,4 +85,7 @@ export class ChannelOpenInit {

@BigIntColumn_({nullable: true})
polymerBlockNumber!: bigint | undefined | null

@OneToMany_(() => OpenChannelFeeDeposited, e => e.openChannel)
feesDeposited!: OpenChannelFeeDeposited[]
}
7 changes: 6 additions & 1 deletion src/model/generated/openChannelFeeDeposited.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, IntColumn as IntColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, IntColumn as IntColumn_, BigIntColumn as BigIntColumn_, ManyToOne as ManyToOne_} from "@subsquid/typeorm-store"
import {ChannelOpenInit} from "./channelOpenInit.model"

@Entity_()
export class OpenChannelFeeDeposited {
Expand Down Expand Up @@ -46,4 +47,8 @@ export class OpenChannelFeeDeposited {
@Index_()
@StringColumn_({nullable: false})
from!: string

@Index_()
@ManyToOne_(() => ChannelOpenInit, {nullable: true})
openChannel!: ChannelOpenInit | undefined | null
}
7 changes: 6 additions & 1 deletion src/model/generated/sendPacket.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, ManyToOne as ManyToOne_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, OneToOne as OneToOne_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, ManyToOne as ManyToOne_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, OneToOne as OneToOne_, OneToMany as OneToMany_} from "@subsquid/typeorm-store"
import {Channel} from "./channel.model"
import {Packet} from "./packet.model"
import {SendPacketFeeDeposited} from "./sendPacketFeeDeposited.model"

@Entity_()
export class SendPacket {
Expand Down Expand Up @@ -36,6 +37,7 @@ export class SendPacket {
@StringColumn_({nullable: false})
packet!: string

@Index_()
@BigIntColumn_({nullable: false})
sequence!: bigint

Expand Down Expand Up @@ -92,4 +94,7 @@ export class SendPacket {

@BigIntColumn_({nullable: true})
polymerBlockNumber!: bigint | undefined | null

@OneToMany_(() => SendPacketFeeDeposited, e => e.sendPacket)
feesDeposited!: SendPacketFeeDeposited[]
}
11 changes: 10 additions & 1 deletion src/model/generated/sendPacketFeeDeposited.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, ManyToOne as ManyToOne_} from "@subsquid/typeorm-store"
import {SendPacket} from "./sendPacket.model"

@Entity_()
export class SendPacketFeeDeposited {
Expand All @@ -17,15 +18,19 @@ export class SendPacketFeeDeposited {
@BigIntColumn_({nullable: false})
sequence!: bigint

@Index_()
@BigIntColumn_({nullable: false})
sendGasLimit!: bigint

@Index_()
@BigIntColumn_({nullable: false})
sendGasPrice!: bigint

@Index_()
@BigIntColumn_({nullable: false})
ackGasLimit!: bigint

@Index_()
@BigIntColumn_({nullable: false})
ackGasPrice!: bigint

Expand All @@ -47,4 +52,8 @@ export class SendPacketFeeDeposited {
@Index_()
@StringColumn_({nullable: false})
from!: string

@Index_()
@ManyToOne_(() => SendPacket, {nullable: true})
sendPacket!: SendPacket | undefined | null
}
Loading