diff --git a/server/lib/parser/block.js b/server/lib/parser/block.js index b19b2b9..fb0000d 100644 --- a/server/lib/parser/block.js +++ b/server/lib/parser/block.js @@ -1,67 +1,32 @@ const BlockModel = require('../../models/block'); -const InputModel = require('../../models/input'); -const OutputModel = require('../../models/output'); -const config = require('../../config'); -const util = require('../../lib/util'); -const logger = require('../logger'); +const config = require('../../config'); +const util = require('../../lib/util'); +const logger = require('../logger'); function parse(entry, block) { - const rawBlock = block.toRaw().toString('hex'); + const rawBlock = block.toRaw().toString('hex'); const blockJSON = block.toJSON(); - const reward = util.calcBlockReward(entry.height); + const reward = util.calcBlockReward(entry.height); // Can probably use destructuring to build something nicer const newBlock = new BlockModel({ - hash: blockJSON.hash, - height: entry.height, - size: block.getSize(), - version: blockJSON.version, - prevBlock: blockJSON.prevBlock, + hash: blockJSON.hash, + height: entry.height, + size: block.getSize(), + version: blockJSON.version, + prevBlock: blockJSON.prevBlock, merkleRoot: blockJSON.merkleRoot, - ts: blockJSON.ts, - bits: blockJSON.bits, - nonce: blockJSON.nonce, - txs: block.txs.map((tx) => { + ts: blockJSON.ts, + bits: blockJSON.bits, + nonce: blockJSON.nonce, + txs: block.txs.map((tx) => { const txJSON = tx.toJSON(); - return { - hash: txJSON.hash, - witnessHash: txJSON.witnessHash, - fee: txJSON.fee, - rate: txJSON.rate, - ps: txJSON.ps, - height: entry.height, - block: util.revHex(entry.hash), - ts: entry.ts, - date: txJSON.date, - index: txJSON.index, - version: txJSON.version, - flag: txJSON.flag, - inputs: tx.inputs.map((input) => { - const inputJSON = input.toJSON(); - return new InputModel({ - prevout: inputJSON.prevout, - script: inputJSON.script, - witness: inputJSON.witness, - sequence: inputJSON.sequence, - address: inputJSON.address, - }); - }), - outputs: tx.outputs.map((output) => { - const outputJSON = output.toJSON(); - return new OutputModel({ - address: outputJSON.address, - script: outputJSON.script, - value: outputJSON.value, - }); - }), - lockTime: txJSON.locktime, - chain: config.bcoin.network, - }; + return txJSON.hash; }), - chainwork: entry.chainwork, + chainwork: entry.chainwork, reward, - network: config.bcoin.network, - poolInfo: {}, + network: config.bcoin.network, + poolInfo: {}, rawBlock, }); diff --git a/server/lib/parser/index.js b/server/lib/parser/index.js index 13c2d5b..5bd9726 100644 --- a/server/lib/parser/index.js +++ b/server/lib/parser/index.js @@ -1,5 +1,7 @@ const Block = require('./block'); +const Transaction = require('./transaction'); module.exports = { Block, + Transaction, }; diff --git a/server/lib/parser/transaction.js b/server/lib/parser/transaction.js new file mode 100644 index 0000000..7b0a8ab --- /dev/null +++ b/server/lib/parser/transaction.js @@ -0,0 +1,67 @@ +const TxModel = require('../../models/transaction'); +const InputModel = require('../../models/input'); +const OutputModel = require('../../models/output'); +const config = require('../../config'); +const util = require('../../lib/util'); +const logger = require('../logger'); +const db = require('../db'); + +// Bleh, Bcoin pulls in blocks 20 at a time +// Crappy delay for now otherwise async saves +// could miss a tx if an input refs a block within +// the last 20 that hasn't saved. +// Aggregate stuff will replace all of this. + +function parse(entry, txs) { + txs.forEach((tx) => { + const txJSON = tx.toJSON(); + const txRAW = tx.toRaw(); + + const t = new TxModel({ + hash: txJSON.hash, + witnessHash: txJSON.witnessHash, + fee: txJSON.fee, + rate: txJSON.rate, + size: txRAW.length, + ps: txJSON.ps, + height: entry.height, + block: util.revHex(entry.hash), + ts: entry.ts, + date: txJSON.date, + index: txJSON.index, + version: txJSON.version, + flag: txJSON.flag, + inputs: tx.inputs.map((input) => { + const inputJSON = input.toJSON(); + return new InputModel({ + prevout: inputJSON.prevout, + script: inputJSON.script, + witness: inputJSON.witness, + sequence: inputJSON.sequence, + address: inputJSON.address, + }); + }), + outputs: tx.outputs.map((output) => { + const outputJSON = output.toJSON(); + return new OutputModel({ + address: outputJSON.address, + script: outputJSON.script, + value: outputJSON.value, + }); + }), + lockTime: txJSON.locktime, + chain: config.bcoin.network, + }); + + + t.save((err) => { + if (err) { + logger.log('error', err.message); + } + }); + }); +} + +module.exports = { + parse, +}; diff --git a/server/models/block.js b/server/models/block.js index 38ba3aa..84aed55 100644 --- a/server/models/block.js +++ b/server/models/block.js @@ -1,24 +1,24 @@ -const mongoose = require('mongoose'); +const mongoose = require('mongoose'); const Transaction = require('./transaction'); const Schema = mongoose.Schema; const BlockSchema = new Schema({ - hash: { type: String, default: '' }, - height: { type: Number, default: 0 }, - size: { type: Number, default: 0 }, - version: { type: Number, default: 0 }, - prevBlock: { type: String, default: '' }, - merkleRoot: { type: String, default: '' }, - ts: { type: Number, default: 0 }, - bits: { type: Number, default: 0 }, - nonce: { type: Number, default: 0 }, - txs: [Transaction.schema], - chainwork: { type: Number, default: 0 }, - reward: { type: Number, default: 0 }, - network: { type: String, default: '' }, - poolInfo: { type: Object, default: {} }, - rawBlock: { type: String, default: '' }, + hash: { type: String, default: '' }, + height: { type: Number, default: 0 }, + size: { type: Number, default: 0 }, + version: { type: Number, default: 0 }, + prevBlock: { type: String, default: '' }, + merkleRoot: { type: String, default: '' }, + ts: { type: Number, default: 0 }, + bits: { type: Number, default: 0 }, + nonce: { type: Number, default: 0 }, + txs: [{ type: String, default: '' }], + chainwork: { type: Number, default: 0 }, + reward: { type: Number, default: 0 }, + network: { type: String, default: '' }, + poolInfo: { type: Object, default: {} }, + rawBlock: { type: String, default: '' }, }, { toJSON: { virtuals: true, diff --git a/server/models/transaction.js b/server/models/transaction.js index 8d30aba..e4e3416 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -1,25 +1,25 @@ const mongoose = require('mongoose'); -const Input = require('./input'); -const Output = require('./output'); +const Input = require('./input'); +const Output = require('./output'); -const Schema = mongoose.Schema; +const Schema = mongoose.Schema; const TransactionSchema = new Schema({ - hash: { type: String, default: '' }, + hash: { type: String, default: '' }, witnessHash: { type: String, default: '' }, - fee: { type: Number, default: 0 }, - rate: { type: Number, default: 0 }, - ps: { type: Number, default: 0 }, - height: { type: Number, default: 0 }, - block: { type: String, default: '' }, - index: { type: Number, default: 0 }, - version: { type: Number, default: 0 }, - flag: { type: Number, default: 0 }, - lockTime: { type: Number, default: 0 }, - inputs: [Input.schema], - outputs: [Output.schema], - size: { type: Number, default: 0 }, - network: { type: String, default: '' }, + fee: { type: Number, default: 0 }, + rate: { type: Number, default: 0 }, + ps: { type: Number, default: 0 }, + height: { type: Number, default: 0 }, + block: { type: String, default: '' }, + index: { type: Number, default: 0 }, + version: { type: Number, default: 0 }, + flag: { type: Number, default: 0 }, + lockTime: { type: Number, default: 0 }, + inputs: [Input.schema], + outputs: [Output.schema], + size: { type: Number, default: 0 }, + network: { type: String, default: '' }, }); TransactionSchema.index({ hash: 1 });