diff --git a/lib/status.js b/lib/status.js index 0209e85..750550c 100644 --- a/lib/status.js +++ b/lib/status.js @@ -5,7 +5,7 @@ function StatusController(node) { } StatusController.prototype.show = function(req, res) { - + var option = req.query.q; switch(option) { @@ -13,14 +13,14 @@ StatusController.prototype.show = function(req, res) { res.jsonp(this.getDifficulty()); break; case 'getTxOutSetInfo': - // TODO - // break; + res.jsonp(this.getTxOutSetInfo()); + break; case 'getLastBlockHash': - // TODO - // break; + res.jsonp(this.getLastBlockHash()); + break; case 'getBestBlockHash': - // TODO - // break; + res.jsonp(this.getBestBlockHash()); + break; case 'getInfo': default: res.jsonp(this.getInfo()); @@ -34,6 +34,35 @@ StatusController.prototype.getInfo = function() { }; }; +StatusController.prototype.getTxOutSetInfo = function() { + var hash = this.node.services.bitcoind.getBestBlockHash(); + if (hash === this.lastTxOutSetInfo) { + return { + txoutsetinfo: this.txOutSetInfo + }; + } + this.txOutSetInfo = this.node.services.bitcoind.getTxOutSetInfo(); + this.lastTxOutSetInfo = this.txOutSetInfo.bestblock; + return { + txoutsetinfo: this.txOutSetInfo + }; +}; + +StatusController.prototype.getLastBlockHash = function() { + var hash = this.node.services.db.tip.hash; + return { + syncTipHash: hash, + lastblockhash: hash + }; +}; + +StatusController.prototype.getBestBlockHash = function() { + var hash = this.node.services.bitcoind.getBestBlockHash(); + return { + bestblockhash: hash + }; +}; + StatusController.prototype.getDifficulty = function() { var info = this.node.services.bitcoind.getInfo(); return { @@ -73,6 +102,6 @@ StatusController.prototype.version = function(req, res) { res.jsonp({ version: pjson.version }); -} +}; -module.exports = StatusController; \ No newline at end of file +module.exports = StatusController; diff --git a/test/status.js b/test/status.js index 0d7e573..9f9ee1b 100644 --- a/test/status.js +++ b/test/status.js @@ -18,10 +18,27 @@ describe('Status', function() { errors: '' }; + var outSetInfo = { + height: 151, + bestblock: '20b6cc0600037171b8bb634bbd04ea754945be44db8d9199b74798f1abdb382d', + transactions: 151, + txouts: 151, + bytes_serialized: 10431, + hash_serialized: 'c165d5dcb22a897745ee2ee274b47133b995bbcf8dd4a7572fedad87541c7df8', + total_amount: 750000000000 + }; + var node = { services: { bitcoind: { - getInfo: sinon.stub().returns(info) + getInfo: sinon.stub().returns(info), + getTxOutSetInfo: sinon.stub().returns(outSetInfo), + getBestBlockHash: sinon.stub().returns(outSetInfo.bestblock) + }, + db: { + tip: { + hash: outSetInfo.bestblock + } } } }; @@ -64,6 +81,69 @@ describe('Status', function() { status.show(req, res); }); + + it('getTxOutSetInfo', function(done) { + var req = { + query: { + q: 'getTxOutSetInfo' + } + }; + var res = { + jsonp: function(data) { + data.txoutsetinfo.should.equal(outSetInfo); + done(); + } + }; + status.show(req, res); + }); + + it('getTxOutSetInfo (cached)', function(done) { + var req = { + query: { + q: 'getTxOutSetInfo' + } + }; + var res = { + jsonp: function(data) { + data.txoutsetinfo.should.equal(outSetInfo); + done(); + } + }; + status.node.services.bitcoind.getTxOutSetInfo.callCount.should.equal(1); + status.show(req, res); + }); + + it('getBestBlockHash', function(done) { + var req = { + query: { + q: 'getBestBlockHash' + } + }; + var res = { + jsonp: function(data) { + data.bestblockhash.should.equal(outSetInfo.bestblock); + done(); + } + }; + status.show(req, res); + }); + + it('getLastBlockHash', function(done) { + var req = { + query: { + q: 'getLastBlockHash' + } + }; + var res = { + jsonp: function(data) { + data.syncTipHash.should.equal(outSetInfo.bestblock); + data.lastblockhash.should.equal(outSetInfo.bestblock); + done(); + } + }; + status.show(req, res); + }); + }); describe('/sync', function() { @@ -147,4 +227,4 @@ describe('Status', function() { status.version(req, res); }); }); -}); \ No newline at end of file +});