Skip to content

Commit

Permalink
wallet: fix type error in _bump_fee_through_decreasing_outputs
Browse files Browse the repository at this point in the history
fixes #5483
  • Loading branch information
SomberNight committed Jul 4, 2019
1 parent 194bf84 commit 94b721b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
5 changes: 3 additions & 2 deletions electrum/coinchooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def make_Bucket(desc, coins):
def penalty_func(self, base_tx, *, tx_from_buckets) -> Callable[[List[Bucket]], ScoredCandidate]:
raise NotImplementedError

def _change_amounts(self, tx, count, fee_estimator_numchange):
def _change_amounts(self, tx, count, fee_estimator_numchange) -> List[int]:
# Break change up if bigger than max_change
output_amounts = [o.value for o in tx.outputs()]
# Don't split change of less than 0.02 BTC
Expand Down Expand Up @@ -197,7 +197,7 @@ def trailing_zeroes(val):
# no more than 10**max_dp_to_round_for_privacy
# e.g. a max of 2 decimal places means losing 100 satoshis to fees
max_dp_to_round_for_privacy = 2 if self.enable_output_value_rounding else 0
N = pow(10, min(max_dp_to_round_for_privacy, zeroes[0]))
N = int(pow(10, min(max_dp_to_round_for_privacy, zeroes[0])))
amount = (remaining // N) * N
amounts.append(amount)

Expand All @@ -209,6 +209,7 @@ def _change_outputs(self, tx, change_addrs, fee_estimator_numchange, dust_thresh
amounts = self._change_amounts(tx, len(change_addrs), fee_estimator_numchange)
assert min(amounts) >= 0
assert len(change_addrs) >= len(amounts)
assert all([isinstance(amt, int) for amt in amounts])
# If change is above dust threshold after accounting for the
# size of the change output, add it to the transaction.
amounts = [amount for amount in amounts if amount >= dust_threshold]
Expand Down
8 changes: 4 additions & 4 deletions electrum/tests/test_wallet_vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ def test_bump_fee_p2pkh_when_there_is_a_change_address(self, mock_write):
self.assertEqual((0, funding_output_value - 2500000 - 5000, 0), wallet.get_balance())

# bump tx
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70, config=self.config)
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70.0, config=self.config)
tx.locktime = 1325501
tx.version = 1
self.assertFalse(tx.is_complete())
Expand Down Expand Up @@ -985,7 +985,7 @@ def test_bump_fee_p2wpkh_when_there_is_a_change_address(self, mock_write):
self.assertEqual((0, funding_output_value - 2500000 - 5000, 0), wallet.get_balance())

# bump tx
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70, config=self.config)
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70.0, config=self.config)
tx.locktime = 1325500
tx.version = 1
self.assertFalse(tx.is_complete())
Expand Down Expand Up @@ -1039,7 +1039,7 @@ def test_bump_fee_when_user_sends_max(self, mock_write):
self.assertEqual((0, 0, 0), wallet.get_balance())

# bump tx
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70, config=self.config)
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70.0, config=self.config)
tx.locktime = 1325500
tx.version = 1
self.assertFalse(tx.is_complete())
Expand Down Expand Up @@ -1102,7 +1102,7 @@ def test_bump_fee_when_new_inputs_need_to_be_added(self, mock_write):
self.assertEqual((0, 5_000_000, 0), wallet.get_balance())

# bump tx
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70, config=self.config)
tx = wallet.bump_fee(tx=Transaction(tx.serialize()), new_fee_rate=70.0, config=self.config)
tx.locktime = 1325500
tx.version = 1
self.assertFalse(tx.is_complete())
Expand Down
6 changes: 3 additions & 3 deletions electrum/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,13 +1039,13 @@ def add_outputs(self, outputs):
self.raw = None
self.BIP69_sort(inputs=False)

def input_value(self):
def input_value(self) -> int:
return sum(x['value'] for x in self.inputs())

def output_value(self):
def output_value(self) -> int:
return sum(o.value for o in self.outputs())

def get_fee(self):
def get_fee(self) -> int:
return self.input_value() - self.output_value()

def is_final(self):
Expand Down
10 changes: 6 additions & 4 deletions electrum/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,18 +993,20 @@ def _bump_fee_through_decreasing_outputs(self, *, tx, new_fee_rate):
# prioritize low value outputs, to get rid of dust
s = sorted(s, key=lambda o: o.value)
for o in s:
target_fee = tx.estimated_size() * new_fee_rate
target_fee = int(round(tx.estimated_size() * new_fee_rate))
delta = target_fee - tx.get_fee()
i = outputs.index(o)
if o.value - delta >= self.dust_threshold():
outputs[i] = o._replace(value=o.value - delta)
new_output_value = o.value - delta
assert isinstance(new_output_value, int)
outputs[i] = o._replace(value=new_output_value)
delta = 0
break
else:
del outputs[i]
delta -= o.value
if delta > 0:
continue
# note: delta might be negative now, in which case
# the value of the next output will be increased
if delta > 0:
raise CannotBumpFee(_('Cannot bump fee') + ': ' + _('could not find suitable outputs'))

Expand Down

0 comments on commit 94b721b

Please sign in to comment.