Skip to content

Commit 11f7c54

Browse files
committed
Simplify tx_request_and_timeout by converting it from a closure into a FullNode class member.
1 parent d159689 commit 11f7c54

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

chia/full_node/full_node.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3352,6 +3352,47 @@ async def broadcast_uncompact_blocks(
33523352
self.log.error(f"Exception in broadcast_uncompact_blocks: {e}")
33533353
self.log.error(f"Exception Stack: {error_stack}")
33543354

3355+
async def tx_request_and_timeout(self, transaction_id: bytes32, task_id: bytes32) -> None:
3356+
"""
3357+
Request a transaction from peers that advertised it, until we either
3358+
receive it or timeout.
3359+
"""
3360+
counter = 0
3361+
try:
3362+
while True:
3363+
# Limit to asking a few peers, it's possible that this tx got included on chain already
3364+
# Highly unlikely that the peers that advertised a tx don't respond to a request. Also, if we
3365+
# drop some transactions, we don't want to re-fetch too many times
3366+
if counter == 5:
3367+
break
3368+
if transaction_id not in self.full_node_store.peers_with_tx:
3369+
break
3370+
peers_with_tx: set[bytes32] = self.full_node_store.peers_with_tx[transaction_id]
3371+
if len(peers_with_tx) == 0:
3372+
break
3373+
peer_id = peers_with_tx.pop()
3374+
assert self.server is not None
3375+
if peer_id not in self.server.all_connections:
3376+
continue
3377+
random_peer = self.server.all_connections[peer_id]
3378+
request_tx = full_node_protocol.RequestTransaction(transaction_id)
3379+
msg = make_msg(ProtocolMessageTypes.request_transaction, request_tx)
3380+
await random_peer.send_message(msg)
3381+
await asyncio.sleep(5)
3382+
counter += 1
3383+
if self.mempool_manager.seen(transaction_id):
3384+
break
3385+
except asyncio.CancelledError:
3386+
pass
3387+
finally:
3388+
# Always Cleanup
3389+
if transaction_id in self.full_node_store.peers_with_tx:
3390+
self.full_node_store.peers_with_tx.pop(transaction_id)
3391+
if transaction_id in self.full_node_store.pending_tx_request:
3392+
self.full_node_store.pending_tx_request.pop(transaction_id)
3393+
if task_id in self.full_node_store.tx_fetch_tasks:
3394+
self.full_node_store.tx_fetch_tasks.pop(task_id)
3395+
33553396

33563397
async def node_next_block_check(
33573398
peer: WSChiaConnection, potential_peek: uint32, blockchain: BlockchainInterface

chia/full_node/full_node_api.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -201,47 +201,9 @@ async def new_transaction(
201201
new_set = set()
202202
new_set.add(peer.peer_node_id)
203203
self.full_node.full_node_store.peers_with_tx[transaction.transaction_id] = new_set
204-
205-
async def tx_request_and_timeout(full_node: FullNode, transaction_id: bytes32, task_id: bytes32) -> None:
206-
counter = 0
207-
try:
208-
while True:
209-
# Limit to asking a few peers, it's possible that this tx got included on chain already
210-
# Highly unlikely that the peers that advertised a tx don't respond to a request. Also, if we
211-
# drop some transactions, we don't want to re-fetch too many times
212-
if counter == 5:
213-
break
214-
if transaction_id not in full_node.full_node_store.peers_with_tx:
215-
break
216-
peers_with_tx: set[bytes32] = full_node.full_node_store.peers_with_tx[transaction_id]
217-
if len(peers_with_tx) == 0:
218-
break
219-
peer_id = peers_with_tx.pop()
220-
assert full_node.server is not None
221-
if peer_id not in full_node.server.all_connections:
222-
continue
223-
random_peer = full_node.server.all_connections[peer_id]
224-
request_tx = full_node_protocol.RequestTransaction(transaction.transaction_id)
225-
msg = make_msg(ProtocolMessageTypes.request_transaction, request_tx)
226-
await random_peer.send_message(msg)
227-
await asyncio.sleep(5)
228-
counter += 1
229-
if full_node.mempool_manager.seen(transaction_id):
230-
break
231-
except asyncio.CancelledError:
232-
pass
233-
finally:
234-
# Always Cleanup
235-
if transaction_id in full_node.full_node_store.peers_with_tx:
236-
full_node.full_node_store.peers_with_tx.pop(transaction_id)
237-
if transaction_id in full_node.full_node_store.pending_tx_request:
238-
full_node.full_node_store.pending_tx_request.pop(transaction_id)
239-
if task_id in full_node.full_node_store.tx_fetch_tasks:
240-
full_node.full_node_store.tx_fetch_tasks.pop(task_id)
241-
242204
task_id: bytes32 = bytes32.secret()
243205
fetch_task = create_referenced_task(
244-
tx_request_and_timeout(self.full_node, transaction.transaction_id, task_id)
206+
self.full_node.tx_request_and_timeout(transaction.transaction_id, task_id)
245207
)
246208
self.full_node.full_node_store.tx_fetch_tasks[task_id] = fetch_task
247209
return None

0 commit comments

Comments
 (0)