From c010cb8c50b4d565d4659bd0cdcd8b972b0e57f2 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Tue, 31 Mar 2015 16:22:27 +0300 Subject: [PATCH] drop cached value for inputAmount and outputAmount --- lib/transaction/transaction.js | 3 +++ test/transaction/transaction.js | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index c6c9cc0ee..9fc0e7415 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -570,6 +570,7 @@ Transaction.prototype.addInput = function(input, outputScript, satoshis) { Transaction.prototype.uncheckedAddInput = function(input) { $.checkArgumentType(input, Input, 'input'); this.inputs.push(input); + this._inputAmount = undefined; this._updateChangeOutput(); return this; }; @@ -805,6 +806,7 @@ Transaction.prototype._estimateSize = function() { Transaction.prototype._removeOutput = function(index) { var output = this.outputs[index]; this.outputs = _.without(this.outputs, output); + this._outputAmount = undefined; }; Transaction.prototype.removeOutput = function(index) { @@ -864,6 +866,7 @@ Transaction.prototype.removeInput = function(txId, outputIndex) { } var input = this.inputs[index]; this.inputs = _.without(this.inputs, input); + this._inputAmount = undefined; this._updateChangeOutput(); }; diff --git a/test/transaction/transaction.js b/test/transaction/transaction.js index 26842c449..11813419b 100644 --- a/test/transaction/transaction.js +++ b/test/transaction/transaction.js @@ -498,17 +498,19 @@ describe('Transaction', function() { var transaction = new Transaction() .from(simpleUtxoWith1BTC); transaction.inputs.length.should.equal(1); + transaction.inputAmount.should.equal(simpleUtxoWith1BTC.satoshis); transaction.removeInput(0); - transaction.inputAmount.should.equal(0); transaction.inputs.length.should.equal(0); + transaction.inputAmount.should.equal(0); }); it('can remove an input by transaction id', function() { var transaction = new Transaction() .from(simpleUtxoWith1BTC); transaction.inputs.length.should.equal(1); + transaction.inputAmount.should.equal(simpleUtxoWith1BTC.satoshis); transaction.removeInput(simpleUtxoWith1BTC.txId, simpleUtxoWith1BTC.outputIndex); - transaction.inputAmount.should.equal(0); transaction.inputs.length.should.equal(0); + transaction.inputAmount.should.equal(0); }); it('fails if the index provided is invalid', function() { var transaction = new Transaction() @@ -522,8 +524,10 @@ describe('Transaction', function() { .to(toAddress, 40000000) .to(toAddress, 40000000); transaction.outputs.length.should.equal(2); + transaction.outputAmount.should.equal(80000000); transaction.removeOutput(0); transaction.outputs.length.should.equal(1); + transaction.outputAmount.should.equal(40000000); }); });