diff --git a/server/web3bcspider/blockchain.js b/server/web3bcspider/blockchain.js index b1640a7..ca6ba05 100644 --- a/server/web3bcspider/blockchain.js +++ b/server/web3bcspider/blockchain.js @@ -13,6 +13,12 @@ class BlockChain { constructor(netId) { 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) { @@ -75,6 +81,46 @@ class BlockChain { return contract; } + 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); + if (!this.initedBlockNumber) { + this.initedBlockNumber = true; + this.initBlockNumber = 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;