nimbus-eth2/beacon_chain/rpc/rpc_nimbus_api.nim

231 lines
6.7 KiB
Nim
Raw Normal View History

# beacon_chain
# Copyright (c) 2018-2021 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.
{.push raises: [Defect].}
import
std/[deques, sequtils, sets],
chronos,
stew/byteutils,
json_rpc/servers/httpserver,
libp2p/protocols/pubsub/pubsubpeer,
".."/[
beacon_node, nimbus_binary_common, networking/eth2_network,
eth1/eth1_monitor, validators/validator_duties],
../spec/datatypes/base,
../spec/[forks],
./rpc_utils
2020-11-29 19:07:20 +00:00
when defined(chronosFutureTracking):
import stew/base10
logScope: topics = "nimbusapi"
type
RpcServer = RpcHttpServer
2020-11-29 19:07:20 +00:00
FutureInfo* = object
id*: string
child_id*: string
2020-11-29 19:07:20 +00:00
procname*: string
filename*: string
line*: int
state*: string
proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
raises: [Defect, CatchableError].} =
## Install non-standard api handlers - some of these are used by 3rd-parties
## such as eth2stats, pending a full REST api
rpcServer.rpc("getBeaconHead") do () -> Slot:
return node.dag.head.slot
rpcServer.rpc("getChainHead") do () -> JsonNode:
let
head = node.dag.head
finalized = getStateField(node.dag.headState, finalized_checkpoint)
justified =
getStateField(node.dag.headState, 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:
return node.syncManager.inProgress
rpcServer.rpc("getNetworkPeerId") do () -> string:
return $node.network.peerId()
rpcServer.rpc("getNetworkPeers") do () -> seq[string]:
for peerId, peer in node.network.peerPool:
result.add $peerId
rpcServer.rpc("getNodeVersion") do () -> string:
return "Nimbus/" & fullVersionStr
rpcServer.rpc("peers") do () -> JsonNode:
var res = newJObject()
var peers = newJArray()
for id, peer in node.network.peerPool:
peers.add(
%(
id: shortLog(peer.peerId),
connectionState: $peer.connectionState,
score: peer.score,
)
)
res.add("peers", peers)
return res
rpcServer.rpc("setLogLevel") do (level: string) -> bool:
{.gcsafe.}: # It's probably not, actually. Hopefully we don't log from threads...
updateLogLevel(level)
return true
rpcServer.rpc("setGraffiti") do (graffiti: string) -> bool:
node.graffitiBytes = GraffitiBytes.init(graffiti)
return true
2020-11-20 14:42:04 +00:00
rpcServer.rpc("getEth1Chain") do () -> seq[Eth1Block]:
result = if node.eth1Monitor != nil:
mapIt(node.eth1Monitor.depositChainBlocks, it)
2020-11-20 14:42:04 +00:00
else:
@[]
rpcServer.rpc("getEth1ProposalData") do () -> BlockProposalEth1Data:
let
wallSlot = node.beaconClock.now.slotOrZero
head = node.doChecksAndGetCurrentHead(wallSlot)
let proposalState = assignClone(node.dag.headState)
Prune `BlockRef` on finalization (#3513) Up til now, the block dag has been using `BlockRef`, a structure adapted for a full DAG, to represent all of chain history. This is a correct and simple design, but does not exploit the linearity of the chain once parts of it finalize. By pruning the in-memory `BlockRef` structure at finalization, we save, at the time of writing, a cool ~250mb (or 25%:ish) chunk of memory landing us at a steady state of ~750mb normal memory usage for a validating node. Above all though, we prevent memory usage from growing proportionally with the length of the chain, something that would not be sustainable over time - instead, the steady state memory usage is roughly determined by the validator set size which grows much more slowly. With these changes, the core should remain sustainable memory-wise post-merge all the way to withdrawals (when the validator set is expected to grow). In-memory indices are still used for the "hot" unfinalized portion of the chain - this ensure that consensus performance remains unchanged. What changes is that for historical access, we use a db-based linear slot index which is cache-and-disk-friendly, keeping the cost for accessing historical data at a similar level as before, achieving the savings at no percievable cost to functionality or performance. A nice collateral benefit is the almost-instant startup since we no longer load any large indicies at dag init. The cost of this functionality instead can be found in the complexity of having to deal with two ways of traversing the chain - by `BlockRef` and by slot. * use `BlockId` instead of `BlockRef` where finalized / historical data may be required * simplify clearance pre-advancement * remove dag.finalizedBlocks (~50:ish mb) * remove `getBlockAtSlot` - use `getBlockIdAtSlot` instead * `parent` and `atSlot` for `BlockId` now require a `ChainDAGRef` instance, unlike `BlockRef` traversal * prune `BlockRef` parents on finality (~200:ish mb) * speed up ChainDAG init by not loading finalized history index * mess up light client server error handling - this need revisiting :)
2022-03-17 17:42:56 +00:00
node.dag.withUpdatedState(
proposalState[],
head.atSlot(wallSlot).toBlockSlotId().expect("not nil")):
return node.getBlockProposalEth1Data(state)
do:
raise (ref CatchableError)(msg: "Trying to access pruned state")
2020-11-20 14:42:04 +00:00
rpcServer.rpc("debug_getChronosFutures") do () -> seq[FutureInfo]:
2020-11-29 19:07:20 +00:00
when defined(chronosFutureTracking):
var res: seq[FutureInfo]
for item in pendingFutures():
let loc = item.location[LocCreateIndex][]
let futureId = Base10.toString(item.id)
let childId =
if isNil(item.child): ""
else: Base10.toString(item.child.id)
res.add FutureInfo(
id: futureId,
child_id: childId,
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("debug_getGossipSubPeers") do () -> JsonNode:
2020-11-29 19:07:20 +00:00
var res = newJObject()
var gossipsub = newJObject()
proc toNode(v: PubSubPeer, backoff: Moment): JsonNode =
2020-11-29 19:07:20 +00:00
%(
peerId: $v.peerId,
score: v.score,
iWantBudget: v.iWantBudget,
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",
2020-11-29 19:07:20 +00:00
)
for topic, v in node.network.pubsub.gossipsub:
var peers = newJArray()
let backoff = node.network.pubsub.backingOff.getOrDefault(topic)
2020-11-29 19:07:20 +00:00
for peer in v:
peers.add(peer.toNode(backOff.getOrDefault(peer.peerId)))
2020-11-29 19:07:20 +00:00
gossipsub.add(topic, peers)
res.add("gossipsub", gossipsub)
var mesh = newJObject()
for topic, v in node.network.pubsub.mesh:
var peers = newJArray()
let backoff = node.network.pubsub.backingOff.getOrDefault(topic)
2020-11-29 19:07:20 +00:00
for peer in v:
peers.add(peer.toNode(backOff.getOrDefault(peer.peerId)))
2020-11-29 19:07:20 +00:00
mesh.add(topic, peers)
res.add("mesh", mesh)
var coloc = newJArray()
for k, v in node.network.pubsub.peersInIP:
var a = newJObject()
var peers = newJArray()
for p in v:
peers.add(%($p))
a.add($k, peers)
coloc.add(a)
res.add("colocationPeers", coloc)
var stats = newJArray()
for peerId, pstats in node.network.pubsub.peerStats:
let
peer = node.network.pubsub.peers.getOrDefault(peerId)
null = isNil(peer)
connected = if null:
false
else :
peer.connected()
stats.add(%(
peerId: $peerId,
null: null,
connected: connected,
expire: $(pstats.expire - Moment.now()),
score: pstats.score
))
res.add("peerStats", stats)
var peers = newJArray()
for peerId, peer in node.network.pubsub.peers:
peers.add(%(
connected: peer.connected,
peerId: $peerId
))
res.add("allPeers", peers)
2020-11-29 19:07:20 +00:00
return res