Save logic moved to models. Parsers removed

This commit is contained in:
tenthirtyone 2017-08-27 23:44:08 -04:00
parent b836390b22
commit 1a18a369c1
8 changed files with 91 additions and 135 deletions

View File

@ -39,6 +39,10 @@ function getLastBlock(cb) {
.limit(1);
}
function saveBcoinBlock(entry, block, cb) {
return Block.saveBcoinBlock(entry, block, cb);
}
// Returns highest consecutive block height
function getBestBlockHeight(cb) {
logger.log('debug',
@ -66,4 +70,5 @@ module.exports = {
getByHash,
byHeight,
bestHeight,
saveBcoinBlock,
};

View File

@ -2,13 +2,8 @@ const Transactions = require('../../models/transaction.js');
const config = require('../../config');
const logger = require('../logger');
const MAX_PAGE_TXS = config.api.max_page_txs;
function getEmptyInputs(cb) {
return Transactions.getEmptyInputs(cb);
}
function getTopTransactions(cb) {
return Transactions.last(cb);
}
@ -36,13 +31,17 @@ function getTxCountByAddress(address, cb) {
return Transactions.countByAddress(address, cb);
}
function saveBcoinTransactions(entry, txs, cb) {
return Transactions.saveBcoinTransactions(entry, txs, cb);
}
module.exports = {
getEmptyInputs,
getTopTransactions,
getTxById,
getTxByBlock,
getTxCountByBlock,
getTxByAddress,
getTxCountByAddress,
saveBcoinTransactions,
};

View File

@ -1,9 +1,6 @@
const FullNode = require('bcoin/lib/node/fullnode');
const logger = require('../../lib/logger');
const BlockParser = require('../parser').Block;
const TxParser = require('../parser').Transaction;
const config = require('../../config');
const socket = require('../../lib/api/socket');
const db = require('../../lib/db');
const node = new FullNode(config.bcoin);
@ -20,12 +17,24 @@ function start(bestBlockHeight) {
node.chain.on('connect', (entry, block) => {
db.blocks.bestHeight(entry.height);
// Assemble Bcoin block data
node.chain.db.getBlockView(block)
.then((view) => {
const fullBlock = block.getJSON(node.network, view, entry.height);
BlockParser.parse(entry, block);
TxParser.parse(entry, fullBlock.txs);
// Save the block
db.blocks.saveBcoinBlock(entry, block, (err) => {
if (err) {
logger.log('error',
`Error saving block ${err}`);
}
});
// Save the Txs
db.txs.saveBcoinTransactions(entry, fullBlock.txs, (err) => {
if (err) {
logger.log('error',
`Error saving txs ${err}`);
}
});
});
});

View File

@ -1,42 +0,0 @@
const BlockModel = require('../../models/block');
const config = require('../../config');
const util = require('../../lib/util');
const logger = require('../logger');
function parse(entry, block) {
const rawBlock = block.toRaw().toString('hex');
const blockJSON = block.toJSON();
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,
merkleRoot: blockJSON.merkleRoot,
ts: blockJSON.ts,
bits: blockJSON.bits,
nonce: blockJSON.nonce,
txs: block.txs.map((tx) => {
const txJSON = tx.toJSON();
return txJSON.hash;
}),
chainwork: entry.chainwork,
reward,
network: config.bcoin.network,
poolInfo: {},
rawBlock,
});
newBlock.save((err) => {
if (err) {
logger.log('error', err.message);
}
});
}
module.exports = {
parse,
};

View File

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

View File

@ -1,52 +0,0 @@
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');
function parse(entry, txs) {
txs.forEach((tx) => {
const t = new TxModel({
hash: tx.hash,
witnessHash: tx.witnessHash,
fee: tx.fee,
rate: tx.rate,
ps: tx.ps,
height: entry.height,
block: util.revHex(entry.hash),
ts: entry.ts,
date: entry.tx,
index: tx.index,
version: tx.version,
flag: tx.flag,
inputs: tx.inputs.map(input => new InputModel({
value: input.coin ? input.coin.value : 0,
prevout: input.prevout,
script: input.script,
witness: input.witness,
sequence: input.sequence,
address: input.coin ? input.coin.address : '',
})),
outputs: tx.outputs.map(output => new OutputModel({
address: output.address,
script: output.script,
value: output.value,
})),
lockTime: tx.locktime,
chain: config.bcoin.network,
});
t.save((err) => {
if (err) {
logger.log('error', err.message);
}
});
});
}
module.exports = {
parse,
};

View File

@ -1,5 +1,6 @@
const mongoose = require('mongoose');
const config = require('../config');
const util = require('../lib/util');
const Schema = mongoose.Schema;
// These limits can be overriden higher up the stack
@ -66,4 +67,32 @@ BlockSchema.statics.getHeights = function findMissing(cb) {
.sort({ height: 1 });
};
BlockSchema.statics.saveBcoinBlock = function saveBcoinBlock(entry, block, cb) {
const Block = this.model('Block');
const rawBlock = block.toRaw().toString('hex');
const blockJSON = block.toJSON();
const reward = util.calcBlockReward(entry.height);
return new Block({
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) => {
const txJSON = tx.toJSON();
return txJSON.hash;
}),
chainwork: entry.chainwork,
reward,
network: config.bcoin.network,
poolInfo: {},
rawBlock,
}).save(cb);
};
module.exports = mongoose.model('Block', BlockSchema);

View File

@ -3,6 +3,7 @@ const Input = require('./input');
const Output = require('./output');
const logger = require('../lib/logger');
const config = require('../config');
const util = require('../lib/util');
const Schema = mongoose.Schema;
// These limits can be overriden higher up the stack
@ -83,30 +84,44 @@ TransactionSchema.statics.last = function lastTx(cb) {
.sort({ height: -1 });
};
TransactionSchema.statics.getEmptyInputs = function getEmptyInputs(cb) {
return this.model('Transaction').find({
'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' },
'inputs.value': 0,
},
cb);
TransactionSchema.statics.saveBcoinTransactions = function saveBcoinTransactions(entry, txs, cb) {
txs.forEach((tx) => {
this.saveBcoinTransaction(entry, tx, cb);
});
};
TransactionSchema.statics.updateInput = function updateInput(txid, inputid, value, address) {
return this.model('Transaction').findOneAndUpdate(
{ _id: txid, 'inputs._id': inputid },
{
$set: {
'inputs.$.value': value,
'inputs.$.address': address,
},
},
(err, tx) => {
if (err) {
logger.log('error',
`updateInput: ${err}`);
}
},
);
TransactionSchema.statics.saveBcoinTransaction = function saveBcoinTransaction(entry, tx, cb) {
const Transaction = this.model('Transaction');
return new Transaction({
hash: tx.hash,
witnessHash: tx.witnessHash,
fee: tx.fee,
rate: tx.rate,
ps: tx.ps,
height: entry.height,
block: util.revHex(entry.hash),
ts: entry.ts,
date: entry.tx,
index: tx.index,
version: tx.version,
flag: tx.flag,
inputs: tx.inputs.map(input => new Input({
value: input.coin ? input.coin.value : 0,
prevout: input.prevout,
script: input.script,
witness: input.witness,
sequence: input.sequence,
address: input.coin ? input.coin.address : '',
})),
outputs: tx.outputs.map(output => new Output({
address: output.address,
script: output.script,
value: output.value,
})),
lockTime: tx.locktime,
chain: config.bcoin.network,
})
.save(cb);
};
module.exports = mongoose.model('Transaction', TransactionSchema);