添加pullBlockData

This commit is contained in:
aozhiwei 2023-08-30 16:39:06 +08:00
parent c9813430d9
commit 470a785c14
3 changed files with 39 additions and 2 deletions

View File

@ -148,6 +148,10 @@ class BlockChain {
}
}
async getBlockData(blockNumber) {
return await this.web3.eth.getBlock(blockNumber);
}
}
module.exports = BlockChain;

View File

@ -67,6 +67,10 @@ class BlockChain extends BaseService {
return await this.#bc[contractName + 'Instance'].methods.ownerOf(tokenId).call();
}
async getBlockData(blockNumber) {
return await this.#bc.getBlockData(blockNumber);
}
}
module.exports = BlockChain;

View File

@ -70,7 +70,11 @@ class PullBcEvent extends BaseService {
async (event) => {
while (true) {
try {
await this.saveToDb(event);
const blockData = null;
if (this.needPullBlockData()) {
blockData = await this.getBlockData(event['blockNumber']);
}
await this.saveToDb(event, blockData);
return;
} catch (err) {
utils.safeDumpErrStack(err);
@ -81,6 +85,23 @@ class PullBcEvent extends BaseService {
});
}
async getBlockData(blockNumber) {
const logHead = this.genLogHead(' getBlockData: ');
while (true) {
try {
const blockData = await this.bc.getBlockData(blockNumber);
console.log(blockData);
return blockData;
return blockData;
} catch (err) {
utils.safeDumpErrStack(err);
log.error(logHead + err);
await utils.sleep(2000 + utils.randRange(10, 2000));
}
}
return null;
}
async getFromBlock() {
const logHead = this.genLogHead(' getFromBlock: ');
const firstBlockNumber = this.getInitBlock();
@ -166,7 +187,7 @@ class PullBcEvent extends BaseService {
this.lastBlockNumber = blockNumber;
}
async saveToDb(event) {
async saveToDb(event, blockData) {
const logHead = this.genLogHead(' event_process.saveToDb: ');
while (true) {
const nowTime = utils.getUtcTime();
@ -198,6 +219,7 @@ class PullBcEvent extends BaseService {
['block_number', event['blockNumber']],
['raw_data', rawData.length < 1024 * 1024 ? rawData : ''],
['return_values', utils.jsonEncode(returnValues)],
['block_data', utils.jsonEncode(blockData)],
['createtime', utils.getUtcTime()],
['modifytime', utils.getUtcTime()],
]
@ -233,6 +255,13 @@ class PullBcEvent extends BaseService {
return this.eventConf['contract_name'];
}
needPullBlockData() {
if (utils.hasKey(this.eventConf, 'pull_block_data')) {
return this.eventConf['pull_block_data'];
}
return false;
}
genLogHead(msg) {
const logHead = this.getNetId() + ' ' + this.getContractName() + '.' +
this.getEventName() + ' pull_bcevent ' + msg;