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

View File

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

View File

@ -2,72 +2,12 @@ const Block = require('../../models/block.js');
const logger = require('../logger');
const config = require('../../config');
const block = new Block();
const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
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
// Mostly for sync to set height
function bestHeight(height) {
@ -80,8 +20,33 @@ function bestHeight(height) {
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 = {
getBlock,
getBlocks,
getRawBlock,
getTopBlocks,
getLastBlock,
getByHash,
byHeight,
bestHeight,
};

View File

@ -10,43 +10,6 @@ const Txs = new Transactions();
const MAX_TXS = config.api.max_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) {
return Txs.getEmptyInputs(cb);
}
@ -62,12 +25,14 @@ function getTxById(txid, cb) {
function getTxByBlock(blockHash, page, limit, cb) {
return Txs.byBlockHash(blockHash, cb)
.limit(MAX_TXS);
.limit(MAX_PAGE_TXS)
.skip(MAX_PAGE_TXS * page);
}
function getTxByAddress(address, page, limit, cb) {
return Txs.byAddress(address, cb)
.limit(MAX_TXS);
.limit(MAX_PAGE_TXS)
.skip(MAX_PAGE_TXS * page);
}
function getTxCountByBlock(blockHash, cb) {
@ -83,7 +48,6 @@ function updateInput(txid, inputid, value, address) {
}
module.exports = {
getTransactions,
getEmptyInputs,
getTopTransactions,
getTxById,

View File

@ -62,7 +62,7 @@ function parse(entry, txs) {
logger.log('error', err.message);
}
// 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) {
findEmptyInputs();
counter = 0;

View File

@ -29,6 +29,30 @@ const BlockSchema = new Schema({
BlockSchema.index({ hash: 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 TransactionSchema = new Schema({
hash: { type: String, default: '' },
hash: { type: String, default: '' },
witnessHash: { type: String, default: '' },
fee: { type: Number, default: 0 },
rate: { type: Number, default: 0 },
ps: { type: Number, default: 0 },
height: { type: Number, default: 0 },
block: { type: String, default: '' },
index: { type: Number, default: 0 },
version: { type: Number, default: 0 },
flag: { type: Number, default: 0 },
lockTime: { type: Number, default: 0 },
inputs: [Input.schema],
outputs: [Output.schema],
size: { type: Number, default: 0 },
network: { type: String, default: '' },
fee: { type: Number, default: 0 },
rate: { type: Number, default: 0 },
ps: { type: Number, default: 0 },
height: { type: Number, default: 0 },
block: { type: String, default: '' },
index: { type: Number, default: 0 },
version: { type: Number, default: 0 },
flag: { type: Number, default: 0 },
lockTime: { type: Number, default: 0 },
inputs: [Input.schema],
outputs: [Output.schema],
size: { type: Number, default: 0 },
network: { type: String, default: '' },
});
TransactionSchema.index({ hash: 1 });
@ -28,14 +28,7 @@ TransactionSchema.index({ hash: 1 });
TransactionSchema.methods.byId = function txById(txid, cb) {
return this.model('Transaction').findOne(
{ hash: txid },
(err, tx) => {
if (err) {
logger.log('error',
`byId: ${err}`);
return cb(err);
}
return cb(null, tx);
});
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) {
return this.model('Transaction').find(
{ block: hash },
(err, txs) => {
if (err) {
logger.log('error',
`byBlockHash: ${err}`);
return cb(err);
}
return cb(null, txs);
},
cb,
);
};
@ -63,31 +49,14 @@ TransactionSchema.methods.byAddress = function txByAddress(address, cb) {
{ 'inputs.address': address },
{ 'outputs.address': address }],
},
(err, tx) => {
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);
},
cb,
);
};
TransactionSchema.methods.countByBlock = function txByAddress(hash, cb) {
return this.model('Transaction').count(
{ block: hash },
(err, count) => {
if (err) {
logger.log('error',
`countByBlock ${err}`);
return cb(err);
}
return cb(null, count);
},
cb,
);
};
@ -98,17 +67,26 @@ TransactionSchema.methods.countByAddress = function txByAddress(address, cb) {
{ 'inputs.address': address },
{ 'outputs.address': address }],
},
(err, count) => {
if (err) {
logger.log('error',
`countByAddress ${err}`);
return cb(err);
}
return cb(null, count);
},
cb,
);
};
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) {
return this.model('Transaction').findOneAndUpdate(
{ _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);