Allow buffering multiple transfer proofs per peer

Allowing multiple swaps with the same peer requires buffering multiple transfer proofs per peer.
This commit is contained in:
Daniel Karzel 2021-04-08 17:02:18 +10:00
parent bad6ca95e0
commit b60790a32c
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E

View File

@ -52,7 +52,7 @@ pub struct EventLoop<RS> {
/// Tracks [`transfer_proof::Request`]s which could not yet be sent because /// Tracks [`transfer_proof::Request`]s which could not yet be sent because
/// we are currently disconnected from the peer. /// we are currently disconnected from the peer.
buffered_transfer_proofs: HashMap<PeerId, (transfer_proof::Request, bmrng::Responder<()>)>, buffered_transfer_proofs: HashMap<PeerId, Vec<(transfer_proof::Request, bmrng::Responder<()>)>>,
/// Tracks [`transfer_proof::Request`]s which are currently inflight and /// Tracks [`transfer_proof::Request`]s which are currently inflight and
/// awaiting an acknowledgement. /// awaiting an acknowledgement.
@ -225,11 +225,13 @@ where
SwarmEvent::ConnectionEstablished { peer_id: peer, endpoint, .. } => { SwarmEvent::ConnectionEstablished { peer_id: peer, endpoint, .. } => {
tracing::debug!(%peer, address = %endpoint.get_remote_address(), "New connection established"); tracing::debug!(%peer, address = %endpoint.get_remote_address(), "New connection established");
if let Some((transfer_proof, responder)) = self.buffered_transfer_proofs.remove(&peer) { if let Some(transfer_proofs) = self.buffered_transfer_proofs.remove(&peer) {
tracing::debug!(%peer, "Found buffered transfer proof for peer"); for (transfer_proof, responder) in transfer_proofs {
tracing::debug!(%peer, "Found buffered transfer proof for peer");
let id = self.swarm.transfer_proof.send_request(&peer, transfer_proof); let id = self.swarm.transfer_proof.send_request(&peer, transfer_proof);
self.inflight_transfer_proofs.insert(id, responder); self.inflight_transfer_proofs.insert(id, responder);
}
} }
} }
SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => { SwarmEvent::IncomingConnectionError { send_back_addr: address, error, .. } => {
@ -253,7 +255,7 @@ where
Some(Ok((peer, transfer_proof, responder))) => { Some(Ok((peer, transfer_proof, responder))) => {
if !self.swarm.transfer_proof.is_connected(&peer) { if !self.swarm.transfer_proof.is_connected(&peer) {
tracing::warn!(%peer, "No active connection to peer, buffering transfer proof"); tracing::warn!(%peer, "No active connection to peer, buffering transfer proof");
self.buffered_transfer_proofs.insert(peer, (transfer_proof, responder)); self.buffered_transfer_proofs.entry(peer).or_insert_with(Vec::new).push((transfer_proof, responder));
continue; continue;
} }