B7: confirmations with merkle proof, adoption of the confirmed version, RBF bumper and reconciler (TX-30..33, DAT-85)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5.5 committed 2026-11-16 16:29:08 +01:00
1 parent feff993584
commit fa57d2e5ab
20 files changed
+1165 -20

No files matched your search

+71 -2
View File
@@ -176,15 +176,20 @@ class ElectrumChain:
if not self.majority(await self._ask_others(lists_it)):
return False
return await self._proof_holds(active, entry.txid, entry.height)
async def _proof_holds(self, active: ElectrumClient, txid: str, height: int) -> bool:
"""ELC-26, TX-22: a header of the active server at `height` with valid work for the profile, and a merkle
proof from the others (by majority) that puts `txid` in that block."""
try:
header = await asyncio.wait_for(active.get_block_header(entry.height), ANSWER_TIMEOUT)
header = await asyncio.wait_for(active.get_block_header(height), ANSWER_TIMEOUT)
except Exception:
return False
if not header_meets_its_own_target(str(header), self.profile.pow_limit):
return False
async def proves_it(client: ElectrumClient) -> bool:
return proof_matches_header(entry.txid, entry.height, await client.get_merkle(entry.txid, entry.height), str(header))
return proof_matches_header(txid, height, await client.get_merkle(txid, height), str(header))
return self.majority(await self._ask_others(proves_it))
@@ -197,6 +202,70 @@ class ElectrumChain:
return self.majority(await self._ask_others(lacks_it))
# -- confirmations and presence (TX-30, TX-33) ----------------------------------------------------------------
@staticmethod
async def _history(client: ElectrumClient, address: str) -> list[tuple[str, int]] | None:
try:
raw = await asyncio.wait_for(client.get_history(address_to_scripthash(address)), ANSWER_TIMEOUT)
except Exception as error:
logger.warning("history of %s failed: %s", address, error)
return None
try:
return [(str(item["tx_hash"]), int(item["height"])) for item in raw]
except (KeyError, TypeError, ValueError):
return None
async def observe_confirmations(self, wanted: list[tuple[int, str, frozenset[str]]]) -> dict[int, tuple[str, int]]:
"""`wanted` is `(row id, spending address, every txid of the row)`. Returns, per row, the version that is in a
block *and* proven (TX-30): `{row id: (txid, height)}`. An address that cannot be read blocks nobody else."""
active = await self._connect_active()
if active is None:
return {}
try:
histories: dict[str, list[tuple[str, int]] | None] = {}
confirmed: dict[int, tuple[str, int]] = {}
for row_id, address, txids in wanted:
if address not in histories:
histories[address] = await self._history(active, address)
history = histories[address]
if history is None:
continue
for txid, height in history:
if txid in txids and height > 0 and await self._proof_holds(active, txid, height):
confirmed[row_id] = (txid, height)
break
return confirmed
except Exception:
logger.exception("confirmation poll failed")
return {}
finally:
await active.close()
async def versions_known(self, address: str, txids: frozenset[str]) -> tuple[set[str], bool]:
"""TX-33: which of `txids` any server of the network has seen (mempool or block), and whether the network
answered enough for the *absence* of the rest to mean something (the active server and the quorum)."""
active = await self._connect_active()
if active is None:
return set(), False
try:
mine = await self._history(active, address)
async def theirs(client: ElectrumClient):
history = await self._history(client, address)
return None if history is None else {txid for txid, _ in history if txid in txids}
others = await self._ask_others(theirs)
known = ({txid for txid, _ in mine if txid in txids} if mine is not None else set())
for answer in others:
if answer:
known |= answer
# an absence is a statement only if the active server answered and the others agree among themselves
complete = mine is not None and self.majority([a is not None for a in others])
return known, complete
finally:
await active.close()
# -- broadcast (BET-32) -----------------------------------------------------------------------------------
async def transaction_known(self, active: ElectrumClient, txid: str) -> bool | None: