fix date bug, add inv subscription
This commit is contained in:
parent
d67950145c
commit
69c536be7d
72
lib/index.js
72
lib/index.js
@ -6,17 +6,33 @@ var BlockController = require('./blocks');
|
||||
var TxController = require('./transactions');
|
||||
var AddressController = require('./addresses');
|
||||
var StatusController = require('./status');
|
||||
var bitcore = require('bitcore');
|
||||
var $ = bitcore.util.preconditions;
|
||||
var Transaction = bitcore.Transaction;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var InsightAPI = function(options) {
|
||||
BaseService.call(this, options);
|
||||
|
||||
this.subscriptions = {
|
||||
inv: []
|
||||
};
|
||||
|
||||
this.txController = new TxController(this.node);
|
||||
};
|
||||
|
||||
InsightAPI.dependencies = ['address', 'web'];
|
||||
|
||||
inherits(InsightAPI, BaseService);
|
||||
|
||||
InsightAPI.prototype.start = function(callback) {
|
||||
this.node.services.bitcoind.on('tx', this.transactionHandler.bind(this));
|
||||
|
||||
setImmediate(callback);
|
||||
};
|
||||
|
||||
InsightAPI.prototype.setupRoutes = function(app) {
|
||||
var apiPrefix = '/insight-api';
|
||||
var apiPrefix = '';
|
||||
|
||||
//Block routes
|
||||
var blocks = new BlockController(this.node);
|
||||
@ -94,4 +110,58 @@ InsightAPI.prototype.setupRoutes = function(app) {
|
||||
app.get('*', index.render);*/
|
||||
};
|
||||
|
||||
InsightAPI.prototype.getPublishEvents = function() {
|
||||
return [
|
||||
{
|
||||
name: 'inv',
|
||||
scope: this,
|
||||
subscribe: this.subscribe.bind(this),
|
||||
unsubscribe: this.unsubscribe.bind(this),
|
||||
extraEvents: ['tx', 'block']
|
||||
}
|
||||
];
|
||||
};
|
||||
|
||||
InsightAPI.prototype.blockHandler = function(block, add, callback) {
|
||||
// Notify inv subscribers
|
||||
for (var i = 0; i < this.subscriptions.inv.length; i++) {
|
||||
this.subscriptions.inv[i].emit('block', block.hash);
|
||||
}
|
||||
|
||||
setImmediate(function() {
|
||||
callback(null, []);
|
||||
});
|
||||
};
|
||||
|
||||
InsightAPI.prototype.transactionHandler = function(txInfo) {
|
||||
if(txInfo.mempool) {
|
||||
var tx = Transaction().fromBuffer(txInfo.buffer);
|
||||
tx = this.txController.transformTransaction(tx);
|
||||
|
||||
for (var i = 0; i < this.subscriptions.inv.length; i++) {
|
||||
this.subscriptions.inv[i].emit('tx', tx);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
InsightAPI.prototype.subscribe = function(emitter) {
|
||||
$.checkArgument(emitter instanceof EventEmitter, 'First argument is expected to be an EventEmitter');
|
||||
|
||||
var emitters = this.subscriptions.inv;
|
||||
var index = emitters.indexOf(emitter);
|
||||
if(index === -1) {
|
||||
emitters.push(emitter);
|
||||
}
|
||||
};
|
||||
|
||||
InsightAPI.prototype.unsubscribe = function(emitter) {
|
||||
$.checkArgument(emitter instanceof EventEmitter, 'First argument is expected to be an EventEmitter');
|
||||
|
||||
var emitters = this.subscriptions.inv;
|
||||
var index = emitters.indexOf(emitter);
|
||||
if(index > -1) {
|
||||
emitters.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = InsightAPI;
|
||||
@ -70,7 +70,7 @@ TxController.prototype.transformTransaction = function(transaction) {
|
||||
|
||||
transformed.blockhash = transaction.__blockHash;
|
||||
transformed.confirmations = confirmations;
|
||||
transformed.time = transaction.__timestamp ? Math.round(transaction.__timestamp / 1000) : Math.round(Date.now() / 1000); // can we get this from bitcoind?
|
||||
transformed.time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000); // can we get this from bitcoind?
|
||||
transformed.blocktime = transformed.time;
|
||||
|
||||
if(transaction.isCoinbase()) {
|
||||
@ -188,7 +188,7 @@ TxController.prototype.list = function(req, res) {
|
||||
async.mapSeries(txs, function(tx, next) {
|
||||
tx.__blockHash = block.hash;
|
||||
tx.__height = blockInfo.height;
|
||||
tx.__timestamp = block.header.time * 1000;
|
||||
tx.__timestamp = block.header.time;
|
||||
|
||||
tx.populateInputs(self.node.services.db, [], function(err) {
|
||||
if(err) {
|
||||
|
||||
@ -5,36 +5,6 @@ var AddressController = require('../lib/addresses');
|
||||
var _ = require('lodash');
|
||||
var bitcore = require('bitcore');
|
||||
|
||||
var diff = function(a, b) {
|
||||
if(Array.isArray(a)) {
|
||||
var r = [];
|
||||
for(var i = 0; i < a.length; i++) {
|
||||
if(b[i] === a[i]) {
|
||||
break;
|
||||
} else {
|
||||
if(_.isObject(a[i])) {
|
||||
r.push(diff(a[i], b[i]));
|
||||
} else {
|
||||
r.push(a[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
} else {
|
||||
var r = {};
|
||||
_.each(a, function(v,k) {
|
||||
if(b[k] === v) return;
|
||||
// but what if it returns an empty object? still attach?
|
||||
r[k] = _.isObject(v)
|
||||
? diff(v, b[k])
|
||||
: v
|
||||
;
|
||||
});
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
var txinfos = [
|
||||
{
|
||||
"address": "mkPvAKZ2rar6qeG3KjBtJHHMSP1wFZH7Er",
|
||||
@ -163,7 +133,7 @@ var tx = bitcore.Transaction().fromObject({
|
||||
});
|
||||
|
||||
tx.__height = 534181;
|
||||
tx.__timestamp = 1441116143000;
|
||||
tx.__timestamp = 1441116143;
|
||||
tx.__blockHash = '0000000000000041ddc94ecf4f86a456a83b2e320c36c6f0c13ff92c7e75f013';
|
||||
var txinfos2 = [
|
||||
{
|
||||
|
||||
@ -5,49 +5,6 @@ var bitcore = require('bitcore');
|
||||
var TxController = require('../lib/transactions');
|
||||
var _ = require('lodash');
|
||||
|
||||
/*var diff = function(a, b) {
|
||||
var r = {};
|
||||
_.each(a, function(v,k) {
|
||||
if(b[k] === v) return;
|
||||
// but what if it returns an empty object? still attach?
|
||||
r[k] = _.isObject(v)
|
||||
? diff(v, b[k])
|
||||
: v
|
||||
;
|
||||
});
|
||||
return r;
|
||||
};*/
|
||||
|
||||
var diff = function(a, b) {
|
||||
if(Array.isArray(a)) {
|
||||
var r = [];
|
||||
for(var i = 0; i < a.length; i++) {
|
||||
if(b[i] === a[i]) {
|
||||
break;
|
||||
} else {
|
||||
if(_.isObject(a[i])) {
|
||||
r.push(diff(a[i], b[i]));
|
||||
} else {
|
||||
r.push(a[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
} else {
|
||||
var r = {};
|
||||
_.each(a, function(v,k) {
|
||||
if(b[k] === v) return;
|
||||
// but what if it returns an empty object? still attach?
|
||||
r[k] = _.isObject(v)
|
||||
? diff(v, b[k])
|
||||
: v
|
||||
;
|
||||
});
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
describe('Transactions', function() {
|
||||
describe('/tx/:txid', function() {
|
||||
it('should have correct data', function(done) {
|
||||
@ -215,7 +172,7 @@ describe('Transactions', function() {
|
||||
var bitcoreTx = bitcore.Transaction(bitcoreTxObj);
|
||||
bitcoreTx.__blockHash = '0000000000000afa0c3c0afd450c793a1e300ec84cbe9555166e06132f19a8f7';
|
||||
bitcoreTx.__height = 533974;
|
||||
bitcoreTx.__timestamp = 1440987503000;
|
||||
bitcoreTx.__timestamp = 1440987503;
|
||||
bitcoreTx.populateInputs = sinon.stub().callsArg(2);
|
||||
bitcoreTx.toObject = sinon.stub().returns(bitcoreTxObj);
|
||||
|
||||
@ -657,11 +614,11 @@ describe('Transactions', function() {
|
||||
];
|
||||
|
||||
txinfos[0].tx.__blockHash = '00000000000001001aba15de213648f370607fb048288dd27b96f7e833a73520';
|
||||
txinfos[0].tx.__timestamp = 1441068774000;
|
||||
txinfos[0].tx.__timestamp = 1441068774;
|
||||
txinfos[0].tx.__height = 534105;
|
||||
|
||||
txinfos[1].tx.__blockHash = '0000000000000a3acc1f7fe72917eb48bb319ed96c125a6dfcc0ba6acab3c4d0';
|
||||
txinfos[1].tx.__timestamp = 1441072817000;
|
||||
txinfos[1].tx.__timestamp = 1441072817;
|
||||
txinfos[1].tx.__height = 534110;
|
||||
|
||||
var node = {
|
||||
@ -877,7 +834,6 @@ describe('Transactions', function() {
|
||||
|
||||
var res = {
|
||||
jsonp: function(data) {
|
||||
var d = diff(insight, data);
|
||||
var merged = _.merge(data, todos);
|
||||
should(merged).eql(insight);
|
||||
done();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user