Skip to content

Commit

Permalink
Refactor/ibc message add tx hash staging (#399)
Browse files Browse the repository at this point in the history
* refactor: cw20/cw721 index and add relation ( main ) (#306)

* refactor: cw20 index and add relation

* refactor: code

* refactor: ibc message tx hash

* refactor: ibc message add tx hash

* refactor: ibc message add tx hash

* refactor: resolve conflict
  • Loading branch information
phamphong9981 authored Oct 5, 2023
1 parent 865b230 commit bf362f4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
27 changes: 27 additions & 0 deletions migrations/20231002080503_ibc_message_add_tx_hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Knex } from 'knex';
import { IbcMessage } from '../src/models';

export async function up(knex: Knex): Promise<void> {
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<void> {
await knex.schema.alterTable('ibc_message', (table) => {
table.dropColumn('tx_hash');
});
}
2 changes: 2 additions & 0 deletions src/models/ibc_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class IbcMessage extends BaseModel {
data!: any;

message!: TransactionMessage;

tx_hash!: string;

static get tableName() {
return 'ibc_message';
Expand Down
10 changes: 8 additions & 2 deletions src/services/ibc/crawl_ibc_app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/services/ibc/crawl_ibc_app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
});
}
Expand Down Expand Up @@ -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()
Expand All @@ -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();
});
}
Expand Down Expand Up @@ -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()
Expand All @@ -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();
});
}
Expand Down Expand Up @@ -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()
Expand All @@ -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();
});
}
Expand Down

0 comments on commit bf362f4

Please sign in to comment.