Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix last-seen-p2p-address values in /eth/v1/node/peers API endpoint. #6595

Merged
merged 10 commits into from
Jan 8, 2025
26 changes: 18 additions & 8 deletions beacon_chain/rpc/rest_node_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ RestJson.useDefaultSerializationFor(
RestNodePeerCount,
)

proc normalize*(address: MultiAddress, value: PeerId): MaResult[MultiAddress] =
## Checks if `address` has `p2p` suffix, and if not add it.
let
protos = ? address.protocols()
index = protos.find(multiCodec("p2p"))
if index == -1:
let suffix = ? MultiAddress.init(multiCodec("p2p"), value)
concat(address, suffix)
else:
ok(address)

proc validateState(states: seq[PeerStateKind]): Result[ConnectionStateSet,
cstring] =
var res: set[ConnectionState]
Expand Down Expand Up @@ -98,14 +109,13 @@ proc toString(direction: PeerType): string =
"outbound"

proc getLastSeenAddress(node: BeaconNode, id: PeerId): string =
# TODO (cheatfate): We need to provide filter here, which will be able to
# filter such multiaddresses like `/ip4/0.0.0.0` or local addresses or
# addresses with peer ids.
let addrs = node.network.switch.peerStore[AddressBook][id]
if len(addrs) > 0:
$addrs[len(addrs) - 1]
else:
""
let
address = node.network.switch.peerStore[LastSeenBook][id].valueOr:
return ""
normalized = address.normalize(id).valueOr:
return ""
$normalized

proc getDiscoveryAddresses(node: BeaconNode): seq[string] =
let
typedRec = TypedRecord.fromRecord(node.network.enrRecord())
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-libp2p
Loading