nimbus-eth2/beacon_chain/rpc/rest_node_api.nim

299 lines
11 KiB
Nim
Raw Normal View History

# Copyright (c) 2021-2023 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
2021-03-17 18:46:45 +00:00
import
stew/[byteutils, results],
2021-03-17 18:46:45 +00:00
chronicles,
eth/p2p/discoveryv5/enr,
libp2p/[multiaddress, multicodec, peerstore],
../version, ../beacon_node, ../sync/sync_manager,
2021-03-17 20:42:55 +00:00
../networking/[eth2_network, peer_pool],
../spec/datatypes/base,
./rest_utils
2021-03-17 18:46:45 +00:00
export rest_utils
2021-03-17 18:46:45 +00:00
logScope: topics = "rest_node"
type
ConnectionStateSet* = set[ConnectionState]
PeerTypeSet* = set[PeerType]
RestNodePeerCount* = object
disconnected*: uint64
connecting*: uint64
connected*: uint64
disconnecting*: uint64
2021-03-17 18:46:45 +00:00
proc validateState(states: seq[PeerStateKind]): Result[ConnectionStateSet,
cstring] =
var res: set[ConnectionState]
for item in states:
case item
of PeerStateKind.Disconnected:
if ConnectionState.Disconnected in res:
return err("Peer connection states must be unique")
res.incl(ConnectionState.Disconnected)
of PeerStateKind.Connecting:
if ConnectionState.Connecting in res:
return err("Peer connection states must be unique")
res.incl(ConnectionState.Connecting)
of PeerStateKind.Connected:
if ConnectionState.Connected in res:
return err("Peer connection states must be unique")
res.incl(ConnectionState.Connected)
of PeerStateKind.Disconnecting:
if ConnectionState.Disconnecting in res:
return err("Peer connection states must be unique")
res.incl(ConnectionState.Disconnecting)
if res == {}:
res = {ConnectionState.Connecting, ConnectionState.Connected,
ConnectionState.Disconnecting, ConnectionState.Disconnected}
ok(res)
proc validateDirection(directions: seq[PeerDirectKind]): Result[PeerTypeSet,
cstring] =
var res: set[PeerType]
for item in directions:
case item
of PeerDirectKind.Inbound:
if PeerType.Incoming in res:
return err("Peer direction states must be unique")
res.incl(PeerType.Incoming)
of PeerDirectKind.Outbound:
if PeerType.Outgoing in res:
return err("Peer direction states must be unique")
res.incl(PeerType.Outgoing)
if res == {}:
res = {PeerType.Incoming, PeerType.Outgoing}
ok(res)
proc toString(state: ConnectionState): string =
case state
of ConnectionState.Disconnected:
"disconnected"
of ConnectionState.Connecting:
"connecting"
of ConnectionState.Connected:
"connected"
of ConnectionState.Disconnecting:
"disconnecting"
else:
""
proc toString(direction: PeerType): string =
case direction:
of PeerType.Incoming:
"inbound"
of PeerType.Outgoing:
"outbound"
proc getLastSeenAddress(node: BeaconNode, id: PeerId): string =
2021-03-17 18:46:45 +00:00
# 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.
2022-06-08 05:53:50 +00:00
let addrs = node.network.switch.peerStore[AddressBook][id]
if len(addrs) > 0:
$addrs[len(addrs) - 1]
2021-03-17 18:46:45 +00:00
else:
""
proc getDiscoveryAddresses(node: BeaconNode): Option[seq[string]] =
let restr = node.network.enrRecord().toTypedRecord()
if restr.isErr():
return none[seq[string]]()
let respa = restr.get().toPeerAddr(udpProtocol)
if respa.isErr():
return none[seq[string]]()
let pa = respa.get()
let mpa = MultiAddress.init(multiCodec("p2p"), pa.peerId)
2021-03-17 18:46:45 +00:00
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]] =
let pinfo = node.network.switch.peerInfo
let mpa = MultiAddress.init(multiCodec("p2p"), pinfo.peerId)
2021-03-17 18:46:45 +00:00
if mpa.isErr():
return none[seq[string]]()
var addresses = newSeqOfCap[string](len(pinfo.addrs))
for item in pinfo.addrs:
let resa = concat(item, mpa.get())
if resa.isOk():
addresses.add($(resa.get()))
return some(addresses)
proc installNodeApiHandlers*(router: var RestRouter, node: BeaconNode) =
let
cachedVersion =
RestApiResponse.prepareJsonResponse((version: "Nimbus/" & fullVersionStr))
# https://ethereum.github.io/beacon-APIs/#/Node/getNetworkIdentity
router.api(MethodGet, "/eth/v1/node/identity") do () -> RestApiResponse:
2021-03-17 18:46:45 +00:00
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]()
return RestApiResponse.jsonResponse(
(
2021-03-17 18:46:45 +00:00
peer_id: $node.network.peerId(),
enr: node.network.enrRecord().toURI(),
2021-03-17 18:46:45 +00:00
p2p_addresses: p2pAddresses,
discovery_addresses: discoveryAddresses,
metadata: (
seq_number: node.network.metadata.seq_number,
syncnets: to0xHex(node.network.metadata.syncnets.bytes),
attnets: to0xHex(node.network.metadata.attnets.bytes)
)
2021-03-17 18:46:45 +00:00
)
)
# https://ethereum.github.io/beacon-APIs/#/Node/getPeers
router.api(MethodGet, "/eth/v1/node/peers") do (
state: seq[PeerStateKind],
direction: seq[PeerDirectKind]) -> RestApiResponse:
let connectionMask =
block:
if state.isErr():
return RestApiResponse.jsonError(Http400, InvalidPeerStateValueError,
$state.error())
let sres = validateState(state.get())
if sres.isErr():
return RestApiResponse.jsonError(Http400, InvalidPeerStateValueError,
$sres.error())
sres.get()
let directionMask =
block:
if direction.isErr():
return RestApiResponse.jsonError(Http400,
InvalidPeerDirectionValueError,
$direction.error())
let dres = validateDirection(direction.get())
if dres.isErr():
return RestApiResponse.jsonError(Http400,
InvalidPeerDirectionValueError,
$dres.error())
dres.get()
2021-03-17 18:46:45 +00:00
2022-06-02 09:39:08 +00:00
var res: seq[RestNodePeer]
for peer in node.network.peers.values():
if (peer.connectionState in connectionMask) and
(peer.direction in directionMask):
2022-06-02 09:39:08 +00:00
let peer = RestNodePeer(
peer_id: $peer.peerId,
enr: if peer.enr.isSome(): peer.enr.get().toURI() else: "",
last_seen_p2p_address: getLastSeenAddress(node, peer.peerId),
state: peer.connectionState.toString(),
direction: peer.direction.toString(),
2022-06-02 09:39:08 +00:00
# Fields `agent` and `proto` are not part of specification
2022-06-08 05:53:50 +00:00
agent: node.network.switch.peerStore[AgentBook][peer.peerId],
proto: node.network.switch.peerStore[ProtoVersionBook][peer.peerId]
2021-03-17 18:46:45 +00:00
)
res.add(peer)
2021-04-04 07:23:36 +00:00
return RestApiResponse.jsonResponseWMeta(res, (count: uint64(len(res))))
2021-03-17 18:46:45 +00:00
# https://ethereum.github.io/beacon-APIs/#/Node/getPeerCount
router.api(MethodGet, "/eth/v1/node/peer_count") do () -> RestApiResponse:
var res: RestNodePeerCount
2021-03-17 18:46:45 +00:00
for item in node.network.peers.values():
case item.connectionState
of ConnectionState.Connecting:
2021-03-17 18:46:45 +00:00
inc(res.connecting)
of ConnectionState.Connected:
2021-03-17 18:46:45 +00:00
inc(res.connected)
of ConnectionState.Disconnecting:
2021-03-17 18:46:45 +00:00
inc(res.disconnecting)
of ConnectionState.Disconnected:
2021-03-17 18:46:45 +00:00
inc(res.disconnected)
of ConnectionState.None:
discard
return RestApiResponse.jsonResponse(res)
2021-03-17 18:46:45 +00:00
# https://ethereum.github.io/beacon-APIs/#/Node/getPeer
router.api(MethodGet, "/eth/v1/node/peers/{peer_id}") do (
peer_id: PeerId) -> RestApiResponse:
let peer =
block:
if peer_id.isErr():
return RestApiResponse.jsonError(Http400, InvalidPeerIdValueError,
$peer_id.error())
let res = node.network.peers.getOrDefault(peer_id.get())
if isNil(res):
return RestApiResponse.jsonError(Http404, PeerNotFoundError)
res
2021-03-17 18:46:45 +00:00
return RestApiResponse.jsonResponse(
(
peer_id: $peer.peerId,
enr: if peer.enr.isSome(): peer.enr.get().toURI() else: "",
last_seen_p2p_address: getLastSeenAddress(node, peer.peerId),
2021-03-17 18:46:45 +00:00
state: peer.connectionState.toString(),
direction: peer.direction.toString(),
2022-06-08 05:53:50 +00:00
agent: node.network.switch.peerStore[AgentBook][peer.peerId], # Fields `agent` and `proto` are not
proto: node.network.switch.peerStore[ProtoVersionBook][peer.peerId] # part of specification
2021-03-17 18:46:45 +00:00
)
)
# https://ethereum.github.io/beacon-APIs/#/Node/getNodeVersion
router.api(MethodGet, "/eth/v1/node/version") do () -> RestApiResponse:
return RestApiResponse.response(cachedVersion, Http200,
"application/json")
2021-03-17 18:46:45 +00:00
# https://ethereum.github.io/beacon-APIs/#/Node/getSyncingStatus
router.api(MethodGet, "/eth/v1/node/syncing") do () -> RestApiResponse:
let
wallSlot = node.beaconClock.now().slotOrZero()
headSlot = node.dag.head.slot
distance = wallSlot - headSlot
isSyncing =
if isNil(node.syncManager):
false
else:
node.syncManager.inProgress
isOptimistic =
if node.currentSlot().epoch() >= node.dag.cfg.BELLATRIX_FORK_EPOCH:
some(not node.dag.head.executionValid)
else:
none[bool]()
elOffline =
if node.currentSlot().epoch() >= node.dag.cfg.CAPELLA_FORK_EPOCH:
some(not node.elManager.hasAnyWorkingConnection)
else:
none[bool]() # Added with ethereum/beacon-APIs v2.4.0
info = RestSyncInfo(
head_slot: headSlot, sync_distance: distance,
is_syncing: isSyncing, is_optimistic: isOptimistic,
el_offline: elOffline
)
return RestApiResponse.jsonResponse(info)
2021-03-17 18:46:45 +00:00
# https://ethereum.github.io/beacon-APIs/#/Node/getHealth
router.api(MethodGet, "/eth/v1/node/health") do () -> RestApiResponse:
2021-03-17 18:46:45 +00:00
# TODO: Add ability to detect node's issues and return 503 error according
# to specification.
let status =
2021-03-17 18:46:45 +00:00
if node.syncManager.inProgress:
Http206
2021-03-17 18:46:45 +00:00
else:
Http200
return RestApiResponse.response("", status, contentType = "")