walletdb: better pathinfo and details handling.

This commit is contained in:
Christopher Jeffrey 2016-08-14 17:26:24 -07:00
parent 0a5fcad5e8
commit e324e08bef
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 33 additions and 19 deletions

View File

@ -1936,13 +1936,15 @@ TXDB.prototype.abandon = function abandon(hash, callback, force) {
* Details * Details
*/ */
function Details(id, tx, table) { function Details(db, id, tx, table) {
this.db = db;
this.network = db.network;
this.id = id; this.id = id;
this.hash = tx.hash('hex'); this.hash = tx.hash('hex');
this.height = tx.height; this.height = tx.height;
this.block = tx.block; this.block = tx.block;
this.index = tx.index; this.index = tx.index;
this.confirmations = tx.getConfirmations(); this.confirmations = tx.getConfirmations(this.db.height);
this.fee = tx.hasCoins() ? tx.getFee() : 0; this.fee = tx.hasCoins() ? tx.getFee() : 0;
this.ts = tx.ts; this.ts = tx.ts;
this.ps = tx.ps; this.ps = tx.ps;
@ -1991,6 +1993,7 @@ Details.prototype._insert = function _insert(vector, target, table) {
}; };
Details.prototype.toJSON = function toJSON() { Details.prototype.toJSON = function toJSON() {
var self = this;
return { return {
id: this.id, id: this.id,
hash: utils.revHex(this.hash), hash: utils.revHex(this.hash),
@ -2002,10 +2005,10 @@ Details.prototype.toJSON = function toJSON() {
fee: utils.btc(this.fee), fee: utils.btc(this.fee),
confirmations: this.confirmations, confirmations: this.confirmations,
inputs: this.inputs.map(function(input) { inputs: this.inputs.map(function(input) {
return input.toJSON(); return input.toJSON(self.network);
}), }),
outputs: this.outputs.map(function(output) { outputs: this.outputs.map(function(output) {
return output.toJSON(); return output.toJSON(self.network);
}), }),
tx: this.tx.toRaw().toString('hex') tx: this.tx.toRaw().toString('hex')
}; };
@ -2021,11 +2024,11 @@ function DetailsMember() {
this.path = null; this.path = null;
} }
DetailsMember.prototype.toJSON = function toJSON() { DetailsMember.prototype.toJSON = function toJSON(network) {
return { return {
value: utils.btc(this.value), value: utils.btc(this.value),
address: this.address address: this.address
? this.address.toBase58() ? this.address.toBase58(network)
: null, : null,
path: this.path path: this.path
? this.path.toJSON() ? this.path.toJSON()

View File

@ -170,6 +170,8 @@ Wallet.prototype.init = function init(options, callback) {
self.account = account; self.account = account;
self.logger.info('Wallet initialized (%s).', self.id);
self.tx.open(callback); self.tx.open(callback);
}); });
}); });
@ -194,6 +196,8 @@ Wallet.prototype.open = function open(callback) {
self.account = account; self.account = account;
self.logger.info('Wallet opened (%s).', self.id);
self.tx.open(callback); self.tx.open(callback);
}); });
}; };

View File

