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: create job to re decode tx by type #255

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,8 @@
"httpBatchRequest": {
"dispatchMilisecond": 1000,
"batchSizeLimit": 10
},
"jobRedecodeTx": {
"limitRecordGet": 100
}
}
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,8 @@
"httpBatchRequest": {
"dispatchMilisecond": 1000,
"batchSizeLimit": 10
},
"jobRedecodeTx": {
"limitRecordGet": 100
}
}
5 changes: 5 additions & 0 deletions src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const BULL_JOB_NAME = {
JOB_CREATE_EVENT_ATTR_PARTITION: 'job:create-event-attr-partition',
CRAWL_GENESIS_FEEGRANT: 'crawl:genesis-feegrant',
HANDLE_MIGRATE_CONTRACT: 'handle:migrate-contract',
JOB_REDECODE_TX_IBC: 'job:redecode-tx-ibc',
};

export const SERVICE = {
Expand Down Expand Up @@ -203,6 +204,10 @@ export const SERVICE = {
key: 'CreateEventAttrPartition',
path: 'v1.CreateEventAttrPartition',
},
ReDecodeTx: {
key: 'ReDecodeTx',
path: 'v1.ReDecodeTx',
},
},
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/services/crawl-tx/aura.registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export default class AuraRegistry {
'/cosmos.feegrant.v1beta1.AllowedMsgAllowance',
'/cosmos.vesting.v1beta1.MsgCreatePeriodicVestingAccount',

// ibc header
// ibc
'/ibc.lightclients.tendermint.v1.Header',
'/ibc.lightclients.tendermint.v1.ClientState',
'/ibc.lightclients.tendermint.v1.ConsensusState',

// slashing
'/cosmos.slashing.v1beta1.MsgUnjail',
Expand Down
120 changes: 120 additions & 0 deletions src/services/job/update_tx_ibc.service.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

đổi tên cái file này đi, nó dùng chung mà

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable no-await-in-loop */
import { Service } from '@ourparentcenter/moleculer-decorators-extended';
import { ServiceBroker } from 'moleculer';
import { decodeTxRaw } from '@cosmjs/proto-signing';
import { fromBase64 } from '@cosmjs/encoding';
import { BlockCheckpoint, Transaction, TransactionMessage } from '../../models';
import BullableService, { QueueHandler } from '../../base/bullable.service';
import { BULL_JOB_NAME, SERVICE } from '../../common';
import config from '../../../config.json' assert { type: 'json' };
import AuraRegistry from '../crawl-tx/aura.registry';
import Utils from '../../common/utils/utils';
import knex from '../../common/utils/db_connection';

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

async redecodeTxByType(type: string) {
this.logger.info('Job re decode tx ibc');
const registry = new AuraRegistry(this.logger);
let currentId = 0;
await knex.transaction(async (trx) => {
let done = false;
while (!done) {
this.logger.info(`Re decode tx ${type} at current id: ${currentId}`);
const txOnDB = await Transaction.query()
.select('transaction.id', 'transaction.data')
.distinct()
.joinRelated('messages')
.where('transaction.id', '>', currentId)
.andWhere('messages.type', type)
.limit(config.jobRedecodeTx.limitRecordGet);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

như này là nó limit theo số message đấy

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

em distinct đó anh, nên câu này limit theo số tx đó


if (txOnDB.length === 0) {
done = true;
break;
}

const patchQueriesMsg: any[] = [];
const patchQueriesTx = txOnDB.map((tx) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái đoạn này có dùng chung được cái gì đã code ko?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

không có anh ạ

const decodedTxRaw = decodeTxRaw(fromBase64(tx.data.tx_response.tx));
const messages = decodedTxRaw.body.messages.map((msg) =>
Utils.camelizeKeys(registry.decodeMsg(msg))
);

// eslint-disable-next-line no-param-reassign
tx.data.tx.body.messages = messages;
messages.forEach((msg, index: number) => {
patchQueriesMsg.push(
TransactionMessage.query()
.patch({
content: msg,
})
.where({
tx_id: tx.id,
index,
})
);
});
return Transaction.query()
.patch({
data: tx.data,
})
.where({ id: tx.id })
.transacting(trx);
});

await Promise.all(patchQueriesTx);
await Promise.all(patchQueriesMsg);
currentId = txOnDB[txOnDB.length - 1].id;
}
});
}

@QueueHandler({
queueName: BULL_JOB_NAME.JOB_REDECODE_TX_IBC,
jobName: BULL_JOB_NAME.JOB_REDECODE_TX_IBC,
})
async jobRedecodeTxIBC() {
// check if job not done, execute as
const blockCheckpoint = await BlockCheckpoint.query()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái này thì cứ gọi là chạy chứ sao cần checkpoint nhỉ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thêm checkpoint để biết là nó đã chạy ấy anh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nếu muốn biết nó đã chạy thì để ở trong job là được, chứ lưu vào nó lại lẫn với các job khác

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái này đang ở trong job luôn mà anh

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peara em cần cái này ddeert làm ibc luôn ạ

.where({
job_name: BULL_JOB_NAME.JOB_REDECODE_TX_IBC,
})
.findOne({});

if (!blockCheckpoint || blockCheckpoint.height === 0) {
this.logger.info('Job redecode tx ibc start');
await this.redecodeTxByType('/ibc.core.client.v1.MsgCreateClient');
}
// mark as job completed
await BlockCheckpoint.query()
.insert({
job_name: BULL_JOB_NAME.JOB_REDECODE_TX_IBC,
height: 1,
})
.onConflict('job_name')
.merge();
}

async _start(): Promise<void> {
this.createJob(
BULL_JOB_NAME.JOB_REDECODE_TX_IBC,
BULL_JOB_NAME.JOB_REDECODE_TX_IBC,
{},
{
removeOnComplete: true,
removeOnFail: {
count: 3,
},
}
);
return super._start();
}
}
Loading