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

View File

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

View File

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