2021-03-17 18:46:45 +00:00
|
|
|
import
|
2021-10-21 11:01:29 +00:00
|
|
|
std/[sequtils],
|
2021-03-17 18:46:45 +00:00
|
|
|
stew/results,
|
|
|
|
chronicles,
|
|
|
|
eth/p2p/discoveryv5/enr,
|
2021-10-21 11:01:29 +00:00
|
|
|
libp2p/[multiaddress, multicodec, peerstore],
|
2021-03-17 18:46:45 +00:00
|
|
|
nimcrypto/utils as ncrutils,
|
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
|
|
|
../spec/eth2_apis/rpc_types,
|
|
|
|
./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()
|
|
|
|
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]] =
|
|
|
|
let pinfo = node.network.switch.peerInfo
|
|
|
|
let mpa = MultiAddress.init(multicodec("p2p"), pinfo.peerId)
|
|
|
|
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
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/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]()
|
|
|
|
|
|
|
|
return RestApiResponse.jsonResponse(
|
2021-03-23 22:50:18 +00:00
|
|
|
(
|
2021-03-17 18:46:45 +00:00
|
|
|
peer_id: $node.network.peerId(),
|
|
|
|
enr: node.network.enrRecord().toUri(),
|
|
|
|
p2p_addresses: p2pAddresses,
|
|
|
|
discovery_addresses: discoveryAddresses,
|
2021-03-23 22:50:18 +00:00
|
|
|
metadata: (
|
|
|
|
seq_number: node.network.metadata.seq_number,
|
2021-12-01 15:01:18 +00:00
|
|
|
syncnets: "0x" & ncrutils.toHex(node.network.metadata.syncnets.bytes),
|
2021-03-23 22:50:18 +00:00
|
|
|
attnets: "0x" & ncrutils.toHex(node.network.metadata.attnets.bytes)
|
|
|
|
)
|
2021-03-17 18:46:45 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
# https://ethereum.github.io/beacon-APIs/#/Node/getPeers
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/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
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
var res: seq[RpcNodePeer]
|
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):
|
2021-03-17 18:46:45 +00:00
|
|
|
let peer = (
|
2021-10-21 11:01:29 +00:00
|
|
|
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(),
|
|
|
|
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
|
|
|
)
|
|
|
|
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
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/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
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/eth/v1/node/peers/{peer_id}") do (
|
|
|
|
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,
|
2021-03-17 18:46:45 +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
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/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
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/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
|
2021-03-17 18:46:45 +00:00
|
|
|
router.api(MethodGet, "/api/eth/v1/node/health") do () -> RestApiResponse:
|
|
|
|
# TODO: Add ability to detect node's issues and return 503 error according
|
|
|
|
# to specification.
|
|
|
|
let res =
|
|
|
|
if node.syncManager.inProgress:
|
|
|
|
(health: 206)
|
|
|
|
else:
|
|
|
|
(health: 200)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
2021-04-13 10:19:31 +00:00
|
|
|
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/identity",
|
|
|
|
"/api/eth/v1/node/identity"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/peers",
|
|
|
|
"/api/eth/v1/node/peers"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/peer_count",
|
|
|
|
"/api/eth/v1/node/peer_count"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/peers/{peer_id}",
|
|
|
|
"/api/eth/v1/node/peers/{peer_id}"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/version",
|
|
|
|
"/api/eth/v1/node/version"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/syncing",
|
|
|
|
"/api/eth/v1/node/syncing"
|
|
|
|
)
|
|
|
|
router.redirect(
|
|
|
|
MethodGet,
|
|
|
|
"/eth/v1/node/health",
|
|
|
|
"/api/eth/v1/node/health"
|
|
|
|
)
|