@ -67,7 +67,6 @@ function WalletDB(options) {
this.accountCache = new bcoin.lru(10000, 1); this.accountCache = new bcoin.lru(10000, 1);
this.pathCache = new bcoin.lru(100000, 1); this.pathCache = new bcoin.lru(100000, 1);
// TODO: Move to walletdb.
// Try to optimize for up to 1m addresses. // Try to optimize for up to 1m addresses.
// We use a regular bloom filter here // We use a regular bloom filter here
// because we never want members to // because we never want members to
@ -978,8 +977,9 @@ WalletDB.prototype.fetchWallet = function fetchWallet(id, callback, handler) {
*/ */
WalletDB.prototype.mapWallets = function mapWallets(tx, callback) { WalletDB.prototype.mapWallets = function mapWallets(tx, callback) {
var self = this;
var addresses = tx.getHashes('hex'); var addresses = tx.getHashes('hex');
var info; var wallets;
if (!this.testFilter(addresses)) if (!this.testFilter(addresses))
return callback(); return callback();
@ -991,9 +991,9 @@ WalletDB.prototype.mapWallets = function mapWallets(tx, callback) {
if (!table) if (!table)
return callback(); return callback();
info = PathInfo.map(tx, table); wallets = PathInfo.map(self, tx, table);
return callback(null, info); return callback(null, wallets);
}); });
}; };
@ -1004,6 +1004,7 @@ WalletDB.prototype.mapWallets = function mapWallets(tx, callback) {
*/ */
WalletDB.prototype.getPathInfo = function getPathInfo(id, tx, callback) { WalletDB.prototype.getPathInfo = function getPathInfo(id, tx, callback) {
var self = this;
var addresses = tx.getHashes('hex'); var addresses = tx.getHashes('hex');
var info; var info;
@ -1014,7 +1015,7 @@ WalletDB.prototype.getPathInfo = function getPathInfo(id, tx, callback) {
if (!table) if (!table)
return callback(); return callback();
info = new PathInfo(id, tx, table); info = new PathInfo(self, id, tx, table);
return callback(null, info); return callback(null, info);
}); });
@ -1026,13 +1027,13 @@ WalletDB.prototype.getPathInfo = function getPathInfo(id, tx, callback) {
* @param {Function} callback - Returns [Error, {@link AddressTable}]. * @param {Function} callback - Returns [Error, {@link AddressTable}].
*/ */
WalletDB.prototype.getTable = function getTable(address, callback) { WalletDB.prototype.getTable = function getTable(addresses, callback) {
var self = this; var self = this;
var table = {}; var table = {};
var count = 0; var count = 0;
var i, keys, values; var i, keys, values;
utils.forEachSerial(address, function(address, next) { utils.forEachSerial(addresses, function(address, next) {
self.getAddress(address, function(err, paths) { self.getAddress(address, function(err, paths) {
if (err) if (err)
return next(err); return next(err);
@ -1454,7 +1455,13 @@ Path.prototype.inspect = function() {
* Path Info * Path Info
*/ */
function PathInfo(id, tx, table) { function PathInfo(db, id, tx, table) {
if (!(this instanceof PathInfo))
return new PathInfo(db, id, tx, table);
// Reference to the walletdb.
this.db = db;
// All relevant Accounts for // All relevant Accounts for
// inputs and outputs (for database indexing). // inputs and outputs (for database indexing).
this.accounts = []; this.accounts = [];
@ -1482,7 +1489,7 @@ function PathInfo(id, tx, table) {
this.fromTX(tx, table); this.fromTX(tx, table);
} }
PathInfo.map = function map(tx, table) { PathInfo.map = function map(db, tx, table) {
var hashes = Object.keys(table); var hashes = Object.keys(table);
var wallets = {}; var wallets = {};
var info = []; var info = [];
@ -1504,7 +1511,7 @@ PathInfo.map = function map(tx, table) {
for (i = 0; i < wallets.length; i++) { for (i = 0; i < wallets.length; i++) {
id = wallets[i]; id = wallets[i];
info.push(new PathInfo(id, tx, table)); info.push(new PathInfo(db, id, tx, table));
} }
return info; return info;
@ -1550,8 +1557,8 @@ PathInfo.prototype.fromTX = function fromTX(tx, table) {
return this; return this;
}; };
PathInfo.fromTX = function fromTX(id, tx, table) { PathInfo.fromTX = function fromTX(db, id, tx, table) {
return new PathInfo(id).fromTX(tx, table); return new PathInfo(db, id).fromTX(tx, table);
}; };
/** /**
@ -1585,7 +1592,7 @@ PathInfo.prototype.toDetails = function toDetails() {
var details = this._details; var details = this._details;
if (!details) { if (!details) {
details = new TXDB.Details(this.id, this.tx, this.table); details = new TXDB.Details(this.db, this.id, this.tx, this.table);
this._details = details; this._details = details;
} }