paging added back. Going over max config limit will default to config limit and still support paging.

This commit is contained in:
tenthirtyone 2017-08-21 22:53:04 -04:00
parent cae4a45572
commit 98164e4447
4 changed files with 19 additions and 27 deletions

View File

@ -4,8 +4,6 @@ const config = require('../../config');
const block = new Block(); const block = new Block();
const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
let bestBlockHeight = 0; let bestBlockHeight = 0;
// 1e9 limit = ~2M years from now // 1e9 limit = ~2M years from now
@ -29,8 +27,7 @@ function byHeight(height, cb) {
} }
function getTopBlocks(cb) { function getTopBlocks(cb) {
return block.last(cb) return block.last(cb);
.limit(MAX_BLOCKS);
} }
function getByHash(hash, cb) { function getByHash(hash, cb) {

View File

@ -3,11 +3,6 @@ const logger = require('../logger');
const config = require('../../config'); const config = require('../../config');
const Txs = new Transactions(); const Txs = new Transactions();
// No optimization yet.
// Will be replaced with a more sophisticated api soon
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 getEmptyInputs(cb) { function getEmptyInputs(cb) {
@ -15,8 +10,7 @@ function getEmptyInputs(cb) {
} }
function getTopTransactions(cb) { function getTopTransactions(cb) {
return Txs.last(cb) return Txs.last(cb);
.limit(MAX_TXS);
} }
function getTxById(txid, cb) { function getTxById(txid, cb) {
@ -25,14 +19,13 @@ 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_PAGE_TXS) .skip(limit * page);
.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_PAGE_TXS) .limit(limit)
.skip(MAX_PAGE_TXS * page); .skip(limit * page);
} }
function getTxCountByBlock(blockHash, cb) { function getTxCountByBlock(blockHash, cb) {

View File

@ -1,7 +1,8 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const Transaction = require('./transaction'); const config = require('../config');
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours
const BlockSchema = new Schema({ const BlockSchema = new Schema({
hash: { type: String, default: '' }, hash: { type: String, default: '' },
@ -48,10 +49,11 @@ BlockSchema.methods.getRawBlock = function getRawBlock(hash, cb) {
cb); cb);
}; };
BlockSchema.methods.last = function lastTx(cb) { BlockSchema.methods.last = function lastBlocks(cb) {
return this.model('Block').find( return this.model('Block').find(
{}, {},
cb) cb)
.limit(MAX_BLOCKS)
.sort({ height: -1 }); .sort({ height: -1 });
}; };

View File

@ -2,8 +2,11 @@ const mongoose = require('mongoose');
const Input = require('./input'); 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 Schema = mongoose.Schema; const Schema = mongoose.Schema;
const MAX_TXS = config.api.max_txs;
const MAX_PAGE_TXS = config.api.max_page_txs;
const TransactionSchema = new Schema({ const TransactionSchema = new Schema({
hash: { type: String, default: '' }, hash: { type: String, default: '' },
@ -38,8 +41,8 @@ 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 },
cb, cb)
); .limit(MAX_PAGE_TXS);
}; };
TransactionSchema.methods.byAddress = function txByAddress(address, cb) { TransactionSchema.methods.byAddress = function txByAddress(address, cb) {
@ -49,15 +52,13 @@ TransactionSchema.methods.byAddress = function txByAddress(address, cb) {
{ 'inputs.address': address }, { 'inputs.address': address },
{ 'outputs.address': address }], { 'outputs.address': address }],
}, },
cb, cb);
);
}; };
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 },
cb, cb);
);
}; };
TransactionSchema.methods.countByAddress = function txByAddress(address, cb) { TransactionSchema.methods.countByAddress = function txByAddress(address, cb) {
@ -67,15 +68,14 @@ TransactionSchema.methods.countByAddress = function txByAddress(address, cb) {
{ 'inputs.address': address }, { 'inputs.address': address },
{ 'outputs.address': address }], { 'outputs.address': address }],
}, },
cb, cb);
);
}; };
TransactionSchema.methods.last = function lastTx(cb) { TransactionSchema.methods.last = function lastTx(cb) {
return this.model('Transaction').find( return this.model('Transaction').find(
{}, {},
cb, cb)
) .limit(MAX_TXS)
.sort({ height: -1 }); .sort({ height: -1 });
}; };