Massive debug/dev code cleanup. Most db logic moved to models.

This commit is contained in:
tenthirtyone 2017-08-21 22:38:20 -04:00
parent 050f45015a
commit cae4a45572
7 changed files with 105 additions and 225 deletions

View File

@ -13,10 +13,7 @@ module.exports = function BlockAPI(router) {
} }
// Pass Mongo params, fields and limit to db api. // Pass Mongo params, fields and limit to db api.
db.blocks.getBlock( return db.blocks.getByHash(blockHash,
{ hash: blockHash },
{ rawBlock: 0 },
1,
(err, block) => { (err, block) => {
if (err) { if (err) {
logger.log('err', err); logger.log('err', err);
@ -48,16 +45,7 @@ module.exports = function BlockAPI(router) {
router.get('/blocks', (req, res) => { router.get('/blocks', (req, res) => {
const limit = parseInt(req.query.limit, 10) || 100; const limit = parseInt(req.query.limit, 10) || 100;
// Pass Mongo params, fields and limit to db api. // Pass Mongo params, fields and limit to db api.
db.blocks.getBlocks( db.blocks.getTopBlocks(
{},
{ height: 1,
size: 1,
hash: 1,
ts: 1,
txs: 1,
poolInfo: 1,
},
limit,
(err, blocks) => { (err, blocks) => {
if (err) { if (err) {
logger.log('error', logger.log('error',
@ -90,10 +78,7 @@ module.exports = function BlockAPI(router) {
} }
// Pass Mongo params, fields and limit to db api. // Pass Mongo params, fields and limit to db api.
db.blocks.getBlock( return db.blocks.getRawBlock(blockHash,
{ hash: blockHash },
{ rawBlock: 1 },
1,
(err, block) => { (err, block) => {
if (err) { if (err) {
logger.log('error', logger.log('error',
@ -105,12 +90,9 @@ module.exports = function BlockAPI(router) {
}); });
router.get('/block-index/:height', (req, res) => { router.get('/block-index/:height', (req, res) => {
const blockHeight = parseInt(req.params.height, 10) || 1; const height = parseInt(req.params.height, 10) || 1;
// Pass Mongo params, fields and limit to db api. // Pass Mongo params, fields and limit to db api.
db.blocks.getBlock( return db.blocks.byHeight(height,
{ height: blockHeight },
{ hash: 1 },
1,
(err, block) => { (err, block) => {
if (err) { if (err) {
logger.log('error', logger.log('error',

View File

@ -30,10 +30,7 @@ module.exports = function statusAPI(router) {
// Get last block hash or node status // Get last block hash or node status
router.get('/status', (req, res) => { router.get('/status', (req, res) => {
if (req.query.q === 'getLastBlockHash') { if (req.query.q === 'getLastBlockHash') {
db.blocks.getBlock( db.blocks.getLastBlock(
{},
{ hash: 1 },
1,
(err, block) => { (err, block) => {
if (err) { if (err) {
logger.log('error', logger.log('error',
@ -99,17 +96,13 @@ module.exports = function statusAPI(router) {
}); });
}); });
// Copied from previous source // Copied from previous source
router.get('/peer', (req, res) => { router.get('/peer', (req, res) => res.json({
return res.json({
connected: true, connected: true,
host: '127.0.0.1', host: '127.0.0.1',
port: null, port: null,
}); }));
});
router.get('/version', (req, res) => { router.get('/version', (req, res) => res.json({
return res.json({
version: pkg.version, version: pkg.version,
}); }));
});
}; };

View File

@ -2,72 +2,12 @@ const Block = require('../../models/block.js');
const logger = require('../logger'); const logger = require('../logger');
const config = require('../../config'); const config = require('../../config');
const block = new Block();
const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
let bestBlockHeight = 0; let bestBlockHeight = 0;
// This naive querying will be replaced by more advanced mongo
function getBlocks(params, options, limit, cb) {
// Do not return mongo ids
const defaultOptions = { _id: 0 };
// Copy over mongo options
Object.assign(defaultOptions, options);
// Simple sanitizing
if (!Number.isInteger(limit)) {
limit = 1;
}
if (limit > MAX_BLOCKS) {
limit = MAX_BLOCKS;
}
if (limit < 1) {
limit = 1;
}
// Query mongo
Block.find(
params,
defaultOptions,
(err, blocks) => {
if (err) {
logger.log('error',
`getBlocks: ${err}`);
return cb(err);
}
if (!blocks.length > 0) {
return cb({ err: 'Block not found' });
}
return cb(null, blocks);
})
.sort({ height: -1 })
.limit(limit);
}
// Retrieve a single block. For convenience mostly
function getBlock(params, options, limit, cb) {
getBlocks(params, options, limit, (err, blocks) => {
if (err) {
logger.log('error',
`getBlock: ${err.err}`);
return cb(err);
}
if (!blocks.length > 0) {
return cb({ err: 'Block not found' });
}
return cb(null, blocks[0]);
});
}
// Highest known height in mongo - Not Used
function getBestHeight() {
getBlock({}, {}, 1, (err, block) => {
if (err) {
return logger.log('error',
`getBestHeight: ${err.err}`);
}
bestBlockHeight = block.height;
});
}
// 1e9 limit = ~2M years from now // 1e9 limit = ~2M years from now
// Mostly for sync to set height // Mostly for sync to set height
function bestHeight(height) { function bestHeight(height) {
@ -80,8 +20,33 @@ function bestHeight(height) {
return bestBlockHeight; return bestBlockHeight;
} }
function getRawBlock(hash, cb) {
return block.getRawBlock(hash, cb);
}
function byHeight(height, cb) {
return block.byHeight(height, cb);
}
function getTopBlocks(cb) {
return block.last(cb)
.limit(MAX_BLOCKS);
}
function getByHash(hash, cb) {
return block.byHash(hash, cb);
}
function getLastBlock(cb) {
return block.last(cb)
.limit(1);
}
module.exports = { module.exports = {
getBlock, getRawBlock,
getBlocks, getTopBlocks,
getLastBlock,
getByHash,
byHeight,
bestHeight, bestHeight,
}; };

View File

@ -10,43 +10,6 @@ const Txs = new Transactions();
const MAX_TXS = config.api.max_txs; const MAX_TXS = config.api.max_txs;
const MAX_PAGE_TXS = config.api.max_page_txs; const MAX_PAGE_TXS = config.api.max_page_txs;
function getTransactions(params, options, limit, skip, cb) {
// Do not return mongo ids
const defaultOptions = { };
// Copy over mongo options
Object.assign(defaultOptions, options);
// Simple sanitizing
if (!Number.isInteger(limit)) {
limit = 1;
}
if (limit > MAX_PAGE_TXS) {
limit = MAX_PAGE_TXS;
}
if (limit < 1) {
limit = 1;
}
// Query mongo
Transactions.find(
params,
defaultOptions,
(err, txs) => {
if (err) {
logger.log('error',
`getTransactions: ${err}`);
return cb(err);
}
if (!txs.length > 0) {
return cb({ err: 'Tx not found' });
}
return cb(null, txs);
})
.sort({ height: 1 })
.skip()
.limit(limit);
}
function getEmptyInputs(cb) { function getEmptyInputs(cb) {
return Txs.getEmptyInputs(cb); return Txs.getEmptyInputs(cb);
} }
@ -62,12 +25,14 @@ function getTxById(txid, cb) {
function getTxByBlock(blockHash, page, limit, cb) { function getTxByBlock(blockHash, page, limit, cb) {
return Txs.byBlockHash(blockHash, cb) return Txs.byBlockHash(blockHash, cb)
.limit(MAX_TXS); .limit(MAX_PAGE_TXS)
.skip(MAX_PAGE_TXS * page);
} }
function getTxByAddress(address, page, limit, cb) { function getTxByAddress(address, page, limit, cb) {
return Txs.byAddress(address, cb) return Txs.byAddress(address, cb)
.limit(MAX_TXS); .limit(MAX_PAGE_TXS)
.skip(MAX_PAGE_TXS * page);
} }
function getTxCountByBlock(blockHash, cb) { function getTxCountByBlock(blockHash, cb) {
@ -83,7 +48,6 @@ function updateInput(txid, inputid, value, address) {
} }
module.exports = { module.exports = {
getTransactions,
getEmptyInputs, getEmptyInputs,
getTopTransactions, getTopTransactions,
getTxById, getTxById,

View File

@ -62,7 +62,7 @@ function parse(entry, txs) {
logger.log('error', err.message); logger.log('error', err.message);
} }
// As long as this modulo is divisible by 20 we should be OK for now. // As long as this modulo is divisible by 20 we should be OK for now.
// Closer to 20 = chattier at start but ideal later on // Closer to 20 = chattier at start and less ideal later on
if (counter % 20 === 0) { if (counter % 20 === 0) {
findEmptyInputs(); findEmptyInputs();
counter = 0; counter = 0;

View File

@ -29,6 +29,30 @@ const BlockSchema = new Schema({
BlockSchema.index({ hash: 1 }); BlockSchema.index({ hash: 1 });
BlockSchema.index({ height: 1 }); BlockSchema.index({ height: 1 });
const Block = mongoose.model('Block', BlockSchema); BlockSchema.methods.byHeight = function blockByHeight(height, cb) {
return this.model('Block').findOne(
{ height },
cb);
};
module.exports = Block; BlockSchema.methods.byHash = function byHash(hash, cb) {
return this.model('Block').findOne(
{ hash },
cb);
};
BlockSchema.methods.getRawBlock = function getRawBlock(hash, cb) {
return this.model('Block').findOne(
{ hash },
{ rawBlock: 1 },
cb);
};
BlockSchema.methods.last = function lastTx(cb) {
return this.model('Block').find(
{},
cb)
.sort({ height: -1 });
};
module.exports = mongoose.model('Block', BlockSchema);

View File

@ -6,21 +6,21 @@ const logger = require('../lib/logger');
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 });
@ -28,14 +28,7 @@ TransactionSchema.index({ hash: 1 });
TransactionSchema.methods.byId = function txById(txid, cb) { TransactionSchema.methods.byId = function txById(txid, cb) {
return this.model('Transaction').findOne( return this.model('Transaction').findOne(
{ hash: txid }, { hash: txid },
(err, tx) => { cb);
if (err) {
logger.log('error',
`byId: ${err}`);
return cb(err);
}
return cb(null, tx);
});
}; };
TransactionSchema.methods.byHash = function txByHash(hash, cb) { TransactionSchema.methods.byHash = function txByHash(hash, cb) {
@ -45,14 +38,7 @@ TransactionSchema.methods.byHash = function txByHash(hash, cb) {
TransactionSchema.methods.byBlockHash = function txByBlockHash(hash, cb) { TransactionSchema.methods.byBlockHash = function txByBlockHash(hash, cb) {
return this.model('Transaction').find( return this.model('Transaction').find(
{ block: hash }, { block: hash },
(err, txs) => { cb,
if (err) {
logger.log('error',
`byBlockHash: ${err}`);
return cb(err);
}
return cb(null, txs);
},
); );
}; };
@ -63,31 +49,14 @@ TransactionSchema.methods.byAddress = function txByAddress(address, cb) {
{ 'inputs.address': address }, { 'inputs.address': address },
{ 'outputs.address': address }], { 'outputs.address': address }],
}, },
(err, tx) => { cb,
if (err) {
logger.log('error',
`byAddress: ${err.err}`);
return cb(err);
}
if (!tx.length > 0) {
return cb({ err: 'Tx not found' });
}
return cb(null, tx);
},
); );
}; };
TransactionSchema.methods.countByBlock = function txByAddress(hash, cb) { TransactionSchema.methods.countByBlock = function txByAddress(hash, cb) {
return this.model('Transaction').count( return this.model('Transaction').count(
{ block: hash }, { block: hash },
(err, count) => { cb,
if (err) {
logger.log('error',
`countByBlock ${err}`);
return cb(err);
}
return cb(null, count);
},
); );
}; };
@ -98,17 +67,26 @@ TransactionSchema.methods.countByAddress = function txByAddress(address, cb) {
{ 'inputs.address': address }, { 'inputs.address': address },
{ 'outputs.address': address }], { 'outputs.address': address }],
}, },
(err, count) => { cb,
if (err) {
logger.log('error',
`countByAddress ${err}`);
return cb(err);
}
return cb(null, count);
},
); );
}; };
TransactionSchema.methods.last = function lastTx(cb) {
return this.model('Transaction').find(
{},
cb,
)
.sort({ height: -1 });
};
TransactionSchema.methods.getEmptyInputs = function findEmptyInputs(cb) {
return this.model('Transaction').find({
'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' },
'inputs.address': '',
},
cb);
};
TransactionSchema.methods.updateInput = function updateInput(txid, inputid, value, address) { TransactionSchema.methods.updateInput = function updateInput(txid, inputid, value, address) {
return this.model('Transaction').findOneAndUpdate( return this.model('Transaction').findOneAndUpdate(
{ _id: txid, 'inputs._id': inputid }, { _id: txid, 'inputs._id': inputid },
@ -127,30 +105,4 @@ TransactionSchema.methods.updateInput = function updateInput(txid, inputid, valu
); );
}; };
TransactionSchema.methods.last = function lastTx(cb) {
return this.model('Transaction').find(
{},
(err, txs) => {
if (err) {
logger.log('error',
`TransactionSchema last: ${err}`);
return cb(err);
}
if (!txs.length > 0) {
return cb({ err: 'Tx not found' });
}
return cb(null, txs);
},
);
};
TransactionSchema.methods.getEmptyInputs = function findEmptyInputs(cb) {
return this.model('Transaction').find({
'inputs.prevout.hash': { $ne: '0000000000000000000000000000000000000000000000000000000000000000' },
'inputs.address': '',
},
cb);
};
module.exports = mongoose.model('Transaction', TransactionSchema); module.exports = mongoose.model('Transaction', TransactionSchema);