Merge pull request #209 from cmgustavo/feature/01streamed
Feature/01streamed
This commit is contained in:
commit
e00b5edf48
|
@ -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,18 @@ 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);
|
||||
if (blocks.length < limit) {
|
||||
limit = blocks.length;
|
||||
}
|
||||
for(var i=0;i<limit;i++) {
|
||||
blockshashList.push(blocks[i].hash);
|
||||
}
|
||||
async.mapSeries(blockshashList, getBlock, function(err, allblocks) {
|
||||
res.jsonp({
|
||||
|
|
|
@ -153,13 +153,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({
|
||||
|
@ -171,7 +170,7 @@ function spec(b) {
|
|||
return cb(err);
|
||||
})
|
||||
.on('end', function () {
|
||||
return cb(null, list);
|
||||
return cb(null, list.reverse());
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@ function spec(b) {
|
|||
var RpcClient = require('bitcore/RpcClient').class(),
|
||||
// networks = require('bitcore/network'),
|
||||
BitcoreTransaction = require('bitcore/Transaction').class(),
|
||||
BitcoreBlock = require('bitcore/Block').class(),
|
||||
util = require('bitcore/util/util'),
|
||||
// BitcoreBlock = require('bitcore/Block').class(),
|
||||
// util = require('bitcore/util/util'),
|
||||
config = require('../config/config');
|
||||
|
||||
var rpc = b.rpc || new RpcClient(config.bitcoind);
|
||||
|
|
|
@ -4,9 +4,18 @@ 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, Transaction) {
|
||||
$scope.global = Global;
|
||||
|
||||
var _getBlocks = function() {
|
||||
Blocks.get({
|
||||
limit: BLOCKS_DISPLAYED
|
||||
}, function(res) {
|
||||
$scope.blocks = res.blocks;
|
||||
$scope.blocksLength = res.lenght;
|
||||
});
|
||||
};
|
||||
|
||||
var _getTransaction = function(txid, cb) {
|
||||
Transaction.get({
|
||||
txId: txid
|
||||
|
@ -15,14 +24,6 @@ angular.module('insight.system').controller('IndexController',
|
|||
});
|
||||
};
|
||||
|
||||
var _getBlock = function(hash) {
|
||||
Block.get({
|
||||
blockHash: hash
|
||||
}, function(res) {
|
||||
$scope.blocks.unshift(res);
|
||||
});
|
||||
};
|
||||
|
||||
var socket = getSocket($scope);
|
||||
socket.emit('subscribe', 'inv');
|
||||
|
||||
|
@ -42,14 +43,9 @@ angular.module('insight.system').controller('IndexController',
|
|||
});
|
||||
|
||||
socket.on('block', function(block) {
|
||||
console.log('Block received! ' + JSON.stringify(block));
|
||||
|
||||
var blockHash = block.toString();
|
||||
if (parseInt($scope.blocks.length, 10) > parseInt(BLOCKS_DISPLAYED, 10) - 1) {
|
||||
$scope.blocks.pop();
|
||||
}
|
||||
|
||||
_getBlock(blockHash);
|
||||
console.log('Block received! ' + JSON.stringify(blockHash));
|
||||
_getBlocks();
|
||||
});
|
||||
|
||||
$scope.humanSince = function(time) {
|
||||
|
@ -58,18 +54,7 @@ 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();
|
||||
};
|
||||
|
||||
$scope.txs = [];
|
||||
|
|
|
@ -46,13 +46,13 @@
|
|||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr data-ng-show="!txs.length"><td colspan="5">Waiting for transactions...</td></tr>
|
||||
<tr data-ng-show="!txs.length"><td colspan="3">Waiting for transactions...</td></tr>
|
||||
<tr class="fader" data-ng-repeat='tx in txs'>
|
||||
<td>
|
||||
<a class="ellipsis" href="/tx/{{tx.txid}}">{{tx.txid}}</a>
|
||||
</td>
|
||||
<td><span class="ellipsis">{{humanSince(tx.time)}}</span></td>
|
||||
<td>{{tx.valueOut}}</td>
|
||||
<td><span class="ellipsis">{{tx.valueOut}}</span></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in New Issue