diff --git a/server/lib/db/blocks.js b/server/lib/db/blocks.js index 4c4ef25..623dcde 100644 --- a/server/lib/db/blocks.js +++ b/server/lib/db/blocks.js @@ -4,8 +4,6 @@ const config = require('../../config'); const block = new Block(); -const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours - let bestBlockHeight = 0; // 1e9 limit = ~2M years from now @@ -29,8 +27,7 @@ function byHeight(height, cb) { } function getTopBlocks(cb) { - return block.last(cb) - .limit(MAX_BLOCKS); + return block.last(cb); } function getByHash(hash, cb) { diff --git a/server/lib/db/transactions.js b/server/lib/db/transactions.js index 72e3a2b..8ea5d1f 100644 --- a/server/lib/db/transactions.js +++ b/server/lib/db/transactions.js @@ -3,11 +3,6 @@ const logger = require('../logger'); const config = require('../../config'); 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; function getEmptyInputs(cb) { @@ -15,8 +10,7 @@ function getEmptyInputs(cb) { } function getTopTransactions(cb) { - return Txs.last(cb) - .limit(MAX_TXS); + return Txs.last(cb); } function getTxById(txid, cb) { @@ -25,14 +19,13 @@ function getTxById(txid, cb) { function getTxByBlock(blockHash, page, limit, cb) { return Txs.byBlockHash(blockHash, cb) - .limit(MAX_PAGE_TXS) - .skip(MAX_PAGE_TXS * page); + .skip(limit * page); } function getTxByAddress(address, page, limit, cb) { return Txs.byAddress(address, cb) - .limit(MAX_PAGE_TXS) - .skip(MAX_PAGE_TXS * page); + .limit(limit) + .skip(limit * page); } function getTxCountByBlock(blockHash, cb) { diff --git a/server/models/block.js b/server/models/block.js index 6aa0d1e..22ef2c3 100644 --- a/server/models/block.js +++ b/server/models/block.js @@ -1,7 +1,8 @@ const mongoose = require('mongoose'); -const Transaction = require('./transaction'); +const config = require('../config'); const Schema = mongoose.Schema; +const MAX_BLOCKS = config.api.max_blocks; // ~ 12 hours const BlockSchema = new Schema({ hash: { type: String, default: '' }, @@ -48,10 +49,11 @@ BlockSchema.methods.getRawBlock = function getRawBlock(hash, cb) { cb); }; -BlockSchema.methods.last = function lastTx(cb) { +BlockSchema.methods.last = function lastBlocks(cb) { return this.model('Block').find( {}, cb) + .limit(MAX_BLOCKS) .sort({ height: -1 }); }; diff --git a/server/models/transaction.js b/server/models/transaction.js index 284b4a0..e199330 100644 --- a/server/models/transaction.js +++ b/server/models/transaction.js @@ -2,8 +2,11 @@ const mongoose = require('mongoose'); const Input = require('./input'); const Output = require('./output'); const logger = require('../lib/logger'); +const config = require('../config'); const Schema = mongoose.Schema; +const MAX_TXS = config.api.max_txs; +const MAX_PAGE_TXS = config.api.max_page_txs; const TransactionSchema = new Schema({ hash: { type: String, default: '' }, @@ -38,8 +41,8 @@ TransactionSchema.methods.byHash = function txByHash(hash, cb) { TransactionSchema.methods.byBlockHash = function txByBlockHash(hash, cb) { return this.model('Transaction').find( { block: hash }, - cb, - ); + cb) + .limit(MAX_PAGE_TXS); }; TransactionSchema.methods.byAddress = function txByAddress(address, cb) { @@ -49,15 +52,13 @@ TransactionSchema.methods.byAddress = function txByAddress(address, cb) { { 'inputs.address': address }, { 'outputs.address': address }], }, - cb, - ); + cb); }; TransactionSchema.methods.countByBlock = function txByAddress(hash, cb) { return this.model('Transaction').count( { block: hash }, - cb, - ); + cb); }; TransactionSchema.methods.countByAddress = function txByAddress(address, cb) { @@ -67,15 +68,14 @@ TransactionSchema.methods.countByAddress = function txByAddress(address, cb) { { 'inputs.address': address }, { 'outputs.address': address }], }, - cb, - ); + cb); }; TransactionSchema.methods.last = function lastTx(cb) { return this.model('Transaction').find( {}, - cb, - ) + cb) + .limit(MAX_TXS) .sort({ height: -1 }); };