Skip to content

Commit

Permalink
(WIP) Namecoin: Refactor name_show to use gettransaction
Browse files Browse the repository at this point in the history
This lets gettransaction do all the hard work regarding SPV verification.

TODO: Needs rebase once 5147, 5574, 5653, and 5660 merged upstream.
  • Loading branch information
JeremyRand committed Nov 5, 2019
1 parent 925b201 commit 9f3bb89
Showing 1 changed file with 2 additions and 33 deletions.
35 changes: 2 additions & 33 deletions electrum_nmc/electrum/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1156,39 +1156,8 @@ async def name_show(self, identifier, stream_id=None):

# The height is now verified to be safe.

# (from verifier._request_proofs) if it's in the checkpoint region, we still might not have the header
header = self.network.blockchain().read_header(height)
if header is None:
if height < constants.net.max_checkpoint():
self.network.run_from_another_thread(self.network.request_chunk(height, None, stream_id=stream_id))

# (from verifier._request_and_verify_single_proof)
merkle = self.network.run_from_another_thread(self.network.get_merkle_for_transaction(txid, height, stream_id=stream_id))
if height != merkle.get('block_height'):
raise Exception('requested height {} differs from received height {} for txid {}'
.format(height, merkle.get('block_height'), txid))
pos = merkle.get('pos')
merkle_branch = merkle.get('merkle')
async def wait_for_header():
# we need to wait if header sync/reorg is still ongoing, hence lock:
async with self.network.bhi_lock:
return self.network.blockchain().read_header(height)
header = self.network.run_from_another_thread(wait_for_header())
verify_tx_is_in_block(txid, merkle_branch, pos, header, height)

# The txid is now verified to come from a safe height in the blockchain.

if self.wallet and txid in self.wallet.db.transactions:
tx = self.wallet.db.transactions[txid]
else:
raw = self.network.run_from_another_thread(self.network.get_transaction(txid, stream_id=stream_id))
if raw:
tx = Transaction(raw)
else:
raise Exception("Unknown transaction")

if tx.txid() != txid:
raise Exception("txid mismatch")
raw = self.gettransaction(txid, verify=True, height=height, stream_id=stream_id)['hex']
tx = Transaction(raw)

# the tx is now verified to come from a safe height in the blockchain

Expand Down

0 comments on commit 9f3bb89

Please sign in to comment.