1
This commit is contained in:
parent
484cb24597
commit
a929459db1
@ -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;
|
|
@ -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;
|
|
@ -22,7 +22,6 @@ async function addSingle(clsName, modName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
await add("BlockChain", 'blockchain');
|
|
||||||
await add(['PullDbEvent'], 'pull_dbevent');
|
await add(['PullDbEvent'], 'pull_dbevent');
|
||||||
await add(['DbEventProcess'], 'dbevent_process');
|
await add(['DbEventProcess'], 'dbevent_process');
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ class PullDbEvent extends BaseService {
|
|||||||
}
|
}
|
||||||
const updateMaxIdxFunc = async () => {
|
const updateMaxIdxFunc = async () => {
|
||||||
while (true) {
|
while (true) {
|
||||||
const {err, maxIdx} = await conn.getMaxIdx('BCEVENT_TABLE_NAME');
|
const {err, maxIdx} = await conn.getMaxIdx(BCEVENT_TABLE_NAME);
|
||||||
if (!err) {
|
if (!err) {
|
||||||
PullDbEvent.#maxIdx = maxIdx;
|
PullDbEvent.#maxIdx = maxIdx;
|
||||||
await utils.sleep(500 + utils.randRange(500, 1500));
|
await utils.sleep(500 + utils.randRange(500, 1500));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user