Introduce MemPoolTxSummary

This commit is contained in:
Neil Booth 2018-08-11 07:33:20 +09:00
parent 90dcf87536
commit 564449e223
2 changed files with 23 additions and 23 deletions

View File

@ -31,6 +31,13 @@ class MemPoolTx(object):
size = attr.ib() size = attr.ib()
@attr.s(slots=True)
class MemPoolTxSummary(object):
hash = attr.ib()
fee = attr.ib()
has_unconfirmed_inputs = attr.ib()
class MemPoolAPI(ABC): class MemPoolAPI(ABC):
'''A concrete instance of this class is passed to the MemPool object '''A concrete instance of this class is passed to the MemPool object
and used by it to query DB and blockchain state.''' and used by it to query DB and blockchain state.'''
@ -299,7 +306,6 @@ class MemPool(object):
Can be positive or negative. Can be positive or negative.
''' '''
value = 0 value = 0
# hashXs is a defaultdict
if hashX in self.hashXs: if hashX in self.hashXs:
for hash in self.hashXs[hashX]: for hash in self.hashXs[hashX]:
tx = self.txs[hash] tx = self.txs[hash]
@ -325,18 +331,12 @@ class MemPool(object):
return result return result
async def transaction_summaries(self, hashX): async def transaction_summaries(self, hashX):
'''Return a list of (tx_hash, tx_fee, unconfirmed) tuples for '''Return a list of MemPoolTxSummary objects for the hashX.'''
mempool entries for the hashX.
unconfirmed is True if any txin is unconfirmed.
'''
# hashXs is a defaultdict, so use get() to query
result = [] result = []
for tx_hash in self.hashXs.get(hashX, ()): for tx_hash in self.hashXs.get(hashX, ()):
tx = self.txs[tx_hash] tx = self.txs[tx_hash]
unconfirmed = any(prev_hash in self.txs has_ui = any(hash in self.txs for hash, idx in tx.prevouts)
for prev_hash, prev_idx in tx.prevouts) result.append(MemPoolTxSummary(tx_hash, tx.fee, has_ui))
result.append((tx_hash, tx.fee, unconfirmed))
return result return result
async def unordered_UTXOs(self, hashX): async def unordered_UTXOs(self, hashX):
@ -347,7 +347,6 @@ class MemPool(object):
the outputs. the outputs.
''' '''
utxos = [] utxos = []
# hashXs is a defaultdict, so use get() to query
for tx_hash in self.hashXs.get(hashX, ()): for tx_hash in self.hashXs.get(hashX, ()):
tx = self.txs.get(tx_hash) tx = self.txs.get(tx_hash)
for pos, (hX, value) in enumerate(tx.out_pairs): for pos, (hX, value) in enumerate(tx.out_pairs):

View File

@ -779,15 +779,16 @@ class ElectrumX(SessionBase):
Status is a hex string, but must be None if there is no history. Status is a hex string, but must be None if there is no history.
''' '''
# Note history is ordered and mempool unordered in electrum-server # Note history is ordered and mempool unordered in electrum-server
# For mempool, height is -1 if unconfirmed txins, otherwise 0 # For mempool, height is -1 if it has unconfirmed inputs, otherwise 0
history = await self.session_mgr.limited_history(hashX) db_history = await self.session_mgr.limited_history(hashX)
mempool = await self.mempool.transaction_summaries(hashX) mempool = await self.mempool.transaction_summaries(hashX)
status = ''.join('{}:{:d}:'.format(hash_to_hex_str(tx_hash), height) status = ''.join(f'{hash_to_hex_str(tx_hash)}:'
for tx_hash, height in history) f'{height:d}:'
status += ''.join('{}:{:d}:'.format(hash_to_hex_str(hex_hash), for tx_hash, height in db_history)
-unconfirmed) status += ''.join(f'{hash_to_hex_str(tx.hash)}:'
for hex_hash, tx_fee, unconfirmed in mempool) f'{-tx.has_unconfirmed_inputs:d}:'
for tx in mempool)
if status: if status:
status = sha256(status.encode()).hex() status = sha256(status.encode()).hex()
else: else:
@ -872,11 +873,11 @@ class ElectrumX(SessionBase):
async def unconfirmed_history(self, hashX): async def unconfirmed_history(self, hashX):
# Note unconfirmed history is unordered in electrum-server # Note unconfirmed history is unordered in electrum-server
# Height is -1 if unconfirmed txins, otherwise 0 # height is -1 if it has unconfirmed inputs, otherwise 0
mempool = await self.mempool.transaction_summaries(hashX) return [{'tx_hash': hash_to_hex_str(tx.hash),
return [{'tx_hash': hash_to_hex_str(tx_hash), 'height': -unconfirmed, 'height': -tx.has_unconfirmed_inputs,
'fee': fee} 'fee': tx.fee}
for tx_hash, fee, unconfirmed in mempool] for tx in await self.mempool.transaction_summaries(hashX)]
async def confirmed_and_unconfirmed_history(self, hashX): async def confirmed_and_unconfirmed_history(self, hashX):
# Note history is ordered but unconfirmed is unordered in e-s # Note history is ordered but unconfirmed is unordered in e-s