Skip to content

Commit

Permalink
wallet: only select mature coins by default
Browse files Browse the repository at this point in the history
this is a regression from #5721

Removed the `TxInput.is_coinbase` method as I think it is a confusing API,
instead we now have `TxInput.is_coinbase_input` and `TxInput.is_coinbase_output`.

related #5872
  • Loading branch information
SomberNight committed Jan 1, 2020
1 parent 6709ec4 commit d2f1327
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 24 deletions.
13 changes: 7 additions & 6 deletions electrum/address_synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def get_conflicting_transactions(self, tx_hash, tx: Transaction, include_self=Fa
conflicting_txns = set()
with self.transaction_lock:
for txin in tx.inputs():
if txin.is_coinbase():
if txin.is_coinbase_input():
continue
prevout_hash = txin.prevout.txid.hex()
prevout_n = txin.prevout.out_idx
Expand Down Expand Up @@ -228,7 +228,7 @@ def add_transaction(self, tx: Transaction, *, allow_unrelated=False) -> bool:
# BUT we track is_mine inputs in a txn, and during subsequent calls
# of add_transaction tx, we might learn of more-and-more inputs of
# being is_mine, as we roll the gap_limit forward
is_coinbase = tx.inputs()[0].is_coinbase()
is_coinbase = tx.inputs()[0].is_coinbase_input()
tx_height = self.get_tx_height(tx_hash).height
if not allow_unrelated:
# note that during sync, if the transactions are not properly sorted,
Expand Down Expand Up @@ -279,7 +279,7 @@ def add_value_from_prev_output():
self._get_addr_balance_cache.pop(addr, None) # invalidate cache
return
for txi in tx.inputs():
if txi.is_coinbase():
if txi.is_coinbase_input():
continue
prevout_hash = txi.prevout.txid.hex()
prevout_n = txi.prevout.out_idx
Expand Down Expand Up @@ -314,7 +314,7 @@ def remove_from_spent_outpoints():
if tx is not None:
# if we have the tx, this branch is faster
for txin in tx.inputs():
if txin.is_coinbase():
if txin.is_coinbase_input():
continue
prevout_hash = txin.prevout.txid.hex()
prevout_n = txin.prevout.out_idx
Expand Down Expand Up @@ -758,7 +758,8 @@ def get_addr_utxo(self, address: str) -> Dict[TxOutpoint, PartialTxInput]:
for prevout_str, v in coins.items():
tx_height, value, is_cb = v
prevout = TxOutpoint.from_str(prevout_str)
utxo = PartialTxInput(prevout=prevout)
utxo = PartialTxInput(prevout=prevout,
is_coinbase_output=is_cb)
utxo._trusted_address = address
utxo._trusted_value_sats = value
utxo.block_height = tx_height
Expand Down Expand Up @@ -825,7 +826,7 @@ def get_utxos(self, domain=None, *, excluded_addresses=None,
continue
if nonlocal_only and utxo.block_height == TX_HEIGHT_LOCAL:
continue
if (mature_only and utxo.prevout.is_coinbase()
if (mature_only and utxo.is_coinbase_output()
and utxo.block_height + COINBASE_MATURITY > mempool_height):
continue
coins.append(utxo)
Expand Down
2 changes: 1 addition & 1 deletion electrum/gui/qt/transaction_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def format_amount(amt):
i_text.setReadOnly(True)
cursor = i_text.textCursor()
for txin in self.tx.inputs():
if txin.is_coinbase():
if txin.is_coinbase_input():
cursor.insertText('coinbase')
else:
prevout_hash = txin.prevout.txid.hex()
Expand Down
2 changes: 1 addition & 1 deletion electrum/json_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def _convert_version_17(self):
for txid, raw_tx in transactions.items():
tx = Transaction(raw_tx)
for txin in tx.inputs():
if txin.is_coinbase():
if txin.is_coinbase_input():
continue
prevout_hash = txin.prevout.txid.hex()
prevout_n = txin.prevout.out_idx
Expand Down
2 changes: 1 addition & 1 deletion electrum/plugins/digitalbitbox/digitalbitbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def sign_transaction(self, tx, password):

# Build hasharray from inputs
for i, txin in enumerate(tx.inputs()):
if txin.is_coinbase():
if txin.is_coinbase_input():
self.give_error("Coinbase not supported") # should never happen

if txin.script_type != 'p2pkh':
Expand Down
2 changes: 1 addition & 1 deletion electrum/plugins/keepkey/keepkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def tx_inputs(self, tx: Transaction, *, for_sig=False, keystore: 'KeepKey_KeySto
inputs = []
for txin in tx.inputs():
txinputtype = self.types.TxInputType()
if txin.is_coinbase():
if txin.is_coinbase_input():
prev_hash = b"\x00"*32
prev_index = 0xffffffff # signed int -1
else:
Expand Down
2 changes: 1 addition & 1 deletion electrum/plugins/ledger/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def sign_transaction(self, tx, password):

# Fetch inputs of the transaction to sign
for txin in tx.inputs():
if txin.is_coinbase():
if txin.is_coinbase_input():
self.give_error("Coinbase not supported") # should never happen

if txin.script_type in ['p2sh']:
Expand Down
2 changes: 1 addition & 1 deletion electrum/plugins/safe_t/safe_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def tx_inputs(self, tx: Transaction, *, for_sig=False, keystore: 'SafeTKeyStore'
inputs = []
for txin in tx.inputs():
txinputtype = self.types.TxInputType()
if txin.is_coinbase():
if txin.is_coinbase_input():
prev_hash = b"\x00"*32
prev_index = 0xffffffff # signed int -1
else:
Expand Down
2 changes: 1 addition & 1 deletion electrum/plugins/trezor/trezor.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def tx_inputs(self, tx: Transaction, *, for_sig=False, keystore: 'TrezorKeyStore
inputs = []
for txin in tx.inputs():
txinputtype = TxInputType()
if txin.is_coinbase():
if txin.is_coinbase_input():
prev_hash = b"\x00"*32
prev_index = 0xffffffff # signed int -1
else:
Expand Down
4 changes: 2 additions & 2 deletions electrum/plugins/trustedcoin/legacy_tx_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def serialize_tx_in_legacy_format(tx: PartialTransaction, *, wallet: Multisig_Wa
tx = copy.deepcopy(tx)

def get_siglist(txin: 'PartialTxInput', *, estimate_size=False):
if txin.prevout.is_coinbase():
if txin.is_coinbase_input():
return [], []
if estimate_size:
try:
Expand Down Expand Up @@ -80,7 +80,7 @@ def serialize_witness(self, txin: PartialTxInput, *, estimate_size=False):
assert estimate_size is False
if txin.witness is not None:
return txin.witness.hex()
if txin.prevout.is_coinbase():
if txin.is_coinbase_input():
return ''
assert isinstance(txin, PartialTxInput)
if not self.is_segwit_input(txin):
Expand Down
29 changes: 20 additions & 9 deletions electrum/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,28 +192,38 @@ class TxInput:
script_sig: Optional[bytes]
nsequence: int
witness: Optional[bytes]
_is_coinbase_output: bool

def __init__(self, *,
prevout: TxOutpoint,
script_sig: bytes = None,
nsequence: int = 0xffffffff - 1,
witness: bytes = None):
witness: bytes = None,
is_coinbase_output: bool = False):
self.prevout = prevout
self.script_sig = script_sig
self.nsequence = nsequence
self.witness = witness
self._is_coinbase_output = is_coinbase_output

def is_coinbase(self) -> bool:
def is_coinbase_input(self) -> bool:
"""Whether this is the input of a coinbase tx."""
return self.prevout.is_coinbase()

def is_coinbase_output(self) -> bool:
"""Whether the coin being spent is an output of a coinbase tx.
This matters for coin maturity.
"""
return self._is_coinbase_output

def value_sats(self) -> Optional[int]:
return None

def to_json(self):
d = {
'prevout_hash': self.prevout.txid.hex(),
'prevout_n': self.prevout.out_idx,
'coinbase': self.is_coinbase(),
'coinbase': self.is_coinbase_output(),
'nsequence': self.nsequence,
}
if self.script_sig is not None:
Expand Down Expand Up @@ -550,7 +560,7 @@ def deserialize(self) -> None:

@classmethod
def get_siglist(self, txin: 'PartialTxInput', *, estimate_size=False):
if txin.prevout.is_coinbase():
if txin.is_coinbase_input():
return [], []

if estimate_size:
Expand Down Expand Up @@ -579,7 +589,7 @@ def get_siglist(self, txin: 'PartialTxInput', *, estimate_size=False):
def serialize_witness(cls, txin: TxInput, *, estimate_size=False) -> str:
if txin.witness is not None:
return txin.witness.hex()
if txin.prevout.is_coinbase():
if txin.is_coinbase_input():
return ''
assert isinstance(txin, PartialTxInput)

Expand Down Expand Up @@ -643,7 +653,7 @@ def guess_txintype_from_address(cls, addr: Optional[str]) -> str:
def input_script(self, txin: TxInput, *, estimate_size=False) -> str:
if txin.script_sig is not None:
return txin.script_sig.hex()
if txin.prevout.is_coinbase():
if txin.is_coinbase_input():
return ''
assert isinstance(txin, PartialTxInput)

Expand Down Expand Up @@ -1090,7 +1100,8 @@ def from_txin(cls, txin: TxInput, *, strip_witness: bool = True) -> 'PartialTxIn
res = PartialTxInput(prevout=txin.prevout,
script_sig=None if strip_witness else txin.script_sig,
nsequence=txin.nsequence,
witness=None if strip_witness else txin.witness)
witness=None if strip_witness else txin.witness,
is_coinbase_output=txin.is_coinbase_output())
return res

def validate_data(self, *, for_signing=False) -> None:
Expand Down Expand Up @@ -1243,7 +1254,7 @@ def scriptpubkey(self) -> Optional[bytes]:
def is_complete(self) -> bool:
if self.script_sig is not None and self.witness is not None:
return True
if self.prevout.is_coinbase():
if self.is_coinbase_input():
return True
if self.script_sig is not None and not Transaction.is_segwit_input(self):
return True
Expand Down Expand Up @@ -1750,7 +1761,7 @@ def signature_count(self) -> Tuple[int, int]:
s = 0 # "num Sigs we have"
r = 0 # "Required"
for txin in self.inputs():
if txin.prevout.is_coinbase():
if txin.is_coinbase_input():
continue
signatures = list(txin.part_sigs.values())
s += len(signatures)
Expand Down

0 comments on commit d2f1327

Please sign in to comment.