解决事件问题

This commit is contained in:
aozhiwei 2022-04-22 00:12:46 +08:00
parent f39d425a94
commit 1e624b58ef
3 changed files with 44 additions and 21 deletions

View File

@ -1,3 +1,5 @@
const BC_INITIALIZED_EVENT = 'bc_initialized';
const DESTORY_EXEC_CONFIRM_OWNER_EVENT = 'destory_exec_confirm_owner_event';
exports.BC_INITIALIZED_EVENT = BC_INITIALIZED_EVENT;
exports.DESTORY_EXEC_CONFIRM_OWNER_EVENT = DESTORY_EXEC_CONFIRM_OWNER_EVENT;

View File

@ -2,9 +2,11 @@ const app = require('j7/app');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const log = require('j7/log');
const event = require('j7/event');
const serviceFactory = require('./factory');
const metaFactory = require('../metadata/factory');
const bc = require('../blockchain');
const C = require('../C');
const LIMIT_COUNT = 100;
@ -20,6 +22,17 @@ class EventCenter {
}
this.conn = conn;
event.addListener(C.DESTORY_EXEC_CONFIRM_OWNER_EVENT, (tokenId) => {
log.info('destory:' + tokenId);
try {
if (utils.getVal(this.pendingConfirmOwnerHash, tokenId)) {
delete this.pendingConfirmOwnerHash[tokenId];
}
} catch(err) {
log.error('destory confirm owner:' + err);
}
});
this.confirmOwner();
this.pullNftTransferEvent();
this.pullBoxOpenedEvent();
@ -55,6 +68,7 @@ class EventCenter {
}
if (rows.length < 1 && this.lastIdx + LIMIT_COUNT < maxIdx) {
this.lastIdx += LIMIT_COUNT;
continue;
}
}
}

View File

@ -2,13 +2,15 @@ const bcutils = require('j7/bcutils');
const utils = require('j7/utils');
const app = require('j7/app');
const log = require('j7/log');
const event = require('j7/event');
const bc = require('../blockchain');
const C = require('../C');
class ExecConfirmOwner {
async init(tokenId) {
try {
this.tryCount = 0;
this.tryCount = 1;
this.tokenId = tokenId;
this.nftDb = await this.getNftDb(tokenId);
if (!this.nftDb) {
@ -24,7 +26,7 @@ class ExecConfirmOwner {
}
destory() {
event.emitEvent(C.DESTORY_EXEC_CONFIRM_OWNER_EVENT, this.tokenId);
}
addTryCount() {
@ -33,16 +35,18 @@ class ExecConfirmOwner {
async doConfirm() {
while (true) {
const oldTryCount = this.tryCount;
await bc.mustBeActive();
const oldTryCount = this.tryCount;
const oldBlockNumber = bc.getCurrBlockNumber();
let oldOwner = await this.getOwner();
while (oldBlockNumber + 8 < bc.getCurrBlockNumber()) {
await utils.sleep(1000 + utils.randRange(0, 500));
}
await bc.mustBeActive();
const newBlockNumber = bc.getCurrBlockNumber();
let newOwner = await this.getOwner();
if (oldOwner == newOwner) {
await this.updateConfirmed(newOwner, oldBlockNumber);
if (oldTryCount == this.tryCount) {
@ -81,7 +85,7 @@ class ExecConfirmOwner {
async getNftDb(tokenId) {
const {err, conn} = await app.getDbConn('MarketDb0');
if (err) {
throw 'db error:' + err;
return null;
}
try {
const {err, row} = await conn.ormSelectOne(
@ -89,9 +93,6 @@ class ExecConfirmOwner {
[
['token_id', tokenId]
]);
if (err) {
throw 'db error:' + err;
}
return row;
} finally {
conn.release()
@ -100,7 +101,7 @@ class ExecConfirmOwner {
async getOwner() {
const instanceName = this.getInstanceName();
//log.info('getOwner:' + instanceName + ' tryCount:' + this.tryCount);
log.info('getOwner:' + instanceName + ' tryCount:' + this.tryCount);
while (true) {
await bc.lockQuery();
try {
@ -111,7 +112,7 @@ class ExecConfirmOwner {
if (reason == 'ERC721: owner query for nonexistent token') {
return '';
}
console.log(err);
log.error(err);
} finally {
await bc.unlockQuery();
}
@ -122,20 +123,26 @@ class ExecConfirmOwner {
async updateConfirmed(newOwner, blockNumber) {
const {err, conn} = await app.getDbConn('MarketDb0');
if (err) {
throw 'db error:' + err;
return;
}
try {
const {err} = await conn.update(
't_nft',
[
['token_id', this.tokenId]
],
[
['owner_address', bcutils.toNormalAddress(newOwner)],
['confirm_block_number', blockNumber]
]);
if (err) {
throw 'db error:' + err;
{
const {err} = await conn.update(
't_nft',
[
['token_id', this.tokenId]
],
[
['owner_address', bcutils.toNormalAddress(newOwner)],
['confirm_block_number', blockNumber]
]);
}
{
await conn.execScript(
'UPDATE t_nft_transfer SET `owner_confirmed` = 1 WHERE block_number < ?',
[
blockNumber
]);
}
} finally {
conn.release()