2020-10-27 09:00:57 +00:00
|
|
|
# Copyright (c) 2018-2020 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.
|
|
|
|
|
|
|
|
import
|
2020-11-29 19:07:20 +00:00
|
|
|
std/[deques, sequtils],
|
2020-11-15 15:35:16 +00:00
|
|
|
chronos,
|
2020-10-27 09:00:57 +00:00
|
|
|
stew/shims/macros,
|
|
|
|
stew/byteutils,
|
|
|
|
json_rpc/[rpcserver, jsonmarshal],
|
|
|
|
|
2020-11-20 14:42:04 +00:00
|
|
|
rpc_utils,
|
2020-11-12 10:46:02 +00:00
|
|
|
../beacon_node_common, ../nimbus_binary_common, ../eth2_network,
|
2020-11-20 14:42:04 +00:00
|
|
|
../eth1_monitor, ../validator_duties,
|
2020-11-29 19:07:20 +00:00
|
|
|
../spec/[digest, datatypes, presets],
|
|
|
|
|
|
|
|
libp2p/protocols/pubsub/[gossipsub, pubsubpeer]
|
|
|
|
|
2020-10-27 09:00:57 +00:00
|
|
|
|
|
|
|
logScope: topics = "nimbusapi"
|
|
|
|
|
|
|
|
type
|
|
|
|
RpcServer = RpcHttpServer
|
|
|
|
|
2020-11-29 19:07:20 +00:00
|
|
|
FutureInfo* = object
|
|
|
|
id*: int
|
|
|
|
procname*: string
|
|
|
|
filename*: string
|
|
|
|
line*: int
|
|
|
|
state*: string
|
2020-11-15 15:35:16 +00:00
|
|
|
|
2020-10-27 09:00:57 +00:00
|
|
|
proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
|
2020-10-28 18:51:38 +00:00
|
|
|
## Install non-standard api handlers - some of these are used by 3rd-parties
|
|
|
|
## such as eth2stats, pending a full REST api
|
2020-10-27 09:00:57 +00:00
|
|
|
rpcServer.rpc("getBeaconHead") do () -> Slot:
|
|
|
|
return node.chainDag.head.slot
|
|
|
|
|
|
|
|
rpcServer.rpc("getChainHead") do () -> JsonNode:
|
|
|
|
let
|
|
|
|
head = node.chainDag.head
|
|
|
|
finalized = node.chainDag.headState.data.data.finalized_checkpoint
|
|
|
|
justified = node.chainDag.headState.data.data.current_justified_checkpoint
|
|
|
|
return %* {
|
|
|
|
"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(),
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcServer.rpc("getSyncing") do () -> bool:
|
2020-10-28 18:51:38 +00:00
|
|
|
return node.syncManager.inProgress
|
2020-10-27 09:00:57 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("getNetworkPeerId") do () -> string:
|
2020-10-28 18:51:38 +00:00
|
|
|
return $node.network.peerId()
|
2020-10-27 09:00:57 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("getNetworkPeers") do () -> seq[string]:
|
|
|
|
for peerId, peer in node.network.peerPool:
|
|
|
|
result.add $peerId
|
|
|
|
|
2020-10-28 18:51:38 +00:00
|
|
|
rpcServer.rpc("getNodeVersion") do () -> string:
|
|
|
|
return "Nimbus/" & fullVersionStr
|
|
|
|
|
2020-10-27 09:00:57 +00:00
|
|
|
rpcServer.rpc("peers") do () -> JsonNode:
|
|
|
|
var res = newJObject()
|
|
|
|
var peers = newJArray()
|
|
|
|
for id, peer in node.network.peerPool:
|
|
|
|
peers.add(
|
|
|
|
%(
|
|
|
|
info: shortLog(peer.info),
|
|
|
|
connectionState: $peer.connectionState,
|
|
|
|
score: peer.score,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
res.add("peers", peers)
|
|
|
|
|
|
|
|
return res
|
2020-11-12 10:46:02 +00:00
|
|
|
|
|
|
|
rpcServer.rpc("setLogLevel") do (level: string) -> bool:
|
|
|
|
{.gcsafe.}: # It's probably not, actually. Hopefully we don't log from threads...
|
|
|
|
updateLogLevel(level)
|
|
|
|
return true
|
2020-11-15 15:35:16 +00:00
|
|
|
|
2020-11-20 14:42:04 +00:00
|
|
|
rpcServer.rpc("getEth1Chain") do () -> seq[Eth1Block]:
|
|
|
|
result = if node.eth1Monitor != nil:
|
|
|
|
mapIt(node.eth1Monitor.blocks, it)
|
|
|
|
else:
|
|
|
|
@[]
|
|
|
|
|
|
|
|
rpcServer.rpc("getEth1ProposalData") do () -> BlockProposalEth1Data:
|
|
|
|
let
|
|
|
|
wallSlot = node.beaconClock.now.slotOrZero
|
|
|
|
head = node.doChecksAndGetCurrentHead(wallSlot)
|
|
|
|
|
|
|
|
node.chainDag.withState(node.chainDag.tmpState, head.atSlot(wallSlot)):
|
|
|
|
return node.getBlockProposalEth1Data(state)
|
|
|
|
|
2020-11-29 19:07:20 +00:00
|
|
|
rpcServer.rpc("getChronosFutures") do () -> seq[FutureInfo]:
|
|
|
|
when defined(chronosFutureTracking):
|
2020-11-15 15:35:16 +00:00
|
|
|
var res: seq[FutureInfo]
|
|
|
|
|
|
|
|
for item in pendingFutures():
|
|
|
|
let loc = item.location[LocCreateIndex][]
|
|
|
|
res.add FutureInfo(
|
|
|
|
id: item.id,
|
|
|
|
procname: $loc.procedure,
|
|
|
|
filename: $loc.file,
|
|
|
|
line: loc.line,
|
|
|
|
state: $item.state
|
|
|
|
)
|
|
|
|
|
|
|
|
return res
|
2020-11-29 19:07:20 +00:00
|
|
|
else:
|
|
|
|
raise (ref CatchableError)(
|
|
|
|
msg: "Compile with '-d:chronosFutureTracking' to enable this request")
|
|
|
|
|
|
|
|
rpcServer.rpc("getGossipSubPeers") do () -> JsonNode:
|
|
|
|
var res = newJObject()
|
|
|
|
var gossipsub = newJObject()
|
|
|
|
|
|
|
|
proc toNode(v: PubSubPeer): JsonNode =
|
|
|
|
%(
|
|
|
|
peerId: $v.peerId,
|
|
|
|
score: v.score,
|
|
|
|
iWantBudget: v.iWantBudget,
|
|
|
|
iHaveBudget: v.iHaveBudget,
|
|
|
|
outbound: v.outbound,
|
|
|
|
appScore: v.appScore,
|
|
|
|
behaviourPenalty: v.behaviourPenalty
|
|
|
|
)
|
|
|
|
|
|
|
|
for topic, v in node.network.pubsub.gossipsub:
|
|
|
|
var peers = newJArray()
|
|
|
|
for peer in v:
|
|
|
|
peers.add(peer.toNode())
|
|
|
|
|
|
|
|
gossipsub.add(topic, peers)
|
|
|
|
|
|
|
|
res.add("gossipsub", gossipsub)
|
|
|
|
|
|
|
|
var mesh = newJObject()
|
|
|
|
for topic, v in node.network.pubsub.mesh:
|
|
|
|
var peers = newJArray()
|
|
|
|
for peer in v:
|
|
|
|
peers.add(peer.toNode())
|
|
|
|
|
|
|
|
mesh.add(topic, peers)
|
|
|
|
|
|
|
|
res.add("mesh", mesh)
|
|
|
|
|
|
|
|
return res
|