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

Feat/ibc app ( develop ) #324

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
45fa9b4
docs: cw721
phamphong9981 Jul 5, 2023
b27aa4c
docs: cw20
phamphong9981 Jul 7, 2023
ccc8fbd
Merge branch 'main' into docs/cw721
fibonacci998 Jul 10, 2023
0fa8225
Merge branch 'main' into docs/cw20
fibonacci998 Jul 10, 2023
5e50079
fix: update validator as UNSPECIFIED when not found onchain
fibonacci998 Jul 31, 2023
147fb9b
fix: remove token and delegator_shares when validator is UNRECOGNIZED
fibonacci998 Aug 1, 2023
dde6426
feat: crawl ibc tao
phamphong9981 Aug 2, 2023
6fd8005
feat: add test for unrecognized validator
fibonacci998 Aug 3, 2023
6db3990
feat: crawl genesis ibc tao
phamphong9981 Aug 4, 2023
d8eb803
feat: add migration to add index time to block, tx
fibonacci998 Aug 7, 2023
928e7b9
fix: code
phamphong9981 Aug 7, 2023
f38720d
Merge pull request #289 from aura-nw/feat/ibc_tao_genesis
phamphong9981 Aug 7, 2023
454a38a
fix: code
phamphong9981 Aug 8, 2023
49ad0b2
Merge pull request #293 from aura-nw/feat/add-index-by-time-to-block-tx
peara Aug 9, 2023
a901780
Merge pull request #276 from aura-nw/fix/update-validator-not-found
peara Aug 9, 2023
49bcd7c
Merge pull request #249 from aura-nw/docs/cw20
peara Aug 9, 2023
6924407
Merge pull request #243 from aura-nw/docs/cw721
peara Aug 9, 2023
2f4cc12
feat: ibc app
phamphong9981 Aug 11, 2023
fc2478d
feat: ibc app
phamphong9981 Aug 11, 2023
f1344e0
feat: ibc app
phamphong9981 Aug 11, 2023
89c4bd0
feat: ibc app
phamphong9981 Aug 11, 2023
32d103a
fix: allow whitelist query can have depth more than config (#312)
fibonacci998 Aug 15, 2023
685bb4b
feat: reindex cw20 service ( main ) (#277)
phamphong9981 Aug 16, 2023
92795eb
refactor: cw20/cw721 index and add relation ( main ) (#306)
phamphong9981 Aug 16, 2023
b6b007c
fix: duplicate latest migrate contract (#304)
phamphong9981 Aug 16, 2023
3640cff
test: ibc app
phamphong9981 Aug 16, 2023
4344974
Merge branch 'main' of github.com:aura-nw/horoscope-v2 into feat/ibc_app
phamphong9981 Aug 16, 2023
0ab648d
Merge branch 'feat/ibc_app' of github.com:aura-nw/horoscope-v2 into f…
phamphong9981 Aug 16, 2023
b153205
fix: code
phamphong9981 Aug 16, 2023
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
5 changes: 5 additions & 0 deletions ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,10 @@
"key": "crawlIbcTao",
"millisecondRepeatJob": 2000,
"blocksPerCall": 100
},
"crawlIbcApp": {
"key": "crawlIbcApp",
"millisecondRepeatJob": 2000,
"blocksPerCall": 100
}
}
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,10 @@
"key": "crawlIbcTao",
"millisecondRepeatJob": 2000,
"blocksPerCall": 100
},
"crawlIbcApp": {
"key": "crawlIbcApp",
"millisecondRepeatJob": 2000,
"blocksPerCall": 100
}
}
23 changes: 23 additions & 0 deletions migrations/20230810094108_create_ibc_tx_model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable('ibc_message', (table) => {
table.increments();
table.integer('transaction_message_id').index();
table.string('src_channel_id').notNullable().index();
table.string('src_port_id').notNullable().index();
table.string('dst_channel_id').notNullable().index();
table.string('dst_port_id').notNullable().index();
table.string('type').notNullable().index();
table.integer('sequence').notNullable().index();
table.string('sequence_key').notNullable().index();
table.jsonb('data');
table
.foreign('transaction_message_id')
.references('transaction_message.id');
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTableIfExists('ibc_message');
}
5 changes: 5 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const BULL_JOB_NAME = {
CRAWL_GENESIS_IBC_TAO: 'crawl:genesis-ibc-tao',
REINDEX_CW20_CONTRACT: 'reindex:cw20-contract',
REINDEX_CW20_HISTORY: 'reindex:cw20-history',
CRAWL_IBC_APP: 'crawl:ibc-app',
};

export const SERVICE = {
Expand Down Expand Up @@ -264,6 +265,10 @@ export const SERVICE = {
path: 'v1.Cw20ReindexingService.reindexing',
},
},
CrawlIBCAppService: {
key: 'CrawlIBCAppService',
name: 'v1.CrawlIBCAppService',
},
},
};

