1
This commit is contained in:
parent
2e67b1939c
commit
fd792d2259
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user