From fd792d22592a1bc3152d4cf479e464116517fa68 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 3 Jul 2023 20:43:26 +0800 Subject: [PATCH] 1 --- server/web3bcspider/services/blockchain.js | 91 +++------------------- 1 file changed, 10 insertions(+), 81 deletions(-) diff --git a/server/web3bcspider/services/blockchain.js b/server/web3bcspider/services/blockchain.js index c3f18a8..6291ac8 100644 --- a/server/web3bcspider/services/blockchain.js +++ b/server/web3bcspider/services/blockchain.js @@ -28,103 +28,32 @@ class BlockChain extends BaseService { return this.#bc != null; } - constructor() { - this.actived = false; - this.currBlockNumber = 0; - this.refreshCond = new sync.Cond(); - this.lastRefreshTime = utils.getUtcTime(); - this.lastQueryTime = utils.getUtcTime(); - this.netId = 0; - this.queryLockTimes = 0; - 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.web3 = new Web3(metaFactory.getWeb3Conf()['block_server']); - this.web3.eth.handleRevert = utils.isOnlineEnv(); - for (const data of metaFactory.getContracts()) { - this[`${data.name}Instance`] = await this.initInstance - (metaFactory.getUserAddress(), data.address, data.json); - } - event.emitEvent(C.BC_INITIALIZED_EVENT); - { - await this.mustBeActive(); - this.netId = await this.asyncGetNetId(); - console.log('net_id:', this.getNetId(), ' blockNumber:', this.getCurrBlockNumber(), - ' handleRevert:', this.web3.eth.handleRevert, ' isOnlineEnv:', utils.isOnlineEnv()); - } - /*let banlances = await this['shardInstance'].methods.balanceOfBatch - ( - [], - [1] - ).call();*/ - } - async mustBeActive() { - while (!this.actived) { - await utils.sleep(1000); - } - } - - async asyncGetNetId() { - return await this.web3.eth.getChainId(); + await this.#bc.mustBeActive(); } getNetId() { - return '' + this.netId; - } - - async refreshBlockNumber() { - const logClass = 'refreshBlockNumber'; - while (true) { - try { - this.currBlockNumber = await this.web3.eth.getBlockNumber(); - this.actived = true; - this.lastRefreshTime = utils.getUtcTime(); - console.log('currBlockNumber', this.currBlockNumber); - if (!this.initedBlockNumber) { - this.initedBlockNumber = true; - this.initBlockNumber = this.currBlockNumber; - } - } catch (e) { - this.actived = false; - log.warning(util.format('%s err:%s', - logClass, - e - )); - } - await this.refreshCond.wait(1000 * 3); - } + return this.#bc.getNetId(); } getCurrBlockNumber() { - return this.currBlockNumber; + return this.#bc.currBlockNumber; } async lockQuery() { - while (this.queryLockTimes > 3) { - await utils.sleep(100 + utils.randRange(10, 100)); - } - ++this.queryLockTimes; - this.lastQueryTime = utils.getUtcTime(); + await this.#bc.lockQuery(); } async unlockQuery() { - await utils.sleep(10 + utils.randRange(10, 50)); - --this.queryLockTimes; + await this.#bc.unlockQuery(); } isAddress(address) { - return this.web3.utils.isAddress(address); + return this.#bc.isAddress(); + } + + getContractByName(name) { + return this.#bc.getContractByName(name); } }