Browse Source

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.
master
SomberNight 3 years ago
parent
commit
f0e89b3ef6
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 22
      electrum/address_synchronizer.py
  2. 4
      electrum/gui/qt/transaction_dialog.py
  3. 6
      electrum/gui/qt/utxo_dialog.py
  4. 4
      electrum/transaction.py

22
electrum/address_synchronizer.py

@ -244,12 +244,11 @@ class AddressSynchronizer(Logger, EventListener):
def get_transaction(self, txid: str) -> Optional[Transaction]: def get_transaction(self, txid: str) -> Optional[Transaction]:
tx = self.db.get_transaction(txid) tx = self.db.get_transaction(txid)
if tx: if tx:
# add verified tx info
tx.deserialize() tx.deserialize()
for txin in tx._inputs: for txin in tx._inputs:
tx_height, tx_pos = self.get_txpos(txin.prevout.txid.hex()) tx_mined_info = self.get_tx_height(txin.prevout.txid.hex())
txin.block_height = tx_height txin.block_height = tx_mined_info.height # not SPV-ed
txin.block_txpos = tx_pos txin.block_txpos = tx_mined_info.txpos
return tx return tx
def add_transaction(self, tx: Transaction, *, allow_unrelated=False, is_new=True) -> bool: 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._history_local.clear()
self._get_balance_cache.clear() # invalidate cache self._get_balance_cache.clear() # invalidate cache
def get_txpos(self, tx_hash: str) -> Tuple[int, int]: def _get_txpos(self, tx_hash: str) -> Tuple[int, int]:
"""Returns (height, txpos) tuple, even if the tx is unverified.""" """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: with self.lock:
verified_tx_mined_info = self.db.get_verified_tx(tx_hash) verified_tx_mined_info = self.db.get_verified_tx(tx_hash)
if verified_tx_mined_info: if verified_tx_mined_info:
@ -529,7 +530,7 @@ class AddressSynchronizer(Logger, EventListener):
tx_mined_status = self.get_tx_height(tx_hash) tx_mined_status = self.get_tx_height(tx_hash)
fee = self.get_tx_fee(tx_hash) fee = self.get_tx_fee(tx_hash)
history.append((tx_hash, tx_mined_status, delta, fee)) 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 # 3. add balance
h2 = [] h2 = []
balance = 0 balance = 0
@ -783,13 +784,14 @@ class AddressSynchronizer(Logger, EventListener):
received = {} received = {}
sent = {} sent = {}
for tx_hash, height in h: 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) d = self.db.get_txo_addr(tx_hash, address)
for n, (v, is_cb) in d.items(): 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) l = self.db.get_txi_addr(tx_hash, address)
for txi, v in l: for txi, v in l:
sent[txi] = tx_hash, height, pos sent[txi] = tx_hash, height, txpos
return received, sent return received, sent
def get_addr_outputs(self, address: str) -> Dict[TxOutpoint, PartialTxInput]: def get_addr_outputs(self, address: str) -> Dict[TxOutpoint, PartialTxInput]:

4
electrum/gui/qt/transaction_dialog.py

@ -246,7 +246,9 @@ class TxInOutWidget(QWidget):
tx_height, tx_pos = None, None tx_height, tx_pos = None, None
tx_hash = self.tx.txid() tx_hash = self.tx.txid()
if tx_hash: 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() cursor = o_text.textCursor()
for txout_idx, o in enumerate(self.tx.outputs()): 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: if tx_height is not None and tx_pos is not None and tx_pos >= 0:

6
electrum/gui/qt/utxo_dialog.py

@ -105,8 +105,10 @@ class UTXODialog(WindowModalDialog):
def print_ascii_tree(_txid, prefix, is_last, is_uncle): def print_ascii_tree(_txid, prefix, is_last, is_uncle):
if _txid not in parents: if _txid not in parents:
return return
tx_height, tx_pos = self.wallet.adb.get_txpos(_txid) tx_mined_info = self.wallet.adb.get_tx_height(_txid)
key = "%dx%d"%(tx_height, tx_pos) if tx_pos >= 0 else _txid[0:8] 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 "" label = self.wallet.get_label_for_txid(_txid) or ""
if _txid not in parents_copy: if _txid not in parents_copy:
label = '[duplicate]' label = '[duplicate]'

4
electrum/transaction.py

@ -260,8 +260,8 @@ class TxInput:
self.witness = witness self.witness = witness
self._is_coinbase_output = is_coinbase_output self._is_coinbase_output = is_coinbase_output
# blockchain fields # blockchain fields
self.block_height = None # type: Optional[int] # height at which the TXO is mined; None means unknown self.block_height = None # type: Optional[int] # height at which the TXO is mined; None means unknown. not SPV-ed.
self.block_txpos = None 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_height = None # type: Optional[int] # height at which the TXO got spent
self.spent_txid = None # type: Optional[str] # txid of the spender self.spent_txid = None # type: Optional[str] # txid of the spender
self._utxo = None # type: Optional[Transaction] self._utxo = None # type: Optional[Transaction]

Loading…
Cancel
Save