From fc8eb9d8709c4c4ea9980b13ec3019b3f16f5e57 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 3 Jul 2023 19:58:55 +0800 Subject: [PATCH] 1 --- server/web3bcspider/blockchain.js | 80 ++++++++++++++++++++++ server/web3bcspider/services/blockchain.js | 55 ++++++--------- 2 files changed, 102 insertions(+), 33 deletions(-) create mode 100644 server/web3bcspider/blockchain.js diff --git a/server/web3bcspider/blockchain.js b/server/web3bcspider/blockchain.js new file mode 100644 index 0000000..b1640a7 --- /dev/null +++ b/server/web3bcspider/blockchain.js @@ -0,0 +1,80 @@ +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.netId = netId; + } + + 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 + )); + } + + 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; + } + +} + +module.exports = BlockChain; diff --git a/server/web3bcspider/services/blockchain.js b/server/web3bcspider/services/blockchain.js index ae8cd0d..c3f18a8 100644 --- a/server/web3bcspider/services/blockchain.js +++ b/server/web3bcspider/services/blockchain.js @@ -1,21 +1,39 @@ -const util = require('util'); -const Web3 = require('web3'); const app = require('j7/app'); const utils = require('j7/utils'); -const event = require('j7/event'); const sync = require("j7/sync"); const log = require("j7/log"); const metaFactory = require('./metadata/factory'); +const bcClass = require('../blockchain'); + +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['net_id']); + netIdHash[net['net_id']] = bc; + await bc.init(); + }); + } + + async init(netId) { + this.#bc = getBc(netId); + 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.firstBlockNumber = -1; this.netId = 0; this.queryLockTimes = 0; setTimeout(this.refreshBlockNumber.bind(this), 1000 * 0.01); @@ -92,35 +110,6 @@ class BlockChain extends BaseService { return this.currBlockNumber; } - async getFirstBlockNumber() { - while (this.firstBlockNumber < 0) { - const {err, conn} = await app.getDbConn('MarketDb0'); - if (err) { - await utils.sleep(1000 + utils.randRange(500, 1500)); - continue; - } - try { - const {err, row} = await conn.ormSelectOne( - 't_parameter', - [ - ['name', 'first_block_number.' + this.getNetId() + '.'] - ] - ); - if (!err && row) { - this.firstBlockNumber = Number(row['value']); - await utils.sleep(500 + utils.randRange(500, 1500)); - continue; - } - } catch (e) { - console.log(e); - } finally { - conn.release(); - } - await utils.sleep(5000 + utils.randRange(500, 1500)); - } - return this.firstBlockNumber; - } - async lockQuery() { while (this.queryLockTimes > 3) { await utils.sleep(100 + utils.randRange(10, 100));