This commit is contained in:
aozhiwei 2023-07-03 20:11:21 +08:00
parent fc8eb9d870
commit f506ee0169

View File

@ -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;