From a929459db1d79f67c894d3a579b9557d68a8fa6a Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Wed, 5 Jul 2023 19:28:54 +0800 Subject: [PATCH] 1 --- server/web3dbspider/blockchain.js | 139 ------------------- server/web3dbspider/services/blockchain.js | 68 --------- server/web3dbspider/services/factory.js | 1 - server/web3dbspider/services/pull_dbevent.js | 2 +- 4 files changed, 1 insertion(+), 209 deletions(-) delete mode 100644 server/web3dbspider/blockchain.js delete mode 100644 server/web3dbspider/services/blockchain.js diff --git a/server/web3dbspider/blockchain.js b/server/web3dbspider/blockchain.js deleted file mode 100644 index 772eb80..0000000 --- a/server/web3dbspider/blockchain.js +++ /dev/null @@ -1,139 +0,0 @@ -const util = require('util'); -const Web3 = require('web3'); -const utils = require('j7/utils'); -const bcutils = require('j7/bcutils'); -const event = require('j7/event'); -const sync = require("j7/sync"); -const log = require("j7/log"); -const contract = require('common/contract'); -const bcconst = require('common/bcconst'); -const metaFactory = require('./metadata/factory'); - -class BlockChain { - - constructor(netId) { - this.actived = false; - this.netId = netId; - this.lastQueryTime = utils.getUtcTime(); - this.queryLockTimes = 0; - this.currBlockNumber = 0; - this.refreshCond = new sync.Cond(); - this.lastRefreshTime = utils.getUtcTime(); - setTimeout(this.refreshBlockNumber.bind(this), 1000 * 0.01); - } - - async initInstance(user, address, jsonUrl) { - const json = utils.readJsonFromFile(jsonUrl); - return new this.web3.eth.Contract( - json.abi, - address, - { from: user } - ); - } - - async init() { - this.web3Conf = metaFactory.getWeb3Conf(this.netId); - this.contractsConf = metaFactory.getContractsConf(this.netId); - this.netDir = metaFactory.getNetDir(this.netId); - - this.web3 = new Web3(this.getRpcUrl()); - this.web3.eth.handleRevert = true; - this.web3.eth.accounts.wallet.add(this.getPrivateKey()); - for (const data of this.contractsConf) { - this[`${data.name}Instance`] = await this.initInstance - (this.getUserAddress(), data.address, this.netDir + data.json); - } - const chainId = await this.web3.eth.getChainId(); - if (chainId != this.netId) { - log.warning(util.format('net id error %s %s', - chainId, - this.netId - )); - } - log.info(util.format('local.net_id:%s remote_net_id:%s', - this.netId, - chainId - )); - { - await this.mustBeActive(); - const netId = this.getNetId(); - console.log('net_id:', netId, ' blockNumber:', this.getCurrBlockNumber(), - ' handleRevert:', this.web3.eth.handleRevert, ' isOnlineEnv:', utils.isOnlineEnv()); - } - } - - async mustBeActive() { - while (!this.actived) { - await utils.sleep(1000); - } - } - - getNetId() { - return this.netId; - } - - getRpcUrl() { - return this.web3Conf['block_server']; - } - - getUserAddress() { - return this.web3Conf['user_address']; - } - - getPrivateKey() { - return this.web3Conf['private_key']; - } - - getContractByName(name) { - let contract = null; - this.contractsConf.forEach((item) => { - if (item['name'] == name) { - contract = item; - } - }); - return contract; - } - - getCurrBlockNumber() { - return this.currBlockNumber; - } - - async lockQuery() { - while (this.queryLockTimes > 3) { - await utils.sleep(100 + utils.randRange(10, 100)); - } - ++this.queryLockTimes; - this.lastQueryTime = utils.getUtcTime(); - } - - async unlockQuery() { - await utils.sleep(10 + utils.randRange(10, 50)); - --this.queryLockTimes; - } - - isAddress(address) { - return this.web3.utils.isAddress(address); - } - - async refreshBlockNumber() { - const logHead = ' refreshBlockNumber:'; - while (true) { - try { - this.currBlockNumber = await this.web3.eth.getBlockNumber(); - this.actived = true; - this.lastRefreshTime = utils.getUtcTime(); - console.log('currBlockNumber', this.currBlockNumber); - } catch (e) { - this.actived = false; - log.warning(util.format('%s err:%s', - logHead, - e - )); - } - await this.refreshCond.wait(1000 * 3); - } - } - -} - -module.exports = BlockChain; diff --git a/server/web3dbspider/services/blockchain.js b/server/web3dbspider/services/blockchain.js deleted file mode 100644 index 83f574f..0000000 --- a/server/web3dbspider/services/blockchain.js +++ /dev/null @@ -1,68 +0,0 @@ -const app = require('j7/app'); -const utils = require('j7/utils'); -const bcutils = require('j7/bcutils'); -const sync = require("j7/sync"); -const log = require("j7/log"); -const metaFactory = require('../metadata/factory'); -const bcClass = require('../blockchain'); -const BaseService = require('./baseservice'); - -const netIdHash = {}; - -function getBc(netId) { - return utils.hasKey(netIdHash, netId) ? netIdHash[netId] : null; -} - -class BlockChain extends BaseService { - - #bc = null; - - static async staticInit() { - metaFactory.getNetList().forEach(async (net) => { - const bc = new bcClass(net['netId']); - netIdHash[net['netId']] = bc; - await bc.init(); - }); - } - - init(netId) { - this.#bc = getBc(netId); - return this.#bc != null; - } - - async mustBeActive() { - await this.#bc.mustBeActive(); - } - - getNetId() { - return this.#bc.getNetId(); - } - - getCurrBlockNumber() { - return this.#bc.currBlockNumber; - } - - async lockQuery() { - await this.#bc.lockQuery(); - } - - async unlockQuery() { - await this.#bc.unlockQuery(); - } - - isAddress(address) { - return this.#bc.isAddress(); - } - - getContractAddressByName(name) { - const contract = this.#bc.getContractByName(name); - return contract ? bcutils.toNormalAddress(contract['address']) : ''; - } - - async getPastEvents(contractName, eventName, ...args) { - return this.#bc[contractName + 'Instance'].getPastEvents(eventName, ...args); - } - -} - -module.exports = BlockChain; diff --git a/server/web3dbspider/services/factory.js b/server/web3dbspider/services/factory.js index b409163..527e703 100644 --- a/server/web3dbspider/services/factory.js +++ b/server/web3dbspider/services/factory.js @@ -22,7 +22,6 @@ async function addSingle(clsName, modName) { } async function init() { - await add("BlockChain", 'blockchain'); await add(['PullDbEvent'], 'pull_dbevent'); await add(['DbEventProcess'], 'dbevent_process'); } diff --git a/server/web3dbspider/services/pull_dbevent.js b/server/web3dbspider/services/pull_dbevent.js index 7d5a8f2..8bfd892 100644 --- a/server/web3dbspider/services/pull_dbevent.js +++ b/server/web3dbspider/services/pull_dbevent.js @@ -22,7 +22,7 @@ class PullDbEvent extends BaseService { } const updateMaxIdxFunc = async () => { while (true) { - const {err, maxIdx} = await conn.getMaxIdx('BCEVENT_TABLE_NAME'); + const {err, maxIdx} = await conn.getMaxIdx(BCEVENT_TABLE_NAME); if (!err) { PullDbEvent.#maxIdx = maxIdx; await utils.sleep(500 + utils.randRange(500, 1500));