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,6 +1,4 @@
const BlockModel = require('../../models/block'); const BlockModel = require('../../models/block');
const InputModel = require('../../models/input');
const OutputModel = require('../../models/output');
const config = require('../../config'); const config = require('../../config');
const util = require('../../lib/util'); const util = require('../../lib/util');
const logger = require('../logger'); const logger = require('../logger');
@ -23,40 +21,7 @@ function parse(entry, block) {
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,

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

@ -13,7 +13,7 @@ const BlockSchema = new Schema({
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: '' },