Merge pull request #335 from matiu/feat/unconfirmed-inputs
add unconfirmed inputs banner
This commit is contained in:
commit
2709031ec3
@ -59,10 +59,15 @@ exports.transaction = function(req, res, next, txid) {
|
|||||||
tDb.fromIdWithInfo(txid, function(err, tx) {
|
tDb.fromIdWithInfo(txid, function(err, tx) {
|
||||||
if (err || ! tx)
|
if (err || ! tx)
|
||||||
return common.handleErrors(err, res);
|
return common.handleErrors(err, res);
|
||||||
else {
|
|
||||||
|
bdb.fillVinConfirmations(tx.info, function(err) {
|
||||||
|
if (err)
|
||||||
|
return common.handleErrors(err, res);
|
||||||
|
|
||||||
req.transaction = tx.info;
|
req.transaction = tx.info;
|
||||||
return next();
|
return next();
|
||||||
}
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
305
lib/BlockDb.js
305
lib/BlockDb.js
@ -1,35 +1,37 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
var TIMESTAMP_PREFIX = 'bts-'; // bts-<ts> => <hash>
|
var TIMESTAMP_PREFIX = 'bts-'; // bts-<ts> => <hash>
|
||||||
var PREV_PREFIX = 'bpr-'; // bpr-<hash> => <prev_hash>
|
var PREV_PREFIX = 'bpr-'; // bpr-<hash> => <prev_hash>
|
||||||
var NEXT_PREFIX = 'bne-'; // bne-<hash> => <next_hash>
|
var NEXT_PREFIX = 'bne-'; // bne-<hash> => <next_hash>
|
||||||
var MAIN_PREFIX = 'bma-'; // bma-<hash> => <height> (0 is unconnected)
|
var MAIN_PREFIX = 'bma-'; // bma-<hash> => <height> (0 is unconnected)
|
||||||
var TIP = 'bti-'; // bti = <hash>:<height> last block on the chain
|
var TIP = 'bti-'; // bti = <hash>:<height> last block on the chain
|
||||||
var LAST_FILE_INDEX = 'file-'; // last processed file index
|
var LAST_FILE_INDEX = 'file-'; // last processed file index
|
||||||
|
|
||||||
// txid - blockhash mapping (only for confirmed txs, ONLY FOR BEST BRANCH CHAIN)
|
// txid - blockhash mapping (only for confirmed txs, ONLY FOR BEST BRANCH CHAIN)
|
||||||
var IN_BLK_PREFIX = 'btx-'; //btx-<txid> = <block>
|
var IN_BLK_PREFIX = 'btx-'; //btx-<txid> = <block>
|
||||||
|
|
||||||
|
|
||||||
var MAX_OPEN_FILES = 500;
|
var MAX_OPEN_FILES = 500;
|
||||||
var CONCURRENCY = 5;
|
var CONCURRENCY = 5;
|
||||||
var DFLT_REQUIRED_CONFIRMATIONS = 1;
|
var DFLT_REQUIRED_CONFIRMATIONS = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
var levelup = require('levelup'),
|
var levelup = require('levelup'),
|
||||||
config = require('../config/config');
|
config = require('../config/config');
|
||||||
var db = imports.db || levelup(config.leveldb + '/blocks',{maxOpenFiles: MAX_OPEN_FILES} );
|
var db = imports.db || levelup(config.leveldb + '/blocks', {
|
||||||
|
maxOpenFiles: MAX_OPEN_FILES
|
||||||
|
});
|
||||||
var Rpc = imports.rpc || require('./Rpc');
|
var Rpc = imports.rpc || require('./Rpc');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
|
||||||
|
|
||||||
var logger = require('./logger').logger;
|
var logger = require('./logger').logger;
|
||||||
var info = logger.info;
|
var info = logger.info;
|
||||||
|
|
||||||
var BlockDb = function(opts) {
|
var BlockDb = function(opts) {
|
||||||
this.txDb = require('./TransactionDb').default();
|
this.txDb = require('./TransactionDb').default();
|
||||||
this.safeConfirmations = config.safeConfirmations || DEFAULT_SAFE_CONFIRMATIONS;
|
this.safeConfirmations = config.safeConfirmations || DEFAULT_SAFE_CONFIRMATIONS;
|
||||||
BlockDb.super(this, arguments);
|
BlockDb.super(this, arguments);
|
||||||
};
|
};
|
||||||
@ -41,8 +43,10 @@ BlockDb.prototype.close = function(cb) {
|
|||||||
BlockDb.prototype.drop = function(cb) {
|
BlockDb.prototype.drop = function(cb) {
|
||||||
var path = config.leveldb + '/blocks';
|
var path = config.leveldb + '/blocks';
|
||||||
db.close(function() {
|
db.close(function() {
|
||||||
require('leveldown').destroy(path, function () {
|
require('leveldown').destroy(path, function() {
|
||||||
db = levelup(path,{maxOpenFiles: MAX_OPEN_FILES} );
|
db = levelup(path, {
|
||||||
|
maxOpenFiles: MAX_OPEN_FILES
|
||||||
|
});
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -51,31 +55,27 @@ BlockDb.prototype.drop = function(cb) {
|
|||||||
|
|
||||||
BlockDb.prototype._addBlockScript = function(b, height) {
|
BlockDb.prototype._addBlockScript = function(b, height) {
|
||||||
var time_key = TIMESTAMP_PREFIX +
|
var time_key = TIMESTAMP_PREFIX +
|
||||||
( b.time || Math.round(new Date().getTime() / 1000) );
|
(b.time || Math.round(new Date().getTime() / 1000));
|
||||||
|
|
||||||
return [
|
return [{
|
||||||
{
|
type: 'put',
|
||||||
type: 'put',
|
key: time_key,
|
||||||
key: time_key,
|
value: b.hash,
|
||||||
value: b.hash,
|
}, {
|
||||||
},
|
type: 'put',
|
||||||
{
|
key: MAIN_PREFIX + b.hash,
|
||||||
type: 'put',
|
value: height,
|
||||||
key: MAIN_PREFIX + b.hash,
|
}, {
|
||||||
value: height,
|
type: 'put',
|
||||||
},
|
key: PREV_PREFIX + b.hash,
|
||||||
{
|
value: b.previousblockhash,
|
||||||
type: 'put',
|
}, ];
|
||||||
key:PREV_PREFIX + b.hash,
|
|
||||||
value: b.previousblockhash,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype._delTxsScript = function(txs) {
|
BlockDb.prototype._delTxsScript = function(txs) {
|
||||||
var dbScript =[];
|
var dbScript = [];
|
||||||
|
|
||||||
for(var ii in txs){
|
for (var ii in txs) {
|
||||||
dbScript.push({
|
dbScript.push({
|
||||||
type: 'del',
|
type: 'del',
|
||||||
key: IN_BLK_PREFIX + txs[ii],
|
key: IN_BLK_PREFIX + txs[ii],
|
||||||
@ -85,13 +85,13 @@ BlockDb.prototype._delTxsScript = function(txs) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype._addTxsScript = function(txs, hash, height) {
|
BlockDb.prototype._addTxsScript = function(txs, hash, height) {
|
||||||
var dbScript =[];
|
var dbScript = [];
|
||||||
|
|
||||||
for(var ii in txs){
|
for (var ii in txs) {
|
||||||
dbScript.push({
|
dbScript.push({
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: IN_BLK_PREFIX + txs[ii],
|
key: IN_BLK_PREFIX + txs[ii],
|
||||||
value: hash+':'+height,
|
value: hash + ':' + height,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return dbScript;
|
return dbScript;
|
||||||
@ -99,51 +99,53 @@ BlockDb.prototype._addTxsScript = function(txs, hash, height) {
|
|||||||
|
|
||||||
// Returns blockHash and height for a given txId (If the tx is on the MAIN chain).
|
// Returns blockHash and height for a given txId (If the tx is on the MAIN chain).
|
||||||
BlockDb.prototype.getBlockForTx = function(txId, cb) {
|
BlockDb.prototype.getBlockForTx = function(txId, cb) {
|
||||||
db.get(IN_BLK_PREFIX + txId,function (err, val) {
|
db.get(IN_BLK_PREFIX + txId, function(err, val) {
|
||||||
if (err && err.notFound) return cb();
|
if (err && err.notFound) return cb();
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var v = val.split(':');
|
var v = val.split(':');
|
||||||
return cb(err,v[0],parseInt(v[1]));
|
return cb(err, v[0], parseInt(v[1]));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype._changeBlockHeight = function(hash, height, cb) {
|
BlockDb.prototype._changeBlockHeight = function(hash, height, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var dbScript1 = this._setHeightScript(hash,height);
|
var dbScript1 = this._setHeightScript(hash, height);
|
||||||
|
|
||||||
logger.log('Getting TXS FROM %s to set it Main', hash);
|
logger.log('Getting TXS FROM %s to set it Main', hash);
|
||||||
this.fromHashWithInfo(hash, function(err, bi) {
|
this.fromHashWithInfo(hash, function(err, bi) {
|
||||||
if (!bi || !bi.info || !bi.info.tx)
|
if (!bi || !bi.info || !bi.info.tx)
|
||||||
throw new Error('unable to get info for block:'+ hash);
|
throw new Error('unable to get info for block:' + hash);
|
||||||
|
|
||||||
var dbScript2;
|
var dbScript2;
|
||||||
if (height>=0) {
|
if (height >= 0) {
|
||||||
dbScript2 = self._addTxsScript(bi.info.tx, hash, height);
|
dbScript2 = self._addTxsScript(bi.info.tx, hash, height);
|
||||||
logger.info('\t%s %d Txs', 'Confirming', bi.info.tx.length);
|
logger.info('\t%s %d Txs', 'Confirming', bi.info.tx.length);
|
||||||
} else {
|
} else {
|
||||||
dbScript2 = self._delTxsScript(bi.info.tx);
|
dbScript2 = self._delTxsScript(bi.info.tx);
|
||||||
logger.info('\t%s %d Txs', 'Unconfirming', bi.info.tx.length);
|
logger.info('\t%s %d Txs', 'Unconfirming', bi.info.tx.length);
|
||||||
}
|
}
|
||||||
db.batch(dbScript2.concat(dbScript1),cb);
|
db.batch(dbScript2.concat(dbScript1), cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.setBlockMain = function(hash, height, cb) {
|
BlockDb.prototype.setBlockMain = function(hash, height, cb) {
|
||||||
this._changeBlockHeight(hash,height,cb);
|
this._changeBlockHeight(hash, height, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.setBlockNotMain = function(hash, cb) {
|
BlockDb.prototype.setBlockNotMain = function(hash, cb) {
|
||||||
this._changeBlockHeight(hash,-1,cb);
|
this._changeBlockHeight(hash, -1, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
// adds a block (and its txs). Does not update Next pointer in
|
// adds a block (and its txs). Does not update Next pointer in
|
||||||
// the block prev to the new block, nor TIP pointer
|
// the block prev to the new block, nor TIP pointer
|
||||||
//
|
//
|
||||||
BlockDb.prototype.add = function(b, height, cb) {
|
BlockDb.prototype.add = function(b, height, cb) {
|
||||||
var txs = typeof b.tx[0] === 'string' ? b.tx : b.tx.map( function(o){ return o.txid; });
|
var txs = typeof b.tx[0] === 'string' ? b.tx : b.tx.map(function(o) {
|
||||||
|
return o.txid;
|
||||||
|
});
|
||||||
|
|
||||||
var dbScript = this._addBlockScript(b,height);
|
var dbScript = this._addBlockScript(b, height);
|
||||||
dbScript = dbScript.concat(this._addTxsScript(txs, b.hash, height));
|
dbScript = dbScript.concat(this._addTxsScript(txs, b.hash, height));
|
||||||
this.txDb.addMany(b.tx, function(err) {
|
this.txDb.addMany(b.tx, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
@ -153,9 +155,9 @@ BlockDb.prototype.add = function(b, height, cb) {
|
|||||||
|
|
||||||
BlockDb.prototype.getTip = function(cb) {
|
BlockDb.prototype.getTip = function(cb) {
|
||||||
|
|
||||||
if (this.cachedTip){
|
if (this.cachedTip) {
|
||||||
var v = this.cachedTip.split(':');
|
var v = this.cachedTip.split(':');
|
||||||
return cb(null,v[0], parseInt(v[1]));
|
return cb(null, v[0], parseInt(v[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
@ -163,7 +165,7 @@ BlockDb.prototype.getTip = function(cb) {
|
|||||||
if (!val) return cb();
|
if (!val) return cb();
|
||||||
self.cachedTip = val;
|
self.cachedTip = val;
|
||||||
var v = val.split(':');
|
var v = val.split(':');
|
||||||
return cb(err,v[0], parseInt(v[1]));
|
return cb(err, v[0], parseInt(v[1]));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -177,8 +179,8 @@ BlockDb.prototype.setTip = function(hash, height, cb) {
|
|||||||
BlockDb.prototype.getDepth = function(hash, cb) {
|
BlockDb.prototype.getDepth = function(hash, cb) {
|
||||||
var v = this.cachedTip.split(':');
|
var v = this.cachedTip.split(':');
|
||||||
if (!v) throw new Error('getDepth called with not cachedTip');
|
if (!v) throw new Error('getDepth called with not cachedTip');
|
||||||
this.getHeight(hash, function(err,h){
|
this.getHeight(hash, function(err, h) {
|
||||||
return cb(err,parseInt(v[1]) - h);
|
return cb(err, parseInt(v[1]) - h);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -190,9 +192,12 @@ BlockDb.prototype.setPrev = function(hash, prevHash, cb) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.getPrev = function(hash, cb) {
|
BlockDb.prototype.getPrev = function(hash, cb) {
|
||||||
db.get(PREV_PREFIX + hash, function(err,val) {
|
db.get(PREV_PREFIX + hash, function(err, val) {
|
||||||
if (err && err.notFound) { err = null; val = null;}
|
if (err && err.notFound) {
|
||||||
return cb(err,val);
|
err = null;
|
||||||
|
val = null;
|
||||||
|
}
|
||||||
|
return cb(err, val);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -208,28 +213,37 @@ BlockDb.prototype.setLastFileIndex = function(idx, cb) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.getLastFileIndex = function(cb) {
|
BlockDb.prototype.getLastFileIndex = function(cb) {
|
||||||
db.get(LAST_FILE_INDEX, function(err,val) {
|
db.get(LAST_FILE_INDEX, function(err, val) {
|
||||||
if (err && err.notFound) { err = null; val = null;}
|
if (err && err.notFound) {
|
||||||
return cb(err,val);
|
err = null;
|
||||||
|
val = null;
|
||||||
|
}
|
||||||
|
return cb(err, val);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.getNext = function(hash, cb) {
|
BlockDb.prototype.getNext = function(hash, cb) {
|
||||||
db.get(NEXT_PREFIX + hash, function(err,val) {
|
db.get(NEXT_PREFIX + hash, function(err, val) {
|
||||||
if (err && err.notFound) { err = null; val = null;}
|
if (err && err.notFound) {
|
||||||
return cb(err,val);
|
err = null;
|
||||||
|
val = null;
|
||||||
|
}
|
||||||
|
return cb(err, val);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.getHeight = function(hash, cb) {
|
BlockDb.prototype.getHeight = function(hash, cb) {
|
||||||
db.get(MAIN_PREFIX + hash, function(err, val) {
|
db.get(MAIN_PREFIX + hash, function(err, val) {
|
||||||
if (err && err.notFound) { err = null; val = 0;}
|
if (err && err.notFound) {
|
||||||
return cb(err,parseInt(val));
|
err = null;
|
||||||
|
val = 0;
|
||||||
|
}
|
||||||
|
return cb(err, parseInt(val));
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype._setHeightScript = function(hash, height) {
|
BlockDb.prototype._setHeightScript = function(hash, height) {
|
||||||
logger.log('setHeight: %s #%d', hash,height);
|
logger.log('setHeight: %s #%d', hash, height);
|
||||||
return ([{
|
return ([{
|
||||||
type: 'put',
|
type: 'put',
|
||||||
key: MAIN_PREFIX + hash,
|
key: MAIN_PREFIX + hash,
|
||||||
@ -247,14 +261,17 @@ BlockDb.prototype.setNext = function(hash, nextHash, cb) {
|
|||||||
BlockDb.prototype.countConnected = function(cb) {
|
BlockDb.prototype.countConnected = function(cb) {
|
||||||
var c = 0;
|
var c = 0;
|
||||||
console.log('Counting connected blocks. This could take some minutes');
|
console.log('Counting connected blocks. This could take some minutes');
|
||||||
db.createReadStream({start: MAIN_PREFIX, end: MAIN_PREFIX + '~' })
|
db.createReadStream({
|
||||||
.on('data', function (data) {
|
start: MAIN_PREFIX,
|
||||||
|
end: MAIN_PREFIX + '~'
|
||||||
|
})
|
||||||
|
.on('data', function(data) {
|
||||||
if (data.value !== 0) c++;
|
if (data.value !== 0) c++;
|
||||||
})
|
})
|
||||||
.on('error', function (err) {
|
.on('error', function(err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
})
|
})
|
||||||
.on('end', function () {
|
.on('end', function() {
|
||||||
return cb(null, c);
|
return cb(null, c);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -262,7 +279,7 @@ BlockDb.prototype.countConnected = function(cb) {
|
|||||||
// .has() return true orphans also
|
// .has() return true orphans also
|
||||||
BlockDb.prototype.has = function(hash, cb) {
|
BlockDb.prototype.has = function(hash, cb) {
|
||||||
var k = PREV_PREFIX + hash;
|
var k = PREV_PREFIX + hash;
|
||||||
db.get(k, function (err) {
|
db.get(k, function(err) {
|
||||||
var ret = true;
|
var ret = true;
|
||||||
if (err && err.notFound) {
|
if (err && err.notFound) {
|
||||||
err = null;
|
err = null;
|
||||||
@ -282,7 +299,7 @@ BlockDb.prototype.fromHashWithInfo = function(hash, cb) {
|
|||||||
self.getHeight(hash, function(err, height) {
|
self.getHeight(hash, function(err, height) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
info.isMainChain = height>=0 ? true : false;
|
info.isMainChain = height >= 0 ? true : false;
|
||||||
|
|
||||||
return cb(null, {
|
return cb(null, {
|
||||||
hash: hash,
|
hash: hash,
|
||||||
@ -295,30 +312,30 @@ BlockDb.prototype.fromHashWithInfo = function(hash, cb) {
|
|||||||
BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, limit, cb) {
|
BlockDb.prototype.getBlocksByDate = function(start_ts, end_ts, limit, cb) {
|
||||||
var list = [];
|
var list = [];
|
||||||
var opts = {
|
var opts = {
|
||||||
start: TIMESTAMP_PREFIX + end_ts, //Inverted since list is reversed
|
start: TIMESTAMP_PREFIX + end_ts, //Inverted since list is reversed
|
||||||
end: TIMESTAMP_PREFIX + start_ts,
|
end: TIMESTAMP_PREFIX + start_ts,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
reverse: 1,
|
reverse: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
db.createReadStream(opts)
|
db.createReadStream(opts)
|
||||||
.on('data', function (data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
list.push({
|
list.push({
|
||||||
ts: k[1],
|
ts: k[1],
|
||||||
hash: data.value,
|
hash: data.value,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.on('error', function (err) {
|
.on('error', function(err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
})
|
})
|
||||||
.on('end', function () {
|
.on('end', function() {
|
||||||
return cb(null, list.reverse());
|
return cb(null, list.reverse());
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.blockIndex = function(height, cb) {
|
BlockDb.prototype.blockIndex = function(height, cb) {
|
||||||
return Rpc.blockIndex(height,cb);
|
return Rpc.blockIndex(height, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) {
|
BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) {
|
||||||
@ -331,11 +348,11 @@ BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) {
|
|||||||
// Only one will be confirmed
|
// Only one will be confirmed
|
||||||
self.getBlockForTx(oi.txid, function(err, hash, height) {
|
self.getBlockForTx(oi.txid, function(err, hash, height) {
|
||||||
if (err) return;
|
if (err) return;
|
||||||
if (height>=0) {
|
if (height >= 0) {
|
||||||
o.spentTxId = oi.txid;
|
o.spentTxId = oi.txid;
|
||||||
o.index = oi.index;
|
o.index = oi.index;
|
||||||
o.spentIsConfirmed = chainHeight >= height;
|
o.spentIsConfirmed = chainHeight >= height;
|
||||||
o.spentConfirmations = chainHeight - height +1;
|
o.spentConfirmations = chainHeight - height + 1;
|
||||||
}
|
}
|
||||||
return e_c();
|
return e_c();
|
||||||
});
|
});
|
||||||
@ -343,63 +360,93 @@ BlockDb.prototype._fillConfirmationsOneSpent = function(o, chainHeight, cb) {
|
|||||||
} else {
|
} else {
|
||||||
self.getBlockForTx(o.spentTxId, function(err, hash, height) {
|
self.getBlockForTx(o.spentTxId, function(err, hash, height) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (height >=0 ) {
|
if (height >= 0) {
|
||||||
o.spentIsConfirmed = chainHeight >= height;
|
o.spentIsConfirmed = chainHeight >= height;
|
||||||
o.spentConfirmations = chainHeight - height +1;
|
o.spentConfirmations = chainHeight - height + 1;
|
||||||
}
|
}
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
BlockDb.prototype._fillConfirmationsOneVin = function(o, chainHeight, cb) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self.getBlockForTx(o.txid, function(err, hash, height) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
o.isConfirmed = false;
|
||||||
|
o.confirmations = 0;
|
||||||
|
if (height >= 0) {
|
||||||
|
o.isConfirmed = chainHeight >= height;
|
||||||
|
o.confirmations = chainHeight - height + 1;
|
||||||
|
}
|
||||||
|
o.unconfirmedInput = ! o.isConfirmed;
|
||||||
|
return cb();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
BlockDb.prototype._fillConfirmationsOne = function(o, chainHeight, cb) {
|
BlockDb.prototype._fillConfirmationsOne = function(o, chainHeight, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.getBlockForTx(o.txid, function(err, hash, height) {
|
self.getBlockForTx(o.txid, function(err, hash, height) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
if (height>=0) {
|
if (height >= 0) {
|
||||||
o.isConfirmed = chainHeight >= height;
|
o.isConfirmed = chainHeight >= height;
|
||||||
o.confirmations = chainHeight - height +1;
|
o.confirmations = chainHeight - height + 1;
|
||||||
return self._fillConfirmationsOneSpent(o,chainHeight,cb);
|
return self._fillConfirmationsOneSpent(o, chainHeight, cb);
|
||||||
}
|
} else return cb();
|
||||||
else return cb();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.fillConfirmations = function(txouts, cb) {
|
BlockDb.prototype.fillConfirmations = function(txouts, cb) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.getTip(function(err, hash, height){
|
this.getTip(function(err, hash, height) {
|
||||||
var txs = txouts.filter(function(x){
|
var txs = txouts.filter(function(x) {
|
||||||
return !x.spentIsConfirmedCached // not 100%cached
|
return !x.spentIsConfirmedCached // not 100%cached
|
||||||
&& !(x.isConfirmedCached && !x.spentTxId); // and not partial cached but not spent
|
&& !(x.isConfirmedCached && !x.spentTxId); // and not partial cached but not spent
|
||||||
});
|
});
|
||||||
//console.log('[BlockDb.js.373:txs:]',txs.length, txs.slice(0,5)); //TODO
|
//console.log('[BlockDb.js.373:txs:]',txs.length, txs.slice(0,5)); //TODO
|
||||||
|
|
||||||
async.eachLimit(txs, CONCURRENCY, function(txout, e_c) {
|
async.eachLimit(txs, CONCURRENCY, function(txout, e_c) {
|
||||||
if(txout.isConfirmedCached) {
|
if (txout.isConfirmedCached) {
|
||||||
self._fillConfirmationsOneSpent(txout,height, e_c);
|
self._fillConfirmationsOneSpent(txout, height, e_c);
|
||||||
} else {
|
} else {
|
||||||
self._fillConfirmationsOne(txout,height, e_c);
|
self._fillConfirmationsOne(txout, height, e_c);
|
||||||
}
|
}
|
||||||
|
|
||||||
}, cb);
|
}, cb);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BlockDb.prototype.fillVinConfirmations = function(tx, cb) {
|
||||||
|
var self = this;
|
||||||
|
this.getTip(function(err, hash, height) {
|
||||||
|
var vin = tx.vin;
|
||||||
|
if (!vin) return cb();
|
||||||
|
|
||||||
|
async.eachLimit(vin, CONCURRENCY, function(v, e_c) {
|
||||||
|
self._fillConfirmationsOneVin(v, height, e_c);
|
||||||
|
}, cb);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* this is only for migration scripts */
|
/* this is only for migration scripts */
|
||||||
BlockDb.prototype._runScript = function(script, cb) {
|
BlockDb.prototype._runScript = function(script, cb) {
|
||||||
db.batch(script,cb);
|
db.batch(script, cb);
|
||||||
};
|
};
|
||||||
|
|
||||||
BlockDb.prototype.migrateV02 = function(cb) {
|
BlockDb.prototype.migrateV02 = function(cb) {
|
||||||
var k = 'txb-';
|
var k = 'txb-';
|
||||||
var dbScript = [];
|
var dbScript = [];
|
||||||
var c=0;
|
var c = 0;
|
||||||
var c2=0;
|
var c2 = 0;
|
||||||
var N=50000;
|
var N = 50000;
|
||||||
this.txDb._db.createReadStream({
|
this.txDb._db.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.on('data', function(data) {
|
.on('data', function(data) {
|
||||||
var k = data.key.split('-');
|
var k = data.key.split('-');
|
||||||
var v = data.value.split(':');
|
var v = data.value.split(':');
|
||||||
@ -408,18 +455,18 @@ BlockDb.prototype.migrateV02 = function(cb) {
|
|||||||
key: IN_BLK_PREFIX + k[1],
|
key: IN_BLK_PREFIX + k[1],
|
||||||
value: data.value,
|
value: data.value,
|
||||||
});
|
});
|
||||||
if (c++>N) {
|
if (c++ > N) {
|
||||||
console.log('\t%dM txs processed', ((c2+=N)/1e6).toFixed(3));
|
console.log('\t%dM txs processed', ((c2 += N) / 1e6).toFixed(3));
|
||||||
db.batch(dbScript,function () {
|
db.batch(dbScript, function() {
|
||||||
c=0;
|
c = 0;
|
||||||
dbScript=[];
|
dbScript = [];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('error', function(err) {
|
.on('error', function(err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
})
|
})
|
||||||
.on('end', function (){
|
.on('end', function() {
|
||||||
return cb();
|
return cb();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -433,32 +480,38 @@ BlockDb.prototype.migrateV02cleanup = function(cb) {
|
|||||||
var k = 'txb-';
|
var k = 'txb-';
|
||||||
var d = this.txDb._db;
|
var d = this.txDb._db;
|
||||||
d.createReadStream({
|
d.createReadStream({
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.pipe(d.createWriteStream({type:'del'}))
|
.pipe(d.createWriteStream({
|
||||||
.on('close', function(err){
|
type: 'del'
|
||||||
|
}))
|
||||||
|
.on('close', function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
console.log('## deleting tx- from txs db'); //todo
|
console.log('## deleting tx- from txs db'); //todo
|
||||||
|
|
||||||
var k = 'tx-';
|
var k = 'tx-';
|
||||||
var d = self.txDb._db;
|
var d = self.txDb._db;
|
||||||
d.createReadStream({
|
d.createReadStream({
|
||||||
start: k,
|
|
||||||
end: k + '~'
|
|
||||||
})
|
|
||||||
.pipe(d.createWriteStream({type:'del'}))
|
|
||||||
.on('close', function(err){
|
|
||||||
if (err) return cb(err);
|
|
||||||
var k = 'txa-';
|
|
||||||
var d = self.txDb._db;
|
|
||||||
d.createReadStream({
|
|
||||||
start: k,
|
start: k,
|
||||||
end: k + '~'
|
end: k + '~'
|
||||||
})
|
})
|
||||||
.pipe(d.createWriteStream({type:'del'}))
|
.pipe(d.createWriteStream({
|
||||||
.on('close', cb);
|
type: 'del'
|
||||||
});
|
}))
|
||||||
|
.on('close', function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
var k = 'txa-';
|
||||||
|
var d = self.txDb._db;
|
||||||
|
d.createReadStream({
|
||||||
|
start: k,
|
||||||
|
end: k + '~'
|
||||||
|
})
|
||||||
|
.pipe(d.createWriteStream({
|
||||||
|
type: 'del'
|
||||||
|
}))
|
||||||
|
.on('close', cb);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -198,7 +198,6 @@ TransactionDb.prototype._fillOutpoints = function(txInfo, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
txInfo.firstSeenTs = ret.ts;
|
txInfo.firstSeenTs = ret.ts;
|
||||||
i.unconfirmedInput = i.unconfirmedInput;
|
|
||||||
i.addr = ret.addr;
|
i.addr = ret.addr;
|
||||||
i.valueSat = ret.valueSat;
|
i.valueSat = ret.valueSat;
|
||||||
i.value = ret.valueSat / util.COIN;
|
i.value = ret.valueSat / util.COIN;
|
||||||
|
|||||||
@ -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.13",
|
"version": "0.2.15",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Ryan X Charles",
|
"name": "Ryan X Charles",
|
||||||
"email": "ryan@bitpay.com"
|
"email": "ryan@bitpay.com"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user