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
# Standard library
tables, json,
std/[tables, json, typetraits],
# Nimble packages
stew/byteutils, ssz/types,
json_rpc/jsonmarshal,
# 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) =
n.kind.expect(JString, argName)
result = ValidatorPubKey.fromHex(n.getStr()).tryGet().initPubKey()
result = initPubKey(ValidatorPubKey.fromHex(n.getStr()).tryGet().initPubKey())
proc `%`*(pubkey: ValidatorPubKey): JsonNode =
result = newJString($initPubKey(pubkey))
newJString(toJsonHex(toRaw(pubkey)))
proc fromJson*(n: JsonNode, argName: string, result: var List) =
fromJson(n, argName, asSeq result)
@ -31,19 +35,19 @@ proc fromJson*(n: JsonNode, argName: string, result: var ValidatorSig) =
result = ValidatorSig.fromHex(n.getStr()).tryGet()
proc `%`*(value: ValidatorSig): JsonNode =
result = newJString($value)
newJString(toJsonHex(toRaw(value)))
proc fromJson*(n: JsonNode, argName: string, result: var Version) =
n.kind.expect(JString, argName)
hexToByteArray(n.getStr(), array[4, byte](result))
proc `%`*(value: Version): JsonNode =
result = newJString($value)
newJString(toJsonHex(distinctBase(value)))
template genFromJsonForIntType(T: untyped) =
proc fromJson*(n: JsonNode, argName: string, result: var T) =
n.kind.expect(JInt, argName)
let asInt = n.getInt()
let asInt = n.getBiggestInt()
# signed -> unsigned conversions are unchecked
# https://github.com/nim-lang/RFCs/issues/175
if asInt < 0:
@ -56,15 +60,22 @@ genFromJsonForIntType(Slot)
genFromJsonForIntType(CommitteeIndex)
genFromJsonForIntType(ValidatorIndex)
template `%`*(value: GraffitiBytes): JsonNode =
%($value)
proc `%`*(value: GraffitiBytes): JsonNode =
newJString(toJsonHex(distinctBase(value)))
proc fromJson*(n: JsonNode, argName: string, value: var GraffitiBytes) =
n.kind.expect(JString, argName)
value = GraffitiBytes.init n.getStr()
proc `%`*(value: CommitteeIndex): JsonNode =
result = newJInt(value.int)
newJInt(value.BiggestInt)
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
json_rpc/[rpcserver, jsonmarshal],
chronicles,
../beacon_node_common
../beacon_node_common,
../spec/datatypes, ../eth2_json_rpc_serialization
logScope: topics = "configapi"
@ -18,8 +20,8 @@ template unimplemented() =
raise (ref CatchableError)(msg: "Unimplemented")
proc installConfigApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
rpcServer.rpc("get_v1_config_fork_schedule") do () -> JsonNode:
unimplemented()
rpcServer.rpc("get_v1_config_fork_schedule") do () -> seq[Fork]:
return @[node.chainDag.headState.data.data.fork]
rpcServer.rpc("get_v1_config_spec") do () -> JsonNode:
unimplemented()

View File

@ -19,6 +19,8 @@ type
RpcServer = RpcHttpServer
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:
return node.chainDag.head.slot
@ -37,19 +39,18 @@ proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) =
}
rpcServer.rpc("getSyncing") do () -> bool:
let
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
return node.syncManager.inProgress
rpcServer.rpc("getNetworkPeerId") do () -> string:
return $publicKey(node.network)
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("getSpecPreset") do () -> JsonNode:
var res = newJObject()
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 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
proc get_v1_debug_beacon_states_stateId(stateId: string): BeaconState
proc getBeaconHead(): Slot
proc getNetworkPeerId()
proc getNetworkPeers()
proc getNetworkEnr()