Fix REST /eth/v1/node/identity should return proper MultiAddresses (version 2). (#6422)

* Initial commit.

* Bump nim-libp2p with dualstack fixes.

* Pass announcedAddresses to the `p2p_addresses` list.
This commit is contained in:
Eugene Kabanov 2024-07-11 18:39:38 +03:00 committed by GitHub
parent befcf3f56a
commit 031033a6f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 49 deletions

View File

@ -23,13 +23,15 @@ import
libp2p/protocols/pubsub/[ libp2p/protocols/pubsub/[
pubsub, gossipsub, rpc/message, rpc/messages, peertable, pubsubpeer], pubsub, gossipsub, rpc/message, rpc/messages, peertable, pubsubpeer],
libp2p/stream/connection, libp2p/stream/connection,
libp2p/services/wildcardresolverservice,
eth/[keys, async_utils], eth/[keys, async_utils],
eth/net/nat, eth/p2p/discoveryv5/[enr, node, random2], eth/net/nat, eth/p2p/discoveryv5/[enr, node, random2],
".."/[version, conf, beacon_clock, conf_light_client], ".."/[version, conf, beacon_clock, conf_light_client],
../spec/datatypes/[phase0, altair, bellatrix], ../spec/datatypes/[phase0, altair, bellatrix],
../spec/[eth2_ssz_serialization, network, helpers, forks], ../spec/[eth2_ssz_serialization, network, helpers, forks],
../validators/keystore_management, ../validators/keystore_management,
"."/[eth2_discovery, eth2_protocol_dsl, libp2p_json_serialization, peer_pool, peer_scores] "."/[eth2_discovery, eth2_protocol_dsl, libp2p_json_serialization, peer_pool,
peer_scores]
export export
tables, chronos, ratelimit, version, multiaddress, peerinfo, p2pProtocol, tables, chronos, ratelimit, version, multiaddress, peerinfo, p2pProtocol,
@ -81,6 +83,7 @@ type
rng*: ref HmacDrbgContext rng*: ref HmacDrbgContext
peers*: Table[PeerId, Peer] peers*: Table[PeerId, Peer]
directPeers*: DirectPeers directPeers*: DirectPeers
announcedAddresses*: seq[MultiAddress]
validTopics: HashSet[string] validTopics: HashSet[string]
peerPingerHeartbeatFut: Future[void].Raising([CancelledError]) peerPingerHeartbeatFut: Future[void].Raising([CancelledError])
peerTrimmerHeartbeatFut: Future[void].Raising([CancelledError]) peerTrimmerHeartbeatFut: Future[void].Raising([CancelledError])
@ -1767,7 +1770,7 @@ proc new(T: type Eth2Node,
switch: Switch, pubsub: GossipSub, switch: Switch, pubsub: GossipSub,
ip: Opt[IpAddress], tcpPort, udpPort: Opt[Port], ip: Opt[IpAddress], tcpPort, udpPort: Opt[Port],
privKey: keys.PrivateKey, discovery: bool, privKey: keys.PrivateKey, discovery: bool,
directPeers: DirectPeers, directPeers: DirectPeers, announcedAddresses: openArray[MultiAddress],
rng: ref HmacDrbgContext): T {.raises: [CatchableError].} = rng: ref HmacDrbgContext): T {.raises: [CatchableError].} =
when not defined(local_testnet): when not defined(local_testnet):
let let
@ -1811,6 +1814,7 @@ proc new(T: type Eth2Node,
connectTimeout: connectTimeout, connectTimeout: connectTimeout,
seenThreshold: seenThreshold, seenThreshold: seenThreshold,
directPeers: directPeers, directPeers: directPeers,
announcedAddresses: @announcedAddresses,
quota: TokenBucket.new(maxGlobalQuota, fullReplenishTime) quota: TokenBucket.new(maxGlobalQuota, fullReplenishTime)
) )
@ -2223,6 +2227,8 @@ func gossipId(
proc newBeaconSwitch(config: BeaconNodeConf | LightClientConf, proc newBeaconSwitch(config: BeaconNodeConf | LightClientConf,
seckey: PrivateKey, address: MultiAddress, seckey: PrivateKey, address: MultiAddress,
rng: ref HmacDrbgContext): Switch {.raises: [CatchableError].} = rng: ref HmacDrbgContext): Switch {.raises: [CatchableError].} =
let service: Service = WildcardAddressResolverService.new()
var sb = var sb =
if config.enableYamux: if config.enableYamux:
SwitchBuilder.new().withYamux() SwitchBuilder.new().withYamux()
@ -2239,6 +2245,7 @@ proc newBeaconSwitch(config: BeaconNodeConf | LightClientConf,
.withMaxConnections(config.maxPeers) .withMaxConnections(config.maxPeers)
.withAgentVersion(config.agentString) .withAgentVersion(config.agentString)
.withTcpTransport({ServerFlags.ReuseAddr}) .withTcpTransport({ServerFlags.ReuseAddr})
.withServices(@[service])
.build() .build()
proc createEth2Node*(rng: ref HmacDrbgContext, proc createEth2Node*(rng: ref HmacDrbgContext,
@ -2359,7 +2366,8 @@ proc createEth2Node*(rng: ref HmacDrbgContext,
let node = Eth2Node.new( let node = Eth2Node.new(
config, cfg, enrForkId, discoveryForkId, forkDigests, getBeaconTime, switch, pubsub, extIp, config, cfg, enrForkId, discoveryForkId, forkDigests, getBeaconTime, switch, pubsub, extIp,
extTcpPort, extUdpPort, netKeys.seckey.asEthKey, extTcpPort, extUdpPort, netKeys.seckey.asEthKey,
discovery = config.discv5Enabled, directPeers, rng = rng) discovery = config.discv5Enabled, directPeers, announcedAddresses,
rng = rng)
node.pubsub.subscriptionValidator = node.pubsub.subscriptionValidator =
proc(topic: string): bool {.gcsafe, raises: [].} = proc(topic: string): bool {.gcsafe, raises: [].} =

View File

@ -106,35 +106,38 @@ proc getLastSeenAddress(node: BeaconNode, id: PeerId): string =
$addrs[len(addrs) - 1] $addrs[len(addrs) - 1]
else: else:
"" ""
proc getDiscoveryAddresses(node: BeaconNode): Option[seq[string]] = proc getDiscoveryAddresses(node: BeaconNode): seq[string] =
let restr = node.network.enrRecord().toTypedRecord() let
if restr.isErr(): typedRec = node.network.enrRecord().toTypedRecord().valueOr:
return none[seq[string]]() return default(seq[string])
let respa = restr.get().toPeerAddr(udpProtocol) peerAddr = typedRec.toPeerAddr(udpProtocol).valueOr:
if respa.isErr(): return default(seq[string])
return none[seq[string]]() maddress = MultiAddress.init(multiCodec("p2p"), peerAddr.peerId).valueOr:
let pa = respa.get() return default(seq[string])
let mpa = MultiAddress.init(multiCodec("p2p"), pa.peerId)
if mpa.isErr():
return none[seq[string]]()
var addresses = newSeqOfCap[string](len(pa.addrs))
for item in pa.addrs:
let resa = concat(item, mpa.get())
if resa.isOk():
addresses.add($(resa.get()))
return some(addresses)
proc getP2PAddresses(node: BeaconNode): Option[seq[string]] = var addresses: seq[string]
let pinfo = node.network.switch.peerInfo for item in peerAddr.addrs:
let mpa = MultiAddress.init(multiCodec("p2p"), pinfo.peerId) let res = concat(item, maddress)
if mpa.isErr(): if res.isOk():
return none[seq[string]]() addresses.add($(res.get()))
var addresses = newSeqOfCap[string](len(pinfo.addrs)) addresses
proc getP2PAddresses(node: BeaconNode): seq[string] =
let
pinfo = node.network.switch.peerInfo
maddress = MultiAddress.init(multiCodec("p2p"), pinfo.peerId).valueOr:
return default(seq[string])
var addresses: seq[string]
for item in node.network.announcedAddresses:
let res = concat(item, maddress)
if res.isOk():
addresses.add($(res.get()))
for item in pinfo.addrs: for item in pinfo.addrs:
let resa = concat(item, mpa.get()) let res = concat(item, maddress)
if resa.isOk(): if res.isOk():
addresses.add($(resa.get())) addresses.add($(res.get()))
return some(addresses) addresses
proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) = proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
let let
@ -143,28 +146,12 @@ proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
# https://ethereum.github.io/beacon-APIs/#/Node/getNetworkIdentity # https://ethereum.github.io/beacon-APIs/#/Node/getNetworkIdentity
router.api2(MethodGet, "/eth/v1/node/identity") do () -> RestApiResponse: router.api2(MethodGet, "/eth/v1/node/identity") do () -> RestApiResponse:
let discoveryAddresses =
block:
let res = node.getDiscoveryAddresses()
if res.isSome():
res.get()
else:
newSeq[string](0)
let p2pAddresses =
block:
let res = node.getP2PAddresses()
if res.isSome():
res.get()
else:
newSeq[string]()
RestApiResponse.jsonResponse( RestApiResponse.jsonResponse(
( (
peer_id: $node.network.peerId(), peer_id: $node.network.peerId(),
enr: node.network.enrRecord().toURI(), enr: node.network.enrRecord().toURI(),
p2p_addresses: p2pAddresses, p2p_addresses: node.getP2PAddresses(),
discovery_addresses: discoveryAddresses, discovery_addresses: node.getDiscoveryAddresses(),
metadata: ( metadata: (
seq_number: node.network.metadata.seq_number, seq_number: node.network.metadata.seq_number,
syncnets: to0xHex(node.network.metadata.syncnets.bytes), syncnets: to0xHex(node.network.metadata.syncnets.bytes),

2
vendor/nim-libp2p vendored

@ -1 +1 @@
Subproject commit 8cb7dbb425df1124b17c6b3142a19a380114a693 Subproject commit b5fb7b3a97d8977d969d786633f70c4094cd0eaf