fixed: blocklist by date with limit. when arrive new block, reload last 5 blocks on homepage. The same for new txs.

This commit is contained in:
Gustavo Cortez 2014-02-09 15:59:28 -03:00
parent bd3bfe6e37
commit c6d137f856
4 changed files with 27 additions and 49 deletions

View File

@ -72,7 +72,6 @@ console.log('[blocks.js.60]: could not get %s from RPC. Orphan? Error?', blockha
* List of blocks by date
*/
exports.list = function(req, res) {
var limit = req.query.limit || -1;
var isToday = false;
//helper to convert timestamps to yyyy-mm-dd format
@ -103,14 +102,15 @@ exports.list = function(req, res) {
var prev = formatTimestamp(new Date((gte - 86400) * 1000));
var next = formatTimestamp(new Date(lte * 1000));
bdb.getBlocksByDate(gte, lte, limit, function(err, blocks) {
bdb.getBlocksByDate(gte, lte, function(err, blocks) {
if (err) {
res.status(500).send(err);
}
else {
var blockshashList = [];
for(var i=0;i<blocks.length;i++) {
blockshashList.unshift(blocks[i].hash);
var limit = parseInt(req.query.limit || blocks.length);
for(var i=0;i<limit;i++) {
blockshashList.push(blocks[i].hash);
}
async.mapSeries(blockshashList, getBlock, function(err, allblocks) {
res.jsonp({

View File

@ -148,13 +148,12 @@ function spec(b) {
});
};
BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, limit, cb) {
BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, cb) {
var list = [];
db.createReadStream({
start: TIMESTAMP_PREFIX + start_ts,
end: TIMESTAMP_PREFIX + end_ts,
fillCache: true,
limit: parseInt(limit) // force to int
fillCache: true
})
.on('data', function (data) {
list.push({
@ -166,7 +165,7 @@ function spec(b) {
return cb(err);
})
.on('end', function () {
return cb(null, list);
return cb(null, list.reverse());
});
};

View File

@ -4,22 +4,23 @@ var TRANSACTION_DISPLAYED = 5;
var BLOCKS_DISPLAYED = 5;
angular.module('insight.system').controller('IndexController',
function($scope, $rootScope, Global, getSocket, Blocks, Block, Transactions, Transaction) {
function($scope, $rootScope, Global, getSocket, Blocks, Transactions) {
$scope.global = Global;
var _getTransaction = function(txid, cb) {
Transaction.get({
txId: txid
var _getBlocks = function() {
Blocks.get({
limit: BLOCKS_DISPLAYED
}, function(res) {
cb(res);
$scope.blocks = res.blocks;
$scope.blocksLength = res.lenght;
});
};
var _getBlock = function(hash) {
Block.get({
blockHash: hash
var _getTransactions = function() {
Transactions.get({
limit: TRANSACTION_DISPLAYED
}, function(res) {
$scope.blocks.unshift(res);
$scope.txs = res.txs;
});
};
@ -31,24 +32,13 @@ angular.module('insight.system').controller('IndexController',
socket.on('tx', function(tx) {
console.log('Transaction received! ' + JSON.stringify(tx));
var txStr = tx.txid.toString();
_getTransaction(txStr, function(res) {
$scope.txs.unshift(res);
if (parseInt($scope.txs.length, 10) >= parseInt(TRANSACTION_DISPLAYED, 10)) {
$scope.txs = $scope.txs.splice(0, TRANSACTION_DISPLAYED);
}
});
_getTransactions();
});
socket.on('block', function(block) {
var blockHash = block.hash.toString();
console.log('Block received! ' + JSON.stringify(block));
if (parseInt($scope.blocks.length, 10) > parseInt(BLOCKS_DISPLAYED, 10) - 1) {
$scope.blocks.pop();
}
_getBlock(blockHash);
var blockHash = block.hash.hash.toString();
console.log('Block received! ' + JSON.stringify(blockHash));
_getBlocks();
});
$scope.humanSince = function(time) {
@ -57,18 +47,8 @@ angular.module('insight.system').controller('IndexController',
};
$scope.index = function() {
Blocks.get({
limit: BLOCKS_DISPLAYED
}, function(res) {
$scope.blocks = res.blocks;
$scope.blocksLength = res.lenght;
});
Transactions.get({
limit: TRANSACTION_DISPLAYED
}, function(res) {
$scope.txs = res.txs;
});
_getBlocks();
_getTransactions();
};
$scope.txs = [];

View File

@ -3,11 +3,10 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var TESTING_BLOCK0 = '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943';
var TESTING_BLOCK1 = '00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206';
var TESTING_BLOCK0 = '00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206';
var TESTING_BLOCK1 = '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943';
var START_TS = 1;
var END_TS = '1296688928~'; // 2/2/2011 23:23PM
var LIMIT = 2;
var assert = require('assert'),
BlockDb = require('../../lib/BlockDb').class();
@ -24,7 +23,7 @@ describe('BlockDb getBlocksByDate', function(){
it('Get Hash by Date', function(done) {
bDb.getBlocksByDate(START_TS, END_TS, LIMIT, function(err, list) {
bDb.getBlocksByDate(START_TS, END_TS, function(err, list) {
if (err) done(err);
assert(list, 'returns list');
assert.equal(list.length,2, 'list has 2 items');