2023-05-25 13:57:24 +00:00
|
|
|
# beacon_chain
|
2023-05-15 05:05:12 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-03-23 22:50:18 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
import
|
2021-08-27 09:00:06 +00:00
|
|
|
std/[sequtils],
|
2023-07-21 14:54:09 +00:00
|
|
|
stew/[results, base10],
|
2021-03-23 22:50:18 +00:00
|
|
|
chronicles,
|
2023-07-21 14:54:09 +00:00
|
|
|
chronos/apps/http/httpdebug,
|
2021-10-21 11:01:29 +00:00
|
|
|
libp2p/[multiaddress, multicodec, peerstore],
|
2021-03-23 22:50:18 +00:00
|
|
|
libp2p/protocols/pubsub/pubsubpeer,
|
2021-08-03 15:17:11 +00:00
|
|
|
./rest_utils,
|
2023-05-15 05:05:12 +00:00
|
|
|
../el/el_manager,
|
2023-08-23 16:39:57 +00:00
|
|
|
../validators/beacon_validators,
|
2022-07-21 16:54:07 +00:00
|
|
|
../spec/[forks, beacon_time],
|
2021-10-19 14:09:26 +00:00
|
|
|
../beacon_node, ../nimbus_binary_common
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-10-27 12:01:11 +00:00
|
|
|
export rest_utils
|
|
|
|
|
|
|
|
when defined(chronosFutureTracking):
|
|
|
|
import stew/base10
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
logScope: topics = "rest_nimbusapi"
|
|
|
|
|
|
|
|
type
|
2021-05-20 17:56:12 +00:00
|
|
|
RestPeerInfo* = object
|
|
|
|
peerId*: string
|
|
|
|
addrs*: seq[string]
|
|
|
|
protocols*: seq[string]
|
|
|
|
protoVersion*: string
|
|
|
|
agentVersion*: string
|
|
|
|
|
2021-03-29 11:18:17 +00:00
|
|
|
RestPeerInfoTuple* = tuple
|
2021-03-23 22:50:18 +00:00
|
|
|
peerId: string
|
|
|
|
addrs: seq[string]
|
|
|
|
protocols: seq[string]
|
|
|
|
protoVersion: string
|
|
|
|
agentVersion: string
|
|
|
|
|
2021-05-20 17:56:12 +00:00
|
|
|
RestSimplePeer* = object
|
|
|
|
info*: RestPeerInfo
|
|
|
|
connectionState*: string
|
|
|
|
score*: int
|
|
|
|
|
|
|
|
RestFutureInfo* = object
|
2021-10-27 12:01:11 +00:00
|
|
|
id*: string
|
|
|
|
child_id*: string
|
2021-05-20 17:56:12 +00:00
|
|
|
procname*: string
|
|
|
|
filename*: string
|
|
|
|
line*: int
|
|
|
|
state*: string
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2022-10-14 12:19:17 +00:00
|
|
|
RestChronosMetricsInfo* = object
|
|
|
|
tcp_transports*: uint64
|
|
|
|
udp_transports*: uint64
|
|
|
|
tcp_servers*: uint64
|
|
|
|
stream_readers*: uint64
|
|
|
|
stream_writers*: uint64
|
|
|
|
http_client_connections*: uint64
|
|
|
|
http_client_requests*: uint64
|
|
|
|
http_client_responses*: uint64
|
2023-07-21 14:54:09 +00:00
|
|
|
http_server_secure_connections*: uint64
|
|
|
|
http_server_unsecure_connections*: uint64
|
|
|
|
http_server_requests*: uint64
|
|
|
|
http_server_responses*: uint64
|
2022-10-14 12:19:17 +00:00
|
|
|
http_body_readers*: uint64
|
|
|
|
http_body_writers*: uint64
|
|
|
|
|
2023-07-21 14:54:09 +00:00
|
|
|
RestConnectionInfo* = object
|
|
|
|
handle*: string
|
2023-08-15 11:14:34 +00:00
|
|
|
query*: string
|
2023-07-21 14:54:09 +00:00
|
|
|
connection_type*: string
|
|
|
|
connection_state*: string
|
|
|
|
remote_address*: string
|
|
|
|
local_address*: string
|
|
|
|
since_accept*: string
|
|
|
|
since_create*: string
|
|
|
|
|
2021-05-20 17:56:12 +00:00
|
|
|
RestPubSubPeer* = object
|
2022-04-08 16:22:49 +00:00
|
|
|
peerId*: PeerId
|
2021-05-20 17:56:12 +00:00
|
|
|
score*: float64
|
|
|
|
iHaveBudget*: int
|
|
|
|
outbound*: bool
|
|
|
|
appScore*: float64
|
|
|
|
behaviourPenalty*: float64
|
|
|
|
sendConnAvail*: bool
|
|
|
|
closed*: bool
|
|
|
|
atEof*: bool
|
|
|
|
address*: string
|
|
|
|
backoff*: string
|
|
|
|
agent*: string
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-05-20 17:56:12 +00:00
|
|
|
RestPeerStats* = object
|
2022-04-08 16:22:49 +00:00
|
|
|
peerId*: PeerId
|
2021-05-20 17:56:12 +00:00
|
|
|
null*: bool
|
|
|
|
connected*: bool
|
|
|
|
expire*: string
|
|
|
|
score*: float64
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-05-20 17:56:12 +00:00
|
|
|
RestPeerStatus* = object
|
2022-04-08 16:22:49 +00:00
|
|
|
peerId*: PeerId
|
2021-05-20 17:56:12 +00:00
|
|
|
connected*: bool
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2023-12-19 15:44:27 +00:00
|
|
|
RestJson.useDefaultSerializationFor(
|
|
|
|
BlockProposalEth1Data,
|
|
|
|
Eth1BlockObj,
|
|
|
|
RestChronosMetricsInfo,
|
|
|
|
RestConnectionInfo,
|
|
|
|
RestFutureInfo,
|
|
|
|
RestPeerInfo,
|
|
|
|
RestPeerInfoTuple,
|
|
|
|
RestPeerStats,
|
|
|
|
RestPeerStatus,
|
|
|
|
RestPubSubPeer,
|
|
|
|
RestSimplePeer,
|
|
|
|
)
|
|
|
|
|
2021-10-21 11:01:29 +00:00
|
|
|
proc toInfo(node: BeaconNode, peerId: PeerId): RestPeerInfo =
|
2021-05-20 17:56:12 +00:00
|
|
|
RestPeerInfo(
|
2021-10-21 11:01:29 +00:00
|
|
|
peerId: $peerId,
|
2022-06-08 05:53:50 +00:00
|
|
|
addrs: node.network.switch.peerStore[AddressBook][peerId].mapIt($it),
|
|
|
|
protocols: node.network.switch.peerStore[ProtoBook][peerId],
|
|
|
|
protoVersion: node.network.switch.peerStore[ProtoVersionBook][peerId],
|
|
|
|
agentVersion: node.network.switch.peerStore[AgentBook][peerId]
|
2021-05-20 17:56:12 +00:00
|
|
|
)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-05-20 17:56:12 +00:00
|
|
|
proc toNode(v: PubSubPeer, backoff: Moment): RestPubSubPeer =
|
|
|
|
RestPubSubPeer(
|
2021-03-23 22:50:18 +00:00
|
|
|
peerId: v.peerId,
|
|
|
|
score: v.score,
|
|
|
|
iHaveBudget: v.iHaveBudget,
|
|
|
|
outbound: v.outbound,
|
|
|
|
appScore: v.appScore,
|
|
|
|
behaviourPenalty: v.behaviourPenalty,
|
|
|
|
sendConnAvail: v.sendConn != nil,
|
|
|
|
closed: v.sendConn != nil and v.sendConn.closed,
|
|
|
|
atEof: v.sendConn != nil and v.sendConn.atEof,
|
|
|
|
address:
|
|
|
|
if v.address.isSome():
|
|
|
|
$v.address.get()
|
|
|
|
else:
|
|
|
|
"<no address>",
|
|
|
|
backoff: $(backoff - Moment.now()),
|
|
|
|
agent:
|
|
|
|
when defined(libp2p_agents_metrics):
|
|
|
|
v.shortAgent
|
|
|
|
else:
|
|
|
|
"unknown"
|
|
|
|
)
|
|
|
|
|
|
|
|
proc installNimbusApiHandlers*(router: var RestRouter, node: BeaconNode) =
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/beacon/head") do () -> RestApiResponse:
|
2021-06-01 11:13:40 +00:00
|
|
|
return RestApiResponse.jsonResponse(node.dag.head.slot)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/chain/head") do() -> RestApiResponse:
|
2021-03-23 22:50:18 +00:00
|
|
|
let
|
2021-06-01 11:13:40 +00:00
|
|
|
head = node.dag.head
|
2022-03-16 07:20:40 +00:00
|
|
|
finalized = getStateField(node.dag.headState, finalized_checkpoint)
|
2021-04-16 08:49:37 +00:00
|
|
|
justified =
|
2022-03-16 07:20:40 +00:00
|
|
|
getStateField(node.dag.headState, current_justified_checkpoint)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
|
|
|
(
|
|
|
|
head_slot: head.slot,
|
|
|
|
head_block_root: head.root.data.toHex(),
|
|
|
|
finalized_slot: finalized.epoch * SLOTS_PER_EPOCH,
|
|
|
|
finalized_block_root: finalized.root.data.toHex(),
|
|
|
|
justified_slot: justified.epoch * SLOTS_PER_EPOCH,
|
|
|
|
justified_block_root: justified.root.data.toHex()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/syncmanager/status") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
return RestApiResponse.jsonResponse(node.syncManager.inProgress)
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/node/peerid") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
return RestApiResponse.jsonResponse((peerid: $node.network.peerId()))
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/node/version") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
return RestApiResponse.jsonResponse((version: "Nimbus/" & fullVersionStr))
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/network/ids") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
2022-04-08 16:22:49 +00:00
|
|
|
var res: seq[PeerId]
|
2021-03-23 22:50:18 +00:00
|
|
|
for peerId, peer in node.network.peerPool:
|
|
|
|
res.add(peerId)
|
|
|
|
return RestApiResponse.jsonResponse((peerids: res))
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/network/peers") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: seq[RestSimplePeer]
|
2021-03-23 22:50:18 +00:00
|
|
|
for id, peer in node.network.peerPool:
|
2021-05-20 17:56:12 +00:00
|
|
|
res.add(
|
|
|
|
RestSimplePeer(
|
2021-10-21 11:01:29 +00:00
|
|
|
info: toInfo(node, id),
|
2021-05-20 17:56:12 +00:00
|
|
|
connectionState: $peer.connectionState,
|
|
|
|
score: peer.score
|
|
|
|
)
|
|
|
|
)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse((peers: res))
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodPost, "/nimbus/v1/graffiti") do (
|
2022-01-05 15:42:29 +00:00
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
if contentBody.isNone:
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
|
2022-08-06 11:55:40 +00:00
|
|
|
template setGraffitiAux(node: BeaconNode,
|
|
|
|
graffitiStr: string): RestApiResponse =
|
2022-01-05 15:42:29 +00:00
|
|
|
node.graffitiBytes = try:
|
|
|
|
GraffitiBytes.init(graffitiStr)
|
|
|
|
except CatchableError as err:
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidGraffitiBytesValue,
|
|
|
|
err.msg)
|
|
|
|
RestApiResponse.jsonResponse((result: true))
|
|
|
|
|
2022-08-06 11:55:40 +00:00
|
|
|
let body = contentBody.get()
|
|
|
|
if body.contentType == ApplicationJsonMediaType:
|
|
|
|
let graffitiBytes = decodeBody(GraffitiBytes, body)
|
|
|
|
if graffitiBytes.isErr():
|
2022-01-05 15:42:29 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidGraffitiBytesValue,
|
2022-08-06 11:55:40 +00:00
|
|
|
$graffitiBytes.error())
|
|
|
|
node.graffitiBytes = graffitiBytes.get()
|
2021-03-26 19:04:04 +00:00
|
|
|
return RestApiResponse.jsonResponse((result: true))
|
2022-08-06 11:55:40 +00:00
|
|
|
elif body.contentType == TextPlainMediaType:
|
|
|
|
return node.setGraffitiAux body.strData()
|
|
|
|
elif body.contentType == UrlEncodedMediaType:
|
|
|
|
return node.setGraffitiAux decodeUrl(body.strData())
|
2021-03-26 17:57:02 +00:00
|
|
|
else:
|
2022-01-05 15:42:29 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, "Unsupported content type: " &
|
2022-08-06 11:55:40 +00:00
|
|
|
$body.contentType)
|
2021-03-26 17:57:02 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/graffiti") do (
|
2021-03-26 19:04:04 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
return RestApiResponse.jsonResponse(node.graffitiBytes)
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodPost, "/nimbus/v1/chronicles/settings") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
log_level: Option[string]) -> RestApiResponse:
|
|
|
|
if log_level.isSome():
|
|
|
|
let level =
|
|
|
|
block:
|
|
|
|
let res = log_level.get()
|
|
|
|
if res.isErr():
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http400, InvalidLogLevelValueError,
|
2021-03-23 22:50:18 +00:00
|
|
|
$res.error())
|
|
|
|
res.get()
|
|
|
|
{.gcsafe.}:
|
|
|
|
updateLogLevel(level)
|
|
|
|
return RestApiResponse.jsonResponse((result: true))
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/eth1/chain") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
2023-03-05 01:40:21 +00:00
|
|
|
let res = mapIt(node.elManager.eth1ChainBlocks, it)
|
2021-03-23 22:50:18 +00:00
|
|
|
return RestApiResponse.jsonResponse(res)
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/eth1/proposal_data") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
let wallSlot = node.beaconClock.now.slotOrZero
|
|
|
|
let head =
|
|
|
|
block:
|
2022-03-23 11:42:16 +00:00
|
|
|
let res = node.getSyncedHead(wallSlot)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isErr():
|
2023-02-23 23:13:17 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError,
|
|
|
|
$res.error())
|
|
|
|
let tres = res.get()
|
2023-05-25 13:57:24 +00:00
|
|
|
if not tres.executionValid:
|
2021-04-08 14:34:05 +00:00
|
|
|
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
|
2023-05-25 13:57:24 +00:00
|
|
|
tres
|
2021-06-01 11:13:40 +00:00
|
|
|
let proposalState = assignClone(node.dag.headState)
|
2022-03-29 07:15:42 +00:00
|
|
|
node.dag.withUpdatedState(
|
|
|
|
proposalState[],
|
|
|
|
head.atSlot(wallSlot).toBlockSlotId().expect("not nil")):
|
2021-04-14 09:34:35 +00:00
|
|
|
return RestApiResponse.jsonResponse(
|
2022-11-30 14:37:23 +00:00
|
|
|
node.getBlockProposalEth1Data(updatedState))
|
2022-01-05 18:38:04 +00:00
|
|
|
do:
|
|
|
|
return RestApiResponse.jsonError(Http400, PrunedStateError)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/debug/chronos/futures") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
when defined(chronosFutureTracking):
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: seq[RestFutureInfo]
|
2021-03-23 22:50:18 +00:00
|
|
|
for item in pendingFutures():
|
|
|
|
let loc = item.location[LocCreateIndex][]
|
2021-10-27 12:01:11 +00:00
|
|
|
let futureId = Base10.toString(item.id)
|
|
|
|
let childId =
|
|
|
|
if isNil(item.child): ""
|
|
|
|
else: Base10.toString(item.child.id)
|
2021-03-23 22:50:18 +00:00
|
|
|
res.add(
|
2021-05-20 17:56:12 +00:00
|
|
|
RestFutureInfo(
|
2021-10-27 12:01:11 +00:00
|
|
|
id: futureId,
|
|
|
|
child_id: childId,
|
2021-03-23 22:50:18 +00:00
|
|
|
procname: $loc.procedure,
|
|
|
|
filename: $loc.file,
|
|
|
|
line: loc.line,
|
|
|
|
state: $item.state
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return RestApiResponse.jsonResponse(res)
|
|
|
|
else:
|
|
|
|
return RestApiResponse.jsonError(Http503,
|
|
|
|
"Compile with '-d:chronosFutureTracking' to get this request working")
|
|
|
|
|
2022-10-14 12:19:17 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/debug/chronos/metrics") do (
|
|
|
|
) -> RestApiResponse:
|
|
|
|
|
2023-07-21 14:54:09 +00:00
|
|
|
template getCount(name: string): uint64 =
|
|
|
|
let res = getTrackerCounter(name)
|
|
|
|
uint64(res.opened - res.closed)
|
2022-10-14 12:19:17 +00:00
|
|
|
|
|
|
|
let res = RestChronosMetricsInfo(
|
2023-07-21 14:54:09 +00:00
|
|
|
tcp_transports: getCount(StreamTransportTrackerName),
|
|
|
|
udp_transports: getCount(DgramTransportTrackerName),
|
|
|
|
tcp_servers: getCount(StreamServerTrackerName),
|
|
|
|
stream_readers: getCount(AsyncStreamReaderTrackerName),
|
|
|
|
stream_writers: getCount(AsyncStreamWriterTrackerName),
|
|
|
|
http_client_connections: getCount(HttpClientConnectionTrackerName),
|
|
|
|
http_client_requests: getCount(HttpClientRequestTrackerName),
|
|
|
|
http_client_responses: getCount(HttpClientResponseTrackerName),
|
|
|
|
http_server_secure_connections:
|
|
|
|
getCount(HttpServerSecureConnectionTrackerName),
|
|
|
|
http_server_unsecure_connections:
|
|
|
|
getCount(HttpServerUnsecureConnectionTrackerName),
|
|
|
|
http_server_requests: getCount(HttpServerRequestTrackerName),
|
|
|
|
http_server_responses: getCount(HttpServerResponseTrackerName),
|
|
|
|
http_body_readers: getCount(HttpBodyReaderTrackerName),
|
|
|
|
http_body_writers: getCount(HttpBodyWriterTrackerName)
|
2022-10-14 12:19:17 +00:00
|
|
|
)
|
|
|
|
return RestApiResponse.jsonResponse(res)
|
|
|
|
|
2023-07-21 14:54:09 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/debug/chronos/restserver/connections") do (
|
|
|
|
) -> RestApiResponse:
|
|
|
|
var res: seq[RestConnectionInfo]
|
|
|
|
for connection in node.restServer.server.getConnections():
|
|
|
|
let
|
|
|
|
connectionState =
|
|
|
|
case connection.connectionState
|
|
|
|
of httpdebug.ConnectionState.Accepted: "accepted"
|
|
|
|
of httpdebug.ConnectionState.Alive: "alive"
|
|
|
|
of httpdebug.ConnectionState.Closing: "closing"
|
|
|
|
of httpdebug.ConnectionState.Closed: "closed"
|
|
|
|
connectionType =
|
|
|
|
case connection.connectionType
|
|
|
|
of ConnectionType.Secure: "secure"
|
|
|
|
of ConnectionType.NonSecure: "non-secure"
|
|
|
|
localAddress =
|
|
|
|
if connection.localAddress.isNone():
|
|
|
|
"not available"
|
|
|
|
else:
|
|
|
|
$connection.localAddress.get()
|
|
|
|
remoteAddress =
|
|
|
|
if connection.remoteAddress.isNone():
|
|
|
|
"not available"
|
|
|
|
else:
|
|
|
|
$connection.remoteAddress.get()
|
2023-08-15 11:14:34 +00:00
|
|
|
query = connection.query.get("not available")
|
2023-07-21 14:54:09 +00:00
|
|
|
handle = Base10.toString(uint64(connection.handle))
|
|
|
|
moment = Moment.now()
|
|
|
|
sinceAccept = $(moment - connection.acceptMoment)
|
|
|
|
sinceCreate =
|
|
|
|
if connection.createMoment.isSome():
|
|
|
|
$(moment - connection.createMoment.get())
|
|
|
|
else:
|
|
|
|
"not available"
|
|
|
|
|
|
|
|
res.add(
|
|
|
|
RestConnectionInfo(
|
|
|
|
handle: handle,
|
2023-08-15 11:14:34 +00:00
|
|
|
query: query,
|
2023-07-21 14:54:09 +00:00
|
|
|
connection_state: connectionState,
|
|
|
|
connection_type: connectionType,
|
|
|
|
local_address: localAddress,
|
|
|
|
remote_address: remoteAddress,
|
|
|
|
since_accept: sinceAccept,
|
|
|
|
since_create: sinceCreate
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return RestApiResponse.jsonResponse(res)
|
|
|
|
|
2022-07-21 16:54:07 +00:00
|
|
|
router.api(MethodPost, "/nimbus/v1/validator/activity/{epoch}") do (
|
|
|
|
epoch: Epoch, contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let indexList =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
let dres = decodeBody(seq[RestValidatorIndex], contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidValidatorIndexValueError,
|
|
|
|
$dres.error())
|
2022-10-14 12:19:17 +00:00
|
|
|
var
|
|
|
|
res: seq[ValidatorIndex]
|
|
|
|
dupset: HashSet[ValidatorIndex]
|
|
|
|
|
2022-07-21 16:54:07 +00:00
|
|
|
let items = dres.get()
|
|
|
|
for item in items:
|
|
|
|
let vres = item.toValidatorIndex()
|
|
|
|
if vres.isErr():
|
|
|
|
case vres.error()
|
|
|
|
of ValidatorIndexError.TooHighValue:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
TooHighValidatorIndexValueError)
|
|
|
|
of ValidatorIndexError.UnsupportedValue:
|
|
|
|
return RestApiResponse.jsonError(Http500,
|
|
|
|
UnsupportedValidatorIndexValueError)
|
2022-10-14 12:19:17 +00:00
|
|
|
let index = vres.get()
|
|
|
|
if index in dupset:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
DuplicateValidatorIndexArrayError)
|
|
|
|
dupset.incl(index)
|
|
|
|
res.add(index)
|
2022-07-21 16:54:07 +00:00
|
|
|
if len(res) == 0:
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
EmptyValidatorIndexArrayError)
|
|
|
|
res
|
|
|
|
let qepoch =
|
|
|
|
block:
|
|
|
|
if epoch.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
|
|
|
$epoch.error())
|
|
|
|
let
|
|
|
|
res = epoch.get()
|
|
|
|
wallEpoch = node.currentSlot().epoch()
|
|
|
|
nextEpoch =
|
|
|
|
if wallEpoch == FAR_FUTURE_EPOCH:
|
|
|
|
wallEpoch
|
|
|
|
else:
|
|
|
|
wallEpoch + 1
|
|
|
|
prevEpoch = get_previous_epoch(wallEpoch)
|
|
|
|
if (res < prevEpoch) or (res > nextEpoch):
|
|
|
|
return RestApiResponse.jsonError(Http400, InvalidEpochValueError,
|
|
|
|
"Requested epoch is more than one epoch from current epoch")
|
|
|
|
res
|
|
|
|
let response = indexList.mapIt(
|
|
|
|
RestActivityItem(
|
|
|
|
index: it,
|
|
|
|
epoch: qepoch,
|
|
|
|
active: node.attestationPool[].validatorSeenAtEpoch(qepoch, it)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return RestApiResponse.jsonResponse(response)
|
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
router.api(MethodGet, "/nimbus/v1/debug/gossip/peers") do (
|
2021-03-23 22:50:18 +00:00
|
|
|
) -> RestApiResponse:
|
|
|
|
|
|
|
|
let gossipPeers =
|
|
|
|
block:
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: seq[tuple[topic: string, peers: seq[RestPubSubPeer]]]
|
2021-03-23 22:50:18 +00:00
|
|
|
for topic, v in node.network.pubsub.gossipsub:
|
2021-05-20 17:56:12 +00:00
|
|
|
var peers: seq[RestPubSubPeer]
|
2021-03-23 22:50:18 +00:00
|
|
|
let backoff = node.network.pubsub.backingOff.getOrDefault(topic)
|
|
|
|
for peer in v:
|
2022-04-08 16:22:49 +00:00
|
|
|
peers.add(peer.toNode(backoff.getOrDefault(peer.peerId)))
|
2021-03-23 22:50:18 +00:00
|
|
|
res.add((topic: topic, peers: peers))
|
|
|
|
res
|
|
|
|
let meshPeers =
|
|
|
|
block:
|
2021-05-20 17:56:12 +00:00
|
|
|
var res: seq[tuple[topic: string, peers: seq[RestPubSubPeer]]]
|
2021-03-23 22:50:18 +00:00
|
|
|
for topic, v in node.network.pubsub.mesh:
|
2021-05-20 17:56:12 +00:00
|
|
|
var peers: seq[RestPubSubPeer]
|
2021-03-23 22:50:18 +00:00
|
|
|
let backoff = node.network.pubsub.backingOff.getOrDefault(topic)
|
|
|
|
for peer in v:
|
2022-04-08 16:22:49 +00:00
|
|
|
peers.add(peer.toNode(backoff.getOrDefault(peer.peerId)))
|
2021-03-23 22:50:18 +00:00
|
|
|
res.add((topic: topic, peers: peers))
|
|
|
|
res
|
|
|
|
let colocationPeers =
|
|
|
|
block:
|
2022-04-08 16:22:49 +00:00
|
|
|
var res: seq[tuple[address: string, peerids: seq[PeerId]]]
|
2021-03-23 22:50:18 +00:00
|
|
|
for k, v in node.network.pubsub.peersInIP:
|
2022-04-08 16:22:49 +00:00
|
|
|
var peerids: seq[PeerId]
|
2021-03-23 22:50:18 +00:00
|
|
|
for id in v:
|
|
|
|
peerids.add(id)
|
|
|
|
res.add(($k, peerids))
|
|
|
|
res
|
|
|
|
let peerStats =
|
|
|
|
block:
|
2021-05-20 17:56:12 +00:00
|
|
|
var stats: seq[RestPeerStats]
|
2021-03-23 22:50:18 +00:00
|
|
|
for peerId, pstats in node.network.pubsub.peerStats:
|
|
|
|
let peer = node.network.pubsub.peers.getOrDefault(peerId)
|
|
|
|
stats.add(
|
2021-05-20 17:56:12 +00:00
|
|
|
RestPeerStats(
|
2021-03-23 22:50:18 +00:00
|
|
|
peerId: peerId,
|
|
|
|
null: isNil(peer),
|
|
|
|
connected: if isNil(peer): false else: peer.connected(),
|
|
|
|
expire: $(pstats.expire - Moment.now()),
|
|
|
|
score: pstats.score
|
|
|
|
)
|
|
|
|
)
|
|
|
|
stats
|
|
|
|
let allPeers =
|
|
|
|
block:
|
2021-05-20 17:56:12 +00:00
|
|
|
var peers: seq[RestPeerStatus]
|
2021-03-23 22:50:18 +00:00
|
|
|
for peerId, peer in node.network.pubsub.peers:
|
2021-05-20 17:56:12 +00:00
|
|
|
peers.add(RestPeerStatus(peerId: peerId, connected: peer.connected))
|
2021-03-23 22:50:18 +00:00
|
|
|
peers
|
|
|
|
return RestApiResponse.jsonResponse(
|
|
|
|
(
|
|
|
|
gossip_peers: gossipPeers,
|
|
|
|
mesh_peers: meshPeers,
|
|
|
|
colocation_peers: colocationPeers,
|
|
|
|
peer_stats: peerStats,
|
|
|
|
all_peers: allPeers
|
|
|
|
)
|
|
|
|
)
|
2023-06-28 13:33:07 +00:00
|
|
|
|
|
|
|
router.api(MethodPost, "/nimbus/v1/timesync") do (
|
|
|
|
contentBody: Option[ContentBody]) -> RestApiResponse:
|
|
|
|
let
|
|
|
|
timestamp2 = getTimestamp()
|
|
|
|
timestamp1 =
|
|
|
|
block:
|
|
|
|
if contentBody.isNone():
|
|
|
|
return RestApiResponse.jsonError(Http400, EmptyRequestBodyError)
|
|
|
|
let dres = decodeBody(RestNimbusTimestamp1, contentBody.get())
|
|
|
|
if dres.isErr():
|
|
|
|
return RestApiResponse.jsonError(Http400,
|
|
|
|
InvalidTimestampValue,
|
|
|
|
$dres.error())
|
|
|
|
dres.get().timestamp1
|
|
|
|
let
|
|
|
|
delay = node.processingDelay.valueOr: ZeroDuration
|
|
|
|
response = RestNimbusTimestamp2(
|
|
|
|
timestamp1: timestamp1,
|
|
|
|
timestamp2: timestamp2,
|
|
|
|
timestamp3: getTimestamp(),
|
|
|
|
delay: uint64(delay.nanoseconds)
|
|
|
|
)
|
|
|
|
return RestApiResponse.jsonResponsePlain(response)
|