tentatively refactor prefetch
This commit is contained in:
parent
44b1178cfe
commit
0ce4b9a7de
1 changed files with 13 additions and 10 deletions
|
@ -349,17 +349,20 @@ class BaseLedger(metaclass=LedgerRegistry):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _prefetch_history(self, remote_history, local_history):
|
def _prefetch_history(self, remote_history, local_history):
|
||||||
proofs = {
|
proofs, network_txs, deferreds = {}, {}, []
|
||||||
entry['tx_hash']: (
|
for i, (hex_id, remote_height) in enumerate(map(itemgetter('tx_hash', 'height'), remote_history)):
|
||||||
self.network.get_merkle(entry['tx_hash'], entry['height']) if entry['height'] > 0 else None
|
if i < len(local_history) and local_history[i] == (hex_id, remote_height):
|
||||||
|
continue
|
||||||
|
if remote_height > 0:
|
||||||
|
deferreds.append(
|
||||||
|
self.network.get_merkle(hex_id, remote_height).addBoth(
|
||||||
|
lambda result, txid: proofs.__setitem__(txid, result), hex_id)
|
||||||
|
)
|
||||||
|
deferreds.append(
|
||||||
|
self.network.get_transaction(hex_id).addBoth(
|
||||||
|
lambda result, txid: network_txs.__setitem__(txid, result), hex_id)
|
||||||
)
|
)
|
||||||
for index, entry in enumerate(remote_history)
|
yield defer.DeferredList(deferreds)
|
||||||
if index >= len(local_history) or (entry['tx_hash'], entry['height']) != local_history[index]
|
|
||||||
}
|
|
||||||
network_txs = {key: self.network.get_transaction(key) for key in proofs.keys()}
|
|
||||||
yield defer.DeferredList(list(filter(None, proofs.values())) + list(network_txs.values()))
|
|
||||||
proofs = {key: value.result for key, value in proofs.items() if value}
|
|
||||||
network_txs = {key: value.result for key, value in network_txs.items()}
|
|
||||||
return proofs, network_txs
|
return proofs, network_txs
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
|
Loading…
Add table
Reference in a new issue