From 7a1b7db7c82754c5eaa989e8c74c32bee292944d Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Mon, 6 Jul 2020 11:58:23 -0400 Subject: [PATCH] support multiple blocks with `blockchain.transaction.get_batch` --- lbry/wallet/network.py | 4 ++++ lbry/wallet/server/mempool.py | 14 ++++++++++++ lbry/wallet/server/session.py | 40 +++++++++++++---------------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lbry/wallet/network.py b/lbry/wallet/network.py index 92793e9ca..262f46579 100644 --- a/lbry/wallet/network.py +++ b/lbry/wallet/network.py @@ -255,6 +255,10 @@ class Network: restricted = known_height in (None, -1, 0) or 0 > known_height > self.remote_height - 10 return self.rpc('blockchain.transaction.get', [tx_hash], restricted) + def get_transaction_batch(self, txids): + # use any server if its old, otherwise restrict to who gave us the history + return self.rpc('blockchain.transaction.get_batch', txids, True) + def get_transaction_and_merkle(self, tx_hash, known_height=None): # use any server if its old, otherwise restrict to who gave us the history restricted = known_height in (None, -1, 0) or 0 > known_height > self.remote_height - 10 diff --git a/lbry/wallet/server/mempool.py b/lbry/wallet/server/mempool.py index 7ca887892..e253dc7c5 100644 --- a/lbry/wallet/server/mempool.py +++ b/lbry/wallet/server/mempool.py @@ -389,3 +389,17 @@ class MemPool: if hX == hashX: utxos.append(UTXO(-1, pos, tx_hash, 0, value)) return utxos + + def get_mempool_height(self, tx_hash): + # Height Progression + # -2: not broadcast + # -1: in mempool but has unconfirmed inputs + # 0: in mempool and all inputs confirmed + # +num: confirmed in a specific block (height) + if tx_hash not in self.txs: + return -2 + tx = self.txs[tx_hash] + unspent_inputs = sum(1 if hash in self.txs else 0 for hash, idx in tx.prevouts) + if unspent_inputs: + return -1 + return 0 diff --git a/lbry/wallet/server/session.py b/lbry/wallet/server/session.py index 8f3024665..9e897e29a 100644 --- a/lbry/wallet/server/session.py +++ b/lbry/wallet/server/session.py @@ -1539,35 +1539,25 @@ class LBRYElectrumX(SessionBase): for tx_hash in tx_hashes: assert_tx_hash(tx_hash) batch_result = {} - height = None - block_hash = None - block = None for tx_hash in tx_hashes: tx_info = await self.daemon_request('getrawtransaction', tx_hash, True) raw_tx = tx_info['hex'] - if height is None: - if 'blockhash' in tx_info: - block_hash = tx_info['blockhash'] - block = await self.daemon_request('deserialised_block', block_hash) - height = block['height'] - else: - height = -1 - if block_hash != tx_info.get('blockhash'): - raise RPCError(BAD_REQUEST, f'request contains a mix of transaction heights') + block_hash = tx_info.get('blockhash') + merkle = {} + if block_hash: + block = await self.daemon.deserialised_block(block_hash) + height = block['height'] + try: + pos = block['tx'].index(tx_hash) + except ValueError: + raise RPCError(BAD_REQUEST, f'tx hash {tx_hash} not in ' + f'block {block_hash} at height {height:,d}') + merkle["merkle"] = self._get_merkle_branch(block['tx'], pos) + merkle["pos"] = pos else: - if not block_hash: - merkle = {'block_height': -1} - else: - try: - pos = block['tx'].index(tx_hash) - except ValueError: - raise RPCError(BAD_REQUEST, f'tx hash {tx_hash} not in ' - f'block {block_hash} at height {height:,d}') - merkle = { - "merkle": self._get_merkle_branch(block['tx'], pos), - "pos": pos - } - batch_result[tx_hash] = [raw_tx, merkle] + height = -1 + merkle['block_height'] = height + batch_result[tx_hash] = [raw_tx, merkle] return batch_result async def transaction_get(self, tx_hash, verbose=False):