Route functions named. Node & Parsers moved to their own files
This commit is contained in:
parent
836a2c4457
commit
591d904212
@ -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.
|
||||
Reorg testing - Bcoin will handle this but we need to account for this in our mongo indexes.
|
||||
JSDoc & Unit tests
|
||||
2
index.js
2
index.js
@ -14,4 +14,4 @@ Api.listen(config.api.port, () => {
|
||||
'listening on port 3000');
|
||||
});
|
||||
|
||||
//node.start();
|
||||
node.start();
|
||||
|
||||
@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
@ -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) => {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
module.exports = function(app) {
|
||||
module.exports = function currencyAPI(app) {
|
||||
app.get('/currency', (req, res) => {
|
||||
res.send('currency');
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
module.exports = function(app) {
|
||||
module.exports = function messageAPI(app) {
|
||||
app.get('/messages/verify', (req, res) => {
|
||||
res.send('messages verify');
|
||||
});
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
module.exports = function(app) {
|
||||
module.exports = function statusAPI(app) {
|
||||
app.get('/status', (req, res) => {
|
||||
res.send('status');
|
||||
});
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
42
lib/node/block.js
Normal file
42
lib/node/block.js
Normal file
@ -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,
|
||||
};
|
||||
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
58
lib/node/transaction.js
Normal file
58
lib/node/transaction.js
Normal file
@ -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,
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user