Merge pull request #123 from matiu/feature/unspent-interfase-change

Feature/unspent interfase change
This commit is contained in:
Gustavo Maximiliano Cortez 2014-06-05 17:25:53 -03:00
commit c5f0625fbd
6 changed files with 68 additions and 19 deletions

View File

@ -193,12 +193,13 @@ Sample return:
ts: 1401226410, ts: 1401226410,
scriptPubKey: "76a914e50575162795cd77366fb80d728e3216bd52deac88ac", scriptPubKey: "76a914e50575162795cd77366fb80d728e3216bd52deac88ac",
amount: 0.001, amount: 0.001,
confirmations: "6+" confirmation: 6
confirmationsFromCache: true,
} }
] ]
``` ```
Please not that in case confirmations are cached and are more that INSIGHT_SAFE_CONFIRMATIONS setting, the Please note that in case confirmations are cached (because the number of confirmations if bigger that INSIGHT_SAFE_CONFIRMATIONS) the return will include the pair confirmationsFromCache:true, and confirmations will equal INSIGHT_SAFE_CONFIRMATIONS. See noCache and INSIGHT_IGNORE_CACHE options for details.
return can be a string of the form `SAFE_CONFIRMATIONS+`, e.g.: the string `6+`
### Unspent Outputs for multiple addresses ### Unspent Outputs for multiple addresses

View File

@ -179,7 +179,8 @@ Address.prototype.update = function(next, opts) {
ts: x.ts, ts: x.ts,
scriptPubKey: x.scriptPubKey, scriptPubKey: x.scriptPubKey,
amount: x.value_sat / BitcoreUtil.COIN, amount: x.value_sat / BitcoreUtil.COIN,
confirmations: x.isConfirmedCached ? (config.safeConfirmations+'+') : x.confirmations, confirmations: x.isConfirmedCached ? (config.safeConfirmations) : x.confirmations,
confirmationsFromCache: !!x.isConfirmedCached,
}; };
}); });
return next(); return next();

View File

