Skip to content

Commit 47005ce

Browse files
committed
fix(beacon-api): gate disconnect_reason on disconnected/disconnecting state
Per the proposed beacon-API spec (ethereum/beacon-APIs#606), `disconnect_reason` MUST only be populated when the peer's `state` is `disconnected` or `disconnecting`. Wrap the existing `last_disconnect()` lookup in both the single-peer and list handlers so the field is omitted (None) for connected/connecting peers.
1 parent 104524a commit 47005ce

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

  • beacon_node/http_api/src

beacon_node/http_api/src/lib.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,9 +2436,21 @@ pub fn serve<T: BeaconChainTypes>(
24362436
if let Some(&dir) = peer_info.connection_direction() {
24372437
let agent_version = peer_info.client().agent_string.clone();
24382438
let score = Some(peer_info.score().score());
2439-
let disconnect_reason = peer_info.last_disconnect().and_then(|d| {
2440-
map_disconnect_reason(d.reason).map(|s| s.to_string())
2441-
});
2439+
let state: api_types::PeerState =
2440+
peer_info.connection_status().clone().into();
2441+
// Per beacon-API spec, `disconnect_reason` MUST only be populated
2442+
// when `state` is `disconnected` or `disconnecting`.
2443+
let disconnect_reason = if matches!(
2444+
state,
2445+
api_types::PeerState::Disconnected
2446+
| api_types::PeerState::Disconnecting
2447+
) {
2448+
peer_info.last_disconnect().and_then(|d| {
2449+
map_disconnect_reason(d.reason).map(|s| s.to_string())
2450+
})
2451+
} else {
2452+
None
2453+
};
24422454
let downscore_reasons = peer_info
24432455
.last_action()
24442456
.map(|a| vec![map_downscore_reason(a.reason).to_string()]);
@@ -2447,7 +2459,7 @@ pub fn serve<T: BeaconChainTypes>(
24472459
enr: peer_info.enr().map(|enr| enr.to_base64()),
24482460
last_seen_p2p_address: address,
24492461
direction: dir.into(),
2450-
state: peer_info.connection_status().clone().into(),
2462+
state,
24512463
agent_version,
24522464
score,
24532465
disconnect_reason,
@@ -2509,10 +2521,19 @@ pub fn serve<T: BeaconChainTypes>(
25092521
if state_matches && direction_matches {
25102522
let agent_version = peer_info.client().agent_string.clone();
25112523
let score = Some(peer_info.score().score());
2512-
let disconnect_reason =
2524+
// Per beacon-API spec, `disconnect_reason` MUST only be
2525+
// populated when `state` is `disconnected` or `disconnecting`.
2526+
let disconnect_reason = if matches!(
2527+
state,
2528+
api_types::PeerState::Disconnected
2529+
| api_types::PeerState::Disconnecting
2530+
) {
25132531
peer_info.last_disconnect().and_then(|d| {
25142532
map_disconnect_reason(d.reason).map(|s| s.to_string())
2515-
});
2533+
})
2534+
} else {
2535+
None
2536+
};
25162537
let downscore_reasons = peer_info
25172538
.last_action()
25182539
.map(|a| vec![map_downscore_reason(a.reason).to_string()]);

0 commit comments

Comments
 (0)