From d6b97efcc4e6dce8c8de5df241dad869319153ce Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 5 Nov 2016 18:14:10 -0700 Subject: [PATCH] walletdb: fee handling. --- lib/wallet/wallet.js | 17 +++++++++++------ lib/wallet/walletdb.js | 33 ++++++++------------------------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index f5d9b538..275b38a2 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1434,7 +1434,7 @@ Wallet.prototype._fund = co(function* fund(tx, options) { hardFee: options.hardFee, subtractFee: options.subtractFee, changeAddress: account.change.getAddress(), - height: this.db.height, + height: this.db.state.height, rate: rate, maxFee: options.maxFee, m: account.m, @@ -1455,7 +1455,7 @@ Wallet.prototype._fund = co(function* fund(tx, options) { Wallet.prototype.createTX = co(function* createTX(options, force) { var outputs = options.outputs; - var i, tx, total; + var i, tx, total, output; if (!Array.isArray(outputs) || outputs.length === 0) throw new Error('No outputs.'); @@ -1464,8 +1464,11 @@ Wallet.prototype.createTX = co(function* createTX(options, force) { tx = new MTX(); // Add the outputs - for (i = 0; i < outputs.length; i++) + for (i = 0; i < outputs.length; i++) { tx.addOutput(outputs[i]); + if (tx.outputs[i].isDust(constants.tx.MIN_RELAY)) + throw new Error('Output is dust.'); + } // Fill the inputs with unspents yield this.fund(tx, options, force); @@ -1478,18 +1481,18 @@ Wallet.prototype.createTX = co(function* createTX(options, force) { // if (options.locktime != null) // tx.setLocktime(options.locktime); // else - // tx.avoidFeeSniping(this.db.height); + // tx.avoidFeeSniping(this.db.state.height); if (!tx.isSane()) throw new Error('CheckTransaction failed.'); - if (!tx.checkInputs(this.db.height)) + if (!tx.checkInputs(this.db.state.height)) throw new Error('CheckInputs failed.'); total = yield this.template(tx); if (total === 0) - throw new Error('template failed.'); + throw new Error('Templating failed.'); return tx; }); @@ -1531,6 +1534,8 @@ Wallet.prototype._send = co(function* send(options, passphrase) { tx = tx.toTX(); + assert(tx.getFee() <= constants.tx.MAX_FEE, 'TX exceeds maxfee.'); + yield this.db.addTX(tx); this.logger.debug('Sending wallet tx (%s): %s', this.id, tx.rhash); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 9570b5cd..769cf5e0 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -972,19 +972,15 @@ WalletDB.prototype.getAccount = co(function* getAccount(wid, index) { * @returns {Promise} - Returns Array. */ -WalletDB.prototype.getAccounts = co(function* getAccounts(wid) { - var i, items; - - items = yield this.db.values({ +WalletDB.prototype.getAccounts = function getAccounts(wid) { + return this.db.values({ gte: layout.n(wid, 0x00000000), - lte: layout.n(wid, 0xffffffff) - }); - - for (i = 0; i < items.length; i++) - items[i] = items[i].toString('ascii'); - - return items; -}); + lte: layout.n(wid, 0xffffffff), + parse: function(data) { + return data.toString('ascii'); + } + } +}; /** * Lookup the corresponding account name's index. @@ -1179,19 +1175,6 @@ WalletDB.prototype.getHashes = function getHashes() { }); }; -/** - * Get all tx hashes. - * @returns {Promise} - */ - -WalletDB.prototype.getTXHashes = function getTXHashes() { - return this.db.keys({ - gte: layout.e(constants.NULL_HASH), - lte: layout.e(constants.HIGH_HASH), - parse: layout.ee - }); -}; - /** * Get all tx hashes. * @returns {Promise}