Prepare to calculate tx fee given a tx size

This commit is contained in:
Neil Booth 2015-11-29 12:41:54 +09:00
parent 39af17bc23
commit a4dd5acc48
3 changed files with 20 additions and 10 deletions

View File

@ -32,8 +32,7 @@ class CoinChooser(PrintError):
added to the transaction fee.'''
amount = sum(map(lambda x: x[2], outputs))
total = 0
inputs = []
tx = Transaction.from_io(inputs, outputs)
tx = Transaction.from_io([], outputs)
fee = fee_estimator(tx)
# add inputs, sorted by age
for item in coins:

View File

@ -689,14 +689,22 @@ class Transaction:
def get_fee(self):
return self.input_value() - self.output_value()
@profiler
def estimated_fee(self, fee_per_kb):
estimated_size = len(self.serialize(-1)) / 2
fee = int(fee_per_kb * estimated_size / 1000.)
@classmethod
def estimated_fee_for_size(self, fee_per_kb, size):
'''Given a fee per kB in satoshis, and a tx size in bytes,
returns the transaction fee.'''
fee = int(fee_per_kb * size / 1000.)
if fee < MIN_RELAY_TX_FEE:
fee = MIN_RELAY_TX_FEE
return fee
@profiler
def estimated_fee(self, fee_per_kb):
'''Return an estimated fee given a fee per kB in satoshis.'''
# Remember self.serialize returns an ASCII hex string
size = len(self.serialize(-1)) / 2
return self.estimated_fee_for_size(fee_per_kb, size)
def signature_count(self):
r = 0
s = 0

View File

@ -927,11 +927,14 @@ class Abstract_Wallet(PrintError):
else:
change_addrs = [address]
fee_per_kb = self.fee_per_kb(config)
def fee_estimator(tx):
if fixed_fee is not None:
# Fee estimator
if fixed_fee is None:
fee_per_kb = self.fee_per_kb(config)
def fee_estimator(tx):
return tx.estimated_fee(fee_per_kb)
else:
def fee_estimator(tx):
return fixed_fee
return tx.estimated_fee(fee_per_kb)
# Change <= dust threshold is added to the tx fee
dust_threshold = 182 * 3 * MIN_RELAY_TX_FEE / 1000