From d697684b83cf25dbccd51392313a7bd067057356 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 11 Oct 2016 06:34:20 -0700 Subject: [PATCH] txdb: inspection. misc. --- lib/http/rpc.js | 6 +-- lib/wallet/account.js | 7 ++-- lib/wallet/txdb.js | 85 +++++++++++++++++++++++++++---------------- lib/wallet/wallet.js | 16 ++++++-- 4 files changed, 72 insertions(+), 42 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 3cf60c16..f4a34ea1 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -3163,21 +3163,19 @@ RPC.prototype.getunconfirmedbalance = co(function* getunconfirmedbalance(args) { }); RPC.prototype.getwalletinfo = co(function* getwalletinfo(args) { - var balance, hashes; + var balance; if (args.help || args.length !== 0) throw new RPCError('getwalletinfo'); balance = yield this.wallet.getBalance(); - hashes = yield this.wallet.txdb.getHistoryHashes(this.wallet.id); - return { walletid: this.wallet.id, walletversion: 0, balance: +utils.btc(balance.total), unconfirmed_balance: +utils.btc(balance.unconfirmed), - txcount: hashes.length, + txcount: this.wallet.state.tx, keypoololdest: 0, keypoolsize: 0, unlocked_until: this.wallet.master.until, diff --git a/lib/wallet/account.js b/lib/wallet/account.js index b5cb033d..84575b26 100644 --- a/lib/wallet/account.js +++ b/lib/wallet/account.js @@ -59,7 +59,7 @@ function Account(db, options) { this.id = null; this.name = null; this.initialized = false; - this.witness = this.db.options.witness; + this.witness = this.db.options.witness === true; this.watchOnly = false; this.type = Account.types.PUBKEYHASH; this.m = 1; @@ -749,9 +749,10 @@ Account.prototype.inspect = function inspect() { * @returns {Object} */ -Account.prototype.toJSON = function toJSON() { +Account.prototype.toJSON = function toJSON(minimal) { return { - wid: this.wid, + wid: minimal ? undefined : this.wid, + id: minimal ? undefined : this.id, name: this.name, initialized: this.initialized, witness: this.witness, diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index f3ab74c1..89c350c7 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -802,6 +802,9 @@ TXDB.prototype._add = co(function* add(tx, info) { this.coinCache.set(key, coin); } + this.pending.tx++; + this.put(layout.R, this.pending.commit()); + // Clear any locked coins to free up memory. this.unlockTX(tx); @@ -812,9 +815,6 @@ TXDB.prototype._add = co(function* add(tx, info) { this.emit('balance', this.pending.balance, info); - this.pending.tx++; - this.put(layout.R, this.pending.commit()); - return true; }); @@ -1024,12 +1024,12 @@ TXDB.prototype.confirm = co(function* confirm(tx, info) { this.coinCache.set(key, coin); } + this.put(layout.R, this.pending.commit()); + this.emit('tx', tx, info); this.emit('confirmed', tx, info); this.emit('balance', this.pending.balance, info); - this.put(layout.R, this.pending.commit()); - return true; }); @@ -1180,12 +1180,12 @@ TXDB.prototype.__remove = co(function* remove(tx, info) { this.coinCache.remove(key); } - this.emit('remove tx', tx, info); - this.emit('balance', this.pending.balance, info); - this.pending.tx--; this.put(layout.R, this.pending.commit()); + this.emit('remove tx', tx, info); + this.emit('balance', this.pending.balance, info); + return info; }); @@ -1288,11 +1288,11 @@ TXDB.prototype.__unconfirm = co(function* unconfirm(tx, info) { this.coinCache.set(key, coin); } + this.put(layout.R, this.pending.commit()); + this.emit('unconfirmed', tx, info); this.emit('balance', this.pending.balance, info); - this.put(layout.R, this.pending.commit()); - return info; }); @@ -1506,6 +1506,33 @@ TXDB.prototype.getOutpoints = function getOutpoints(account) { }); }; +/** + * Get TX hashes by height range. + * @param {Number?} account + * @param {Object} options + * @param {Number} options.start - Start height. + * @param {Number} options.end - End height. + * @param {Number?} options.limit - Max number of records. + * @param {Boolean?} options.reverse - Reverse order. + * @returns {Promise} - Returns {@link Hash}[]. + */ + +TXDB.prototype.getAccountHeightRangeHashes = function getAccountHeightRangeHashes(account, options) { + var start = options.start || 0; + var end = options.end || 0xffffffff; + + return this.keys({ + gte: layout.H(account, start, constants.NULL_HASH), + lte: layout.H(account, end, constants.HIGH_HASH), + limit: options.limit, + reverse: options.reverse, + parse: function(key) { + key = layout.Hh(key); + return key[2]; + } + }); +}; + /** * Get TX hashes by height range. * @param {Number?} account @@ -1525,22 +1552,12 @@ TXDB.prototype.getHeightRangeHashes = function getHeightRangeHashes(account, opt account = null; } + if (account != null) + return this.getAccountHeightRangeHashes(account, options); + start = options.start || 0; end = options.end || 0xffffffff; - if (account != null) { - return this.keys({ - gte: layout.H(account, start, constants.NULL_HASH), - lte: layout.H(account, end, constants.HIGH_HASH), - limit: options.limit, - reverse: options.reverse, - parse: function(key) { - key = layout.Hh(key); - return key[2]; - } - }); - } - return this.keys({ gte: layout.h(start, constants.NULL_HASH), lte: layout.h(end, constants.HIGH_HASH), @@ -2277,11 +2294,11 @@ Balance.fromRaw = function fromRaw(wid, id, data) { return new Balance(wid, id, -1).fromRaw(data); }; -Balance.prototype.toJSON = function toJSON() { +Balance.prototype.toJSON = function toJSON(minimal) { return { - wid: this.wid, - id: this.id, - account: this.account, + wid: !minimal ? this.wid : undefined, + id: !minimal ? this.id : undefined, + account: !minimal ? this.account : undefined, unconfirmed: utils.btc(this.unconfirmed), confirmed: utils.btc(this.confirmed), total: utils.btc(this.total) @@ -2296,6 +2313,10 @@ Balance.prototype.toString = function toString() { + '>'; }; +Balance.prototype.inspect = function inspect() { + return this.toString(); +}; + /** * Chain State * @constructor @@ -2363,16 +2384,16 @@ TXDBState.prototype.unconfirm = function unconfirm(value) { TXDBState.prototype.toJSON = function toJSON() { return { - wid: this.wid, - id: this.id, tx: this.tx, coin: this.coin, - unconfirmed: utils.btc(this.balance.unconfirmed), - confirmed: utils.btc(this.balance.confirmed), - total: utils.btc(this.balance.total) + balance: this.balance }; }; +TXDBState.prototype.inspect = function inspect() { + return this.toJSON(); +}; + /* * Helpers */ diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 6fca8bdf..b18c7ee2 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -2340,6 +2340,14 @@ Wallet.prototype.__defineGetter__('nested', function() { return this.account.nested; }); +Wallet.prototype.__defineGetter__('state', function() { + return this.txdb.state; +}); + +Wallet.prototype.__defineGetter__('balance', function() { + return this.txdb.balance; +}); + /** * Convert the wallet to a more inspection-friendly object. * @returns {Object} @@ -2354,6 +2362,7 @@ Wallet.prototype.inspect = function inspect() { accountDepth: this.accountDepth, token: this.token.toString('hex'), tokenDepth: this.tokenDepth, + state: this.state ? this.state.toJSON() : null, master: this.master, account: this.account }; @@ -2376,8 +2385,9 @@ Wallet.prototype.toJSON = function toJSON() { accountDepth: this.accountDepth, token: this.token.toString('hex'), tokenDepth: this.tokenDepth, + balance: this.balance.toJSON(true), master: this.master.toJSON(), - account: this.account ? this.account.toJSON() : null + account: this.account ? this.account.toJSON(true) : null }; }; @@ -2407,7 +2417,7 @@ Wallet.prototype.fromJSON = function fromJSON(json) { this.watchOnly = json.watchOnly; this.accountDepth = json.accountDepth; this.token = new Buffer(json.token, 'hex'); - this.master = MasterKey.fromJSON(json.master); + this.master.fromJSON(json.master); assert(network === this.db.network, 'Wallet network mismatch.'); @@ -2457,7 +2467,7 @@ Wallet.prototype.fromRaw = function fromRaw(data) { this.accountDepth = p.readU32(); this.token = p.readBytes(32); this.tokenDepth = p.readU32(); - this.master = MasterKey.fromRaw(p.readVarBytes()); + this.master.fromRaw(p.readVarBytes()); assert(network === this.db.network, 'Wallet network mismatch.');