blocks & tx routes mostly complete updating models for need

This commit is contained in:
tenthirtyone 2017-08-05 20:36:17 -04:00
parent 7948a63875
commit d7a70d5456
12 changed files with 134 additions and 55 deletions

View File

@ -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

View File

@ -1,5 +1,5 @@
const config = {
full_node: false,
full_node: true,
logging: 'debug',
bcoin: {
network: 'main',

View File

@ -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();
}
});
});
};

View File

@ -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,
});
});
};

View File

@ -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,
});
});
};

View File

@ -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) => {

View File

@ -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);
});

View File

@ -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,

View File

@ -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,

View File

@ -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);

View File

@ -14,6 +14,7 @@ const OutputSchema = new Schema({
address: String,
script: String,
value: Number,
type: String,
});
const TransactionSchema = new Schema({

View File

@ -29,30 +29,3 @@
script: <Script: 0x41 0x042200490195c16c77e0d5b28079e218ddf23c0cf23e770b5e1ad0589d03a803cc188a1c79601dd181a67dc121a2bf2c5b2313c89f2b2be758caca6c7ab56959ac OP_CHECKSIG>,
address: <Address: type=pubkeyhash version=-1 str=15WLVXrj2owbChece54ibqqukk2PcA4TLt> } ],
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 }