Skip to content

Commit

Permalink
fix: update msg_index in 1 query per tx for each value
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Aug 31, 2023
1 parent 6a7f49f commit dc2409c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Event extends BaseModel {

tx_id!: number;

tx_msg_index: number | undefined;
tx_msg_index?: number | undefined;

type!: string;

Expand All @@ -32,7 +32,6 @@ export class Event extends BaseModel {
required: ['type'],
properties: {
tx_id: { type: 'number' },
tx_msg_index: { type: 'number' },
type: { type: 'string' },
block_height: { type: 'number' },
source: { type: 'string', enum: Object.values(this.SOURCE) },
Expand Down
35 changes: 24 additions & 11 deletions src/services/job/reassign_msg_index_to_event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { Service } from '@ourparentcenter/moleculer-decorators-extended';
import { ServiceBroker } from 'moleculer';
import { fromBase64, fromUtf8 } from '@cosmjs/encoding';
import { Knex } from 'knex';
import { Transaction, EventAttribute, BlockCheckpoint } from '../../models';
import {
Transaction,
EventAttribute,
BlockCheckpoint,
Event,
} from '../../models';
import BullableService, { QueueHandler } from '../../base/bullable.service';
import { BULL_JOB_NAME, SERVICE } from '../../common';
import config from '../../../config.json' assert { type: 'json' };
Expand Down Expand Up @@ -60,6 +65,7 @@ export default class JobReAssignMsgIndexToEvent extends BullableService {

const eventPatches: any[] = [];
const txPatches: any[] = [];
this.logger.info(`Re assign msg index ${listTx.length} txs`);
listTx.forEach((tx: any) => {
const { eventPatchInTx, txPatchInTx } = this.generateListUpdateMsgIndex(
trx,
Expand Down Expand Up @@ -108,7 +114,7 @@ export default class JobReAssignMsgIndexToEvent extends BullableService {
.where('id', tx.id)
.transacting(trx)
);

const mapUpdateEvent = new Map();
tx.events.forEach((event: any, index: number) => {
const rawEvents = rawData.tx_response.events;
// check if event in raw is the same as event in db
Expand All @@ -127,20 +133,27 @@ export default class JobReAssignMsgIndexToEvent extends BullableService {
if (!checkIndex) {
throw new Error('order attribute is wrong');
} else {
eventPatchInTx.push(
trx.raw(
'UPDATE EVENT SET tx_msg_index = :tx_msg_index WHERE id = :id',
{
tx_msg_index: rawEvents[index].msg_index ?? null,
id: event.id,
}
)
);
const msgIndex = rawEvents[index].msg_index ?? null;
if (mapUpdateEvent.has(msgIndex)) {
mapUpdateEvent.get(msgIndex).push(event.id);
} else {
mapUpdateEvent.set(msgIndex, [event.id]);
}
}
} else {
throw new Error(`order event is wrong, ${event.id}, ${index}`);
}
});
mapUpdateEvent.forEach((value: number[], key: null | number) => {
eventPatchInTx.push(
Event.query()
.patch({
tx_msg_index: key,
})
.whereIn('id', value)
.transacting(trx)
);
});
return {
eventPatchInTx,
txPatchInTx,
Expand Down

0 comments on commit dc2409c

Please sign in to comment.