Expand Down
2 changes: 2 additions & 0 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class Event extends BaseModel {

source!: string;

attributes!: EventAttribute[];

static get tableName() {
return 'event';
}
Expand Down
6 changes: 6 additions & 0 deletions src/models/event_attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ export class EventAttribute extends BaseModel {
COUNTERPARTY_PORT_ID: 'counterparty_port_id',
COUNTERPARTY_CHANNEL_ID: 'counterparty_channel_id',
MINTER: 'minter',
DATA_HEX: 'packet_data_hex',
SEQUENCE: 'packet_sequence',
SRC_PORT: 'packet_src_port',
SRC_CHANNEL: 'packet_src_channel',
DST_PORT: 'packet_dst_port',
DST_CHANNEL: 'packet_dst_channel',
};

static ATTRIBUTE_COMPOSITE_KEY = {
Expand Down
96 changes: 96 additions & 0 deletions src/models/ibc_message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable import/no-cycle */
import { Model } from 'objection';
import BaseModel from './base';
import { IbcChannel } from './ibc_channel';
import { TransactionMessage } from './transaction_message';

export class IbcMessage extends BaseModel {
[relation: string]: any | any[];

id!: number;

transaction_message_id!: number;

src_channel_id!: string;

src_port_id!: string;

dst_channel_id!: string;

dst_port_id!: string;

type!: string;

sequence!: number;

sequence_key!: string;

data!: any;

static get tableName() {
return 'ibc_message';
}

static get jsonSchema() {
return {
type: 'object',
required: [
'transaction_message_id',
'src_channel_id',
'src_port_id',
'dst_channel_id',
'dst_port_id',
'type',
'sequence',
'sequence_key',
],
properties: {
transaction_message_id: { type: 'number' },
src_channel_id: { type: 'string' },
src_port_id: { type: 'string' },
dst_channel_id: { type: 'string' },
dst_port_id: { type: 'string' },
type: { type: 'string' },
sequence: { type: 'number' },
sequence_key: { type: 'string' },
status: { type: 'boolean' },
},
};
}

static get relationMappings() {
return {
message: {
relation: Model.BelongsToOneRelation,
modelClass: TransactionMessage,
join: {
from: 'ibc_message.transaction_message_id',
to: 'transaction_message.id',
},
},
src_channel: {
relation: Model.BelongsToOneRelation,
modelClass: IbcChannel,
join: {
from: 'ibc_message.src_channel_id',
to: 'ibc_channel.id',
},
},
dst_channel: {
relation: Model.BelongsToOneRelation,
modelClass: IbcChannel,
join: {
from: 'ibc_message.dst_channel_id',
to: 'ibc_channel.id',
},
},
};
}

static EVENT_TYPE = {
SEND_PACKET: 'send_packet',
RECV_PACKET: 'recv_packet',
ACKNOWLEDGE_PACKET: 'acknowledge_packet',
TIMEOUT_PACKET: 'timeout_packet',
};
}
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from './cw20_total_holder_stats';
export * from './ibc_client';
export * from './ibc_connection';
export * from './ibc_channel';
export * from './ibc_message';
6 changes: 6 additions & 0 deletions src/services/api-gateways/api_gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ import { bullBoardMixin } from '../../mixins/bullBoard/bullBoard.mixin';
mappingPolicy: 'restrict', // allow action called with exact method
whitelist: ['v1.cw20-admin.*'],
},
{
path: '/admin',
autoAliases: true, // allow generate rest info (GET/PUT/POST...) in the services
mappingPolicy: 'restrict', // allow action called with exact method
whitelist: ['v1.cw20-admin.*'],
},
],
// empty cors object will have moleculer to generate handler for preflight request and CORS header which allow all origin
cors: {},
Expand Down
125 changes: 125 additions & 0 deletions src/services/ibc/crawl_ibc_app.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { fromHex, fromUtf8 } from '@cosmjs/encoding';
import { Service } from '@ourparentcenter/moleculer-decorators-extended';
import { Knex } from 'knex';
import { ServiceBroker } from 'moleculer';
import knex from '../../common/utils/db_connection';
import config from '../../../config.json' assert { type: 'json' };
import BullableService, { QueueHandler } from '../../base/bullable.service';
import { BULL_JOB_NAME, SERVICE } from '../../common';
import { getAttributeFrom } from '../../common/utils/smart_contract';
import {
BlockCheckpoint,
Event,
EventAttribute,
IbcMessage,
} from '../../models';

