From f0e89b3ef600102e78849a4f5392b3d7add4e689 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 4 Apr 2023 17:49:46 +0000 Subject: [PATCH] addr_sync: migrate usages of get_txpos to get_tx_height the return value of get_txpos is fine-tuned for sorting... other uses are highly questionable. --- electrum/address_synchronizer.py | 22 ++++++++++++---------- electrum/gui/qt/transaction_dialog.py | 4 +++- electrum/gui/qt/utxo_dialog.py | 6 ++++-- electrum/transaction.py | 4 ++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py index 88bce48fe..cc7e31b11 100644 --- a/electrum/address_synchronizer.py +++ b/electrum/address_synchronizer.py @@ -244,12 +244,11 @@ class AddressSynchronizer(Logger, EventListener): def get_transaction(self, txid: str) -> Optional[Transaction]: tx = self.db.get_transaction(txid) if tx: - # add verified tx info tx.deserialize() for txin in tx._inputs: - tx_height, tx_pos = self.get_txpos(txin.prevout.txid.hex()) - txin.block_height = tx_height - txin.block_txpos = tx_pos + tx_mined_info = self.get_tx_height(txin.prevout.txid.hex()) + txin.block_height = tx_mined_info.height # not SPV-ed + txin.block_txpos = tx_mined_info.txpos return tx def add_transaction(self, tx: Transaction, *, allow_unrelated=False, is_new=True) -> bool: @@ -477,8 +476,10 @@ class AddressSynchronizer(Logger, EventListener): self._history_local.clear() self._get_balance_cache.clear() # invalidate cache - def get_txpos(self, tx_hash: str) -> Tuple[int, int]: - """Returns (height, txpos) tuple, even if the tx is unverified.""" + def _get_txpos(self, tx_hash: str) -> Tuple[int, int]: + """Returns (height, txpos) tuple, even if the tx is unverified. + If txpos is -1, height should only be used for sorting purposes. + """ with self.lock: verified_tx_mined_info = self.db.get_verified_tx(tx_hash) if verified_tx_mined_info: @@ -529,7 +530,7 @@ class AddressSynchronizer(Logger, EventListener): tx_mined_status = self.get_tx_height(tx_hash) fee = self.get_tx_fee(tx_hash) history.append((tx_hash, tx_mined_status, delta, fee)) - history.sort(key = lambda x: self.get_txpos(x[0])) + history.sort(key = lambda x: self._get_txpos(x[0])) # 3. add balance h2 = [] balance = 0 @@ -783,13 +784,14 @@ class AddressSynchronizer(Logger, EventListener): received = {} sent = {} for tx_hash, height in h: - hh, pos = self.get_txpos(tx_hash) + tx_mined_info = self.get_tx_height(tx_hash) + txpos = tx_mined_info.txpos if tx_mined_info.txpos is not None else -1 d = self.db.get_txo_addr(tx_hash, address) for n, (v, is_cb) in d.items(): - received[tx_hash + ':%d'%n] = (height, pos, v, is_cb) + received[tx_hash + ':%d'%n] = (height, txpos, v, is_cb) l = self.db.get_txi_addr(tx_hash, address) for txi, v in l: - sent[txi] = tx_hash, height, pos + sent[txi] = tx_hash, height, txpos return received, sent def get_addr_outputs(self, address: str) -> Dict[TxOutpoint, PartialTxInput]: diff --git a/electrum/gui/qt/transaction_dialog.py b/electrum/gui/qt/transaction_dialog.py index de648ee38..e860e425c 100644 --- a/electrum/gui/qt/transaction_dialog.py +++ b/electrum/gui/qt/transaction_dialog.py @@ -246,7 +246,9 @@ class TxInOutWidget(QWidget): tx_height, tx_pos = None, None tx_hash = self.tx.txid() if tx_hash: - tx_height, tx_pos = self.wallet.adb.get_txpos(tx_hash) + tx_mined_info = self.wallet.adb.get_tx_height(tx_hash) + tx_height = tx_mined_info.height + tx_pos = tx_mined_info.txpos cursor = o_text.textCursor() for txout_idx, o in enumerate(self.tx.outputs()): if tx_height is not None and tx_pos is not None and tx_pos >= 0: diff --git a/electrum/gui/qt/utxo_dialog.py b/electrum/gui/qt/utxo_dialog.py index c714e41fc..e94eae3db 100644 --- a/electrum/gui/qt/utxo_dialog.py +++ b/electrum/gui/qt/utxo_dialog.py @@ -105,8 +105,10 @@ class UTXODialog(WindowModalDialog): def print_ascii_tree(_txid, prefix, is_last, is_uncle): if _txid not in parents: return - tx_height, tx_pos = self.wallet.adb.get_txpos(_txid) - key = "%dx%d"%(tx_height, tx_pos) if tx_pos >= 0 else _txid[0:8] + tx_mined_info = self.wallet.adb.get_tx_height(_txid) + tx_height = tx_mined_info.height + tx_pos = tx_mined_info.txpos + key = "%dx%d"%(tx_height, tx_pos) if tx_pos is not None else _txid[0:8] label = self.wallet.get_label_for_txid(_txid) or "" if _txid not in parents_copy: label = '[duplicate]' diff --git a/electrum/transaction.py b/electrum/transaction.py index aa9e264dc..0012468d8 100644 --- a/electrum/transaction.py +++ b/electrum/transaction.py @@ -260,8 +260,8 @@ class TxInput: self.witness = witness self._is_coinbase_output = is_coinbase_output # blockchain fields - self.block_height = None # type: Optional[int] # height at which the TXO is mined; None means unknown - self.block_txpos = None + self.block_height = None # type: Optional[int] # height at which the TXO is mined; None means unknown. not SPV-ed. + self.block_txpos = None # type: Optional[int] # position of tx in block, if TXO is mined; otherwise None or -1 self.spent_height = None # type: Optional[int] # height at which the TXO got spent self.spent_txid = None # type: Optional[str] # txid of the spender self._utxo = None # type: Optional[Transaction]