diff --git a/beacon_chain/rpc/eth2_json_rest_serialization.nim b/beacon_chain/rpc/eth2_json_rest_serialization.nim index 88c2edbf9..ab307b537 100644 --- a/beacon_chain/rpc/eth2_json_rest_serialization.nim +++ b/beacon_chain/rpc/eth2_json_rest_serialization.nim @@ -151,6 +151,23 @@ proc readValue*(reader: var JsonReader[RestJson], value: var ValidatorIndex) {. else: reader.raiseUnexpectedValue($res.error()) +## RestValidatorIndex +proc writeValue*(writer: var JsonWriter[RestJson], + value: RestValidatorIndex) {. + raises: [IOError, Defect].} = + writeValue(writer, Base10.toString(uint64(value))) + +proc readValue*(reader: var JsonReader[RestJson], + value: var RestValidatorIndex) {. + raises: [IOError, SerializationError, Defect].} = + let svalue = reader.readValue(string) + let res = Base10.decode(uint64, svalue) + if res.isOk(): + let v = res.get() + value = RestValidatorIndex(v) + else: + reader.raiseUnexpectedValue($res.error()) + ## CommitteeIndex proc writeValue*(writer: var JsonWriter[RestJson], value: CommitteeIndex) {. raises: [IOError, Defect].} = diff --git a/beacon_chain/rpc/validator_rest_api.nim b/beacon_chain/rpc/validator_rest_api.nim index 467454363..81883a489 100644 --- a/beacon_chain/rpc/validator_rest_api.nim +++ b/beacon_chain/rpc/validator_rest_api.nim @@ -48,11 +48,26 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) = block: if contentBody.isNone(): return RestApiResponse.jsonError(Http400, "Empty request's body") - let dres = decodeBody(seq[ValidatorIndex], contentBody.get()) + let dres = decodeBody(seq[RestValidatorIndex], contentBody.get()) if dres.isErr(): return RestApiResponse.jsonError(Http400, "Unable to decode " & "list of validator indexes", $dres.error()) - dres.get() + var res: seq[ValidatorIndex] + let items = dres.get() + for item in items: + let vres = item.toValidatorIndex() + if vres.isErr(): + case vres.error() + of ValidatorIndexError.TooHighValue: + return RestApiResponse.jsonError(Http400, + "Incorrect validator index value") + of ValidatorIndexError.UnsupportedValue: + return RestApiResponse.jsonError(Http500, + "Unsupported validator index value") + res.add(vres.get()) + if len(res) == 0: + return RestApiResponse.jsonError(Http400, "Empty indexes list") + res let qepoch = block: if epoch.isErr():