forked from LBRYCommunity/lbry-sdk
support multiple blocks with blockchain.transaction.get_batch
This commit is contained in:
parent
03a643da52
commit
7a1b7db7c8
3 changed files with 33 additions and 25 deletions
lbry/wallet
|
@ -255,6 +255,10 @@ class Network:
|
||||||
restricted = known_height in (None, -1, 0) or 0 > known_height > self.remote_height - 10
|
restricted = known_height in (None, -1, 0) or 0 > known_height > self.remote_height - 10
|
||||||
return self.rpc('blockchain.transaction.get', [tx_hash], restricted)
|
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):
|
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
|
# 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
|
restricted = known_height in (None, -1, 0) or 0 > known_height > self.remote_height - 10
|
||||||
|
|
|
@ -389,3 +389,17 @@ class MemPool:
|
||||||
if hX == hashX:
|
if hX == hashX:
|
||||||
utxos.append(UTXO(-1, pos, tx_hash, 0, value))
|
utxos.append(UTXO(-1, pos, tx_hash, 0, value))
|
||||||
return utxos
|
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
|
||||||
|
|
|
@ -1539,35 +1539,25 @@ class LBRYElectrumX(SessionBase):
|
||||||
for tx_hash in tx_hashes:
|
for tx_hash in tx_hashes:
|
||||||
assert_tx_hash(tx_hash)
|
assert_tx_hash(tx_hash)
|
||||||
batch_result = {}
|
batch_result = {}
|
||||||
height = None
|
|
||||||
block_hash = None
|
|
||||||
block = None
|
|
||||||
for tx_hash in tx_hashes:
|
for tx_hash in tx_hashes:
|
||||||
tx_info = await self.daemon_request('getrawtransaction', tx_hash, True)
|
tx_info = await self.daemon_request('getrawtransaction', tx_hash, True)
|
||||||
raw_tx = tx_info['hex']
|
raw_tx = tx_info['hex']
|
||||||
if height is None:
|
block_hash = tx_info.get('blockhash')
|
||||||
if 'blockhash' in tx_info:
|
merkle = {}
|
||||||
block_hash = tx_info['blockhash']
|
if block_hash:
|
||||||
block = await self.daemon_request('deserialised_block', block_hash)
|
block = await self.daemon.deserialised_block(block_hash)
|
||||||
height = block['height']
|
height = block['height']
|
||||||
else:
|
try:
|
||||||
height = -1
|
pos = block['tx'].index(tx_hash)
|
||||||
if block_hash != tx_info.get('blockhash'):
|
except ValueError:
|
||||||
raise RPCError(BAD_REQUEST, f'request contains a mix of transaction heights')
|
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:
|
else:
|
||||||
if not block_hash:
|
height = -1
|
||||||
merkle = {'block_height': -1}
|
merkle['block_height'] = height
|
||||||
else:
|
batch_result[tx_hash] = [raw_tx, merkle]
|
||||||
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]
|
|
||||||
return batch_result
|
return batch_result
|
||||||
|
|
||||||
async def transaction_get(self, tx_hash, verbose=False):
|
async def transaction_get(self, tx_hash, verbose=False):
|
||||||
|
|
Loading…
Add table
Reference in a new issue