Skip to content

Commit 3c7abc0

Browse files
committed
feat(connect): propagate courtesy metadata
1 parent 61947f6 commit 3c7abc0

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

crates/core/src/message.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ pub(crate) enum NodeEvent {
367367
/// Register expectation for an inbound connection from the given peer.
368368
ExpectPeerConnection {
369369
peer: PeerId,
370+
courtesy: bool,
370371
},
371372
}
372373

@@ -444,8 +445,11 @@ impl Display for NodeEvent {
444445
"Local subscribe complete (tx: {tx}, key: {key}, subscribed: {subscribed})"
445446
)
446447
}
447-
NodeEvent::ExpectPeerConnection { peer } => {
448-
write!(f, "ExpectPeerConnection (from {peer})")
448+
NodeEvent::ExpectPeerConnection { peer, courtesy } => {
449+
write!(
450+
f,
451+
"ExpectPeerConnection (from {peer}, courtesy: {courtesy})"
452+
)
449453
}
450454
}
451455
}

crates/core/src/node/network_bridge/p2p_protoc.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,14 +629,14 @@ impl P2pConnManager {
629629
)
630630
.await?;
631631
}
632-
NodeEvent::ExpectPeerConnection { peer } => {
633-
tracing::debug!(%peer, "ExpectPeerConnection event received; registering inbound expectation via handshake driver");
632+
NodeEvent::ExpectPeerConnection { peer, courtesy } => {
633+
tracing::debug!(%peer, courtesy, "ExpectPeerConnection event received; registering inbound expectation via handshake driver");
634634
state.outbound_handler.expect_incoming(peer.addr);
635635
if let Err(error) = handshake_cmd_sender
636636
.send(HandshakeCommand::ExpectInbound {
637637
peer: peer.clone(),
638638
transaction: None,
639-
courtesy: false,
639+
courtesy,
640640
})
641641
.await
642642
{
@@ -1370,8 +1370,15 @@ impl P2pConnManager {
13701370
"Inbound connection established"
13711371
);
13721372

1373-
self.handle_successful_connection(peer_id, connection, state, select_stream, None)
1374-
.await?;
1373+
self.handle_successful_connection(
1374+
peer_id,
1375+
connection,
1376+
state,
1377+
select_stream,
1378+
None,
1379+
courtesy,
1380+
)
1381+
.await?;
13751382
}
13761383
HandshakeEvent::OutboundEstablished {
13771384
transaction,
@@ -1385,8 +1392,15 @@ impl P2pConnManager {
13851392
transaction = %transaction,
13861393
"Outbound connection established"
13871394
);
1388-
self.handle_successful_connection(peer, connection, state, select_stream, None)
1389-
.await?;
1395+
self.handle_successful_connection(
1396+
peer,
1397+
connection,
1398+
state,
1399+
select_stream,
1400+
None,
1401+
courtesy,
1402+
)
1403+
.await?;
13901404
}
13911405
HandshakeEvent::OutboundFailed {
13921406
transaction,
@@ -1501,6 +1515,7 @@ impl P2pConnManager {
15011515
state: &mut EventListenerState,
15021516
select_stream: &mut priority_select::ProductionPrioritySelectStream,
15031517
remaining_checks: Option<usize>,
1518+
courtesy: bool,
15041519
) -> anyhow::Result<()> {
15051520
let pending_txs = state
15061521
.awaiting_connection_txs

crates/core/src/node/testing_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,8 +935,8 @@ where
935935
NodeEvent::QueryNodeDiagnostics { .. } => {
936936
unimplemented!()
937937
}
938-
NodeEvent::ExpectPeerConnection { peer } => {
939-
tracing::debug!(%peer, "ExpectPeerConnection ignored in testing impl");
938+
NodeEvent::ExpectPeerConnection { peer, courtesy } => {
939+
tracing::debug!(%peer, courtesy, "ExpectPeerConnection ignored in testing impl");
940940
continue;
941941
}
942942
},

crates/core/src/operations/connect.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,16 @@ pub(crate) trait RelayContext {
178178
}
179179

180180
/// Result of processing a request at a relay.
181+
#[derive(Debug, Clone)]
182+
pub(crate) struct ExpectedConnection {
183+
pub peer: PeerKeyLocation,
184+
pub courtesy: bool,
185+
}
186+
181187
#[derive(Debug, Default)]
182188
pub(crate) struct RelayActions {
183189
pub accept_response: Option<ConnectResponse>,
184-
pub expect_connection_from: Option<PeerKeyLocation>,
190+
pub expect_connection_from: Option<ExpectedConnection>,
185191
pub forward: Option<(PeerKeyLocation, ConnectRequest)>,
186192
pub observed_address: Option<(PeerKeyLocation, SocketAddr)>,
187193
}
@@ -218,7 +224,10 @@ impl RelayState {
218224
acceptor: acceptor.clone(),
219225
courtesy,
220226
});
221-
actions.expect_connection_from = Some(self.request.origin.clone());
227+
actions.expect_connection_from = Some(ExpectedConnection {
228+
peer: self.request.origin.clone(),
229+
courtesy,
230+
});
222231
}
223232

224233
if self.forwarded_to.is_none() && self.request.ttl > 0 {
@@ -589,10 +598,11 @@ impl Operation for ConnectOp {
589598
.await?;
590599
}
591600

592-
if let Some(peer) = actions.expect_connection_from {
601+
if let Some(expected) = actions.expect_connection_from {
593602
op_manager
594603
.notify_node_event(NodeEvent::ExpectPeerConnection {
595-
peer: peer.peer.clone(),
604+
peer: expected.peer.peer.clone(),
605+
courtesy: expected.courtesy,
596606
})
597607
.await?;
598608
}
@@ -651,6 +661,7 @@ impl Operation for ConnectOp {
651661
.notify_node_event(
652662
crate::message::NodeEvent::ExpectPeerConnection {
653663
peer: new_acceptor.peer.peer.clone(),
664+
courtesy: new_acceptor.courtesy,
654665
},
655666
)
656667
.await?;
@@ -1040,7 +1051,10 @@ mod tests {
10401051
let response = actions.accept_response.expect("expected acceptance");
10411052
assert_eq!(response.acceptor.peer, self_loc.peer);
10421053
assert!(response.courtesy);
1043-
assert_eq!(actions.expect_connection_from.unwrap().peer, joiner.peer);
1054+
assert_eq!(
1055+
actions.expect_connection_from.unwrap().peer.peer,
1056+
joiner.peer
1057+
);
10441058
assert!(actions.forward.is_none());
10451059
}
10461060

0 commit comments

Comments
 (0)