From 1a18a369c12855837c4c704196bf6da828cdf1ac Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Sun, 27 Aug 2017 23:44:08 -0400 Subject: [PATCH] Save logic moved to models. Parsers removed --- server/lib/db/blocks.js | 5 +++ server/lib/db/transactions.js | 11 +++--- server/lib/node/index.js | 21 ++++++++---- server/lib/parser/block.js | 42 ----------------------- server/lib/parser/index.js | 7 ---- server/lib/parser/transaction.js | 52 ---------------------------- server/models/block.js | 29 ++++++++++++++++ server/models/transaction.js | 59 ++++++++++++++++++++------------ 8 files changed, 91 insertions(+), 135 deletions(-) delete mode 100644 server/lib/parser/block.js delete mode 100644 server/lib/parser/index.js delete mode 100644 server/lib/parser/transaction.js diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index 198f99c..c4b6956 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -39,6 +39,10 @@ function getLastBlock(cb) { .limit(1); } +function saveBcoinBlock(entry, block, cb) { + return Block.saveBcoinBlock(entry, block, cb); +} + // Returns highest consecutive block height function getBestBlockHeight(cb) { logger.log('debug', @@ -66,4 +70,5 @@ module.exports = { getByHash, byHeight, bestHeight, + saveBcoinBlock, }; diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 340116a..7fa16a9 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -2,13 +2,8 @@ const Transactions = require('../../models/transaction.js'); const config = require('../../config'); const logger = require('../logger'); - const MAX_PAGE_TXS = config.api.max_page_txs; -function getEmptyInputs(cb) { - return Transactions.getEmptyInputs(cb); -} - function getTopTransactions(cb) { return Transactions.last(cb); } @@ -36,13 +31,17 @@ function getTxCountByAddress(address, cb) { return Transactions.countByAddress(address, cb); } +function saveBcoinTransactions(entry, txs, cb) { + return Transactions.saveBcoinTransactions(entry, txs, cb); +} + module.exports = { - getEmptyInputs, getTopTransactions, getTxById, getTxByBlock, getTxCountByBlock, getTxByAddress, getTxCountByAddress, + saveBcoinTransactions, }; diff --git a/server/lib/node/index.js b/server/lib/node/index.js index cd99eb7..fe5d7c8 100644 --- a/server/lib/node/index.js +++ b/server/lib/node/index.js @@ -1,9 +1,6 @@ const FullNode = require('bcoin/lib/node/fullnode'); const logger = require('../../lib/logger'); -const BlockParser = require('../parser').Block; -const TxParser = require('../parser').Transaction; const config = require('../../config'); -const socket = require('../../lib/api/socket'); const db = require('../../lib/db'); const node = new FullNode(config.bcoin); @@ -20,12 +17,24 @@ function start(bestBlockHeight) { node.chain.on('connect', (entry, block) => { db.blocks.bestHeight(entry.height); - + // Assemble Bcoin block data node.chain.db.getBlockView(block) .then((view) => { const fullBlock = block.getJSON(node.network, view, entry.height); - BlockParser.parse(entry, block); - TxParser.parse(entry, fullBlock.txs); + // Save the block + db.blocks.saveBcoinBlock(entry, block, (err) => { + if (err) { + logger.log('error', + `Error saving block ${err}`); + } + }); + // Save the Txs + db.txs.saveBcoinTransactions(entry, fullBlock.txs, (err) => { + if (err) { + logger.log('error', + `Error saving txs ${err}`); + } + }); }); }); diff --git a/server/lib/parser/block.js b/server/lib/parser/block.js deleted file mode 100644 index fb0000d..0000000 --- a/server/lib/parser/block.js +++ /dev/null @@ -1,42 +0,0 @@ -const BlockModel = require('../../models/block'); -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); - - // 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, - merkleRoot: blockJSON.merkleRoot, - ts: blockJSON.ts, - bits: blockJSON.bits, - nonce: blockJSON.nonce, - txs: block.txs.map((tx) => { - const txJSON = tx.toJSON(); - return txJSON.hash; - }), - chainwork: entry.chainwork, - reward, - network: config.bcoin.network, - poolInfo: {}, - rawBlock, - }); - - newBlock.save((err) => { - if (err) { - logger.log('error', err.message); - } - }); -} - -module.exports = { - parse, -}; diff --git a/server/lib/parser/index.js b/server/lib/parser/index.js deleted file mode 100644 index 8989195..0000000 --- a/server/lib/parser/index.js +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 43f634f..0000000 --- a/server/lib/parser/transaction.js +++ /dev/null @@ -1,52 +0,0 @@ -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'); - -function parse(entry, txs) { - txs.forEach((tx) => { - const t = new TxModel({ - hash: tx.hash, - witnessHash: tx.witnessHash, - fee: tx.fee, - rate: tx.rate, - ps: tx.ps, - height: entry.height, - block: util.revHex(entry.hash), - ts: entry.ts, - date: entry.tx, - index: tx.index, - version: tx.version, - flag: tx.flag, - inputs: tx.inputs.map(input => new InputModel({ - value: input.coin ? input.coin.value : 0, - prevout: input.prevout, - script: input.script, - witness: input.witness, - sequence: input.sequence, - address: input.coin ? input.coin.address : '', - })), - outputs: tx.outputs.map(output => new OutputModel({ - address: output.address, - script: output.script, - value: output.value, - })), - lockTime: tx.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 445e119..d02f4e1 100644 --- a/server/models/block.js +++ b/server/models/block.js @@ -1,5 +1,6 @@ const mongoose = require('mongoose'); const config = require('../config'); +const util = require('../lib/util'); const Schema = mongoose.Schema; // These limits can be overriden higher up the stack @@ -66,4 +67,32 @@ BlockSchema.statics.getHeights = function findMissing(cb) { .sort({ height: 1 }); }; +BlockSchema.statics.saveBcoinBlock = function saveBcoinBlock(entry, block, cb) { + const Block = this.model('Block'); + const rawBlock = block.toRaw().toString('hex'); + const blockJSON = block.toJSON(); + const reward = util.calcBlockReward(entry.height); + + return new Block({ + 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) => { + const txJSON = tx.toJSON(); + return txJSON.hash; + }), + chainwork: entry.chainwork, + reward, + network: config.bcoin.network, + poolInfo: {}, + rawBlock, + }).save(cb); +}; + module.exports = mongoose.model('Block', BlockSchema); diff --git a/server/models/transaction.js b/server/models/transaction.js index f6f5c0e..bcce44b 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -3,6 +3,7 @@ const Input = require('./input'); const Output = require('./output'); const logger = require('../lib/logger'); const config = require('../config'); +const util = require('../lib/util'); const Schema = mongoose.Schema; // These limits can be overriden higher up the stack @@ -83,30 +84,44 @@ TransactionSchema.statics.last = function lastTx(cb) { .sort({ height: -1 }); }; -TransactionSchema.statics.getEmptyInputs = function getEmptyInputs(cb) { - return this.model('Transaction').find({ - 'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' }, - 'inputs.value': 0, - }, - cb); +TransactionSchema.statics.saveBcoinTransactions = function saveBcoinTransactions(entry, txs, cb) { + txs.forEach((tx) => { + this.saveBcoinTransaction(entry, tx, cb); + }); }; -TransactionSchema.statics.updateInput = function updateInput(txid, inputid, value, address) { - return this.model('Transaction').findOneAndUpdate( - { _id: txid, 'inputs._id': inputid }, - { - $set: { - 'inputs.$.value': value, - 'inputs.$.address': address, - }, - }, - (err, tx) => { - if (err) { - logger.log('error', - `updateInput: ${err}`); - } - }, - ); +TransactionSchema.statics.saveBcoinTransaction = function saveBcoinTransaction(entry, tx, cb) { + const Transaction = this.model('Transaction'); + return new Transaction({ + hash: tx.hash, + witnessHash: tx.witnessHash, + fee: tx.fee, + rate: tx.rate, + ps: tx.ps, + height: entry.height, + block: util.revHex(entry.hash), + ts: entry.ts, + date: entry.tx, + index: tx.index, + version: tx.version, + flag: tx.flag, + inputs: tx.inputs.map(input => new Input({ + value: input.coin ? input.coin.value : 0, + prevout: input.prevout, + script: input.script, + witness: input.witness, + sequence: input.sequence, + address: input.coin ? input.coin.address : '', + })), + outputs: tx.outputs.map(output => new Output({ + address: output.address, + script: output.script, + value: output.value, + })), + lockTime: tx.locktime, + chain: config.bcoin.network, + }) + .save(cb); }; module.exports = mongoose.model('Transaction', TransactionSchema);