173 lines
4.7 KiB
JavaScript
173 lines
4.7 KiB
JavaScript
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 BaseService = require('./baseservice');
|
|
const bc = require('../blockchain');
|
|
const C = require('../C');
|
|
const factory = require('./factory');
|
|
const BoxOpenedProcess = require('./_internal/boxopened_process');
|
|
const NftTransferProcess = require('./_internal/nft_transfer_process');
|
|
|
|
const LIMIT_COUNT = 100;
|
|
|
|
class EventCenter extends BaseService {
|
|
|
|
instances = [
|
|
{
|
|
'name': 'heroInstance',
|
|
'eventName': 'Transfer',
|
|
},
|
|
{
|
|
'name': 'equipInstance',
|
|
'eventName': 'Transfer',
|
|
},
|
|
{
|
|
'name': 'chipInstance',
|
|
'eventName': 'Transfer',
|
|
},
|
|
{
|
|
'name': 'luckboxInstance',
|
|
'eventName': 'Transfer',
|
|
}
|
|
];
|
|
|
|
boxInstance = {
|
|
'name': 'boxproxyInstance',
|
|
'eventName': 'BoxOpened',
|
|
};
|
|
|
|
async init() {
|
|
this.lastIdx = 0;
|
|
this.pendingConfirmOwnerHash = {};
|
|
|
|
const {err, conn} = await app.getDbConn('MarketDb0');
|
|
if (err) {
|
|
throw 'db error:' + err;
|
|
}
|
|
this.conn = conn;
|
|
|
|
event.addListener(C.DESTORY_EXEC_CONFIRM_OWNER_EVENT, (tokenId) => {
|
|
log.info('destory confirm owner:' + tokenId);
|
|
try {
|
|
if (utils.getVal(this.pendingConfirmOwnerHash, tokenId)) {
|
|
delete this.pendingConfirmOwnerHash[tokenId];
|
|
}
|
|
} catch(err) {
|
|
log.error('destory confirm owner:' + err);
|
|
}
|
|
});
|
|
event.addListener(C.CREATE_EXEC_CONFIRM_OWNER_EVENT, (tokenId) => {
|
|
log.info('create confirm owner:' + tokenId);
|
|
try {
|
|
this.addConfirmOwnerRequest(tokenId);
|
|
} catch(err) {
|
|
log.error('create confirm owner:' + err);
|
|
}
|
|
});
|
|
|
|
this.confirmOwner();
|
|
await this.initEventProcess();
|
|
}
|
|
|
|
async initEventProcess() {
|
|
const initInstance = (instance) => {
|
|
instance['pullCount'] = 0;
|
|
instance['eventCount'] = 0;
|
|
instance['fromBlock'] = 0;
|
|
instance['toBlock'] = 0;
|
|
instance['currBlock'] = 0;
|
|
};
|
|
const allInstances = [];
|
|
{
|
|
this.instances.forEach((item) => {
|
|
allInstances.push(item);
|
|
});
|
|
allInstances.push(this.boxInstance);
|
|
allInstances.forEach((item) => {
|
|
initInstance(item);
|
|
});
|
|
|
|
let count = 0;
|
|
const outputLog = async () => {
|
|
while (true) {
|
|
log.info(++count + '-------------------------------------------------------------');
|
|
log.info('pendingConfirmOwnerHash.size:' +
|
|
Object.keys(this.pendingConfirmOwnerHash).length);
|
|
allInstances.forEach((item) => {
|
|
log.info(utils.jsonEncode(item));
|
|
});
|
|
await utils.sleep(1000 * 10);
|
|
}
|
|
};
|
|
setTimeout(outputLog, 1000 * 3);
|
|
}
|
|
{
|
|
this.instances.forEach(async (item) => {
|
|
factory.create('EventProcess', null)
|
|
.init(this.conn, item, async (event) => {
|
|
await (new NftTransferProcess()).start(item, this.conn, event);
|
|
})
|
|
});
|
|
}
|
|
{
|
|
factory.create('EventProcess', null)
|
|
.init(this.conn, this.boxInstance, async (event) => {
|
|
await (new BoxOpenedProcess()).start(this.boxInstance, this.conn, event);
|
|
});
|
|
}
|
|
}
|
|
|
|
async addConfirmOwnerRequest(tokenId) {
|
|
const pendingRequest = utils.getVal(this.pendingConfirmOwnerHash, tokenId);
|
|
if (!pendingRequest) {
|
|
const newRequest = new factory.create('ExecConfirmOwner', null);
|
|
this.pendingConfirmOwnerHash[tokenId] = newRequest;
|
|
newRequest.init(tokenId);
|
|
return;
|
|
} else {
|
|
pendingRequest.addTryCount();
|
|
}
|
|
}
|
|
|
|
async confirmOwner() {
|
|
let maxIdx = 0;
|
|
while (true) {
|
|
{
|
|
const {err, rows} = await this.conn.execQuery(
|
|
'SELECT * FROM t_nft_transfer WHERE `idx` > ? AND ' +
|
|
'`owner_confirmed` = 0 LIMIT ' + LIMIT_COUNT,
|
|
[this.lastIdx]);
|
|
if (!err) {
|
|
for (let i in rows) {
|
|
const row = rows[i];
|
|
this.addConfirmOwnerRequest(row['token_id']);
|
|
if (row['idx'] > this.lastIdx) {
|
|
this.lastIdx = row['idx'];
|
|
}
|
|
}
|
|
if (rows.length < 1 && this.lastIdx + LIMIT_COUNT < maxIdx) {
|
|
this.lastIdx += LIMIT_COUNT;
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
{
|
|
const {err, row} = await this.conn.execQueryOne(
|
|
'SELECT max(idx) max_idx FROM t_nft_transfer', []);
|
|
if (!err && row['max_idx'] != null) {
|
|
maxIdx = row['max_idx'];
|
|
}
|
|
}
|
|
while (Object.keys(this.pendingConfirmOwnerHash).length > 50) {
|
|
await utils.sleep(1000 + utils.randRange(500, 1500));
|
|
}
|
|
await utils.sleep(2000 + utils.randRange(500, 1500));
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = EventCenter;
|