From 591d90421278c72c8719606d944fb1d7580babf0 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Thu, 3 Aug 2017 22:52:58 -0400 Subject: [PATCH] Route functions named. Node & Parsers moved to their own files --- README.md | 3 +- index.js | 2 +- lib/api/address.js | 34 +++++++-------- lib/api/block.js | 18 ++++---- lib/api/currency.js | 2 +- lib/api/message.js | 2 +- lib/api/status.js | 2 +- lib/api/transaction.js | 2 +- lib/node/block.js | 42 +++++++++++++++++++ lib/node/index.js | 91 ++--------------------------------------- lib/node/transaction.js | 58 ++++++++++++++++++++++++++ 11 files changed, 138 insertions(+), 118 deletions(-) create mode 100644 lib/node/block.js create mode 100644 lib/node/transaction.js diff --git a/README.md b/README.md index 4f705f7..9efa322 100644 --- a/README.md +++ b/README.md @@ -52,4 +52,5 @@ sockets Some data is stubbed. This is due to Bcoin primitives being different from Bitcore. It's unclear whether Mongo models or multiple queries at the api layer will better serve us. Obviously multiple queries are easier but I would prefer a clear cut data model because that leads to fewer problems in the future and gives us greater flexibility in our API and the other microservices we implement in the future. # ToDo -Reorg testing - Bcoin will handle this but we need to account for this in our mongo indexes. \ No newline at end of file +Reorg testing - Bcoin will handle this but we need to account for this in our mongo indexes. +JSDoc & Unit tests \ No newline at end of file diff --git a/index.js b/index.js index 3c7968d..d47d732 100644 --- a/index.js +++ b/index.js @@ -14,4 +14,4 @@ Api.listen(config.api.port, () => { 'listening on port 3000'); }); -//node.start(); +node.start(); diff --git a/lib/api/address.js b/lib/api/address.js index f7677e9..5ac517f 100644 --- a/lib/api/address.js +++ b/lib/api/address.js @@ -1,5 +1,5 @@ -module.exports = function(app) { +module.exports = function addressAPI(app) { app.get('/addr/:addr', (req, res) => { res.send(req.params.addr); }); @@ -8,6 +8,22 @@ module.exports = function(app) { res.send(req.params.addr); }); + app.get('/addr/:addr/balance', (req, res) => { + res.send(req.params.addr); + }); + + app.get('/addr/:addr/totalReceived', (req, res) => { + res.send(req.params.addr); + }); + + app.get('/addr/:addr/totalSent', (req, res) => { + res.send(req.params.addr); + }); + + app.get('/addr/:addr/unconfirmedBalance', (req, res) => { + res.send(req.params.addr); + }); + app.get('/addrs/:addrs/utxo', (req, res) => { res.send(req.params.addrs); }); @@ -23,20 +39,4 @@ module.exports = function(app) { app.post('/addrs/txs', (req, res) => { res.send('post stub'); }); - - app.get('/addr/:addr/balance', (req, res) => { - res.send(req.params.addr); - }); - - app.get('/addr/:addr/totalReceived', (req, res) => { - res.send(req.params.addr); - }); - - app.get('/addr/:addr/totalSent', (req, res) => { - res.send(req.params.addr); - }); - - app.get('/addr/:addr/unconfirmedBalance', (req, res) => { - res.send(req.params.addr); - }); }; diff --git a/lib/api/block.js b/lib/api/block.js index c46d0b3..531210c 100644 --- a/lib/api/block.js +++ b/lib/api/block.js @@ -1,13 +1,17 @@ const Block = require('../../models/block.js'); +const logger = require('../logger'); -module.exports = function (app) { +module.exports = function BlockAPI(app) { app.get('/block/:blockHash', (req, res) => { - Block.find({ hash: req.params.blockHash }, (err, block) => { - if (err) { - console.log(err); - } - res.send(block); - }); + Block.find({ hash: req.params.blockHash }, + { _id: 0 }, + (err, block) => { + if (err) { + res.status(501).send(); + logger.log('err', err); + } + res.send(block); + }); }); app.get('/blocks', (req, res) => { diff --git a/lib/api/currency.js b/lib/api/currency.js index 5c056c5..fdab193 100644 --- a/lib/api/currency.js +++ b/lib/api/currency.js @@ -1,4 +1,4 @@ -module.exports = function(app) { +module.exports = function currencyAPI(app) { app.get('/currency', (req, res) => { res.send('currency'); }); diff --git a/lib/api/message.js b/lib/api/message.js index 34045ac..531a330 100644 --- a/lib/api/message.js +++ b/lib/api/message.js @@ -1,4 +1,4 @@ -module.exports = function(app) { +module.exports = function messageAPI(app) { app.get('/messages/verify', (req, res) => { res.send('messages verify'); }); diff --git a/lib/api/status.js b/lib/api/status.js index 3cb1e5a..8125f88 100644 --- a/lib/api/status.js +++ b/lib/api/status.js @@ -1,4 +1,4 @@ -module.exports = function(app) { +module.exports = function statusAPI(app) { app.get('/status', (req, res) => { res.send('status'); }); diff --git a/lib/api/transaction.js b/lib/api/transaction.js index 26c162c..510153f 100644 --- a/lib/api/transaction.js +++ b/lib/api/transaction.js @@ -1,6 +1,6 @@ const Transaction = require('../../models/transaction.js').Transaction; -module.exports = function (app) { +module.exports = function transactionAPI(app) { app.get('/tx/:txid', (req, res) => { res.send(req.params.txid); }); diff --git a/lib/node/block.js b/lib/node/block.js new file mode 100644 index 0000000..16d3c4d --- /dev/null +++ b/lib/node/block.js @@ -0,0 +1,42 @@ +const BlockModel = require('../../models/block'); +const TxParser = require('./transaction'); +const util = require('../../lib/util'); +const logger = require('../logger'); + +function parse(entry, block) { + const blockHash = util.revHex(block.hash().toString('hex')); + const merkle = util.revHex(block.merkleRoot); + + const newBlock = new BlockModel({ + hash: blockHash, + size: block.size, + height: block.height, + version: block.version, + merkleRoot: merkle, + tx: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))), + time: block.ts, + nonce: block.nonce, + bits: block.bits, + difficulty: block.bits, + chainwork: entry.chainwork, + confirmations: 0, + previousBlockHash: block.previousBlockHash, + nextBlockHash: 0, + reward: 0, + timeNormalized: block.ts, + isMainChain: true, + poolInfo: Object, + transactionCount: block.txs.length, + }); + + newBlock.save((err) => { + if (err) { + logger.log('error', err.message); + } + TxParser.parse(entry, block.txs); + }); +} + +module.exports = { + parse, +}; diff --git a/lib/node/index.js b/lib/node/index.js index becf9d3..61c2a0c 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -1,11 +1,7 @@ const FullNode = require('bcoin/lib/node/fullnode'); -const config = require('../../config'); const logger = require('../../lib/logger'); -const util = require('../../lib/util'); -const BlockModel = require('../../models/block'); -const TxModel = require('../../models/transaction').Transaction; -const InputModel = require('../../models/transaction').Input; -const OutputModel = require('../../models/transaction').Output; +const BlockParser = require('./block'); +const config = require('../../config'); const node = new FullNode(config.bcoin); @@ -20,88 +16,7 @@ function start() { node.chain.on('connect', (entry, block) => { logger.log('debug', 'New Block & Ledger Entry'); - processBlock(entry, block); - }); -} - -function processBlock(entry, block, cb) { - const blockHash = util.revHex(block.hash().toString('hex')); - - const newBlock = new BlockModel({ - hash: blockHash, - size: block.size, - height: block.height, - version: block.version, - merkleRoot: block.merkleRoot, - tx: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))), - time: block.ts, - nonce: block.nonce, - bits: block.bits, - difficulty: block.bits, - chainwork: entry.chainwork, - confirmations: 0, - previousBlockHash: block.previousBlockHash, - nextBlockHash: 0, - reward: 0, - timeNormalized: block.ts, - isMainChain: true, - poolInfo: Object, - transactionCount: block.txs.length, - }); - - newBlock.save((err) => { - if (err) { - console.log(err.message); - } - processTx(entry, block.txs); - }); -} - -function processTx(entry, txs) { - txs.forEach((tx) => { - const txHash = util.revHex(tx.hash().toString('hex')); - const blockHash = util.revHex(entry.hash); - - const t = new TxModel({ - txid: txHash, - version: 1, - lockTime: tx.lockTime, - vin: tx.inputs.map((input) => { - const inputJSON = input.toJSON(); - - return new InputModel({ - utxo: inputJSON.prevout.hash, - vout: inputJSON.prevout.index, - address: inputJSON.address, - amount: 0, - }); - }), - vout: tx.outputs.map((output) => { - const outputJSON = output.toJSON(); - - return new OutputModel({ - address: outputJSON.address, - amount: outputJSON.value, - vout: 0, - }); - }), - blockHash, - blockHeight: entry.height, - confirmations: 0, - time: entry.ts, - blockTime: entry.ts, - blockTimeNormalized: entry.ts, - valueOut: tx.value, - size: tx.size, - valueIn: tx.value, - fees: tx.fee, - chain: config.bcoin.network, - }); - t.save((err) => { - if (err) { - console.log(err.message); - } - }); + BlockParser.parse(entry, block); }); } diff --git a/lib/node/transaction.js b/lib/node/transaction.js new file mode 100644 index 0000000..765fe54 --- /dev/null +++ b/lib/node/transaction.js @@ -0,0 +1,58 @@ +const TxModel = require('../../models/transaction').Transaction; +const InputModel = require('../../models/transaction').Input; +const OutputModel = require('../../models/transaction').Output; +const config = require('../../config'); +const util = require('../../lib/util'); +const logger = require('../logger'); + +function parse(entry, txs) { + txs.forEach((tx) => { + const txHash = util.revHex(tx.hash().toString('hex')); + const blockHash = util.revHex(entry.hash); + + const t = new TxModel({ + txid: txHash, + version: 1, + lockTime: tx.lockTime, + vin: tx.inputs.map((input) => { + const inputJSON = input.toJSON(); + + return new InputModel({ + utxo: inputJSON.prevout.hash, + vout: inputJSON.prevout.index, + address: inputJSON.address, + amount: 0, + }); + }), + vout: tx.outputs.map((output) => { + const outputJSON = output.toJSON(); + + return new OutputModel({ + address: outputJSON.address, + amount: outputJSON.value, + vout: 0, + }); + }), + blockHash, + blockHeight: entry.height, + confirmations: 0, + time: entry.ts, + blockTime: entry.ts, + blockTimeNormalized: entry.ts, + valueOut: tx.value, + size: tx.size, + valueIn: tx.value, + fees: tx.fee, + chain: config.bcoin.network, + }); + t.save((err) => { + if (err) { + logger.log('error', err.message); + } + }); + }); +} + +module.exports = { + parse, +};