@Service({
name: SERVICE.V1.CrawlIBCAppService.key,
version: 1,
})
export default class CrawlIbcAppService extends BullableService {
public constructor(public broker: ServiceBroker) {
super(broker);
}

@QueueHandler({
queueName: BULL_JOB_NAME.CRAWL_IBC_APP,
jobName: BULL_JOB_NAME.CRAWL_IBC_APP,
})
public async crawlIbcApp(): Promise<void> {
const [startHeight, endHeight, updateBlockCheckpoint] =
await BlockCheckpoint.getCheckpoint(
BULL_JOB_NAME.CRAWL_IBC_APP,
[BULL_JOB_NAME.CRAWL_IBC_TAO],
config.crawlIbcTao.key
);
this.logger.info(
`Handle IBC/APP, startHeight: ${startHeight}, endHeight: ${endHeight}`
);
if (startHeight > endHeight) return;
const events = await Event.query()
.withGraphFetched('attributes')
.joinRelated('message')
.select('event.id', 'event.type', 'message.id as message_id')
.whereIn('event.type', [
IbcMessage.EVENT_TYPE.ACKNOWLEDGE_PACKET,
IbcMessage.EVENT_TYPE.RECV_PACKET,
IbcMessage.EVENT_TYPE.SEND_PACKET,
IbcMessage.EVENT_TYPE.TIMEOUT_PACKET,
])
.andWhere('event.block_height', '>', startHeight)
.andWhere('event.block_height', '<=', endHeight)
.orderBy('event.id');
await knex.transaction(async (trx) => {
await this.handleIbcMessage(events, trx);
updateBlockCheckpoint.height = endHeight;
await BlockCheckpoint.query()
.transacting(trx)
.insert(updateBlockCheckpoint)
.onConflict('job_name')
.merge();
});
}

async handleIbcMessage(events: Event[], trx: Knex.Transaction) {
const ibcMessage = events.map((event) => {
const srcChannel = getAttributeFrom(
event.attributes,
EventAttribute.ATTRIBUTE_KEY.SRC_CHANNEL
);
const srcPort = getAttributeFrom(
event.attributes,
EventAttribute.ATTRIBUTE_KEY.SRC_PORT
);
const dstChannel = getAttributeFrom(
event.attributes,
EventAttribute.ATTRIBUTE_KEY.DST_CHANNEL
);
const dstPort = getAttributeFrom(
event.attributes,
EventAttribute.ATTRIBUTE_KEY.DST_PORT
);
const sequence = getAttributeFrom(
event.attributes,
EventAttribute.ATTRIBUTE_KEY.SEQUENCE
);
const dataHex = getAttributeFrom(
event.attributes,
EventAttribute.ATTRIBUTE_KEY.DATA_HEX
);
return IbcMessage.fromJson({
transaction_message_id: event.message_id,
src_channel_id: srcChannel,
src_port_id: srcPort,
dst_channel_id: dstChannel,
dst_port_id: dstPort,
type: event.type,
sequence,
sequence_key: `${srcChannel}.${srcPort}.${dstChannel}.${dstPort}.${sequence}`,
data: dataHex ? fromUtf8(fromHex(dataHex)) : null,
});
});
if (ibcMessage.length > 0) {
await IbcMessage.query().insert(ibcMessage).transacting(trx);
}
}

async _start(): Promise<void> {
await this.createJob(
BULL_JOB_NAME.CRAWL_IBC_APP,
BULL_JOB_NAME.CRAWL_IBC_APP,
{},
{
removeOnComplete: true,
removeOnFail: {
count: 3,
},
repeat: {
every: config.crawlIbcApp.millisecondRepeatJob,
},
}
);
return super._start();
}
}
Loading
Loading