txdb-wallet.

This commit is contained in:
Christopher Jeffrey 2016-08-12 04:29:20 -07:00
parent edb8a50c63
commit 5b1a7a89ac
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
9 changed files with 1284 additions and 1029 deletions

View File

@ -79,7 +79,7 @@ function runBench(callback) {
function(next) {
var nonce = new bn(0);
var end;
utils.forRange(0, 100000, function(i, next) {
utils.forRange(0, 10000, function(i, next) {
var t1 = bcoin.mtx()
.addOutput(addrs[(i + 0) % addrs.length], 50460)
.addOutput(addrs[(i + 1) % addrs.length], 50460)
@ -97,7 +97,7 @@ function runBench(callback) {
});
}, function(err) {
assert.ifError(err);
end(100000);
end(10000);
next();
});
end = bench('tx');

View File

@ -298,7 +298,7 @@ Fullnode.prototype._open = function open(callback) {
},
function(next) {
if (self.options.noScan) {
self.walletdb.tx.writeTip(self.chain.tip.hash, next);
self.walletdb.writeTip(self.chain.tip.hash, next);
return next();
}
// Always rescan to make sure we didn't miss anything:
@ -307,7 +307,7 @@ Fullnode.prototype._open = function open(callback) {
},
function(next) {
var i;
self.walletdb.getUnconfirmed(function(err, txs) {
self.wallet.getUnconfirmed(function(err, txs) {
if (err)
return next(err);

View File

@ -2516,7 +2516,7 @@ RPC.prototype.resendwallettransactions = function resendwallettransactions(args,
if (args.help || args.length !== 0)
return callback(new RPCError('resendwallettransactions'));
this.walletdb.getUnconfirmed(function(err, txs) {
this.wallet.getUnconfirmed(function(err, txs) {
if (err)
return callback(err);
@ -2603,7 +2603,7 @@ RPC.prototype.dumpwallet = function dumpwallet(args, callback) {
''
];
this.walletdb.getAddresses(this.wallet.id, function(err, hashes) {
this.wallet.getAddresses(function(err, hashes) {
if (err)
return callback(err);
@ -2728,7 +2728,7 @@ RPC.prototype.getaddressesbyaccount = function getaddressesbyaccount(args, callb
addrs = [];
this.walletdb.getAddresses(this.wallet.id, function(err, hashes) {
this.wallet.getAddresses(function(err, hashes) {
if (err)
return callback(err);
@ -2909,7 +2909,7 @@ RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) {
var self = this;
var i, det, receive, member, sent, received, json;
this.walletdb.tx.toDetails(this.wallet.id, tx, function(err, details) {
this.wallet.tx.toDetails(tx, function(err, details) {
if (err)
return callback(err);
@ -3000,7 +3000,7 @@ RPC.prototype.gettransaction = function gettransaction(args, callback) {
hash = utils.revHex(hash);
this.walletdb.getTX(hash, function(err, tx) {
this.wallet.getTX(hash, function(err, tx) {
if (err)
return callback(err);
@ -3024,7 +3024,7 @@ RPC.prototype.abandontransaction = function abandontransaction(args, callback) {
hash = utils.revHex(hash);
this.walletdb.tx.remove(hash, function(err, result) {
this.wallet.abandon(hash, function(err, result) {
if (err)
return callback(err);
@ -3057,7 +3057,7 @@ RPC.prototype.getwalletinfo = function getwalletinfo(args, callback) {
if (err)
return callback(err);
self.walletdb.tx.getHistoryHashes(self.wallet.id, function(err, hashes) {
self.wallet.tx.getHistoryHashes(self.wallet.id, function(err, hashes) {
if (err)
return callback(err);
@ -3124,7 +3124,7 @@ RPC.prototype.listaccounts = function listaccounts(args, callback) {
map = {};
this.walletdb.getAccounts(this.wallet.id, function(err, accounts) {
this.wallet.getAccounts(function(err, accounts) {
if (err)
return callback(err);
@ -3240,7 +3240,7 @@ RPC.prototype._toListTX = function _toListTX(tx, callback) {
var i, receive, member, det, sent, received, index;
var sendMember, recMember, sendIndex, recIndex, json;
this.walletdb.tx.toDetails(this.wallet.id, tx, function(err, details) {
this.wallet.tx.toDetails(tx, function(err, details) {
if (err)
return callback(err);

View File

@ -251,6 +251,44 @@ LowlevelUp.prototype.fetch = function fetch(key, parse, callback) {
});
};
LowlevelUp.prototype.each = function each(options, handler, callback) {
var opt, iter;
opt = {
gte: options.gte,
lte: options.lte,
keys: true,
values: options.values || false,
fillCache: options.fillCache || false,
keyAsBuffer: options.keyAsBuffer || false,
valueAsBuffer: true,
reverse: options.reverse || false
};
if (options.limit != null)
opt.limit = options.limit;
iter = this.iterator(opt);
(function next(stop) {
if (stop === true)
return iter.end(callback);
iter.next(function(err, key, value) {
if (err) {
return iter.end(function() {
callback(err);
});
}
if (key === undefined)
return iter.end(callback);
handler(key, value, next);
});
})();
};
/**
* Collect all keys from iterator options.
* @param {Object} options - Iterator options.

View File

@ -213,7 +213,7 @@ SPVNode.prototype._open = function open(callback) {
},
function(next) {
var i;
self.walletdb.getUnconfirmed(function(err, txs) {
self.wallet.getUnconfirmed(function(err, txs) {
if (err)
return next(err);

File diff suppressed because it is too large Load Diff

View File

@ -884,20 +884,6 @@ utils.sortKeys = function sortKeys(keys) {
});
};
/**
* Sort transactions by timestamp.
* @param {TX[]} txs
* @returns {TX[]} Sorted transactions.
*/
utils.sortTX = function sortTX(txs) {
return txs.slice().sort(function(a, b) {
a = a.ts || a.ps;
b = b.ts || b.ps;
return a - b;
});
};
/**
* Unique-ify an array of strings.
* @param {String[]} obj

View File

@ -14,6 +14,7 @@ var utils = require('./utils');
var assert = utils.assert;
var BufferReader = require('./reader');
var BufferWriter = require('./writer');
var TXDB = require('./txdb');
/**
* BIP44 Wallet
@ -60,6 +61,7 @@ function Wallet(db, options) {
this.accountDepth = 0;
this.token = constants.ZERO_HASH;
this.tokenDepth = 0;
this.tx = new TXDB(this.db);
this.account = null;
@ -126,6 +128,7 @@ Wallet.prototype.fromOptions = function fromOptions(options) {
this.id = id;
this.token = token;
this.tx.id = this.id;
return this;
};
@ -517,6 +520,15 @@ Wallet.prototype.getAccounts = function getAccounts(callback) {
this.db.getAccounts(this.id, callback);
};
/**
* Get all wallet address hashes.
* @param {Function} callback - Returns [Error, Array].
*/
Wallet.prototype.getAddresses = function getAddresses(callback) {
this.db.getAddresses(this.id, callback);
};
/**
* Retrieve an account from the database.
* @param {Number|String} account
@ -1030,7 +1042,7 @@ Wallet.prototype.getOutputPaths = function getOutputPaths(tx, callback) {
* Sync address depths based on a transaction's outputs.
* This is used for deriving new addresses when
* a confirmed transaction is seen.
* @param {WalletMap} info
* @param {PathInfo} info
* @param {Function} callback - Returns [Errr, Boolean]
* (true if new addresses were allocated).
*/
@ -1054,9 +1066,6 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(info, callback) {
for (i = 0; i < info.paths.length; i++) {
path = info.paths[i];
if (path.id !== this.id)
continue;
if (!accounts[path.account])
accounts[path.account] = [];
@ -1278,7 +1287,7 @@ Wallet.prototype._sign = function _sign(addresses, master, tx, index, type, call
*/
Wallet.prototype.fillCoins = function fillCoins(tx, callback) {
return this.db.fillCoins(tx, callback);
return this.tx.fillCoins(tx, callback);
};
/**
@ -1289,7 +1298,7 @@ Wallet.prototype.fillCoins = function fillCoins(tx, callback) {
*/
Wallet.prototype.getCoin = function getCoin(hash, index, callback) {
return this.db.getCoin(hash, index, callback);
return this.tx.getCoin(hash, index, callback);
};
/**
@ -1299,7 +1308,7 @@ Wallet.prototype.getCoin = function getCoin(hash, index, callback) {
*/
Wallet.prototype.getTX = function getTX(hash, callback) {
return this.db.getTX(hash, callback);
return this.tx.getTX(hash, callback);
};
/**
@ -1309,7 +1318,7 @@ Wallet.prototype.getTX = function getTX(hash, callback) {
*/
Wallet.prototype.addTX = function addTX(tx, callback) {
return this.db.addTX(tx, callback);
this.db.addTX(tx, callback);
};
/**
@ -1319,7 +1328,9 @@ Wallet.prototype.addTX = function addTX(tx, callback) {
*/
Wallet.prototype.getHistory = function getHistory(account, callback) {
return this.db.getHistory(this.id, account, callback);
this._getKey(account, callback, function(account, callback) {
this.tx.getHistory(account, callback);
});
};
/**
@ -1329,7 +1340,9 @@ Wallet.prototype.getHistory = function getHistory(account, callback) {
*/
Wallet.prototype.getCoins = function getCoins(account, callback) {
return this.db.getCoins(this.id, account, callback);
this._getKey(account, callback, function(account, callback) {
this.tx.getCoins(account, callback);
});
};
/**
@ -1339,7 +1352,9 @@ Wallet.prototype.getCoins = function getCoins(account, callback) {
*/
Wallet.prototype.getUnconfirmed = function getUnconfirmed(account, callback) {
return this.db.getUnconfirmed(this.id, account, callback);
this._getKey(account, callback, function(account, callback) {
this.tx.getUnconfirmed(account, callback);
});
};
/**
@ -1349,7 +1364,9 @@ Wallet.prototype.getUnconfirmed = function getUnconfirmed(account, callback) {
*/
Wallet.prototype.getBalance = function getBalance(account, callback) {
return this.db.getBalance(this.id, account, callback);
this._getKey(account, callback, function(account, callback) {
this.tx.getBalance(account, callback);
});
};
/**
@ -1361,7 +1378,9 @@ Wallet.prototype.getBalance = function getBalance(account, callback) {
*/
Wallet.prototype.getLastTime = function getLastTime(account, callback) {
return this.db.getLastTime(this.id, account, callback);
this._getKey(account, callback, function(account, callback) {
this.tx.getLastTime(account, callback);
});
};
/**
@ -1372,7 +1391,14 @@ Wallet.prototype.getLastTime = function getLastTime(account, callback) {
*/
Wallet.prototype.getLast = function getLast(account, limit, callback) {
return this.db.getLast(this.id, account, limit, callback);
if (typeof limit === 'function') {
callback = limit;
limit = account;
account = null;
}
this._getKey(account, callback, function(account, callback) {
this.tx.getLast(account, limit, callback);
});
};
/**
@ -1385,7 +1411,14 @@ Wallet.prototype.getLast = function getLast(account, limit, callback) {
*/
Wallet.prototype.getTimeRange = function getTimeRange(account, options, callback) {
return this.db.getTimeRange(this.id, account, options, callback);
if (typeof options === 'function') {
callback = options;
options = account;
account = null;
}
this._getKey(account, callback, function(account, callback) {
this.tx.getTimeRange(account, options, callback);
});
};
/**
@ -1396,7 +1429,61 @@ Wallet.prototype.getTimeRange = function getTimeRange(account, options, callback
*/
Wallet.prototype.zap = function zap(account, age, callback) {
return this.db.zap(this.id, account, age, callback);
if (typeof age === 'function') {
callback = age;
age = account;
account = null;
}
this._getKey(account, callback, function(account, callback) {
this.tx.zap(account, age, callback);
});
};
/**
* Abandon transaction (accesses db).
* @param {Hash} hash
* @param {Function} callback - Returns [Error].
*/
Wallet.prototype.abandon = function abandon(account, hash, callback) {
if (typeof hash === 'function') {
callback = hash;
hash = account;
account = null;
}
this._getKey(account, callback, function(account, callback) {
this.tx.abandon(account, hash, callback);
});
};
/**
* Resolve account index.
* @private
* @param {(Number|String)?} account
* @param {Function} errback - Returns [Error].
* @param {Function} callback
*/
Wallet.prototype._getKey = function _getKey(account, errback, callback) {
var self = this;
if (typeof account === 'function') {
errback = account;
account = null;
}
if (account == null)
return callback.call(this, null, errback);
this.db.getAccountIndex(this.id, account, function(err, index) {
if (err)
return errback(err);
if (index === -1)
return errback(new Error('Account not found.'));
return callback.call(self, index, errback);
});
};
/**
@ -1695,6 +1782,7 @@ Wallet.prototype.fromJSON = function fromJSON(json) {
this.accountDepth = json.accountDepth;
this.token = new Buffer(json.token, 'hex');
this.master = MasterKey.fromJSON(json.master);
this.tx.id = this.id;
return this;
};
@ -1736,6 +1824,7 @@ Wallet.prototype.fromRaw = function fromRaw(data) {
this.token = p.readBytes(32);
this.tokenDepth = p.readU32();
this.master = MasterKey.fromRaw(p.readVarBytes());
this.tx.id = this.id;
return this;
};

File diff suppressed because it is too large Load Diff