Save logic moved to models. Parsers removed
This commit is contained in:
parent
b836390b22
commit
1a18a369c1
@ -39,6 +39,10 @@ function getLastBlock(cb) {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function saveBcoinBlock(entry, block, cb) {
|
||||||
|
return Block.saveBcoinBlock(entry, block, cb);
|
||||||
|
}
|
||||||
|
|
||||||
// Returns highest consecutive block height
|
// Returns highest consecutive block height
|
||||||
function getBestBlockHeight(cb) {
|
function getBestBlockHeight(cb) {
|
||||||
logger.log('debug',
|
logger.log('debug',
|
||||||
@ -66,4 +70,5 @@ module.exports = {
|
|||||||
getByHash,
|
getByHash,
|
||||||
byHeight,
|
byHeight,
|
||||||
bestHeight,
|
bestHeight,
|
||||||
|
saveBcoinBlock,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,13 +2,8 @@ const Transactions = require('../../models/transaction.js');
|
|||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
const logger = require('../logger');
|
const logger = require('../logger');
|
||||||
|
|
||||||
|
|
||||||
const MAX_PAGE_TXS = config.api.max_page_txs;
|
const MAX_PAGE_TXS = config.api.max_page_txs;
|
||||||
|
|
||||||
function getEmptyInputs(cb) {
|
|
||||||
return Transactions.getEmptyInputs(cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTopTransactions(cb) {
|
function getTopTransactions(cb) {
|
||||||
return Transactions.last(cb);
|
return Transactions.last(cb);
|
||||||
}
|
}
|
||||||
@ -36,13 +31,17 @@ function getTxCountByAddress(address, cb) {
|
|||||||
return Transactions.countByAddress(address, cb);
|
return Transactions.countByAddress(address, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function saveBcoinTransactions(entry, txs, cb) {
|
||||||
|
return Transactions.saveBcoinTransactions(entry, txs, cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getEmptyInputs,
|
|
||||||
getTopTransactions,
|
getTopTransactions,
|
||||||
getTxById,
|
getTxById,
|
||||||
getTxByBlock,
|
getTxByBlock,
|
||||||
getTxCountByBlock,
|
getTxCountByBlock,
|
||||||
getTxByAddress,
|
getTxByAddress,
|
||||||
getTxCountByAddress,
|
getTxCountByAddress,
|
||||||
|
saveBcoinTransactions,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
const FullNode = require('bcoin/lib/node/fullnode');
|
const FullNode = require('bcoin/lib/node/fullnode');
|
||||||
const logger = require('../../lib/logger');
|
const logger = require('../../lib/logger');
|
||||||
const BlockParser = require('../parser').Block;
|
|
||||||
const TxParser = require('../parser').Transaction;
|
|
||||||
const config = require('../../config');
|
const config = require('../../config');
|
||||||
const socket = require('../../lib/api/socket');
|
|
||||||
const db = require('../../lib/db');
|
const db = require('../../lib/db');
|
||||||
|
|
||||||
const node = new FullNode(config.bcoin);
|
const node = new FullNode(config.bcoin);
|
||||||
@ -20,12 +17,24 @@ function start(bestBlockHeight) {
|
|||||||
|
|
||||||
node.chain.on('connect', (entry, block) => {
|
node.chain.on('connect', (entry, block) => {
|
||||||
db.blocks.bestHeight(entry.height);
|
db.blocks.bestHeight(entry.height);
|
||||||
|
// Assemble Bcoin block data
|
||||||
node.chain.db.getBlockView(block)
|
node.chain.db.getBlockView(block)
|
||||||
.then((view) => {
|
.then((view) => {
|
||||||
const fullBlock = block.getJSON(node.network, view, entry.height);
|
const fullBlock = block.getJSON(node.network, view, entry.height);
|
||||||
BlockParser.parse(entry, block);
|
// Save the block
|
||||||
TxParser.parse(entry, fullBlock.txs);
|
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}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -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,
|
|
||||||
};
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
const Block = require('./block');
|
|
||||||
const Transaction = require('./transaction');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
Block,
|
|
||||||
Transaction,
|
|
||||||
};
|
|
||||||
@ -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,
|
|
||||||
};
|
|
||||||
@ -1,5 +1,6 @@
|
|||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
|
const util = require('../lib/util');
|
||||||
|
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
// These limits can be overriden higher up the stack
|
// These limits can be overriden higher up the stack
|
||||||
@ -66,4 +67,32 @@ BlockSchema.statics.getHeights = function findMissing(cb) {
|
|||||||
.sort({ height: 1 });
|
.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);
|
module.exports = mongoose.model('Block', BlockSchema);
|
||||||
|
|||||||
@ -3,6 +3,7 @@ const Input = require('./input');
|
|||||||
const Output = require('./output');
|
const Output = require('./output');
|
||||||
const logger = require('../lib/logger');
|
const logger = require('../lib/logger');
|
||||||
const config = require('../config');
|
const config = require('../config');
|
||||||
|
const util = require('../lib/util');
|
||||||
|
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
// These limits can be overriden higher up the stack
|
// These limits can be overriden higher up the stack
|
||||||
@ -83,30 +84,44 @@ TransactionSchema.statics.last = function lastTx(cb) {
|
|||||||
.sort({ height: -1 });
|
.sort({ height: -1 });
|
||||||
};
|
};
|
||||||
|
|
||||||
TransactionSchema.statics.getEmptyInputs = function getEmptyInputs(cb) {
|
TransactionSchema.statics.saveBcoinTransactions = function saveBcoinTransactions(entry, txs, cb) {
|
||||||
return this.model('Transaction').find({
|
txs.forEach((tx) => {
|
||||||
'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' },
|
this.saveBcoinTransaction(entry, tx, cb);
|
||||||
'inputs.value': 0,
|
});
|
||||||
},
|
|
||||||
cb);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
TransactionSchema.statics.updateInput = function updateInput(txid, inputid, value, address) {
|
TransactionSchema.statics.saveBcoinTransaction = function saveBcoinTransaction(entry, tx, cb) {
|
||||||
return this.model('Transaction').findOneAndUpdate(
|
const Transaction = this.model('Transaction');
|
||||||
{ _id: txid, 'inputs._id': inputid },
|
return new Transaction({
|
||||||
{
|
hash: tx.hash,
|
||||||
$set: {
|
witnessHash: tx.witnessHash,
|
||||||
'inputs.$.value': value,
|
fee: tx.fee,
|
||||||
'inputs.$.address': address,
|
rate: tx.rate,
|
||||||
},
|
ps: tx.ps,
|
||||||
},
|
height: entry.height,
|
||||||
(err, tx) => {
|
block: util.revHex(entry.hash),
|
||||||
if (err) {
|
ts: entry.ts,
|
||||||
logger.log('error',
|
date: entry.tx,
|
||||||
`updateInput: ${err}`);
|
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);
|
module.exports = mongoose.model('Transaction', TransactionSchema);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user