Browse Source

minimize calls to estimated_fee

283
ThomasV 10 years ago
parent
commit
e0939348c3
  1. 19
      lib/wallet.py

19
lib/wallet.py

@ -878,6 +878,7 @@ class Abstract_Wallet(object):
# this method can be overloaded # this method can be overloaded
return tx.get_fee() return tx.get_fee()
@profiler
def estimated_fee(self, tx, fee_per_kb): def estimated_fee(self, tx, fee_per_kb):
estimated_size = len(tx.serialize(-1))/2 estimated_size = len(tx.serialize(-1))/2
fee = int(fee_per_kb * estimated_size / 1000.) fee = int(fee_per_kb * estimated_size / 1000.)
@ -897,7 +898,7 @@ class Abstract_Wallet(object):
inputs = [] inputs = []
tx = Transaction.from_io(inputs, outputs) tx = Transaction.from_io(inputs, outputs)
fee = fixed_fee if fixed_fee is not None else 0 fee = fixed_fee if fixed_fee is not None else 0
# add old inputs first # add inputs, sorted by age
for item in coins: for item in coins:
v = item.get('value') v = item.get('value')
total += v total += v
@ -911,15 +912,27 @@ class Abstract_Wallet(object):
break break
else: else:
raise NotEnoughFunds() raise NotEnoughFunds()
# remove unneeded inputs # remove unneeded inputs.
removed = False
for item in sorted(tx.inputs, key=itemgetter('value')): for item in sorted(tx.inputs, key=itemgetter('value')):
v = item.get('value') v = item.get('value')
if total - v >= amount + fee: if total - v >= amount + fee:
tx.inputs.remove(item) tx.inputs.remove(item)
total -= v total -= v
fee = fixed_fee if fixed_fee is not None else self.estimated_fee(tx, fee_per_kb) removed = True
continue
else: else:
break break
if removed:
fee = fixed_fee if fixed_fee is not None else self.estimated_fee(tx, fee_per_kb)
for item in sorted(tx.inputs, key=itemgetter('value')):
v = item.get('value')
if total - v >= amount + fee:
tx.inputs.remove(item)
total -= v
fee = fixed_fee if fixed_fee is not None else self.estimated_fee(tx, fee_per_kb)
continue
break
print_error("using %d inputs"%len(tx.inputs)) print_error("using %d inputs"%len(tx.inputs))
# change address # change address

Loading…
Cancel
Save