1
This commit is contained in:
parent
6c554bf735
commit
a33920ed28
140
server/web3spider/services/blockchain.js
Normal file
140
server/web3spider/services/blockchain.js
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
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 C = require("./C");
|
||||||
|
|
||||||
|
class BlockChain {
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
const netId = await this.getNetId();
|
||||||
|
console.log('net_id:', netId, ' 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 getNetId() {
|
||||||
|
return await this.web3.eth.getChainId();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getCurrBlockNumber() {
|
||||||
|
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']
|
||||||
|
]
|
||||||
|
);
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
++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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = new BlockChain();
|
@ -24,7 +24,7 @@ class EventProcess extends BaseService {
|
|||||||
async pullEvent() {
|
async pullEvent() {
|
||||||
const logClass = this.getInstanceName() + ' pullEvent:';
|
const logClass = this.getInstanceName() + ' pullEvent:';
|
||||||
while (true) {
|
while (true) {
|
||||||
await bc.lockQuery();
|
await this.bc.lockQuery();
|
||||||
try {
|
try {
|
||||||
const fromBlock = await this.getFromBlock();
|
const fromBlock = await this.getFromBlock();
|
||||||
const toBlock = await this.calcToBlock(fromBlock);
|
const toBlock = await this.calcToBlock(fromBlock);
|
||||||
@ -38,7 +38,7 @@ class EventProcess extends BaseService {
|
|||||||
);
|
);
|
||||||
this.instance['fromBlock'] = fromBlock;
|
this.instance['fromBlock'] = fromBlock;
|
||||||
this.instance['toBlock'] = toBlock;
|
this.instance['toBlock'] = toBlock;
|
||||||
this.instance['currBlock'] = bc.getCurrBlockNumber();
|
this.instance['currBlock'] = this.bc.getCurrBlockNumber();
|
||||||
this.instance['eventCount'] += events.length;
|
this.instance['eventCount'] += events.length;
|
||||||
if (events.length > 0) {
|
if (events.length > 0) {
|
||||||
console.log(events);
|
console.log(events);
|
||||||
@ -53,7 +53,7 @@ class EventProcess extends BaseService {
|
|||||||
log.error(logClass + err);
|
log.error(logClass + err);
|
||||||
await utils.sleep(1000 + utils.randRange(10, 2000));
|
await utils.sleep(1000 + utils.randRange(10, 2000));
|
||||||
} finally {
|
} finally {
|
||||||
await bc.unlockQuery();
|
await this.bc.unlockQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -79,7 +79,7 @@ class EventProcess extends BaseService {
|
|||||||
|
|
||||||
async getFromBlock() {
|
async getFromBlock() {
|
||||||
const logClass = this.getInstanceName() + ' getFromBlock:';
|
const logClass = this.getInstanceName() + ' getFromBlock:';
|
||||||
const firstBlockNumber = await bc.getFirstBlockNumber();
|
const firstBlockNumber = await this.bc.getFirstBlockNumber();
|
||||||
while (this.lastBlockNumber < 1) {
|
while (this.lastBlockNumber < 1) {
|
||||||
try {
|
try {
|
||||||
const {err, row} = await this.conn.ormSelectOne(
|
const {err, row} = await this.conn.ormSelectOne(
|
||||||
@ -95,8 +95,8 @@ class EventProcess extends BaseService {
|
|||||||
this.lastBlockNumber = firstBlockNumber;
|
this.lastBlockNumber = firstBlockNumber;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(logClass, this.lastBlockNumber, bc.getCurrBlockNumber());
|
console.log(logClass, this.lastBlockNumber, this.bc.getCurrBlockNumber());
|
||||||
while (this.lastBlockNumber + 8 > bc.getCurrBlockNumber()) {
|
while (this.lastBlockNumber + 8 > this.bc.getCurrBlockNumber()) {
|
||||||
await utils.sleep(1000 + utils.randRange(500, 1500));
|
await utils.sleep(1000 + utils.randRange(500, 1500));
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
@ -109,7 +109,7 @@ class EventProcess extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async calcToBlock(fromBlock) {
|
async calcToBlock(fromBlock) {
|
||||||
const currBlockNumber = bc.getCurrBlockNumber();
|
const currBlockNumber = this.bc.getCurrBlockNumber();
|
||||||
const distanceBlock = currBlockNumber - fromBlock - 8;
|
const distanceBlock = currBlockNumber - fromBlock - 8;
|
||||||
const batchBlockNum = 888;
|
const batchBlockNum = 888;
|
||||||
if (distanceBlock > 0) {
|
if (distanceBlock > 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user