Models updated to match bcoin data

This commit is contained in:
tenthirtyone 2017-08-05 02:13:36 -04:00
parent d9a9b64650
commit a038159526
10 changed files with 302 additions and 106 deletions

View File

@ -7,6 +7,7 @@
"rules": {
"no-multi-spaces": 0,
"no-use-before-define": 1,
"object-shorthand": 1
"object-shorthand": 1,
"key-spacing": 0
}
}

View File

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

View File

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

View File

@ -1,4 +1,4 @@
const Block = require('./block');
const Block = require('./block');
const Transaction = require('./transaction');
module.exports = {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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