2021-03-17 18:46:45 +00:00
|
|
|
import
|
2021-10-21 11:01:29 +00:00
|
|
|
std/[sequtils],
|
2022-01-08 20:06:34 +00:00
|
|
|
stew/[byteutils, results],
|
2021-03-17 18:46:45 +00:00
|
|
|
chronicles,
|
|
|
|
eth/p2p/discoveryv5/enr,
|
2021-10-21 11:01:29 +00:00
|
|
|
libp2p/[multiaddress, multicodec, peerstore],
|
2021-10-19 14:09:26 +00:00
|
|
|
../version, ../beacon_node, ../sync/sync_manager,
|
2021-03-17 20:42:55 +00:00
|
|
|
../networking/[eth2_network, peer_pool],
|
2021-06-23 14:43:18 +00:00
|
|
|
../spec/datatypes/base,
|
2021-08-03 15:17:11 +00:00
|
|
|
./rest_utils
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-10-27 12:01:11 +00:00
|
|
|
export rest_utils
|
|
|
|
|
2021-03-17 18:46:45 +00:00
|
|
|
logScope: topics = "rest_node"
|
|
|
|
|
|
|
|
type
|
|
|
|
ConnectionStateSet* = set[ConnectionState]
|
|
|
|
PeerTypeSet* = set[PeerType]
|
|
|
|
|
2021-05-20 17:56:12 +00:00
|
|
|
RestNodePeerCount* = object
|
|
|
|
disconnected*: uint64
|
|
|
|
connecting*: uint64
|
|
|
|
connected*: uint64
|
|
|
|
disconnecting*: uint64
|
2021-04-03 22:41:46 +00:00
|
|
|
|
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"
|
|
|
|
|
2021-10-21 11:01:29 +00:00
|
|
|
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.
|
2021-10-21 11:01:29 +00:00
|
|
|
let addrs = node.network.switch.peerStore.addressBook.get(id).toSeq()
|
|
|
|
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()
|
2022-04-08 16:22:49 +00:00
|
|
|
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
|
2022-04-08 16:22:49 +00:00
|
|
|
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) =
|
2021-10-06 13:43:03 +00:00
|
|
|
let
|
|
|
|
cachedVersion =
|
|
|
|
RestApiResponse.prepareJsonResponse((version: "Nimbus/" & fullVersionStr))
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getNetworkIdentity
|
2022-01-06 07:38:40 +00:00
|
|
|
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-23 22:50:18 +00:00
|
|
|
(
|
2021-03-17 18:46:45 +00:00
|
|
|
peer_id: $node.network.peerId(),
|
2022-04-08 16:22:49 +00:00
|
|
|
enr: node.network.enrRecord().toURI(),
|
2021-03-17 18:46:45 +00:00
|
|
|
p2p_addresses: p2pAddresses,
|
|
|
|
discovery_addresses: discoveryAddresses,
|
2021-03-23 22:50:18 +00:00
|
|
|
metadata: (
|
|
|
|
seq_number: node.network.metadata.seq_number,
|
2022-01-08 20:06:34 +00:00
|
|
|
syncnets: to0xHex(node.network.metadata.syncnets.bytes),
|
|
|
|
attnets: to0xHex(node.network.metadata.attnets.bytes)
|
2021-03-23 22:50:18 +00:00
|
|
|
)
|
2021-03-17 18:46:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getPeers
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/node/peers") do (
|
2021-04-04 09:48:44 +00:00
|
|
|
state: seq[PeerStateKind],
|
|
|
|
direction: seq[PeerDirectKind]) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let connectionMask =
|
|
|
|
block:
|
2021-04-04 09:48:44 +00:00
|
|
|
if state.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidPeerStateValueError,
|
2021-04-04 09:48:44 +00:00
|
|
|
$state.error())
|
|
|
|
let sres = validateState(state.get())
|
2021-03-23 22:50:18 +00:00
|
|
|
if sres.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidPeerStateValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$sres.error())
|
|
|
|
sres.get()
|
|
|
|
let directionMask =
|
|
|
|
block:
|
2021-04-04 09:48:44 +00:00
|
|
|
if direction.isErr():
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 14:34:05 +00:00
|
|
|
InvalidPeerDirectionValueError,
|
2021-04-04 09:48:44 +00:00
|
|
|
$direction.error())
|
|
|
|
let dres = validateDirection(direction.get())
|
2021-03-23 22:50:18 +00:00
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
2021-04-08 14:34:05 +00:00
|
|
|
InvalidPeerDirectionValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$dres.error())
|
|
|
|
dres.get()
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2022-06-02 09:39:08 +00:00
|
|
|
var res: seq[RestNodePeer]
|
2021-10-21 11:01:29 +00:00
|
|
|
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(
|
2021-10-21 11:01:29 +00:00
|
|
|
peer_id: $peer.peerId,
|
2022-04-08 16:22:49 +00:00
|
|
|
enr: if peer.enr.isSome(): peer.enr.get().toURI() else: "",
|
2021-10-21 11:01:29 +00:00
|
|
|
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
|
|
|
|
agent: node.network.switch.peerStore.agentBook.get(peer.peerId),
|
|
|
|
proto: node.network.switch.peerStore.protoVersionBook.get(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
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getPeerCount
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/node/peer_count") do () -> RestApiResponse:
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: RestNodePeerCount
|
2021-03-17 18:46:45 +00:00
|
|
|
for item in node.network.peers.values():
|
|
|
|
case item.connectionState
|
2021-07-19 11:58:30 +00:00
|
|
|
of ConnectionState.Connecting:
|
2021-03-17 18:46:45 +00:00
|
|
|
inc(res.connecting)
|
2021-07-19 11:58:30 +00:00
|
|
|
of ConnectionState.Connected:
|
2021-03-17 18:46:45 +00:00
|
|
|
inc(res.connected)
|
2021-07-19 11:58:30 +00:00
|
|
|
of ConnectionState.Disconnecting:
|
2021-03-17 18:46:45 +00:00
|
|
|
inc(res.disconnecting)
|
2021-07-19 11:58:30 +00:00
|
|
|
of ConnectionState.Disconnected:
|
2021-03-17 18:46:45 +00:00
|
|
|
inc(res.disconnected)
|
|
|
|
of ConnectionState.None:
|
|
|
|
discard
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getPeer
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/node/peers/{peer_id}") do (
|
2022-04-08 16:22:49 +00:00
|
|
|
peer_id: PeerId) -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let peer =
|
|
|
|
block:
|
|
|
|
if peer_id.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidPeerIdValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$peer_id.error())
|
|
|
|
let res = node.network.peers.getOrDefault(peer_id.get())
|
|
|
|
if isNil(res):
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http404, PeerNotFoundError)
|
2021-03-23 22:50:18 +00:00
|
|
|
res
|
2021-03-17 18:46:45 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
2021-10-21 11:01:29 +00:00
|
|
|
peer_id: $peer.peerId,
|
2022-04-08 16:22:49 +00:00
|
|
|
enr: if peer.enr.isSome(): peer.enr.get().toURI() else: "",
|
2021-10-21 11:01:29 +00:00
|
|
|
last_seen_p2p_address: getLastSeenAddress(node, peer.peerId),
|
2021-03-17 18:46:45 +00:00
|
|
|
state: peer.connectionState.toString(),
|
|
|
|
direction: peer.direction.toString(),
|
2021-10-21 11:01:29 +00:00
|
|
|
agent: node.network.switch.peerStore.agentBook.get(peer.peerId), # Fields `agent` and `proto` are not
|
|
|
|
proto: node.network.switch.peerStore.protoVersionBook.get(peer.peerId) # part of specification
|
2021-03-17 18:46:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getNodeVersion
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/node/version") do () -> RestApiResponse:
|
2021-10-06 13:43:03 +00:00
|
|
|
return RestApiResponse.response(cachedVersion, Http200,
|
|
|
|
"application/json")
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getSyncingStatus
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/eth/v1/node/syncing") do () -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(node.syncManager.getInfo())
|
2021-03-17 18:46:45 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getHealth
|
2022-01-06 07:38:40 +00:00
|
|
|
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.
|
2022-01-28 11:46:48 +00:00
|
|
|
let status =
|
2021-03-17 18:46:45 +00:00
|
|
|
if node.syncManager.inProgress:
|
2022-01-28 11:46:48 +00:00
|
|
|
Http206
|
2021-03-17 18:46:45 +00:00
|
|
|
else:
|
2022-01-28 11:46:48 +00:00
|
|
|
Http200
|
|
|
|
return RestApiResponse.response("", status, contentType = "")
|
2021-04-13 10:19:31 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
# Legacy URLS - Nimbus <= 1.5.5 used to expose the REST API with an additional
|
|
|
|
# `/api` path component
|
2021-04-13 10:19:31 +00:00
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/identity",
|
|
|
|
"/eth/v1/node/identity"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/peers",
|
|
|
|
"/eth/v1/node/peers"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/peer_count",
|
|
|
|
"/eth/v1/node/peer_count"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/peers/{peer_id}",
|
|
|
|
"/eth/v1/node/peers/{peer_id}"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/version",
|
|
|
|
"/eth/v1/node/version"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/syncing",
|
|
|
|
"/eth/v1/node/syncing"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
2022-01-06 07:38:40 +00:00
|
|
|
"/api/eth/v1/node/health",
|
|
|
|
"/eth/v1/node/health"
|
2021-04-13 10:19:31 +00:00
|
|
|
)
|