From 61ec9ca79298f58d4b9ed07d799711fcfddbdca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20Th=C3=A0nh=20Phong?= <49814372+phamphong9981@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:25:58 +0700 Subject: [PATCH] Refactor/ibc message add tx hash ( develop ) (#393) * docs: cw721 * docs: cw20 * fix: update validator as UNSPECIFIED when not found onchain * fix: remove token and delegator_shares when validator is UNRECOGNIZED * feat: add test for unrecognized validator * feat: add migration to add index time to block, tx * fix: allow whitelist query can have depth more than config (#312) * feat: reindex cw20 service ( main ) (#277) * feat: reindex cw20 service * test: test cw20 reindexing * fix: idiot * refactor: code * feat: api admin * refactor: cw20/cw721 index and add relation ( main ) (#306) * refactor: cw20 index and add relation * refactor: code * fix: duplicate latest migrate contract (#304) * Feat/statistics (#272) * feat: move statistic feature from v1 to v2 (not tested yet) * feat: change job params so it can aggregate data at a specific date * fix: query fields from exact table * fix: query exact fields * feat: add logger when done * feat: finish statistic jobs (not tested) * feat: create a separate interval job that gets the current date and create statistics jobs with date * feat: update logic using dayjs and lodash lib * feat: update cron jobs logic, move api actions to its service folder * fix: query event from db and group it based on event_id and tx_id * fix: just use a single job to query all daily data * fix: move all queries in daily_stats job into a single Promise all * fix: move dayjs.extend to after import statements * fix: remove _start * fix: only count native token in account_stats job * feat: add api to sync stats from prev dates * fix: support case when user just pass startDate to api * fix: fix whitelist graphql * fix: update column in account_statistic table, update bignum top_tx_sent * fix: remove drop index in modify account statistic table --------- Co-authored-by: AnDQK Co-authored-by: Phan Anh Tuan * refactor: migrate cw721 activity missing smart contract event ( main ) (#322) * refactor: migrate cw721 activity missing smart contract event * feat: api * test: test params * test: test params * test: test params * test: test params * test: test params * test: test params * refactor: code * refactor: code * refactor: code * refactor: code * refactor: code * fix: ci * Fix/account stat with feegrant (#337) * fix: get use_feegrant_grantee if has, other get tx_fee_payer * fix: remove code not used * Update api_gateway.service.ts (#336) * fix: cw721 activity missing from/to ( main ) (#294) * fix: cw721 activity missing from/to * fix: fill from/to cw721 activity * fix: fill from/to cw721 activity * fix: fill from/to cw721 activity * fix: fill from/to cw721 activity * refactor: code * fix: code * refactor: code --------- Co-authored-by: Vu Ngoc Quang * feat: crawl ibc tao ( main ) (#278) * feat: crawl ibc tao * feat: crawl genesis ibc tao * fix: code * fix: code * Fix/lint json file (#359) * fix: display json on multiple lines * fix: update launch.json to debug jest in 1 file; use moleculer.config.ts when debug service * Feat/ibc app ( main ) (#321) * feat: crawl ibc tao * feat: crawl genesis ibc tao * fix: code * fix: code * feat: ibc app * feat: ibc app * test: ibc app * fix: review * fix: smart contract missing label ( main ) (#366) * fix: smart contract missing label * fix: test * fix: test * fix: test * fix: test * fix: update msg_index by order in event and log (#327) * fix: update msg_index by order in event and log * feat: add test mapping event and log * feat: add test mapping by authz tx * feat: add test mapping by authz tx * fix: refactor mapping event to log, update unit test * fix: comment set index msg with order, use count attribute to compare * feat: add checkMappingEventToLog after mapped event * fix: check map event log by flatten array * fix: update checking map event log * fix: add null value handle in checkMappingEventToLog * feat: init job re assign msg_index to event * fix: use transaction when update event * fix: use forEach loop in job * fix: sort graph when query db * feat: add unittest for generateListUpdateMsgIndex function * fix: update msg_index in 1 query per tx for each value * fix: fix null when compare value attribute value * fix: fix if tx fail, then no need set msg index * feat: health check job ( main ) (#345) * feat: health check job * feat: validator * refactor: code * refactor: performance cw721 media info ( main ) (#369) * refactor: cw721 media info * refactor: code * refactor: code * fix: fix lint and remove resilient, protobuf, upgrade graphql (#380) * fix: fix lint and remove resilient, protobuf, upgrade graphql * fix: add package-lock.json * Feat/create composite index in partition attribute (#368) * feat: create job to create composite index in event_attribute table * feat: update job create partition with parameter template table * feat: change to create job for each partition * feat: add api to trigger call create composite index * fix: remove createJob when start service create index composite attribute * fix: remove import which not used * feat: update view use new composite index * fix: fix conflict merge main * refactor: ibc message tx hash * refactor: ibc message add tx hash * refactor: ibc message tx hash * refactor: ibc message add tx hash * refactor: ibc message add tx hash --------- Co-authored-by: Tuan Phan Anh <38557844+fibonacci998@users.noreply.github.com> Co-authored-by: Phan Anh Tuan Co-authored-by: Vu Ngoc Quang Co-authored-by: AnDQK --- .../20231002080503_ibc_message_add_tx_hash.ts | 27 +++++++++++++++++++ src/models/ibc_message.ts | 2 ++ src/services/ibc/crawl_ibc_app.service.ts | 10 +++++-- test/unit/services/ibc/crawl_ibc_app.spec.ts | 8 ++++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 migrations/20231002080503_ibc_message_add_tx_hash.ts 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(); }); }