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

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

Draft
wants to merge 4 commits into
base: unstable
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 85 additions & 6 deletions beacon_chain/rpc/rest_node_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{.push raises: [].}

import
std/algorithm,
stew/byteutils,
chronicles,
eth/p2p/discoveryv5/enr,
Expand Down Expand Up @@ -97,15 +98,93 @@ proc toString(direction: PeerType): string =
of PeerType.Outgoing:
"outbound"

proc getProtocolArgument(ma: MultiAddress,
codec: MultiCodec): MaResult[seq[byte]] =
var buffer: seq[byte]
for item in ma:
let
ritem = ? item
code = ? ritem.protoCode()
if code == codec:
let arg = ? ritem.protoAddress()
return ok(arg)
err("Multiaddress codec has not been found")

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]
# Because `last_seen_p2p_address` is single address but libp2p reports lot of
# addresses, we going to sort list of addresses before selecting one. Here
# sort order list:
# 1. Globally routable IPv6 address.
# 2. Globally routable IPv4 address.
# 3. Non IPv4/IPv6 multiaddress.
# 4. Site-local IPv6 address.
# 5. Site-local IPv4 address.
# 6. Link-local IPv6 address.
# 7. Link-local IPv4 address.
# 8. Loopback IPv6 address.
# 9. Loopback IPv4 address.
# 10. All other IPv6 address types.
# 11. All other IPv4 address types.
proc compare(
a, b: tuple[address: MultiAddress, position: int]
): int {.closure.} = cmp(a.position, b.position)
let addresses = node.network.switch.peerStore[AddressBook][id]
var temp: seq[tuple[address: MultiAddress, position: int]]
for address in addresses:
let res =
block:
let
isUDP = UDP.matchPartial(address)
isTCP = TCP.matchPartial(address)

if isUDP or isTCP:
# TODO (cheatfate): We match TCP here because `nim-libp2p` do not have
# QUIC support yet. So we give TCP addresses priority.
let boost = if isUDP: 100 else: 0
if IP4.matchPartial(address):
let address4 =
address.getProtocolArgument(multiCodec("ip4")).valueOr:
continue
var ta4 = TransportAddress(family: AddressFamily.IPv4)
ta4.address_v4[0 .. 3] = address4[0 .. 3]
if ta4.isLoopback():
(address, boost + 9)
elif ta4.isLinkLocal():
(address, boost + 7)
elif ta4.isSiteLocal():
(address, boost + 5)
elif ta4.isGlobal():
(address, boost + 2)
else:
(address, boost + 11)
elif IP6.matchPartial(address):
let address6 =
address.getProtocolArgument(multiCodec("ip6")).valueOr:
continue
var ta6 = TransportAddress(family: AddressFamily.IPv6)
ta6.address_v6[0 .. 15] = address6[0 .. 15]
if ta6.isLoopback():
(address, boost + 8)
elif ta6.isLinkLocal():
(address, boost + 6)
elif ta6.isSiteLocal():
(address, boost + 4)
elif ta6.isGlobal():
(address, boost + 1)
else:
(address, boost + 10)
else:
(address, boost + 3)
else:
(address, 3)
temp.add(res)

if len(temp) > 0:
sort(temp, compare, SortOrder.Ascending)
$temp[0].address
else:
""

proc getDiscoveryAddresses(node: BeaconNode): seq[string] =
let
typedRec = TypedRecord.fromRecord(node.network.enrRecord())
Expand Down
Loading