From 020a32ffa4ed6e4050fc0f24ac54e5247ec519e7 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Wed, 28 Oct 2020 19:51:38 +0100 Subject: [PATCH] minor api cleanups (#1913) * readd getNodeVersion * implement get_v1_config_fork_schedule * fix formatting of several types --- beacon_chain/eth2_json_rpc_serialization.nim | 33 ++++++++++++------- beacon_chain/rpc/config_api.nim | 8 +++-- beacon_chain/rpc/nimbus_api.nim | 13 ++++---- .../spec/eth2_apis/beacon_callsigs.nim | 5 +-- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/beacon_chain/eth2_json_rpc_serialization.nim b/beacon_chain/eth2_json_rpc_serialization.nim index eab18ed9f..fa53258fe 100644 --- a/beacon_chain/eth2_json_rpc_serialization.nim +++ b/beacon_chain/eth2_json_rpc_serialization.nim @@ -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) diff --git a/beacon_chain/rpc/config_api.nim b/beacon_chain/rpc/config_api.nim index 400d2668c..97cbaa40e 100644 --- a/beacon_chain/rpc/config_api.nim +++ b/beacon_chain/rpc/config_api.nim @@ -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() diff --git a/beacon_chain/rpc/nimbus_api.nim b/beacon_chain/rpc/nimbus_api.nim index 0f2ff2847..26de01879 100644 --- a/beacon_chain/rpc/nimbus_api.nim +++ b/beacon_chain/rpc/nimbus_api.nim @@ -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: diff --git a/beacon_chain/spec/eth2_apis/beacon_callsigs.nim b/beacon_chain/spec/eth2_apis/beacon_callsigs.nim index 6d4957e7c..761ed0a24 100644 --- a/beacon_chain/spec/eth2_apis/beacon_callsigs.nim +++ b/beacon_chain/spec/eth2_apis/beacon_callsigs.nim @@ -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() -