From a0381595265b54eab772ce535063b35aeee1e000 Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Sat, 5 Aug 2017 02:13:36 -0400 Subject: [PATCH] Models updated to match bcoin data --- .eslintrc.json | 3 +- lib/api/index.js | 2 +- lib/parser/block.js | 44 ++++---- lib/parser/index.js | 2 +- lib/parser/transaction.js | 50 ++++----- lib/util/index.js | 16 +++ models/block.js | 42 +++---- models/transaction.js | 51 +++++---- test/data/bcoin-blocks-toJSON.json | 174 +++++++++++++++++++++++++++++ test/data/bcoin-tx-toJSON.json | 24 ++++ 10 files changed, 302 insertions(+), 106 deletions(-) create mode 100644 test/data/bcoin-tx-toJSON.json diff --git a/.eslintrc.json b/.eslintrc.json index d3d1938..e66ade8 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,6 +7,7 @@ "rules": { "no-multi-spaces": 0, "no-use-before-define": 1, - "object-shorthand": 1 + "object-shorthand": 1, + "key-spacing": 0 } } \ No newline at end of file diff --git a/lib/api/index.js b/lib/api/index.js index 5c59858..ebf33c7 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -1,7 +1,7 @@ const express = require('express'); const config = require('../../config'); -const app = express(); +const app = express(); app.set('json spaces', config.api.json_spaces); diff --git a/lib/parser/block.js b/lib/parser/block.js index a79b185..a66931e 100644 --- a/lib/parser/block.js +++ b/lib/parser/block.js @@ -1,34 +1,30 @@ const BlockModel = require('../../models/block'); const TxParser = require('./transaction'); +const config = require('../../config'); 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 rawBlock = block.toRaw().toString('hex'); - console.log(block.toJSON().txs); + const rawBlock = block.toRaw().toString('hex'); + const json = block.toJSON(); + const reward = util.calcBlockReward(entry.height); + const newBlock = new BlockModel({ - hash: blockHash, - size: block.size, - height: entry.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: {}, - transactionCount: block.txs.length, - rawBlock: rawBlock, + hash: json.hash, + height: entry.height, + version: json.version, + size: block.size, + prevBlock: json.prevBlock, + merkleRoot: json.merkleRoot, + ts: json.ts, + bits: json.bits, + nonce: json.nonce, + txs: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))), + chainwork: entry.chainwork, + reward: reward, + network: config.bcoin.network, + poolInfo: {}, + rawBlock: rawBlock, }); newBlock.save((err) => { diff --git a/lib/parser/index.js b/lib/parser/index.js index 3135cbf..8989195 100644 --- a/lib/parser/index.js +++ b/lib/parser/index.js @@ -1,4 +1,4 @@ -const Block = require('./block'); +const Block = require('./block'); const Transaction = require('./transaction'); module.exports = { diff --git a/lib/parser/transaction.js b/lib/parser/transaction.js index 1631da9..5c21ffe 100644 --- a/lib/parser/transaction.js +++ b/lib/parser/transaction.js @@ -7,44 +7,42 @@ const logger = require('../logger'); function parse(entry, txs) { txs.forEach((tx) => { - const txHash = util.revHex(tx.hash().toString('hex')); + const txHash = util.revHex(tx.hash().toString('hex')); const blockHash = util.revHex(entry.hash); + const json = tx.toJSON(); const t = new TxModel({ - txid: txHash, - version: 1, - lockTime: tx.lockTime, - vin: tx.inputs.map((input) => { + hash: json.hash, + witnessHash: json.witnessHash, + fee: json.fee, + rate: json.rate, + ps: json.ps, + height: entry.height, + block: entry.hash, + ts: entry.ts, + date: json.date, + index: json.index, + version: json.version, + flag: json.flag, + inputs: tx.inputs.map((input) => { const inputJSON = input.toJSON(); - - //console.log(inputJSON); - return new InputModel({ - utxo: inputJSON.prevout.hash, - vout: inputJSON.prevout.index, - address: inputJSON.address, - amount: 0, + prevout: inputJSON.prevout, + script: inputJSON.script, + witness: inputJSON.witness, + sequence: inputJSON.sequence, + address: inputJSON.address, }); }), - vout: tx.outputs.map((output) => { + outputs: tx.outputs.map((output) => { const outputJSON = output.toJSON(); - return new OutputModel({ address: outputJSON.address, - amount: outputJSON.value, - vout: 0, + script: outputJSON.script, + value: outputJSON.value, }); }), - 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, + lockTime: json.locktime, chain: config.bcoin.network, }); t.save((err) => { diff --git a/lib/util/index.js b/lib/util/index.js index 890b14e..5215dcf 100644 --- a/lib/util/index.js +++ b/lib/util/index.js @@ -7,6 +7,22 @@ function revHex(hex) { return rev; } +function calcBlockReward(height) { + let halvenings = Math.floor(height / 210000); + let reward = 50 * 1e8; + if (halvenings >= 64) { + return 0; + } + + while (halvenings > 0) { + halvenings -= 1; + reward /= 2; + } + return reward; +} + module.exports = { revHex, + calcBlockReward, }; + diff --git a/models/block.js b/models/block.js index 0103e71..73b7404 100644 --- a/models/block.js +++ b/models/block.js @@ -1,35 +1,23 @@ const mongoose = require('mongoose'); -const Schema = mongoose.Schema; - -hash: '0000000000004bb2eda7530f52bf5566161b6d74e752afdaf9656e16c96928ae', - height: undefined, - version: 1, - prevBlock: '00000000000020304684a71d9b8a736c5088d76fb4c6d864f04bcb75d2e20fe4', - merkleRoot: '7c09ce1e6821d9daf04f7aa820a97449005e1a9fb98fedb6504707d08ac1b455', - ts: 1302990080, - bits: 453036989, - nonce: 3214150888, +const Schema = mongoose.Schema; const BlockSchema = new Schema({ - hash: String, - height: Number, - version: Number, - size: Number, - prevBlock: String, + hash: String, + height: Number, + version: Number, + size: Number, + prevBlock: String, merkleRoot: String, - ts: Number, - bits: Number, - nonce: Number, - tx: Array, - difficulty: Number, - chainwork: Number, - nextBlockHash: String, - reward: Number, - network: String, - poolInfo: Object, - txCount: Number, - rawBlock: String, + ts: Number, + bits: Number, + nonce: Number, + txs: Array, + chainwork: Number, + reward: Number, + network: String, + poolInfo: Object, + rawBlock: String, }); const Block = mongoose.model('Block', BlockSchema); diff --git a/models/transaction.js b/models/transaction.js index 17c8c58..900bd5d 100644 --- a/models/transaction.js +++ b/models/transaction.js @@ -1,43 +1,42 @@ const mongoose = require('mongoose'); + const Schema = mongoose.Schema; const InputSchema = new Schema({ - utxo: String, - vout: Number, - address: String, - amount: Number, + prevout: Object, + script: String, + witness: String, + sequence: Number, + address: String, }); const OutputSchema = new Schema({ address: String, - amount: Number, - vout: Number, + script: String, + value: Number, }); const TransactionSchema = new Schema({ - txid: String, - version: Number, - lockTime: Number, - vin: [InputSchema], - vout: [OutputSchema], - blockHash: String, - blockHeight: Number, - confirmations: Number, - time: Date, - blockTime: Date, - blockTimeNormalized: Date, - valueOut: Number, - size: Number, - valueIn: Number, - fees: Number, - chain: String, + hash: String, + witnessHash: String, + fee: Number, + rate: Number, + ps: Number, + height: Number, + block: String, + index: Number, + version: Number, + flag: Number, + lockTime: Number, + inputs: [InputSchema], + outputs: [OutputSchema], + size: Number, + network: String, }); -TransactionSchema.index({ txid: 1 }, { unique: true }); - const Transaction = mongoose.model('Transaction', TransactionSchema); -const Input = mongoose.model('Input', InputSchema); -const Output = mongoose.model('Output', OutputSchema); +const Input = mongoose.model('Input', InputSchema); +const Output = mongoose.model('Output', OutputSchema); module.exports = { Transaction, diff --git a/test/data/bcoin-blocks-toJSON.json b/test/data/bcoin-blocks-toJSON.json index e69de29..e0072ec 100644 --- a/test/data/bcoin-blocks-toJSON.json +++ b/test/data/bcoin-blocks-toJSON.json @@ -0,0 +1,174 @@ +{ hash: '0000000000004bb2eda7530f52bf5566161b6d74e752afdaf9656e16c96928ae', + height: undefined, + version: 1, + prevBlock: '00000000000020304684a71d9b8a736c5088d76fb4c6d864f04bcb75d2e20fe4', + merkleRoot: '7c09ce1e6821d9daf04f7aa820a97449005e1a9fb98fedb6504707d08ac1b455', + ts: 1302990080, + bits: 453036989, + nonce: 3214150888, + txs: + [ { hash: '2c39e95223360e90a0e9a8d28a25957df7fcbb5b50d878085dabbd09394e6137', + witnessHash: '2c39e95223360e90a0e9a8d28a25957df7fcbb5b50d878085dabbd09394e6137', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 0, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: 'b0d7f8f30fc7c55e958d5e546ca057beec1f5f751185971bd0c8a71642df19ef', + witnessHash: 'b0d7f8f30fc7c55e958d5e546ca057beec1f5f751185971bd0c8a71642df19ef', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 1, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '6ce52f37ed388b6599432c62a79ed33a22f33437e82dd13b2bc545cc08473c86', + witnessHash: '6ce52f37ed388b6599432c62a79ed33a22f33437e82dd13b2bc545cc08473c86', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 2, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '1579689efe71a8845c6b5c8243d6c8ba48bcf036dbd672b68e747d47a29dc5f5', + witnessHash: '1579689efe71a8845c6b5c8243d6c8ba48bcf036dbd672b68e747d47a29dc5f5', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 3, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '6e0f6701fb70b37ba4a6559265587c6ff152ee69080333420de5069b67c685e5', + witnessHash: '6e0f6701fb70b37ba4a6559265587c6ff152ee69080333420de5069b67c685e5', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 4, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '908df304c50ca70feecbb43b357889aa89972ded7160d557b02719fb98075369', + witnessHash: '908df304c50ca70feecbb43b357889aa89972ded7160d557b02719fb98075369', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 5, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '773283249deafdc797fb611e974cafa7156d3325bf33b6cd18751d4939b6cb29', + witnessHash: '773283249deafdc797fb611e974cafa7156d3325bf33b6cd18751d4939b6cb29', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 6, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '74ce24969c4f56050c518c5c025247f1696abd6e5ab3b2f0d19a4518fc645953', + witnessHash: '74ce24969c4f56050c518c5c025247f1696abd6e5ab3b2f0d19a4518fc645953', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 7, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: 'f73647a6235d079623e7cce68fef65f140051b428a3a136aef29c0b813904e2c', + witnessHash: 'f73647a6235d079623e7cce68fef65f140051b428a3a136aef29c0b813904e2c', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 8, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '58a0875204b972280046aa8fb41701e4fa3a111c4a518b3b2166f03be43a1f27', + witnessHash: '58a0875204b972280046aa8fb41701e4fa3a111c4a518b3b2166f03be43a1f27', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 9, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 }, + { hash: '14859f37cd4b53694c63747264959db40f77ef0c17507e9cad486cb997a1fddf', + witnessHash: '14859f37cd4b53694c63747264959db40f77ef0c17507e9cad486cb997a1fddf', + fee: undefined, + rate: undefined, + ps: 1501905056, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: 10, + version: 1, + flag: 1, + inputs: [Array], + outputs: [Array], + locktime: 0 } ] } diff --git a/test/data/bcoin-tx-toJSON.json b/test/data/bcoin-tx-toJSON.json new file mode 100644 index 0000000..708351a --- /dev/null +++ b/test/data/bcoin-tx-toJSON.json @@ -0,0 +1,24 @@ +{ hash: 'f6f9e5e17dcb3ee11275db84b2f92c53b3850daca0858221be17ac34ba489e07', + witnessHash: 'f6f9e5e17dcb3ee11275db84b2f92c53b3850daca0858221be17ac34ba489e07', + fee: undefined, + rate: undefined, + ps: 1501910880, + height: undefined, + block: undefined, + ts: undefined, + date: undefined, + index: undefined, + version: 1, + flag: 1, + inputs: + [ { prevout: [Object], + script: '04ffff001d024104', + witness: '00', + sequence: 4294967295, + address: undefined, + coin: undefined } ], + outputs: + [ { value: 5000000000, + script: '410439ce830b9605d527662e5631caf3b7e89625e3d95a30765168c2aaa788783b129ff29e3ff3d890374fb85854f0c8621d0cbb097ab50b6196522e7e4ef80e0af3ac', + address: '17hTgWr2uuhH5Ke9boSnfd66JzxR9N6hUC' } ], + locktime: 0 }