@ -406,13 +406,12 @@ TransactionDb.prototype.cacheScriptPubKey = function(txouts,cb) {
TransactionDb.prototype._parseAddrData = function(data, ignoreCache) { TransactionDb.prototype._parseAddrData = function(k, data, ignoreCache) {
var k = data.key.split('-');
var v = data.value.split(':'); var v = data.value.split(':');
// console.log('[TransactionDb.js.375]',data.key,data.value); //TODO // console.log('[TransactionDb.js.375]',data.key,data.value); //TODO
var item = { var item = {
key: data.key, key: data.key,
ts: parseInt(k[2]), ts: END_OF_WORLD_TS - parseInt(k[2]),
txid: k[3], txid: k[3],
index: parseInt(k[4]), index: parseInt(k[4]),
value_sat: parseInt(v[0]), value_sat: parseInt(v[0]),
@ -450,6 +449,7 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
var self = this; var self = this;
var k = ADDR_PREFIX + addr + '-'; var k = ADDR_PREFIX + addr + '-';
var ret = []; var ret = [];
var unique={};
db.createReadStream({ db.createReadStream({
start: k, start: k,
@ -457,7 +457,12 @@ TransactionDb.prototype.fromAddr = function(addr, opts, cb) {
limit: opts.txLimit>0 ? opts.txLimit: -1, // -1 means not limit limit: opts.txLimit>0 ? opts.txLimit: -1, // -1 means not limit
}) })
.on('data', function(data) { .on('data', function(data) {
ret.push(self._parseAddrData(data, opts.ignoreCache)); var k = data.key.split('-');
var index = k[3]+k[4];
if (!unique[index]) {
unique[index]=1;
ret.push(self._parseAddrData(k, data, opts.ignoreCache));
}
}) })
.on('error', cb) .on('error', cb)
.on('end', function() { .on('end', function() {

View File

@ -1,7 +1,7 @@
{ {
"name": "insight-bitcore-api", "name": "insight-bitcore-api",
"description": "An open-source bitcoin blockchain API. The Insight API provides you with a convenient, powerful and simple way to query and broadcast data on the bitcoin network and build your own services with it.", "description": "An open-source bitcoin blockchain API. The Insight API provides you with a convenient, powerful and simple way to query and broadcast data on the bitcoin network and build your own services with it.",
"version": "0.2.4", "version": "0.2.6",
"author": { "author": {
"name": "Ryan X Charles", "name": "Ryan X Charles",
"email": "ryan@bitpay.com" "email": "ryan@bitpay.com"

View File

@ -22,7 +22,10 @@ describe('Address cache ', function() {
before(function(c) { before(function(c) {
txDb = TransactionDb; txDb = TransactionDb;
txDb.deleteCacheForAddress('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG',function(){ txDb.deleteCacheForAddress('muAt5RRqDarPFCe6qDXGZc54xJjXYUyepG',function(){
txDb.deleteCacheForAddress('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3',c); txDb.deleteCacheForAddress('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3',function(){
txDb.deleteCacheForAddress('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk',c);
});
}); });
}); });
@ -51,7 +54,7 @@ describe('Address cache ', function() {
it('cache case unspent w/o cache', function(done) { it('cache case 2 w/o cache', function(done) {
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb); var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
@ -62,8 +65,7 @@ describe('Address cache ', function() {
}); });
}); });
it('cache case 2 w cache', function(done) {
it('cache case unspent w cache', function(done) {
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb); var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
@ -73,7 +75,34 @@ describe('Address cache ', function() {
return done(); return done();
}); });
}); });
it('cache case 2 w/o cache', function(done) {
it('cache case 2 unspent wo cache', function(done) {
txDb.deleteCacheForAddress('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk',function() {
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) {
if (err) done(err);
a.unspent.length.should.equal(1);
a.unspent[0].confirmations.should.be.above(15000);
a.unspent[0].confirmationsFromCache.should.equal(false);
return done();
}, {onlyUnspent:1});
});
});
it('cache case 2 unspent w cache', function(done) {
var a = new Address('2N7zvqQTUYFfhYvFs1NEzureMLvhwk5FSsk', txDb);
a.update(function(err) {
if (err) done(err);
a.unspent.length.should.equal(1);
a.unspent[0].confirmationsFromCache.should.equal(true);
a.unspent[0].confirmations.should.equal(6);
return done();
}, {onlyUnspent:1});
});
it('cache case 3 w/o cache', function(done) {
var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
@ -83,7 +112,7 @@ describe('Address cache ', function() {
return done(); return done();
}); });
},1); },1);
it('cache case 2 w cache', function(done) { it('cache case 4 w cache', function(done) {
var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
@ -93,7 +122,7 @@ describe('Address cache ', function() {
return done(); return done();
},{txLimit:0}); },{txLimit:0});
}); });
it('cache case 2 w ignore cache', function(done) { it('cache case 4 w ignore cache', function(done) {
var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb); var a = new Address('mt2AzeCorSf7yFckj19HFiXJgh9aNyc4h3', txDb);
a.update(function(err) { a.update(function(err) {
if (err) done(err); if (err) done(err);
@ -104,6 +133,19 @@ describe('Address cache ', function() {
},{txLimit:0, ignoreCache:1}); },{txLimit:0, ignoreCache:1});
}); });
it('cache case 5 unspent w cache', function(done) {
var a = new Address('2NBuTjjZrURxLaMyPUu2sJwNrtpt7GtPX2p', txDb);
a.update(function(err) {
if (err) done(err);
a.unspent.length.should.equal(1);
a.unspent[0].confirmationsFromCache.should.equal(true);
a.unspent[0].confirmations.should.equal(6);
return done();
}, {onlyUnspent:1});
});
}); });

View File

@ -23,12 +23,12 @@ describe('BlockDb getBlocksByDate', function(){
it('Get Hash by Date', function(done) { it('Get Hash by Date', function(done) {
bDb.getBlocksByDate(START_TS, END_TS, 1000, function(err, list) { bDb.getBlocksByDate(START_TS, END_TS, 2, function(err, list) {
if (err) done(err); if (err) done(err);
assert(list, 'returns list'); assert(list, 'returns list');
assert.equal(list.length,2, 'list has 2 items'); assert.equal(list.length,2, 'list has 2 items');
assert.equal(list[0].hash, TESTING_BLOCK0); assert.equal(list[1].hash, TESTING_BLOCK0);
assert.equal(list[1].hash, TESTING_BLOCK1); assert.equal(list[0].hash, TESTING_BLOCK1);
done(); done();
}); });
}); });