diff --git a/migrations/20231002080503_ibc_message_add_tx_hash.ts b/migrations/20231002080503_ibc_message_add_tx_hash.ts new file mode 100644 index 000000000..afc7c4d2c --- /dev/null +++ b/migrations/20231002080503_ibc_message_add_tx_hash.ts @@ -0,0 +1,27 @@ +import { Knex } from 'knex'; +import { IbcMessage } from '../src/models'; + +export async function up(knex: Knex): Promise { + await knex.schema.alterTable('ibc_message', (table) => { + table.string('tx_hash').defaultTo(''); + }); + await knex.transaction(async (trx) => { + const ibcMsgs = await IbcMessage.query() + .transacting(trx) + .joinRelated('message.transaction') + .select('ibc_message.*', 'message:transaction.hash as tx_hash'); + if (ibcMsgs.length > 0) { + await IbcMessage.query() + .transacting(trx) + .insert(ibcMsgs) + .onConflict(['id']) + .merge(); + } + }); +} + +export async function down(knex: Knex): Promise { + await knex.schema.alterTable('ibc_message', (table) => { + table.dropColumn('tx_hash'); + }); +} diff --git a/src/models/ibc_message.ts b/src/models/ibc_message.ts index 5ea5d8f99..fb960d4c9 100644 --- a/src/models/ibc_message.ts +++ b/src/models/ibc_message.ts @@ -30,6 +30,8 @@ export class IbcMessage extends BaseModel { message!: TransactionMessage; + tx_hash!: string; + static get tableName() { return 'ibc_message'; } diff --git a/src/services/ibc/crawl_ibc_app.service.ts b/src/services/ibc/crawl_ibc_app.service.ts index 7ae993bf3..e21979aa5 100644 --- a/src/services/ibc/crawl_ibc_app.service.ts +++ b/src/services/ibc/crawl_ibc_app.service.ts @@ -40,8 +40,13 @@ export default class CrawlIbcAppService extends BullableService { if (startHeight > endHeight) return; const events = await Event.query() .withGraphFetched('attributes') - .joinRelated('message') - .select('event.id', 'event.type', 'message.id as message_id') + .joinRelated('message.transaction') + .select( + 'event.id', + 'event.type', + 'message.id as message_id', + 'message:transaction.hash as tx_hash' + ) .whereIn('event.type', [ IbcMessage.EVENT_TYPE.ACKNOWLEDGE_PACKET, IbcMessage.EVENT_TYPE.RECV_PACKET, @@ -98,6 +103,7 @@ export default class CrawlIbcAppService extends BullableService { sequence, sequence_key: `${srcChannel}.${srcPort}.${dstChannel}.${dstPort}.${sequence}`, data: dataHex ? fromUtf8(fromHex(dataHex)) : null, + tx_hash: event.tx_hash, }); }); if (ibcMessages.length > 0) { diff --git a/test/unit/services/ibc/crawl_ibc_app.spec.ts b/test/unit/services/ibc/crawl_ibc_app.spec.ts index f1776e01d..c1f209720 100644 --- a/test/unit/services/ibc/crawl_ibc_app.spec.ts +++ b/test/unit/services/ibc/crawl_ibc_app.spec.ts @@ -151,6 +151,7 @@ export default class CrawlIbcTest { attributes, content, message_id: 1, + tx_hash: this.transaction.hash, }); await this.crawlIbcAppSerivce.handleIbcMessage([event], trx); const newPacket = await IbcMessage.query() @@ -164,6 +165,7 @@ export default class CrawlIbcTest { expect(newPacket.type).toEqual(event.type); expect(newPacket.sequence).toEqual(parseInt(attributes[4].value, 10)); expect(newPacket.data).toEqual(JSON.parse(attributes[0].value)); + expect(newPacket.tx_hash).toEqual(this.transaction.hash); await trx.rollback(); }); } @@ -258,6 +260,7 @@ export default class CrawlIbcTest { attributes, content, message_id: 1, + tx_hash: this.transaction.hash, }); await this.crawlIbcAppSerivce.handleIbcMessage([event], trx); const newPacket = await IbcMessage.query() @@ -271,6 +274,7 @@ export default class CrawlIbcTest { expect(newPacket.type).toEqual(event.type); expect(newPacket.sequence).toEqual(parseInt(attributes[4].value, 10)); expect(newPacket.data).toEqual(JSON.parse(attributes[0].value)); + expect(newPacket.tx_hash).toEqual(this.transaction.hash); await trx.rollback(); }); } @@ -362,6 +366,7 @@ export default class CrawlIbcTest { attributes, content, message_id: 1, + tx_hash: this.transaction.hash, }); await this.crawlIbcAppSerivce.handleIbcMessage([event], trx); const newPacket = await IbcMessage.query() @@ -375,6 +380,7 @@ export default class CrawlIbcTest { expect(newPacket.type).toEqual(event.type); expect(newPacket.sequence).toEqual(parseInt(attributes[2].value, 10)); expect(newPacket.data).toEqual(null); + expect(newPacket.tx_hash).toEqual(this.transaction.hash); await trx.rollback(); }); } @@ -453,6 +459,7 @@ export default class CrawlIbcTest { attributes, content, message_id: 1, + tx_hash: this.transaction.hash, }); await this.crawlIbcAppSerivce.handleIbcMessage([event], trx); const newPacket = await IbcMessage.query() @@ -466,6 +473,7 @@ export default class CrawlIbcTest { expect(newPacket.type).toEqual(event.type); expect(newPacket.sequence).toEqual(parseInt(attributes[2].value, 10)); expect(newPacket.data).toEqual(null); + expect(newPacket.tx_hash).toEqual(this.transaction.hash); await trx.rollback(); }); }