fix: remove extraneous fields from /eth/v1/node/peers response (#8554)

**Motivation**

Closes https://github.com/ChainSafe/lodestar/issues/7909

**Description**

- remove extraneous fields from `/eth/v1/node/peers` response
- return `null` as enr of peers instead of empty string `""`


```bash
~> curl -s localhost:9596/eth/v1/node/peers | jq ".data[0]"
{
  "peer_id": "16Uiu2HAmLiPFRNHiS7FdJb8hX3wfVWF5EUVdTunbvN1L3mYeSVLa",
  "enr": null,
  "last_seen_p2p_address": "/ip4/188.165.77.35/tcp/9000/p2p/16Uiu2HAmLiPFRNHiS7FdJb8hX3wfVWF5EUVdTunbvN1L3mYeSVLa",
  "state": "connected",
  "direction": "outbound"
}

```
This commit is contained in:
Nico Flaig
2025-10-22 20:21:29 +01:00
committed by GitHub
parent 8ac1ed5b17
commit 392e49fd0b
2 changed files with 26 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
import {ContainerType, ValueOf} from "@chainsafe/ssz";
import {ContainerType, OptionalType, ValueOf} from "@chainsafe/ssz";
import {ChainForkConfig} from "@lodestar/config";
import {fulu, ssz, stringType} from "@lodestar/types";
import {StringType, fulu, ssz, stringType} from "@lodestar/types";
import {
ArrayOf,
EmptyArgs,
@@ -64,14 +64,21 @@ export type NetworkIdentity = ValueOf<typeof NetworkIdentityType> & {
export type PeerState = "disconnected" | "connecting" | "connected" | "disconnecting";
export type PeerDirection = "inbound" | "outbound";
export type NodePeer = {
peerId: string;
enr: string;
lastSeenP2pAddress: string;
state: PeerState;
// the spec does not specify direction for a disconnected peer, lodestar uses null in that case
direction: PeerDirection | null;
};
export const NodePeerType = new ContainerType(
{
peerId: stringType,
enr: new OptionalType(stringType),
lastSeenP2pAddress: stringType,
state: new StringType<PeerState>(),
// the spec does not specify direction for a disconnected peer, lodestar uses null in that case
direction: new OptionalType(new StringType<PeerDirection>()),
},
{jsonCase: "eth2"}
);
export const NodePeersType = ArrayOf(NodePeerType);
export type NodePeer = ValueOf<typeof NodePeerType>;
export type NodePeers = ValueOf<typeof NodePeersType>;
export type PeersMeta = {count: number};
@@ -115,7 +122,7 @@ export type Endpoints = {
"GET",
FilterGetPeers,
{query: {state?: PeerState[]; direction?: PeerDirection[]}},
NodePeer[],
NodePeers,
PeersMeta
>;
@@ -225,7 +232,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
schema: {query: {state: Schema.StringArray, direction: Schema.StringArray}},
},
resp: {
...JsonOnlyResponseCodec,
data: NodePeersType,
meta: {
toJson: (d) => d,
fromJson: (d) => ({count: (d as PeersMeta).count}),
@@ -236,6 +243,7 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
toResponse: (data, meta) => ({data, meta}),
fromResponse: (resp) => resp as {data: NodePeer[]; meta: PeersMeta},
},
onlySupport: WireFormat.json,
},
},
getPeer: {
@@ -246,7 +254,11 @@ export function getDefinitions(_config: ChainForkConfig): RouteDefinitions<Endpo
parseReq: ({params}) => ({peerId: params.peer_id}),
schema: {params: {peer_id: Schema.StringRequired}},
},
resp: JsonOnlyResponseCodec,
resp: {
data: NodePeerType,
meta: EmptyMetaCodec,
onlySupport: WireFormat.json,
},
},
getPeerCount: {
url: "/eth/v1/node/peer_count",

View File

@@ -10,7 +10,7 @@ export function formatNodePeer(peerIdStr: string, connections: Connection[]): ro
return {
peerId: conn ? conn.remotePeer.toString() : peerIdStr,
// TODO: figure out how to get enr of peer
enr: "",
enr: null,
lastSeenP2pAddress: conn ? conn.remoteAddr.toString() : "",
direction: conn ? (conn.direction as routes.node.PeerDirection) : null,
state: conn ? getPeerState(conn.status) : "disconnected",