flosight-api/lib/parser/block.js

41 lines
1.1 KiB
JavaScript

const BlockModel = require('../../models/block');
const TxParser = require('./transaction');
const config = require('../../config');
const util = require('../../lib/util');
const logger = require('../logger');
function parse(entry, block) {
const rawBlock = block.toRaw().toString('hex');
const blockJSON = block.toJSON();
const reward = util.calcBlockReward(entry.height);
const newBlock = new BlockModel({
hash: blockJSON.hash,
height: entry.height,
version: blockJSON.version,
size: block.size,
prevBlock: blockJSON.prevBlock,
merkleRoot: blockJSON.merkleRoot,
ts: blockJSON.ts,
bits: blockJSON.bits,
nonce: blockJSON.nonce,
txs: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))),
chainwork: entry.chainwork,
reward: reward,
network: config.bcoin.network,
poolInfo: {},
rawBlock: rawBlock,
});
newBlock.save((err) => {
if (err) {
logger.log('error', err.message);
}
TxParser.parse(entry, block.txs);
});
}
module.exports = {
parse,
};