Skip to content

Commit

Permalink
feat: add TransactionsHandle function (paradigmxyz#5198)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
smatthewenglish and mattsse authored Nov 6, 2023
1 parent 648f54e commit dea22ad
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions crates/net/network/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ impl TransactionsHandle {
let _ = self.manager_tx.send(cmd);
}

/// Fetch the [`PeerRequestSender`] for the given peer.
async fn peer_handle(&self, peer_id: PeerId) -> Result<Option<PeerRequestSender>, RecvError> {
let (tx, rx) = oneshot::channel();
self.send(TransactionsCommand::GetPeerSender { peer_id, peer_request_sender: tx });
rx.await
}

/// Requests the transactions directly from the given peer.
///
/// Returns `None` if the peer is not connected.
///
/// **Note**: this returns the response from the peer as received.
pub async fn get_pooled_transactions_from(
&self,
peer_id: PeerId,
hashes: Vec<B256>,
) -> Result<Option<Vec<PooledTransactionsElement>>, RequestError> {
let Some(peer) = self.peer_handle(peer_id).await? else { return Ok(None) };

let (tx, rx) = oneshot::channel();
let request = PeerRequest::GetPooledTransactions { request: hashes.into(), response: tx };
peer.try_send(request).ok();

rx.await?.map(|res| Some(res.0))
}

/// Manually propagate the transaction that belongs to the hash.
pub fn propagate(&self, hash: TxHash) {
self.send(TransactionsCommand::PropagateHash(hash))
Expand Down Expand Up @@ -603,6 +629,10 @@ where
}
tx.send(res).ok();
}
TransactionsCommand::GetPeerSender { peer_id, peer_request_sender } => {
let sender = self.peers.get(&peer_id).map(|peer| peer.request_tx.clone());
peer_request_sender.send(sender).ok();
}
}
}

Expand Down Expand Up @@ -1222,6 +1252,11 @@ enum TransactionsCommand {
peers: Vec<PeerId>,
tx: oneshot::Sender<HashMap<PeerId, HashSet<TxHash>>>,
},
/// Requests a clone of the sender sender channel to the peer.
GetPeerSender {
peer_id: PeerId,
peer_request_sender: oneshot::Sender<Option<PeerRequestSender>>,
},
}

/// All events related to transactions emitted by the network.
Expand Down

0 comments on commit dea22ad

Please sign in to comment.