minor api cleanups (#1913)

* readd getNodeVersion
* implement get_v1_config_fork_schedule
* fix formatting of several types
This commit is contained in:
Jacek Sieka 2020-10-28 19:51:38 +01:00 committed by GitHub
parent 19c51e5900
commit 020a32ffa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 24 deletions

View File

@ -1,20 +1,24 @@
import import
# Standard library # Standard library
tables, json, std/[tables, json, typetraits],
# Nimble packages # Nimble packages
stew/byteutils, ssz/types, stew/byteutils, ssz/types,
json_rpc/jsonmarshal, json_rpc/jsonmarshal,
# Local modules # Local modules
spec/[datatypes, crypto] spec/[datatypes, crypto, digest]
proc toJsonHex(data: openArray[byte]): string =
# Per the eth2 API spec, hex arrays are printed with leading 0x
"0x" & toHex(data)
proc fromJson*(n: JsonNode, argName: string, result: var ValidatorPubKey) = proc fromJson*(n: JsonNode, argName: string, result: var ValidatorPubKey) =
n.kind.expect(JString, argName) n.kind.expect(JString, argName)
result = ValidatorPubKey.fromHex(n.getStr()).tryGet().initPubKey() result = initPubKey(ValidatorPubKey.fromHex(n.getStr()).tryGet().initPubKey())
proc `%`*(pubkey: ValidatorPubKey): JsonNode = proc `%`*(pubkey: ValidatorPubKey): JsonNode =
result = newJString($initPubKey(pubkey)) newJString(toJsonHex(toRaw(pubkey)))
proc fromJson*(n: JsonNode, argName: string, result: var List) = proc fromJson*(n: JsonNode, argName: string, result: var List) =
fromJson(n, argName, asSeq result) fromJson(n, argName, asSeq result)
@ -31,19 +35,19 @@ proc fromJson*(n: JsonNode, argName: string, result: var ValidatorSig) =
result = ValidatorSig.fromHex(n.getStr()).tryGet() result = ValidatorSig.fromHex(n.getStr()).tryGet()
proc `%`*(value: ValidatorSig): JsonNode = proc `%`*(value: ValidatorSig): JsonNode =
result = newJString($value) newJString(toJsonHex(toRaw(value)))
proc fromJson*(n: JsonNode, argName: string, result: var Version) = proc fromJson*(n: JsonNode, argName: string, result: var Version) =
n.kind.expect(JString, argName) n.kind.expect(JString, argName)
hexToByteArray(n.getStr(), array[4, byte](result)) hexToByteArray(n.getStr(), array[4, byte](result))
proc `%`*(value: Version): JsonNode = proc `%`*(value: Version): JsonNode =
result = newJString($value) newJString(toJsonHex(distinctBase(value)))
template genFromJsonForIntType(T: untyped) = template genFromJsonForIntType(T: untyped) =
proc fromJson*(n: JsonNode, argName: string, result: var T) = proc fromJson*(n: JsonNode, argName: string, result: var T) =
n.kind.expect(JInt, argName) n.kind.expect(JInt, argName)
let asInt = n.getInt() let asInt = n.getBiggestInt()
# signed -> unsigned conversions are unchecked # signed -> unsigned conversions are unchecked
# https://github.com/nim-lang/RFCs/issues/175 # https://github.com/nim-lang/RFCs/issues/175
if asInt < 0: if asInt < 0:
@ -56,15 +60,22 @@ genFromJsonForIntType(Slot)
genFromJsonForIntType(CommitteeIndex) genFromJsonForIntType(CommitteeIndex)
genFromJsonForIntType(ValidatorIndex) genFromJsonForIntType(ValidatorIndex)
template `%`*(value: GraffitiBytes): JsonNode = proc `%`*(value: GraffitiBytes): JsonNode =
%($value) newJString(toJsonHex(distinctBase(value)))
proc fromJson*(n: JsonNode, argName: string, value: var GraffitiBytes) = proc fromJson*(n: JsonNode, argName: string, value: var GraffitiBytes) =
n.kind.expect(JString, argName) n.kind.expect(JString, argName)
value = GraffitiBytes.init n.getStr() value = GraffitiBytes.init n.getStr()
proc `%`*(value: CommitteeIndex): JsonNode = proc `%`*(value: CommitteeIndex): JsonNode =
result = newJInt(value.int) newJInt(value.BiggestInt)
proc `%`*(value: ValidatorIndex): JsonNode = proc `%`*(value: ValidatorIndex): JsonNode =
result = newJInt(value.int) newJInt(value.BiggestInt)
proc `%`*(value: Eth2Digest): JsonNode =
newJString(toJsonHex(value.data))
proc fromJson*(n: JsonNode, argName: string, result: var Eth2Digest) =
n.kind.expect(JString, argName)
hexToByteArray(n.getStr(), result.data)

View File

@ -7,7 +7,9 @@
import import
json_rpc/[rpcserver, jsonmarshal], json_rpc/[rpcserver, jsonmarshal],
chronicles, chronicles,
../beacon_node_common ../beacon_node_common,
../spec/datatypes, ../eth2_json_rpc_serialization
logScope: topics = "configapi" logScope: topics = "configapi"
@ -18,8 +20,8 @@ template unimplemented() =
raise (ref CatchableError)(msg: "Unimplemented") raise (ref CatchableError)(msg: "Unimplemented")
proc installConfigApiHandlers*(rpcServer: RpcServer, node: BeaconNode) = proc installConfigApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
rpcServer.rpc("get_v1_config_fork_schedule") do () -> JsonNode: rpcServer.rpc("get_v1_config_fork_schedule") do () -> seq[Fork]:
unimplemented() return @[node.chainDag.headState.data.data.fork]
rpcServer.rpc("get_v1_config_spec") do () -> JsonNode: rpcServer.rpc("get_v1_config_spec") do () -> JsonNode:
unimplemented() unimplemented()

View File

@ -19,6 +19,8 @@ type
RpcServer = RpcHttpServer RpcServer = RpcHttpServer
proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) = proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
## 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: rpcServer.rpc("getBeaconHead") do () -> Slot:
return node.chainDag.head.slot return node.chainDag.head.slot
@ -37,19 +39,18 @@ proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
} }
rpcServer.rpc("getSyncing") do () -> bool: rpcServer.rpc("getSyncing") do () -> bool:
let return node.syncManager.inProgress
wallSlot = currentSlot(node)
headSlot = node.chainDag.head.slot
# FIXME: temporary hack: If more than 1 block away from expected head, then we are "syncing"
return (headSlot + 1) < wallSlot
rpcServer.rpc("getNetworkPeerId") do () -> string: rpcServer.rpc("getNetworkPeerId") do () -> string:
return $publicKey(node.network) return $node.network.peerId()
rpcServer.rpc("getNetworkPeers") do () -> seq[string]: rpcServer.rpc("getNetworkPeers") do () -> seq[string]:
for peerId, peer in node.network.peerPool: for peerId, peer in node.network.peerPool:
result.add $peerId result.add $peerId
rpcServer.rpc("getNodeVersion") do () -> string:
return "Nimbus/" & fullVersionStr
rpcServer.rpc("getSpecPreset") do () -> JsonNode: rpcServer.rpc("getSpecPreset") do () -> JsonNode:
var res = newJObject() var res = newJObject()
genStmtList: genStmtList:

View File

@ -57,15 +57,12 @@ proc get_v1_beacon_blocks_blockId_attestations(blockId: string): seq[Attestation
proc post_v1_beacon_pool_attestations(attestation: Attestation): bool proc post_v1_beacon_pool_attestations(attestation: Attestation): bool
proc get_v1_config_fork_schedule(): seq[tuple[epoch: uint64, version: Version]] proc get_v1_config_fork_schedule(): seq[Fork]
# TODO stateId is part of the REST path # TODO stateId is part of the REST path
proc get_v1_debug_beacon_states_stateId(stateId: string): BeaconState proc get_v1_debug_beacon_states_stateId(stateId: string): BeaconState
proc getBeaconHead(): Slot proc getBeaconHead(): Slot
proc getNetworkPeerId() proc getNetworkPeerId()
proc getNetworkPeers() proc getNetworkPeers()
proc getNetworkEnr() proc getNetworkEnr()