This commit is contained in:
Christopher Jeffrey 2016-03-31 14:27:29 -07:00
parent 3031c4cd5c
commit b0e6826232
3 changed files with 115 additions and 50 deletions

View File

@ -940,17 +940,22 @@ TXPool.prototype._unconfirm = function unconfirm(tx, map, callback, force) {
}); });
}; };
TXPool.prototype.getTXHashes = function getTXHashes(address, callback) { TXPool.prototype.getAllHashes = function getAllHashes(address, callback) {
var self = this; var self = this;
var prefix = this.prefix + '/'; var prefix = this.prefix + '/';
var txs = []; var txs = [];
var iter; var iter;
if (typeof address === 'function') {
callback = address;
address = null;
}
callback = utils.ensure(callback); callback = utils.ensure(callback);
if (Array.isArray(address)) { if (Array.isArray(address)) {
return utils.forEachSerial(address, function(address, next) { return utils.forEachSerial(address, function(address, next) {
self.getTXHashes(address, function(err, tx) { self.getAllHashes(address, function(err, tx) {
if (err) if (err)
return next(err); return next(err);
@ -1009,6 +1014,11 @@ TXPool.prototype.getPendingHashes = function getPendingHashes(address, callback)
var txs = []; var txs = [];
var iter; var iter;
if (typeof address === 'function') {
callback = address;
address = null;
}
callback = utils.ensure(callback); callback = utils.ensure(callback);
if (Array.isArray(address)) { if (Array.isArray(address)) {
@ -1067,17 +1077,22 @@ TXPool.prototype.getPendingHashes = function getPendingHashes(address, callback)
})(); })();
}; };
TXPool.prototype.getCoinIDs = function getCoinIDs(address, callback) { TXPool.prototype.getCoinHashes = function getCoinHashes(address, callback) {
var self = this; var self = this;
var prefix = this.prefix + '/'; var prefix = this.prefix + '/';
var coins = []; var coins = [];
var iter; var iter;
if (typeof address === 'function') {
callback = address;
address = null;
}
callback = utils.ensure(callback); callback = utils.ensure(callback);
if (Array.isArray(address)) { if (Array.isArray(address)) {
return utils.forEachSerial(address, function(address, next) { return utils.forEachSerial(address, function(address, next) {
self.getCoinIDs(address, function(err, coin) { self.getCoinHashes(address, function(err, coin) {
if (err) if (err)
return next(err); return next(err);
@ -1089,15 +1104,17 @@ TXPool.prototype.getCoinIDs = function getCoinIDs(address, callback) {
if (err) if (err)
return callback(err); return callback(err);
coins = utils.uniqs(coins);
return callback(null, coins); return callback(null, coins);
}); });
} }
iter = this.db.iterator({ iter = this.db.iterator({
gte: address ? prefix + 'u/a/' + address : prefix + 'u/t', gte: address
lte: address ? prefix + 'u/a/' + address + '~' : prefix + 'u/t~', ? prefix + 'u/a/' + address
: prefix + 'u/t',
lte: address
? prefix + 'u/a/' + address + '~'
: prefix + 'u/t~',
keys: true, keys: true,
values: false, values: false,
fillCache: false, fillCache: false,
@ -1120,10 +1137,12 @@ TXPool.prototype.getCoinIDs = function getCoinIDs(address, callback) {
}); });
} }
key = key.split('/');
if (address) if (address)
coins.push(key.split('/').slice(4).join('/')); coins.push([key[4], +key[5]]);
else else
coins.push(key.split('/').slice(3).join('/')); coins.push([key[3], +key[4]]);
next(); next();
}); });
@ -1135,6 +1154,11 @@ TXPool.prototype.getHeightRangeHashes = function getHeightRangeHashes(address, o
var txs = []; var txs = [];
var iter; var iter;
if (typeof address === 'function') {
callback = address;
address = null;
}
callback = utils.ensure(callback); callback = utils.ensure(callback);
iter = this.db.iterator({ iter = this.db.iterator({
@ -1187,6 +1211,11 @@ TXPool.prototype.getRangeHashes = function getRangeHashes(address, options, call
var txs = []; var txs = [];
var iter; var iter;
if (typeof address === 'function') {
callback = address;
address = null;
}
callback = utils.ensure(callback); callback = utils.ensure(callback);
iter = this.db.iterator({ iter = this.db.iterator({
@ -1234,6 +1263,11 @@ TXPool.prototype.getRange = function getLast(address, options, callback) {
var self = this; var self = this;
var txs = []; var txs = [];
if (typeof address === 'function') {
callback = address;
address = null;
}
return this.getRangeHashes(address, options, function(err, hashes) { return this.getRangeHashes(address, options, function(err, hashes) {
if (err) if (err)
return callback(err); return callback(err);
@ -1260,6 +1294,12 @@ TXPool.prototype.getRange = function getLast(address, options, callback) {
}; };
TXPool.prototype.getLast = function getLast(address, limit, callback) { TXPool.prototype.getLast = function getLast(address, limit, callback) {
if (typeof limit === 'function') {
callback = limit;
limit = address;
address = null;
}
return this.getRange(address, { return this.getRange(address, {
start: 0, start: 0,
end: 0xffffffff, end: 0xffffffff,
@ -1268,11 +1308,16 @@ TXPool.prototype.getLast = function getLast(address, limit, callback) {
}, callback); }, callback);
}; };
TXPool.prototype.getAllByAddress = function getAllByAddress(address, callback) { TXPool.prototype.getAll = function getAll(address, callback) {
var self = this; var self = this;
var txs = []; var txs = [];
return this.getTXHashes(address, function(err, hashes) { if (typeof address === 'function') {
callback = address;
address = null;
}
return this.getAllHashes(address, function(err, hashes) {
if (err) if (err)
return callback(err); return callback(err);
@ -1298,7 +1343,12 @@ TXPool.prototype.getAllByAddress = function getAllByAddress(address, callback) {
}; };
TXPool.prototype.getLastTime = function getLastTime(address, callback) { TXPool.prototype.getLastTime = function getLastTime(address, callback) {
return this.getAllByAddress(address, function(err, txs) { if (typeof address === 'function') {
callback = address;
address = null;
}
return this.getAll(address, function(err, txs) {
var lastTs, lastHeight; var lastTs, lastHeight;
if (err) if (err)
@ -1319,10 +1369,15 @@ TXPool.prototype.getLastTime = function getLastTime(address, callback) {
}); });
}; };
TXPool.prototype.getPendingByAddress = function getPendingByAddress(address, callback) { TXPool.prototype.getPending = function getPending(address, callback) {
var self = this; var self = this;
var txs = []; var txs = [];
if (typeof address === 'function') {
callback = address;
address = null;
}
return this.getPendingHashes(address, function(err, hashes) { return this.getPendingHashes(address, function(err, hashes) {
if (err) if (err)
return callback(err); return callback(err);
@ -1348,17 +1403,21 @@ TXPool.prototype.getPendingByAddress = function getPendingByAddress(address, cal
}); });
}; };
TXPool.prototype.getCoinsByAddress = function getCoinsByAddress(address, callback) { TXPool.prototype.getCoins = function getCoins(address, callback) {
var self = this; var self = this;
var coins = []; var coins = [];
return this.getCoinIDs(address, function(err, map) { if (typeof address === 'function') {
callback = address;
address = null;
}
return this.getCoinHashes(address, function(err, hashes) {
if (err) if (err)
return callback(err); return callback(err);
utils.forEachSerial(map, function(id, next) { utils.forEachSerial(hashes, function(key, next) {
var parts = id.split('/'); self.getCoin(key[0], key[1], function(err, coin) {
self.getCoin(parts[0], +parts[1], function(err, coin) {
if (err) if (err)
return callback(err); return callback(err);
@ -1458,14 +1517,11 @@ TXPool.prototype.fillCoins = function fillCoins(tx, callback) {
TXPool.prototype.getTX = function getTX(hash, callback) { TXPool.prototype.getTX = function getTX(hash, callback) {
var prefix = this.prefix + '/'; var prefix = this.prefix + '/';
var id = prefix + 't/t/' + hash; var key = prefix + 't/t/' + hash;
this.db.get(id, function(err, tx) { this.db.get(key, function(err, tx) {
if (err) { if (err && err.type !== 'NotFoundError')
if (err.type === 'NotFoundError')
return callback();
return callback(err); return callback(err);
}
if (!tx) if (!tx)
return callback(); return callback();
@ -1482,14 +1538,11 @@ TXPool.prototype.getTX = function getTX(hash, callback) {
TXPool.prototype.getCoin = function getCoin(hash, index, callback) { TXPool.prototype.getCoin = function getCoin(hash, index, callback) {
var prefix = this.prefix + '/'; var prefix = this.prefix + '/';
var id = prefix + 'u/t/' + hash + '/' + index; var key = prefix + 'u/t/' + hash + '/' + index;
this.db.get(id, function(err, coin) { this.db.get(key, function(err, coin) {
if (err) { if (err && err.type !== 'NotFoundError')
if (err.type === 'NotFoundError')
return callback();
return callback(err); return callback(err);
}
if (!coin) if (!coin)
return callback(); return callback();
@ -1506,37 +1559,48 @@ TXPool.prototype.getCoin = function getCoin(hash, index, callback) {
}); });
}; };
TXPool.prototype.getBalanceByAddress = function getBalanceByAddress(address, callback) { TXPool.prototype.getBalance = function getBalance(address, callback) {
return this.getCoinsByAddress(address, function(err, coins) { var confirmed = new bn(0);
var unconfirmed = new bn(0);
var i;
if (typeof address === 'function') {
callback = address;
address = null;
}
return this.getCoins(address, function(err, coins) {
if (err) if (err)
return callback(err); return callback(err);
coins = coins.reduce(function(acc, coin) { for (i = 0; i < coins.length; i++) {
return acc.iadd(coin.value); if (coins[i].height !== -1)
}, new bn(0)); confirmed.iadd(coins[i].value);
unconfirmed.iadd(coins[i].value);
}
return callback(null, coins); return callback(null, unconfirmed, confirmed, coins);
}); });
}; };
TXPool.prototype.getAllHashes = function getAllHashes(callback) { TXPool.prototype.getAllHashesByAddress = function getAllHashesByAddress(address, callback) {
return this.getTXHashes(null, callback); return this.getAllHashes(address, callback);
}; };
TXPool.prototype.getAll = function getAll(callback) { TXPool.prototype.getAllByAddress = function getAllByAddress(address, callback) {
return this.getAllByAddress(null, callback); return this.getAll(address, callback);
}; };
TXPool.prototype.getCoins = function getCoins(callback) { TXPool.prototype.getCoinsByAddress = function getCoins(address, callback) {
return this.getCoinsByAddress(null, callback); return this.getCoins(address, callback);
}; };
TXPool.prototype.getPending = function getPending(callback) { TXPool.prototype.getPendingByAddress = function getPendingByAddress(address, callback) {
return this.getPendingByAddress(null, callback); return this.getPending(address, callback);
}; };
TXPool.prototype.getBalance = function getBalance(callback) { TXPool.prototype.getBalanceByAddress = function getBalanceByAddress(address, callback) {
return this.getBalanceByAddress(null, callback); return this.getBalance(address, callback);
}; };
TXPool.prototype.addUnchecked = function addUnchecked(tx, callback, force) { TXPool.prototype.addUnchecked = function addUnchecked(tx, callback, force) {

View File

@ -85,8 +85,8 @@ WalletDB.prototype._init = function _init() {
return; return;
this.db = bcoin.ldb('wallet', { this.db = bcoin.ldb('wallet', {
cacheSize: 4 * 1024 * 1024, cacheSize: 8 << 20,
writeBufferSize: 4 * 1024 * 1024 writeBufferSize: 4 << 20
}); });
this.db.open(function(err) { this.db.open(function(err) {
@ -101,7 +101,7 @@ WalletDB.prototype._init = function _init() {
indexExtra: true, indexExtra: true,
indexAddress: true, indexAddress: true,
mapAddress: true, mapAddress: true,
verify: true verify: this.options.verify
}); });
this.tx.on('error', function(err) { this.tx.on('error', function(err) {

View File

@ -25,6 +25,7 @@ var dummyInput = {
describe('Wallet', function() { describe('Wallet', function() {
var wdb = new bcoin.walletdb(); var wdb = new bcoin.walletdb();
wdb.tx.options.verify = true;
it('should generate new key and address', function() { it('should generate new key and address', function() {
var w = bcoin.wallet(); var w = bcoin.wallet();