-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: update sender in transaction_message (main) #419
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f2d9b6
fix: update sender in transaction_message; create job to migrate old …
fibonacci998 c777cda
fix: use 1 raw query update multiple row transaction_message
fibonacci998 5b048d0
fix: refactor 2 for-loop in findFirstAttribute function
fibonacci998 c63d299
refactor: update name job (add .service)
fibonacci998 d3143c7
fix: fix conflict merge main
fibonacci998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/services/job/update_sender_in_tx_message.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
import { Service } from '@ourparentcenter/moleculer-decorators-extended'; | ||
import { ServiceBroker } from 'moleculer'; | ||
import { Transaction, BlockCheckpoint } 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 knex from '../../common/utils/db_connection'; | ||
|
||
@Service({ | ||
name: SERVICE.V1.JobService.UpdateSenderInTxMessages.key, | ||
version: 1, | ||
}) | ||
export default class UpdateSenderInTxMessages extends BullableService { | ||
public constructor(public broker: ServiceBroker) { | ||
super(broker); | ||
} | ||
|
||
@QueueHandler({ | ||
queueName: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
jobName: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
}) | ||
async updateSender(_payload: { lastBlockCrawled: number }) { | ||
const blockCheckpoint = await BlockCheckpoint.query().findOne({ | ||
job_name: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
}); | ||
this.logger.info( | ||
`Update sender in transaction_message table start from block ${blockCheckpoint?.height}` | ||
); | ||
if (blockCheckpoint?.height === _payload.lastBlockCrawled) { | ||
return; | ||
} | ||
|
||
let lastBlock = | ||
(blockCheckpoint?.height ?? 0) + | ||
config.jobUpdateSenderInTxMessages.blocksPerCall; | ||
if (lastBlock > _payload.lastBlockCrawled) { | ||
lastBlock = _payload.lastBlockCrawled; | ||
} | ||
const listTx = await Transaction.query() | ||
.withGraphFetched('events.[attributes]') | ||
.modifyGraph('events', (builder) => { | ||
builder.orderBy('id', 'asc'); | ||
}) | ||
.modifyGraph('events.[attributes]', (builder) => { | ||
builder.orderBy('index', 'asc'); | ||
}) | ||
.modifyGraph('messages', (builder) => { | ||
builder.orderBy('id', 'asc'); | ||
}) | ||
.orderBy('id', 'asc') | ||
.where('height', '>=', blockCheckpoint?.height ?? 0) | ||
.andWhere('height', '<', lastBlock); | ||
const listUpdates = listTx.map((tx) => { | ||
try { | ||
const sender = this._findFirstAttribute(tx.events, 'message', 'sender'); | ||
return { | ||
tx_id: tx.id, | ||
sender, | ||
}; | ||
} catch (error) { | ||
this.logger.warn('Tx error not has message.sender: ', tx.hash); | ||
return { | ||
tx_id: tx.id, | ||
sender: '', | ||
}; | ||
} | ||
}); | ||
|
||
await knex.transaction(async (trx) => { | ||
if (listUpdates.length > 0) { | ||
const stringListUpdates = listUpdates | ||
.map((update) => `(${update.tx_id}, '${update.sender}')`) | ||
.join(','); | ||
|
||
await knex | ||
.raw( | ||
`UPDATE transaction_message SET sender = temp.sender from (VALUES ${stringListUpdates}) as temp(tx_id, sender) where temp.tx_id = transaction_message.tx_id` | ||
) | ||
.transacting(trx); | ||
} | ||
await BlockCheckpoint.query() | ||
.update( | ||
BlockCheckpoint.fromJson({ | ||
job_name: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
height: lastBlock, | ||
}) | ||
) | ||
.where({ | ||
job_name: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
}) | ||
.transacting(trx); | ||
}); | ||
} | ||
|
||
private _findFirstAttribute( | ||
events: any, | ||
eventType: string, | ||
attributeKey: string | ||
): string { | ||
let result = ''; | ||
const foundEvent = events.find( | ||
(event: any) => | ||
event.type === eventType && | ||
event.attributes.some( | ||
(attribute: any) => attribute.key === attributeKey | ||
) | ||
); | ||
if (foundEvent) { | ||
const foundAttribute = foundEvent.attributes.find( | ||
(attribute: any) => attribute.key === attributeKey | ||
); | ||
result = foundAttribute.value; | ||
} | ||
if (!result.length) { | ||
throw new Error( | ||
`Could not find attribute ${attributeKey} in event type ${eventType}` | ||
); | ||
} | ||
return result; | ||
} | ||
|
||
async _start(): Promise<void> { | ||
const blockCheckpoint = await BlockCheckpoint.query().findOne({ | ||
job_name: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
}); | ||
if (!blockCheckpoint) { | ||
await BlockCheckpoint.query().insert({ | ||
job_name: BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
height: config.crawlBlock.startBlock, | ||
}); | ||
const crawlBlockCheckpoint = await BlockCheckpoint.query().findOne({ | ||
job_name: BULL_JOB_NAME.CRAWL_BLOCK, | ||
}); | ||
|
||
this.createJob( | ||
BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
BULL_JOB_NAME.JOB_UPDATE_SENDER_IN_TX_MESSAGES, | ||
{ | ||
lastBlockCrawled: crawlBlockCheckpoint?.height ?? 0, | ||
}, | ||
{ | ||
removeOnComplete: true, | ||
removeOnFail: { | ||
count: 3, | ||
}, | ||
repeat: { | ||
every: config.jobUpdateSenderInTxMessages.millisecondCrawl, | ||
}, | ||
} | ||
); | ||
} | ||
return super._start(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, nhẽ ra thì for thường xong rồi return luôn cũng được đúng ko? cơ mà để sau cũng được, tách hẳn cái hàm này cho vào ultility hay helper gì đó