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 BlockModel = require('../../models/block');
const InputModel = require('../../models/input'); const config = require('../../config');
const OutputModel = require('../../models/output'); const util = require('../../lib/util');
const config = require('../../config'); const logger = require('../logger');
const util = require('../../lib/util');
const logger = require('../logger');
function parse(entry, block) { function parse(entry, block) {
const rawBlock = block.toRaw().toString('hex'); const rawBlock = block.toRaw().toString('hex');
const blockJSON = block.toJSON(); const blockJSON = block.toJSON();
const reward = util.calcBlockReward(entry.height); const reward = util.calcBlockReward(entry.height);
// Can probably use destructuring to build something nicer // Can probably use destructuring to build something nicer
const newBlock = new BlockModel({ const newBlock = new BlockModel({
hash: blockJSON.hash, hash: blockJSON.hash,
height: entry.height, height: entry.height,
size: block.getSize(), size: block.getSize(),
version: blockJSON.version, version: blockJSON.version,
prevBlock: blockJSON.prevBlock, prevBlock: blockJSON.prevBlock,
merkleRoot: blockJSON.merkleRoot, merkleRoot: blockJSON.merkleRoot,
ts: blockJSON.ts, ts: blockJSON.ts,
bits: blockJSON.bits, bits: blockJSON.bits,
nonce: blockJSON.nonce, nonce: blockJSON.nonce,
txs: block.txs.map((tx) => { txs: block.txs.map((tx) => {
const txJSON = tx.toJSON(); const txJSON = tx.toJSON();
return { return txJSON.hash;
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,
};
}), }),
chainwork: entry.chainwork, chainwork: entry.chainwork,
reward, reward,
network: config.bcoin.network, network: config.bcoin.network,
poolInfo: {}, poolInfo: {},
rawBlock, rawBlock,
}); });

View File

@ -1,5 +1,7 @@
const Block = require('./block'); const Block = require('./block');
const Transaction = require('./transaction');
module.exports = { module.exports = {
Block, 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 Transaction = require('./transaction');
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const BlockSchema = new Schema({ const BlockSchema = new Schema({
hash: { type: String, default: '' }, hash: { type: String, default: '' },
height: { type: Number, default: 0 }, height: { type: Number, default: 0 },
size: { type: Number, default: 0 }, size: { type: Number, default: 0 },
version: { type: Number, default: 0 }, version: { type: Number, default: 0 },
prevBlock: { type: String, default: '' }, prevBlock: { type: String, default: '' },
merkleRoot: { type: String, default: '' }, merkleRoot: { type: String, default: '' },
ts: { type: Number, default: 0 }, ts: { type: Number, default: 0 },
bits: { type: Number, default: 0 }, bits: { type: Number, default: 0 },
nonce: { type: Number, default: 0 }, nonce: { type: Number, default: 0 },
txs: [Transaction.schema], txs: [{ type: String, default: '' }],
chainwork: { type: Number, default: 0 }, chainwork: { type: Number, default: 0 },
reward: { type: Number, default: 0 }, reward: { type: Number, default: 0 },
network: { type: String, default: '' }, network: { type: String, default: '' },
poolInfo: { type: Object, default: {} }, poolInfo: { type: Object, default: {} },
rawBlock: { type: String, default: '' }, rawBlock: { type: String, default: '' },
}, { }, {
toJSON: { toJSON: {
virtuals: true, virtuals: true,

View File

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