Models updated to match bcoin data
This commit is contained in:
parent
d9a9b64650
commit
a038159526
@ -7,6 +7,7 @@
|
|||||||
"rules": {
|
"rules": {
|
||||||
"no-multi-spaces": 0,
|
"no-multi-spaces": 0,
|
||||||
"no-use-before-define": 1,
|
"no-use-before-define": 1,
|
||||||
"object-shorthand": 1
|
"object-shorthand": 1,
|
||||||
|
"key-spacing": 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.set('json spaces', config.api.json_spaces);
|
app.set('json spaces', config.api.json_spaces);
|
||||||
|
|
||||||
|
|||||||
@ -1,34 +1,30 @@
|
|||||||
const BlockModel = require('../../models/block');
|
const BlockModel = require('../../models/block');
|
||||||
const TxParser = require('./transaction');
|
const TxParser = require('./transaction');
|
||||||
|
const config = require('../../config');
|
||||||
const util = require('../../lib/util');
|
const util = require('../../lib/util');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
|
|
||||||
function parse(entry, block) {
|
function parse(entry, block) {
|
||||||
const blockHash = util.revHex(block.hash().toString('hex'));
|
const rawBlock = block.toRaw().toString('hex');
|
||||||
const merkle = util.revHex(block.merkleRoot);
|
const json = block.toJSON();
|
||||||
const rawBlock = block.toRaw().toString('hex');
|
const reward = util.calcBlockReward(entry.height);
|
||||||
console.log(block.toJSON().txs);
|
|
||||||
const newBlock = new BlockModel({
|
const newBlock = new BlockModel({
|
||||||
hash: blockHash,
|
hash: json.hash,
|
||||||
size: block.size,
|
height: entry.height,
|
||||||
height: entry.height,
|
version: json.version,
|
||||||
version: block.version,
|
size: block.size,
|
||||||
merkleRoot: merkle,
|
prevBlock: json.prevBlock,
|
||||||
tx: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))),
|
merkleRoot: json.merkleRoot,
|
||||||
time: block.ts,
|
ts: json.ts,
|
||||||
nonce: block.nonce,
|
bits: json.bits,
|
||||||
bits: block.bits,
|
nonce: json.nonce,
|
||||||
difficulty: block.bits,
|
txs: block.txs.map(tx => util.revHex(tx.hash().toString('hex'))),
|
||||||
chainwork: entry.chainwork,
|
chainwork: entry.chainwork,
|
||||||
confirmations: 0,
|
reward: reward,
|
||||||
previousBlockHash: block.previousBlockHash,
|
network: config.bcoin.network,
|
||||||
nextBlockHash: 0,
|
poolInfo: {},
|
||||||
reward: 0,
|
rawBlock: rawBlock,
|
||||||
timeNormalized: block.ts,
|
|
||||||
isMainChain: true,
|
|
||||||
poolInfo: {},
|
|
||||||
transactionCount: block.txs.length,
|
|
||||||
rawBlock: rawBlock,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
newBlock.save((err) => {
|
newBlock.save((err) => {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
const Block = require('./block');
|
const Block = require('./block');
|
||||||
const Transaction = require('./transaction');
|
const Transaction = require('./transaction');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@ -7,44 +7,42 @@ const logger = require('../logger');
|
|||||||
|
|
||||||
function parse(entry, txs) {
|
function parse(entry, txs) {
|
||||||
txs.forEach((tx) => {
|
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 blockHash = util.revHex(entry.hash);
|
||||||
|
const json = tx.toJSON();
|
||||||
|
|
||||||
const t = new TxModel({
|
const t = new TxModel({
|
||||||
txid: txHash,
|
hash: json.hash,
|
||||||
version: 1,
|
witnessHash: json.witnessHash,
|
||||||
lockTime: tx.lockTime,
|
fee: json.fee,
|
||||||
vin: tx.inputs.map((input) => {
|
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();
|
const inputJSON = input.toJSON();
|
||||||
|
|
||||||
//console.log(inputJSON);
|
|
||||||
|
|
||||||
return new InputModel({
|
return new InputModel({
|
||||||
utxo: inputJSON.prevout.hash,
|
prevout: inputJSON.prevout,
|
||||||
vout: inputJSON.prevout.index,
|
script: inputJSON.script,
|
||||||
address: inputJSON.address,
|
witness: inputJSON.witness,
|
||||||
amount: 0,
|
sequence: inputJSON.sequence,
|
||||||
|
address: inputJSON.address,
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
vout: tx.outputs.map((output) => {
|
outputs: tx.outputs.map((output) => {
|
||||||
const outputJSON = output.toJSON();
|
const outputJSON = output.toJSON();
|
||||||
|
|
||||||
return new OutputModel({
|
return new OutputModel({
|
||||||
address: outputJSON.address,
|
address: outputJSON.address,
|
||||||
amount: outputJSON.value,
|
script: outputJSON.script,
|
||||||
vout: 0,
|
value: outputJSON.value,
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
blockHash,
|
lockTime: json.locktime,
|
||||||
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,
|
chain: config.bcoin.network,
|
||||||
});
|
});
|
||||||
t.save((err) => {
|
t.save((err) => {
|
||||||
|
|||||||
@ -7,6 +7,22 @@ function revHex(hex) {
|
|||||||
return rev;
|
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 = {
|
module.exports = {
|
||||||
revHex,
|
revHex,
|
||||||
|
calcBlockReward,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,35 +1,23 @@
|
|||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
hash: '0000000000004bb2eda7530f52bf5566161b6d74e752afdaf9656e16c96928ae',
|
|
||||||
height: undefined,
|
|
||||||
version: 1,
|
|
||||||
prevBlock: '00000000000020304684a71d9b8a736c5088d76fb4c6d864f04bcb75d2e20fe4',
|
|
||||||
merkleRoot: '7c09ce1e6821d9daf04f7aa820a97449005e1a9fb98fedb6504707d08ac1b455',
|
|
||||||
ts: 1302990080,
|
|
||||||
bits: 453036989,
|
|
||||||
nonce: 3214150888,
|
|
||||||
|
|
||||||
const BlockSchema = new Schema({
|
const BlockSchema = new Schema({
|
||||||
hash: String,
|
hash: String,
|
||||||
height: Number,
|
height: Number,
|
||||||
version: Number,
|
version: Number,
|
||||||
size: Number,
|
size: Number,
|
||||||
prevBlock: String,
|
prevBlock: String,
|
||||||
merkleRoot: String,
|
merkleRoot: String,
|
||||||
ts: Number,
|
ts: Number,
|
||||||
bits: Number,
|
bits: Number,
|
||||||
nonce: Number,
|
nonce: Number,
|
||||||
tx: Array,
|
txs: Array,
|
||||||
difficulty: Number,
|
chainwork: Number,
|
||||||
chainwork: Number,
|
reward: Number,
|
||||||
nextBlockHash: String,
|
network: String,
|
||||||
reward: Number,
|
poolInfo: Object,
|
||||||
network: String,
|
rawBlock: String,
|
||||||
poolInfo: Object,
|
|
||||||
txCount: Number,
|
|
||||||
rawBlock: String,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const Block = mongoose.model('Block', BlockSchema);
|
const Block = mongoose.model('Block', BlockSchema);
|
||||||
|
|||||||
@ -1,43 +1,42 @@
|
|||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
const InputSchema = new Schema({
|
const InputSchema = new Schema({
|
||||||
utxo: String,
|
prevout: Object,
|
||||||
vout: Number,
|
script: String,
|
||||||
address: String,
|
witness: String,
|
||||||
amount: Number,
|
sequence: Number,
|
||||||
|
address: String,
|
||||||
});
|
});
|
||||||
|
|
||||||
const OutputSchema = new Schema({
|
const OutputSchema = new Schema({
|
||||||
address: String,
|
address: String,
|
||||||
amount: Number,
|
script: String,
|
||||||
vout: Number,
|
value: Number,
|
||||||
});
|
});
|
||||||
|
|
||||||
const TransactionSchema = new Schema({
|
const TransactionSchema = new Schema({
|
||||||
txid: String,
|
hash: String,
|
||||||
version: Number,
|
witnessHash: String,
|
||||||
lockTime: Number,
|
fee: Number,
|
||||||
vin: [InputSchema],
|
rate: Number,
|
||||||
vout: [OutputSchema],
|
ps: Number,
|
||||||
blockHash: String,
|
height: Number,
|
||||||
blockHeight: Number,
|
block: String,
|
||||||
confirmations: Number,
|
index: Number,
|
||||||
time: Date,
|
version: Number,
|
||||||
blockTime: Date,
|
flag: Number,
|
||||||
blockTimeNormalized: Date,
|
lockTime: Number,
|
||||||
valueOut: Number,
|
inputs: [InputSchema],
|
||||||
size: Number,
|
outputs: [OutputSchema],
|
||||||
valueIn: Number,
|
size: Number,
|
||||||
fees: Number,
|
network: String,
|
||||||
chain: String,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
TransactionSchema.index({ txid: 1 }, { unique: true });
|
|
||||||
|
|
||||||
const Transaction = mongoose.model('Transaction', TransactionSchema);
|
const Transaction = mongoose.model('Transaction', TransactionSchema);
|
||||||
const Input = mongoose.model('Input', InputSchema);
|
const Input = mongoose.model('Input', InputSchema);
|
||||||
const Output = mongoose.model('Output', OutputSchema);
|
const Output = mongoose.model('Output', OutputSchema);
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
Transaction,
|
Transaction,
|
||||||
|
|||||||
@ -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 } ] }
|
||||||
24
test/data/bcoin-tx-toJSON.json
Normal file
24
test/data/bcoin-tx-toJSON.json
Normal 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 }
|
||||||
Loading…
Reference in New Issue
Block a user