This commit is contained in:
aozhiwei 2023-07-05 19:28:54 +08:00
parent 484cb24597
commit a929459db1
4 changed files with 1 additions and 209 deletions

View File

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

View File

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

View File

@ -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');
}

View File

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