Route functions named. Node & Parsers moved to their own files
This commit is contained in:
parent
836a2c4457
commit
591d904212
@ -53,3 +53,4 @@ Some data is stubbed. This is due to Bcoin primitives being different from Bitco
|
|||||||
|
|
||||||
# ToDo
|
# 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');
|
'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) => {
|
app.get('/addr/:addr', (req, res) => {
|
||||||
res.send(req.params.addr);
|
res.send(req.params.addr);
|
||||||
});
|
});
|
||||||
@ -8,6 +8,22 @@ module.exports = function(app) {
|
|||||||
res.send(req.params.addr);
|
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) => {
|
app.get('/addrs/:addrs/utxo', (req, res) => {
|
||||||
res.send(req.params.addrs);
|
res.send(req.params.addrs);
|
||||||
});
|
});
|
||||||
@ -23,20 +39,4 @@ module.exports = function(app) {
|
|||||||
app.post('/addrs/txs', (req, res) => {
|
app.post('/addrs/txs', (req, res) => {
|
||||||
res.send('post stub');
|
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 Block = require('../../models/block.js');
|
||||||
|
const logger = require('../logger');
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function BlockAPI(app) {
|
||||||
app.get('/block/:blockHash', (req, res) => {
|
app.get('/block/:blockHash', (req, res) => {
|
||||||
Block.find({ hash: req.params.blockHash }, (err, block) => {
|
Block.find({ hash: req.params.blockHash },
|
||||||
if (err) {
|
{ _id: 0 },
|
||||||
console.log(err);
|
(err, block) => {
|
||||||
}
|
if (err) {
|
||||||
res.send(block);
|
res.status(501).send();
|
||||||
});
|
logger.log('err', err);
|
||||||
|
}
|
||||||
|
res.send(block);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/blocks', (req, res) => {
|
app.get('/blocks', (req, res) => {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
module.exports = function(app) {
|
module.exports = function currencyAPI(app) {
|
||||||
app.get('/currency', (req, res) => {
|
app.get('/currency', (req, res) => {
|
||||||
res.send('currency');
|
res.send('currency');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
module.exports = function(app) {
|
module.exports = function messageAPI(app) {
|
||||||
app.get('/messages/verify', (req, res) => {
|
app.get('/messages/verify', (req, res) => {
|
||||||
res.send('messages verify');
|
res.send('messages verify');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
module.exports = function(app) {
|
module.exports = function statusAPI(app) {
|
||||||
app.get('/status', (req, res) => {
|
app.get('/status', (req, res) => {
|
||||||
res.send('status');
|
res.send('status');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
const Transaction = require('../../models/transaction.js').Transaction;
|
const Transaction = require('../../models/transaction.js').Transaction;
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function transactionAPI(app) {
|
||||||
app.get('/tx/:txid', (req, res) => {
|
app.get('/tx/:txid', (req, res) => {
|
||||||
res.send(req.params.txid);
|
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 FullNode = require('bcoin/lib/node/fullnode');
|
||||||
const config = require('../../config');
|
|
||||||
const logger = require('../../lib/logger');
|
const logger = require('../../lib/logger');
|
||||||
const util = require('../../lib/util');
|
const BlockParser = require('./block');
|
||||||
const BlockModel = require('../../models/block');
|
const config = require('../../config');
|
||||||
const TxModel = require('../../models/transaction').Transaction;
|
|
||||||
const InputModel = require('../../models/transaction').Input;
|
|
||||||
const OutputModel = require('../../models/transaction').Output;
|
|
||||||
|
|
||||||
const node = new FullNode(config.bcoin);
|
const node = new FullNode(config.bcoin);
|
||||||
|
|
||||||
@ -20,88 +16,7 @@ function start() {
|
|||||||
node.chain.on('connect', (entry, block) => {
|
node.chain.on('connect', (entry, block) => {
|
||||||
logger.log('debug',
|
logger.log('debug',
|
||||||
'New Block & Ledger Entry');
|
'New Block & Ledger Entry');
|
||||||
processBlock(entry, block);
|
BlockParser.parse(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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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