From d7a70d54568c42fba03eda67cba82129ee11f754 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Sat, 5 Aug 2017 20:36:17 -0400 Subject: [PATCH] blocks & tx routes mostly complete updating models for need --- README.md | 8 +++--- config/index.js | 2 +- lib/api/block.js | 48 +++++++++++++++++++++++++++++------ lib/api/currency.js | 5 ++-- lib/api/status.js | 28 ++++++++++++++++++--- lib/api/transaction.js | 53 ++++++++++++++++++++++++++++++++++----- lib/node/index.js | 6 +++++ lib/parser/block.js | 2 +- lib/parser/transaction.js | 2 +- models/block.js | 7 +++++- models/transaction.js | 1 + test/data/bcoin-tx.json | 27 -------------------- 12 files changed, 134 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index aedf981..e006682 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,12 @@ The API is configured to run on port 3000 by default. Use the standard Nginx rev * X /block/:blockhash * X /blocks * X /block-index/:blockHeight -* /currency -* /version +* X /currency +* X /version * /status * /sync -* /peer -* /tx/:txId +* X /peer +* X /tx/:txId * /txs * /txs diff --git a/config/index.js b/config/index.js index cbbcfab..7830f23 100644 --- a/config/index.js +++ b/config/index.js @@ -1,5 +1,5 @@ const config = { - full_node: false, + full_node: true, logging: 'debug', bcoin: { network: 'main', diff --git a/lib/api/block.js b/lib/api/block.js index 20b5b53..7612d1d 100644 --- a/lib/api/block.js +++ b/lib/api/block.js @@ -26,7 +26,30 @@ module.exports = function BlockAPI(router) { res.status(501).send(); logger.log('err', err); } - res.json(block[0]); + if (block[0]) { + const b = block[0]; + res.json({ + hash: b.hash, + size: b.size, + height: b.height, + version: b.version, + merkleroot: b.merkleRoot, + tx: b.txs, + time: b.ts, + nonce: b.nonce, + bits: b.bits.toString(16), + difficulty: 1, + chainwork: b.chainwork.toString(16), + confirmations: 0, + previousblockhash: b.prevBlock, + nextblockhash: 0, + reward: b.reward / 1e8, + isMainChain: true, + poolInfo: {}, + }); + } else { + res.send(); + } }); }); @@ -36,8 +59,8 @@ module.exports = function BlockAPI(router) { { height: 1, size: 1, hash: 1, - time: 1, - transactionCount: 1, + ts: 1, + txs: 1, poolInfo: 1 }, (err, blocks) => { if (err) { @@ -49,11 +72,13 @@ module.exports = function BlockAPI(router) { return { hash: block.hash, height: block.height, - time: block.time, - txlength: block.transactionCount + size: block.size, + time: block.ts, + txlength: block.txs.length, + poolInfo: {}, }; }), - lenght: blocks.length, + length: blocks.length, pagination: {}, }); }); @@ -75,13 +100,20 @@ module.exports = function BlockAPI(router) { router.get('/block-index/:height', (req, res) => { getBlock( { height: req.params.height }, - {}, + { hash: 1 }, (err, block) => { if (err) { res.status(501).send(); logger.log('err', err); } - res.json(block[0]); + + if (block[0]) { + res.json({ + blockHash: block[0].hash, + }); + } else { + res.send(); + } }); }); }; diff --git a/lib/api/currency.js b/lib/api/currency.js index ab06f80..9c932f0 100644 --- a/lib/api/currency.js +++ b/lib/api/currency.js @@ -24,9 +24,9 @@ function getRate() { lastRate = ticker.last; logger.log('debug', `getRate: ${lastRate}`); - } catch (err) { + } catch (error) { logger.log('error', - `getRate: ${err}`); + `getRate: ${error}`); } }); } @@ -36,6 +36,5 @@ module.exports = function currencyAPI(app) { res.json({ bitstamp: lastRate, }); - }); }; diff --git a/lib/api/status.js b/lib/api/status.js index 5348e13..a6833d5 100644 --- a/lib/api/status.js +++ b/lib/api/status.js @@ -1,6 +1,22 @@ +const pkg = require('../../package.json'); + module.exports = function statusAPI(router) { router.get('/status', (req, res) => { - res.send('status'); + res.json({ + info: { + version: 120100, + protocolversion: 70012, + blocks: 479275, + timeoffset: 0, + connections: 79, + proxy: '', + difficulty: 8.60222E11, + testnet: false, + relayfee: 1.0E-5, + errors: "Warning: Unknown block versions being mined! It's possible unknown rules are in effect", + network: 'livenet', + }, + }); }); router.get('/sync', (req, res) => { @@ -8,10 +24,16 @@ module.exports = function statusAPI(router) { }); router.get('/peer', (req, res) => { - res.send('peer'); + res.json({ + connected: true, + host: '127.0.0.1', + port: null, + }); }); router.get('/version', (req, res) => { - res.send('version'); + res.json({ + version: pkg.version, + }); }); }; diff --git a/lib/api/transaction.js b/lib/api/transaction.js index 41aa451..3fd1e7d 100644 --- a/lib/api/transaction.js +++ b/lib/api/transaction.js @@ -1,4 +1,5 @@ const Transaction = require('../../models/transaction.js').Transaction; +const logger = require('../logger'); const MAX_TXS = 200; @@ -17,12 +18,52 @@ function getTransaction(params, options, cb) { module.exports = function transactionAPI(router) { router.get('/tx/:txid', (req, res) => { - Transaction.find({ txid: req.params.txid }, (err, tx) => { - if (err) { - res.status(501).send(); - } - res.send(tx); - }); + getTransaction( + { hash: req.params.txid }, + { }, + (err, tx) => { + if (err) { + res.status(501).send(); + logger.log('err', err); + } + if (tx[0]) { + const t = tx[0]; + + res.json({ + txid: t.hash, + version: t.version, + locktime: t.lockTime, + vin: t.inputs.map(input => ({ + coinbase: input.script, + sequence: input.sequence, + n: 0, + })), + vout: t.outputs.map(output => ({ + value: output.value / 1e8, + n: 0, + scriptPubKey: { + hex: output.script, + asm: '', + addresses: [output.address], + type: null, + }, + spentTxId: null, + spentIndex: null, + spentHeight: null, + })), + blockhash: t.block, + blockheight: t.height, + confirmations: 0, + time: 0, + blocktime: 0, + isCoinBase: false, + valueOut: t.outputs.reduce((a, b) => a.value + b.value).value / 1e8, + size: 0, + }); + } else { + res.send(); + } + }); }); router.get('/txs', (req, res) => { diff --git a/lib/node/index.js b/lib/node/index.js index a5da6aa..4efc2a1 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -5,6 +5,8 @@ const config = require('../../config'); const node = new FullNode(config.bcoin); +let max = 0; + function start() { node.open() .then(() => { @@ -16,6 +18,10 @@ function start() { node.chain.on('connect', (entry, block) => { logger.log('debug', 'New Block & Ledger Entry'); + if (max < entry.height) { + max = entry.height; + } + console.log(max); BlockParser.parse(entry, block); }); diff --git a/lib/parser/block.js b/lib/parser/block.js index b90908d..1ccfe3b 100644 --- a/lib/parser/block.js +++ b/lib/parser/block.js @@ -12,8 +12,8 @@ function parse(entry, block) { const newBlock = new BlockModel({ hash: blockJSON.hash, height: entry.height, + size: block.getSize(), version: blockJSON.version, - size: block.size, prevBlock: blockJSON.prevBlock, merkleRoot: blockJSON.merkleRoot, ts: blockJSON.ts, diff --git a/lib/parser/transaction.js b/lib/parser/transaction.js index 82088e9..c7d4bb2 100644 --- a/lib/parser/transaction.js +++ b/lib/parser/transaction.js @@ -16,7 +16,7 @@ function parse(entry, txs) { rate: txJSON.rate, ps: txJSON.ps, height: entry.height, - block: entry.hash, + block: util.revHex(entry.hash), ts: entry.ts, date: txJSON.date, index: txJSON.index, diff --git a/models/block.js b/models/block.js index 73b7404..415eeba 100644 --- a/models/block.js +++ b/models/block.js @@ -5,8 +5,8 @@ const Schema = mongoose.Schema; const BlockSchema = new Schema({ hash: String, height: Number, - version: Number, size: Number, + version: Number, prevBlock: String, merkleRoot: String, ts: Number, @@ -18,6 +18,11 @@ const BlockSchema = new Schema({ network: String, poolInfo: Object, rawBlock: String, +}, { + toJSON: { + virtuals: true, + }, + id: false, }); const Block = mongoose.model('Block', BlockSchema); diff --git a/models/transaction.js b/models/transaction.js index 900bd5d..979760e 100644 --- a/models/transaction.js +++ b/models/transaction.js @@ -14,6 +14,7 @@ const OutputSchema = new Schema({ address: String, script: String, value: Number, + type: String, }); const TransactionSchema = new Schema({ diff --git a/test/data/bcoin-tx.json b/test/data/bcoin-tx.json index eb4301e..7b30cb2 100644 --- a/test/data/bcoin-tx.json +++ b/test/data/bcoin-tx.json @@ -29,30 +29,3 @@ script: , address: } ], locktime: 0 } - -toJSON(): - -{ hash: '0fa53ce8b5aa3eb9e46a9dde25c26df02f4b0cf11eb0920be2780a8c7b9a6e33', - witnessHash: '0fa53ce8b5aa3eb9e46a9dde25c26df02f4b0cf11eb0920be2780a8c7b9a6e33', - fee: undefined, - rate: undefined, - ps: 1501788263, - height: undefined, - block: undefined, - ts: undefined, - date: undefined, - index: undefined, - version: 1, - flag: 1, - inputs: - [ { prevout: [Object], - script: '04ffff001d02de00', - witness: '00', - sequence: 4294967295, - address: undefined, - coin: undefined } ], - outputs: - [ { value: 5000000000, - script: '41042200490195c16c77e0d5b28079e218ddf23c0cf23e770b5e1ad0589d03a803cc188a1c79601dd181a67dc121a2bf2c5b2313c89f2b2be758caca6c7ab56959acac', - address: '15WLVXrj2owbChece54ibqqukk2PcA4TLt' } ], - locktime: 0 }