解决事件问题

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 BC_INITIALIZED_EVENT = 'bc_initialized';
const DESTORY_EXEC_CONFIRM_OWNER_EVENT = 'destory_exec_confirm_owner_event';
exports.BC_INITIALIZED_EVENT = BC_INITIALIZED_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 utils = require('j7/utils');
const bcutils = require('j7/bcutils'); const bcutils = require('j7/bcutils');
const log = require('j7/log'); const log = require('j7/log');
const event = require('j7/event');
const serviceFactory = require('./factory'); const serviceFactory = require('./factory');
const metaFactory = require('../metadata/factory'); const metaFactory = require('../metadata/factory');
const bc = require('../blockchain'); const bc = require('../blockchain');
const C = require('../C');
const LIMIT_COUNT = 100; const LIMIT_COUNT = 100;
@ -20,6 +22,17 @@ class EventCenter {
} }
this.conn = conn; 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.confirmOwner();
this.pullNftTransferEvent(); this.pullNftTransferEvent();
this.pullBoxOpenedEvent(); this.pullBoxOpenedEvent();
@ -55,6 +68,7 @@ class EventCenter {
} }
if (rows.length < 1 && this.lastIdx + LIMIT_COUNT < maxIdx) { if (rows.length < 1 && this.lastIdx + LIMIT_COUNT < maxIdx) {
this.lastIdx += LIMIT_COUNT; this.lastIdx += LIMIT_COUNT;
continue;
} }
} }
} }

View File

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