From ab70aa666ef459c34e9c9fc31e633274c2e46f02 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 5 Apr 2016 10:32:07 -0400 Subject: [PATCH] bitcoind: add address utxos --- lib/services/bitcoind.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 4c70f910..edf0537e 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -36,6 +36,7 @@ function Bitcoin(options) { Service.call(this, options); // caches valid until there is a new block + this.utxosCache = LRU(50000); this.txidsCache = LRU(50000); this.balanceCache = LRU(50000); this.summaryCache = LRU(50000); @@ -146,6 +147,7 @@ Bitcoin.prototype._loadConfiguration = function() { }; Bitcoin.prototype._resetCaches = function() { + this.utxosCache.reset(); this.txidsCache.reset(); this.balanceCache.reset(); this.summaryCache.reset(); @@ -369,8 +371,27 @@ Bitcoin.prototype.getAddressBalance = function(addressArg, options, callback) { } }; -Bitcoin.prototype.getAddressUnspentOutputs = function() { - // TODO add this rpc method to bitcoind +Bitcoin.prototype.getAddressUnspentOutputs = function(addressArg, options, callback) { + var self = this; + var addresses = [addressArg]; + if (Array.isArray(addressArg)) { + addresses = addressArg; + } + var cacheKey = addresses.join(''); + var utxos = self.utxosCache.get(cacheKey); + if (utxos) { + return setImmediate(function() { + callback(null, utxos); + }); + } else { + self.client.getAddressUtxos({addresses: addresses}, function(err, response) { + if (err) { + return callback(err); + } + self.utxosCache.set(cacheKey, response.result); + callback(null, response.result); + }); + } }; Bitcoin.prototype.getAddressTxids = function(addressArg, options, callback) {