1
0

synchronizer: get_transaction should discard tx_height as it can change

tx_height comes from the `get_history` RPC, we then call the `get_transaction` RPC.
By the time the `get_transaction` RPC returns, we might have received another
scripthash status update, called `get_history` again, and updated height for the txid.
Synchronizer._get_transaction() should not call adb.receive_tx_callback() with
the old tx_height (but it was doing exactly that).

Patch to trigger, e.g. regtest failures:
(e.g. for tests.regtest.TestLightningAB.test_extract_preimage)
```
diff --git a/electrum/interface.py b/electrum/interface.py
index 8649652b9c..fce7a1c6de 100644
--- a/electrum/interface.py
+++ b/electrum/interface.py
@@ -991,6 +991,7 @@ class Interface(Logger):
         return res

     async def get_transaction(self, tx_hash: str, *, timeout=None) -> str:
+        await asyncio.sleep(3)
         if not is_hash256_str(tx_hash):
             raise Exception(f"{repr(tx_hash)} is not a txid")
         raw = await self.session.send_request('blockchain.transaction.get', [tx_hash], timeout=timeout)

```
This commit is contained in:
SomberNight
2025-05-15 19:31:39 +00:00
parent 61283fe18b
commit b1f0c6e353
2 changed files with 14 additions and 11 deletions

View File

@@ -143,7 +143,7 @@ class Synchronizer(SynchronizerBase):
def _reset(self):
super()._reset()
self._init_done = False
self.requested_tx = {}
self.requested_tx = set() # type: Set[str]
self.requested_histories = set()
self._stale_histories = dict() # type: Dict[str, asyncio.Task]
@@ -208,14 +208,15 @@ class Synchronizer(SynchronizerBase):
async def _request_missing_txs(self, hist, *, allow_server_not_finding_tx=False):
# "hist" is a list of [tx_hash, tx_height] lists
transaction_hashes = []
for tx_hash, tx_height in hist:
for tx_hash, _tx_height in hist:
if tx_hash in self.requested_tx:
continue
tx = self.adb.db.get_transaction(tx_hash)
if tx and not isinstance(tx, PartialTransaction):
continue # already have complete tx
transaction_hashes.append(tx_hash)
self.requested_tx[tx_hash] = tx_height
# note: tx_height might change by the time we get the raw_tx
self.requested_tx.add(tx_hash)
if not transaction_hashes: return
async with OldTaskGroup() as group:
@@ -230,7 +231,7 @@ class Synchronizer(SynchronizerBase):
except RPCError as e:
# most likely, "No such mempool or blockchain transaction"
if allow_server_not_finding_tx:
self.requested_tx.pop(tx_hash)
self.requested_tx.remove(tx_hash)
return
else:
raise
@@ -239,9 +240,9 @@ class Synchronizer(SynchronizerBase):
tx = Transaction(raw_tx)
if tx_hash != tx.txid():
raise SynchronizerFailure(f"received tx does not match expected txid ({tx_hash} != {tx.txid()})")
tx_height = self.requested_tx.pop(tx_hash)
self.adb.receive_tx_callback(tx, tx_height=tx_height)
self.logger.info(f"received tx {tx_hash} height: {tx_height} bytes: {len(raw_tx)}")
self.requested_tx.remove(tx_hash)
self.adb.receive_tx_callback(tx)
self.logger.info(f"received tx {tx_hash}. bytes: {len(raw_tx)}")
async def main(self):
self.adb.up_to_date_changed()