New models and parser up

This commit is contained in:
tenthirtyone 2017-08-17 17:44:47 -04:00
parent d2f68fefe9
commit 9b7336b066
5 changed files with 120 additions and 86 deletions

View File

@ -1,67 +1,32 @@
const BlockModel = require('../../models/block');
const InputModel = require('../../models/input');
const OutputModel = require('../../models/output');
const config = require('../../config');
const util = require('../../lib/util');
const logger = require('../logger');
const config = require('../../config');
const util = require('../../lib/util');
const logger = require('../logger');
function parse(entry, block) {
const rawBlock = block.toRaw().toString('hex');
const rawBlock = block.toRaw().toString('hex');
const blockJSON = block.toJSON();
const reward = util.calcBlockReward(entry.height);
const reward = util.calcBlockReward(entry.height);
// Can probably use destructuring to build something nicer
const newBlock = new BlockModel({
hash: blockJSON.hash,
height: entry.height,
size: block.getSize(),
version: blockJSON.version,
prevBlock: blockJSON.prevBlock,
hash: blockJSON.hash,
height: entry.height,
size: block.getSize(),
version: blockJSON.version,
prevBlock: blockJSON.prevBlock,
merkleRoot: blockJSON.merkleRoot,
ts: blockJSON.ts,
bits: blockJSON.bits,
nonce: blockJSON.nonce,
txs: block.txs.map((tx) => {
ts: blockJSON.ts,
bits: blockJSON.bits,
nonce: blockJSON.nonce,
txs: block.txs.map((tx) => {
const txJSON = tx.toJSON();
return {
hash: txJSON.hash,
witnessHash: txJSON.witnessHash,
fee: txJSON.fee,
rate: txJSON.rate,
ps: txJSON.ps,
height: entry.height,
block: util.revHex(entry.hash),
ts: entry.ts,
date: txJSON.date,
index: txJSON.index,
version: txJSON.version,
flag: txJSON.flag,
inputs: tx.inputs.map((input) => {
const inputJSON = input.toJSON();
return new InputModel({
prevout: inputJSON.prevout,
script: inputJSON.script,
witness: inputJSON.witness,
sequence: inputJSON.sequence,
address: inputJSON.address,
});
}),
outputs: tx.outputs.map((output) => {
const outputJSON = output.toJSON();
return new OutputModel({
address: outputJSON.address,
script: outputJSON.script,
value: outputJSON.value,
});
}),
lockTime: txJSON.locktime,
chain: config.bcoin.network,
};
return txJSON.hash;
}),
chainwork: entry.chainwork,
chainwork: entry.chainwork,
reward,
network: config.bcoin.network,
poolInfo: {},
network: config.bcoin.network,
poolInfo: {},
rawBlock,
});

View File

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

View File

@ -0,0 +1,67 @@
const TxModel = require('../../models/transaction');
const InputModel = require('../../models/input');
const OutputModel = require('../../models/output');
const config = require('../../config');
const util = require('../../lib/util');
const logger = require('../logger');
const db = require('../db');
// Bleh, Bcoin pulls in blocks 20 at a time
// Crappy delay for now otherwise async saves
// could miss a tx if an input refs a block within
// the last 20 that hasn't saved.
// Aggregate stuff will replace all of this.
function parse(entry, txs) {
txs.forEach((tx) => {
const txJSON = tx.toJSON();
const txRAW = tx.toRaw();
const t = new TxModel({
hash: txJSON.hash,
witnessHash: txJSON.witnessHash,
fee: txJSON.fee,
rate: txJSON.rate,
size: txRAW.length,
ps: txJSON.ps,
height: entry.height,
block: util.revHex(entry.hash),
ts: entry.ts,
date: txJSON.date,
index: txJSON.index,
version: txJSON.version,
flag: txJSON.flag,
inputs: tx.inputs.map((input) => {
const inputJSON = input.toJSON();
return new InputModel({
prevout: inputJSON.prevout,
script: inputJSON.script,
witness: inputJSON.witness,
sequence: inputJSON.sequence,
address: inputJSON.address,
});
}),
outputs: tx.outputs.map((output) => {
const outputJSON = output.toJSON();
return new OutputModel({
address: outputJSON.address,
script: outputJSON.script,
value: outputJSON.value,
});
}),
lockTime: txJSON.locktime,
chain: config.bcoin.network,
});
t.save((err) => {
if (err) {
logger.log('error', err.message);
}
});
});
}
module.exports = {
parse,
};

View File

@ -1,24 +1,24 @@
const mongoose = require('mongoose');
const mongoose = require('mongoose');
const Transaction = require('./transaction');
const Schema = mongoose.Schema;
const BlockSchema = new Schema({
hash: { type: String, default: '' },
height: { type: Number, default: 0 },
size: { type: Number, default: 0 },
version: { type: Number, default: 0 },
prevBlock: { type: String, default: '' },
merkleRoot: { type: String, default: '' },
ts: { type: Number, default: 0 },
bits: { type: Number, default: 0 },
nonce: { type: Number, default: 0 },
txs: [Transaction.schema],
chainwork: { type: Number, default: 0 },
reward: { type: Number, default: 0 },
network: { type: String, default: '' },
poolInfo: { type: Object, default: {} },
rawBlock: { type: String, default: '' },
hash: { type: String, default: '' },
height: { type: Number, default: 0 },
size: { type: Number, default: 0 },
version: { type: Number, default: 0 },
prevBlock: { type: String, default: '' },
merkleRoot: { type: String, default: '' },
ts: { type: Number, default: 0 },
bits: { type: Number, default: 0 },
nonce: { type: Number, default: 0 },
txs: [{ type: String, default: '' }],
chainwork: { type: Number, default: 0 },
reward: { type: Number, default: 0 },
network: { type: String, default: '' },
poolInfo: { type: Object, default: {} },
rawBlock: { type: String, default: '' },
}, {
toJSON: {
virtuals: true,

View File

@ -1,25 +1,25 @@
const mongoose = require('mongoose');
const Input = require('./input');
const Output = require('./output');
const Input = require('./input');
const Output = require('./output');
const Schema = mongoose.Schema;
const Schema = mongoose.Schema;
const TransactionSchema = new Schema({
hash: { type: String, default: '' },
hash: { type: String, default: '' },
witnessHash: { type: String, default: '' },
fee: { type: Number, default: 0 },
rate: { type: Number, default: 0 },
ps: { type: Number, default: 0 },
height: { type: Number, default: 0 },
block: { type: String, default: '' },
index: { type: Number, default: 0 },
version: { type: Number, default: 0 },
flag: { type: Number, default: 0 },
lockTime: { type: Number, default: 0 },
inputs: [Input.schema],
outputs: [Output.schema],
size: { type: Number, default: 0 },
network: { type: String, default: '' },
fee: { type: Number, default: 0 },
rate: { type: Number, default: 0 },
ps: { type: Number, default: 0 },
height: { type: Number, default: 0 },
block: { type: String, default: '' },
index: { type: Number, default: 0 },
version: { type: Number, default: 0 },
flag: { type: Number, default: 0 },
lockTime: { type: Number, default: 0 },
inputs: [Input.schema],
outputs: [Output.schema],
size: { type: Number, default: 0 },
network: { type: String, default: '' },
});
TransactionSchema.index({ hash: 1 });