2022-01-04 09:45:38 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2021-08-03 15:17:11 +00:00
|
|
|
# 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.
|
|
|
|
|
2021-09-16 13:32:32 +00:00
|
|
|
import std/typetraits
|
2022-01-08 20:06:34 +00:00
|
|
|
import stew/[assign2, results, base10, byteutils], presto/common,
|
|
|
|
libp2p/peerid,
|
2021-12-22 12:37:31 +00:00
|
|
|
serialization, json_serialization, json_serialization/std/[options, net, sets]
|
|
|
|
import ".."/[eth2_ssz_serialization, forks],
|
2022-01-06 11:25:35 +00:00
|
|
|
".."/datatypes/[phase0, altair, bellatrix],
|
2021-12-22 12:37:31 +00:00
|
|
|
"."/[rest_types, rest_keymanager_types]
|
2021-11-09 19:21:36 +00:00
|
|
|
|
2021-08-18 18:57:58 +00:00
|
|
|
export
|
2021-11-09 19:21:36 +00:00
|
|
|
eth2_ssz_serialization, results, peerid, common, serialization,
|
|
|
|
json_serialization, options, net, sets, rest_types
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-11-02 18:23:31 +00:00
|
|
|
from web3/ethtypes import BlockHash
|
|
|
|
export ethtypes.BlockHash
|
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
Json.createFlavor RestJson
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
const
|
|
|
|
DecimalSet = {'0' .. '9'}
|
|
|
|
# Base10 (decimal) set of chars
|
|
|
|
ValidatorKeySize = RawPubKeySize * 2
|
|
|
|
# Size of `ValidatorPubKey` hexadecimal value (without 0x)
|
|
|
|
ValidatorSigSize = RawSigSize * 2
|
|
|
|
# Size of `ValidatorSig` hexadecimal value (without 0x)
|
|
|
|
RootHashSize = sizeof(Eth2Digest) * 2
|
|
|
|
# Size of `xxx_root` hexadecimal value (without 0x)
|
2021-09-16 13:32:32 +00:00
|
|
|
Phase0Version =
|
|
|
|
[byte('p'), byte('h'), byte('a'), byte('s'), byte('e'), byte('0')]
|
|
|
|
AltairVersion =
|
|
|
|
[byte('a'), byte('l'), byte('t'), byte('a'), byte('i'), byte('r')]
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
type
|
2021-07-13 11:15:07 +00:00
|
|
|
RestGenericError* = object
|
|
|
|
code*: uint64
|
|
|
|
message*: string
|
|
|
|
stacktraces*: Option[seq[string]]
|
|
|
|
|
|
|
|
RestAttestationError* = object
|
|
|
|
code*: uint64
|
|
|
|
message*: string
|
2021-10-04 19:08:31 +00:00
|
|
|
failures*: seq[RestFailureItem]
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
EncodeTypes* =
|
|
|
|
AttesterSlashing |
|
|
|
|
ProposerSlashing |
|
|
|
|
phase0.SignedBeaconBlock |
|
2021-08-27 09:00:06 +00:00
|
|
|
altair.SignedBeaconBlock |
|
2021-10-19 17:44:05 +00:00
|
|
|
SignedVoluntaryExit |
|
2021-12-22 12:37:31 +00:00
|
|
|
Web3SignerRequest |
|
|
|
|
KeystoresAndSlashingProtection |
|
|
|
|
DeleteKeystoresBody
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
EncodeArrays* =
|
|
|
|
seq[ValidatorIndex] |
|
|
|
|
seq[Attestation] |
|
|
|
|
seq[SignedAggregateAndProof] |
|
2021-10-19 17:44:05 +00:00
|
|
|
seq[RestCommitteeSubscription] |
|
|
|
|
seq[RestSyncCommitteeSubscription] |
|
|
|
|
seq[RestSyncCommitteeMessage] |
|
|
|
|
seq[RestSignedContributionAndProof]
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
DecodeTypes* =
|
|
|
|
DataEnclosedObject |
|
|
|
|
DataMetaEnclosedObject |
|
|
|
|
DataRootEnclosedObject |
|
2021-09-27 18:31:11 +00:00
|
|
|
GetBlockV2Response |
|
2021-12-22 12:37:31 +00:00
|
|
|
GetKeystoresResponse |
|
2021-11-30 01:20:21 +00:00
|
|
|
GetStateV2Response |
|
2021-12-22 12:37:31 +00:00
|
|
|
ProduceBlockResponseV2 |
|
|
|
|
RestAttestationError |
|
|
|
|
RestGenericError |
|
|
|
|
Web3SignerErrorResponse |
|
2021-11-30 01:20:21 +00:00
|
|
|
Web3SignerKeysResponse |
|
|
|
|
Web3SignerSignatureResponse |
|
2021-12-22 12:37:31 +00:00
|
|
|
Web3SignerStatusResponse
|
2021-09-27 18:31:11 +00:00
|
|
|
|
2021-10-20 09:58:38 +00:00
|
|
|
# These types may be extended with additional fields in the future.
|
|
|
|
# Locally unknown fields are silently ignored when decoding them.
|
|
|
|
ExtensibleDecodeTypes* =
|
|
|
|
GetSpecResponse |
|
|
|
|
GetSpecVCResponse
|
|
|
|
|
2021-09-27 18:31:11 +00:00
|
|
|
SszDecodeTypes* =
|
|
|
|
GetPhase0StateSszResponse |
|
2022-01-06 07:38:40 +00:00
|
|
|
GetPhase0BlockSszResponse
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
proc prepareJsonResponse*(t: typedesc[RestApiResponse], d: auto): seq[byte] =
|
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("data", d)
|
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
res
|
|
|
|
|
2021-09-22 12:17:15 +00:00
|
|
|
proc prepareJsonStringResponse*(t: typedesc[RestApiResponse], d: auto): string =
|
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: string
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.writeValue(d)
|
|
|
|
stream.getOutput(string)
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
res
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
proc jsonResponseWRoot*(t: typedesc[RestApiResponse], data: auto,
|
2021-03-23 22:50:18 +00:00
|
|
|
dependent_root: Eth2Digest): RestApiResponse =
|
2021-08-23 10:41:48 +00:00
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("dependent_root", dependent_root)
|
|
|
|
writer.writeField("data", data)
|
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
RestApiResponse.response(res, Http200, "application/json")
|
|
|
|
|
|
|
|
proc jsonResponse*(t: typedesc[RestApiResponse], data: auto): RestApiResponse =
|
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("data", data)
|
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
RestApiResponse.response(res, Http200, "application/json")
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
proc jsonResponsePlain*(t: typedesc[RestApiResponse],
|
|
|
|
data: auto): RestApiResponse =
|
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.writeValue(data)
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
RestApiResponse.response(res, Http200, "application/json")
|
|
|
|
|
2021-04-04 07:23:36 +00:00
|
|
|
proc jsonResponseWMeta*(t: typedesc[RestApiResponse],
|
|
|
|
data: auto, meta: auto): RestApiResponse =
|
2021-08-23 10:41:48 +00:00
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("data", data)
|
|
|
|
writer.writeField("meta", meta)
|
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
RestApiResponse.response(res, Http200, "application/json")
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc jsonMsgResponse*(t: typedesc[RestApiResponse],
|
|
|
|
msg: string = ""): RestApiResponse =
|
|
|
|
let data =
|
|
|
|
block:
|
2021-08-23 10:41:48 +00:00
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var defstrings: seq[string]
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("code", "200")
|
|
|
|
writer.writeField("message", msg)
|
2021-12-22 12:37:31 +00:00
|
|
|
writer.writeField("stacktraces", defstrings)
|
2021-08-23 10:41:48 +00:00
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
2021-07-13 11:15:07 +00:00
|
|
|
RestApiResponse.response(data, Http200, "application/json")
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200,
|
2021-07-13 11:15:07 +00:00
|
|
|
msg: string = ""): RestApiResponse =
|
2021-03-23 22:50:18 +00:00
|
|
|
let data =
|
|
|
|
block:
|
2021-08-23 10:41:48 +00:00
|
|
|
var default: string
|
|
|
|
try:
|
|
|
|
var defstrings: seq[string]
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("code", Base10.toString(uint64(status.toInt())))
|
|
|
|
writer.writeField("message", msg)
|
2021-12-22 12:37:31 +00:00
|
|
|
writer.writeField("stacktraces", defstrings)
|
2021-08-23 10:41:48 +00:00
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(string)
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
2021-07-13 11:15:07 +00:00
|
|
|
RestApiResponse.error(status, data, "application/json")
|
|
|
|
|
|
|
|
proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200,
|
|
|
|
msg: string = "", stacktrace: string): RestApiResponse =
|
|
|
|
let data =
|
|
|
|
block:
|
2021-08-23 10:41:48 +00:00
|
|
|
var default: string
|
|
|
|
try:
|
|
|
|
var defstrings: seq[string]
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("code", Base10.toString(uint64(status.toInt())))
|
|
|
|
writer.writeField("message", msg)
|
|
|
|
if len(stacktrace) > 0:
|
2021-12-22 12:37:31 +00:00
|
|
|
writer.writeField("stacktraces", [stacktrace])
|
2021-08-23 10:41:48 +00:00
|
|
|
else:
|
2021-12-22 12:37:31 +00:00
|
|
|
writer.writeField("stacktraces", defstrings)
|
2021-08-23 10:41:48 +00:00
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(string)
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
2021-07-13 11:15:07 +00:00
|
|
|
RestApiResponse.error(status, data, "application/json")
|
|
|
|
|
|
|
|
proc jsonError*(t: typedesc[RestApiResponse], status: HttpCode = Http200,
|
|
|
|
msg: string = "",
|
|
|
|
stacktraces: openarray[string]): RestApiResponse =
|
|
|
|
let data =
|
|
|
|
block:
|
2021-08-23 10:41:48 +00:00
|
|
|
var default: string
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("code", Base10.toString(uint64(status.toInt())))
|
|
|
|
writer.writeField("message", msg)
|
2021-12-22 12:37:31 +00:00
|
|
|
writer.writeField("stacktraces", stacktraces)
|
2021-08-23 10:41:48 +00:00
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(string)
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
2021-03-23 22:50:18 +00:00
|
|
|
RestApiResponse.error(status, data, "application/json")
|
|
|
|
|
|
|
|
proc jsonErrorList*(t: typedesc[RestApiResponse],
|
|
|
|
status: HttpCode = Http200,
|
|
|
|
msg: string = "", failures: auto): RestApiResponse =
|
|
|
|
let data =
|
|
|
|
block:
|
2021-08-23 10:41:48 +00:00
|
|
|
var default: string
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.beginRecord()
|
|
|
|
writer.writeField("code", Base10.toString(uint64(status.toInt())))
|
|
|
|
writer.writeField("message", msg)
|
|
|
|
writer.writeField("failures", failures)
|
|
|
|
writer.endRecord()
|
|
|
|
stream.getOutput(string)
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
2021-03-23 22:50:18 +00:00
|
|
|
RestApiResponse.error(status, data, "application/json")
|
|
|
|
|
2021-09-16 13:32:32 +00:00
|
|
|
proc sszResponse*(t: typedesc[RestApiResponse], data: auto): RestApiResponse =
|
|
|
|
let res =
|
|
|
|
block:
|
|
|
|
var default: seq[byte]
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = SszWriter.init(stream)
|
|
|
|
writer.writeValue(data)
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except SerializationError:
|
|
|
|
default
|
|
|
|
except IOError:
|
|
|
|
default
|
|
|
|
RestApiResponse.response(res, Http200, "application/octet-stream")
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
template hexOriginal(data: openarray[byte]): string =
|
2022-01-08 20:06:34 +00:00
|
|
|
to0xHex(data)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
proc decodeJsonString*[T](t: typedesc[T],
|
|
|
|
data: JsonString,
|
|
|
|
requireAllFields = true): Result[T, cstring] =
|
|
|
|
try:
|
|
|
|
ok(RestJson.decode(string(data), T, requireAllFields = requireAllFields))
|
|
|
|
except SerializationError:
|
|
|
|
err("Unable to deserialize data")
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
## uint64
|
2021-08-23 10:41:48 +00:00
|
|
|
proc writeValue*(w: var JsonWriter[RestJson], value: uint64) {.
|
|
|
|
raises: [IOError, Defect].} =
|
2021-03-23 22:50:18 +00:00
|
|
|
writeValue(w, Base10.toString(value))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var uint64) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let svalue = reader.readValue(string)
|
|
|
|
let res = Base10.decode(uint64, svalue)
|
|
|
|
if res.isOk():
|
|
|
|
value = res.get()
|
|
|
|
else:
|
2022-01-06 07:38:40 +00:00
|
|
|
reader.raiseUnexpectedValue($res.error() & ": " & svalue)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
proc writeValue*(w: var JsonWriter[RestJson], value: uint8) {.
|
2021-11-02 18:23:31 +00:00
|
|
|
raises: [IOError, Defect].} =
|
2022-01-06 07:38:40 +00:00
|
|
|
writeValue(w, Base10.toString(value))
|
2021-11-02 18:23:31 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var uint8) {.
|
2021-11-02 18:23:31 +00:00
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let svalue = reader.readValue(string)
|
2022-01-06 07:38:40 +00:00
|
|
|
let res = Base10.decode(uint8, svalue)
|
|
|
|
if res.isOk():
|
|
|
|
value = res.get()
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error() & ": " & svalue)
|
|
|
|
|
|
|
|
proc writeValue*(w: var JsonWriter[RestJson], value: JustificationBits) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
w.writeValue hexOriginal([uint8(value)])
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var JustificationBits) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let hex = reader.readValue(string)
|
2021-11-02 18:23:31 +00:00
|
|
|
try:
|
2022-01-06 07:38:40 +00:00
|
|
|
value = JustificationBits(hexToByteArray(hex, 1)[0])
|
2021-11-02 18:23:31 +00:00
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
2022-01-06 07:38:40 +00:00
|
|
|
"The `justification_bits` value must be a hex string")
|
2021-11-02 18:23:31 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
## UInt256
|
|
|
|
proc writeValue*(w: var JsonWriter[RestJson], value: UInt256) {.
|
2021-08-23 10:41:48 +00:00
|
|
|
raises: [IOError, Defect].} =
|
2022-01-06 07:38:40 +00:00
|
|
|
writeValue(w, toString(value))
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2022-01-06 07:38:40 +00:00
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var UInt256) {.
|
2021-07-13 11:15:07 +00:00
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
2022-01-06 07:38:40 +00:00
|
|
|
let svalue = reader.readValue(string)
|
2021-07-13 11:15:07 +00:00
|
|
|
try:
|
2022-01-06 07:38:40 +00:00
|
|
|
value = parse(svalue, UInt256, 10)
|
2021-07-13 11:15:07 +00:00
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
2022-01-06 07:38:40 +00:00
|
|
|
"UInt256 value should be a valid decimal string")
|
2021-07-13 11:15:07 +00:00
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
## Slot
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: Slot) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, Base10.toString(uint64(value)))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var Slot) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let svalue = reader.readValue(string)
|
|
|
|
let res = Base10.decode(uint64, svalue)
|
|
|
|
if res.isOk():
|
|
|
|
value = Slot(res.get())
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
|
|
|
## Epoch
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: Epoch) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, Base10.toString(uint64(value)))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var Epoch) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let svalue = reader.readValue(string)
|
|
|
|
let res = Base10.decode(uint64, svalue)
|
|
|
|
if res.isOk():
|
|
|
|
value = Epoch(res.get())
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
|
|
|
## ValidatorIndex
|
2021-11-07 21:03:23 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: ValidatorIndex)
|
|
|
|
{.raises: [IOError, Defect].} =
|
2021-03-23 22:50:18 +00:00
|
|
|
writeValue(writer, Base10.toString(uint64(value)))
|
|
|
|
|
2021-11-07 21:03:23 +00:00
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var ValidatorIndex)
|
|
|
|
{.raises: [IOError, SerializationError, Defect].} =
|
2021-03-23 22:50:18 +00:00
|
|
|
let svalue = reader.readValue(string)
|
|
|
|
let res = Base10.decode(uint64, svalue)
|
|
|
|
if res.isOk():
|
|
|
|
let v = res.get()
|
|
|
|
if v < VALIDATOR_REGISTRY_LIMIT:
|
|
|
|
value = ValidatorIndex(v)
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Validator index is bigger then VALIDATOR_REGISTRY_LIMIT")
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
2021-11-09 19:21:36 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: IndexInSyncCommittee)
|
2021-11-07 21:03:23 +00:00
|
|
|
{.raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, Base10.toString(distinctBase(value)))
|
|
|
|
|
2021-11-09 19:21:36 +00:00
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var IndexInSyncCommittee)
|
2021-11-07 21:03:23 +00:00
|
|
|
{.raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let svalue = reader.readValue(string)
|
|
|
|
let res = Base10.decode(uint64, svalue)
|
|
|
|
if res.isOk():
|
|
|
|
let v = res.get()
|
|
|
|
if v < SYNC_COMMITTEE_SIZE:
|
2021-11-09 19:21:36 +00:00
|
|
|
value = IndexInSyncCommittee(v)
|
2021-11-07 21:03:23 +00:00
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue(
|
2021-11-09 19:21:36 +00:00
|
|
|
"Index in committee is bigger than SYNC_COMMITTEE_SIZE")
|
2021-11-07 21:03:23 +00:00
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
2021-04-06 08:00:26 +00:00
|
|
|
## 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())
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
## CommitteeIndex
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: CommitteeIndex) {.
|
|
|
|
raises: [IOError, Defect].} =
|
2022-01-08 23:28:49 +00:00
|
|
|
writeValue(writer, value.asUInt64)
|
2021-03-23 22:50:18 +00:00
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var CommitteeIndex) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
2022-01-08 23:28:49 +00:00
|
|
|
var v: uint64
|
|
|
|
reader.readValue(v)
|
|
|
|
|
|
|
|
let res = CommitteeIndex.init(v)
|
2021-03-23 22:50:18 +00:00
|
|
|
if res.isOk():
|
2022-01-08 23:28:49 +00:00
|
|
|
value = res.get()
|
2021-03-23 22:50:18 +00:00
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
|
|
|
## ValidatorSig
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: ValidatorSig) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(toRaw(value)))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var ValidatorSig) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let hexValue = reader.readValue(string)
|
|
|
|
let res = ValidatorSig.fromHex(hexValue)
|
|
|
|
if res.isOk():
|
|
|
|
value = res.get()
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
|
|
|
## TrustedSig
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: TrustedSig) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(toRaw(value)))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var TrustedSig) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let hexValue = reader.readValue(string)
|
|
|
|
let res = ValidatorSig.fromHex(hexValue)
|
|
|
|
if res.isOk():
|
|
|
|
value = cast[TrustedSig](res.get())
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
|
|
|
## ValidatorPubKey
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: ValidatorPubKey) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(toRaw(value)))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var ValidatorPubKey) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
let hexValue = reader.readValue(string)
|
|
|
|
let res = ValidatorPubKey.fromHex(hexValue)
|
|
|
|
if res.isOk():
|
|
|
|
value = res.get()
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue($res.error())
|
|
|
|
|
|
|
|
## BitSeq
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var BitSeq) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
value = BitSeq hexToSeqByte(reader.readValue(string))
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader, "A BitSeq value should be a valid hex string")
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: BitSeq) {.
|
|
|
|
raises: [IOError, Defect].} =
|
2021-06-10 17:51:15 +00:00
|
|
|
writeValue(writer, hexOriginal(value.bytes()))
|
2021-03-23 22:50:18 +00:00
|
|
|
|
|
|
|
## BitList
|
2021-08-23 10:41:48 +00:00
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var BitList) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
2021-03-23 22:50:18 +00:00
|
|
|
type T = type(value)
|
|
|
|
value = T readValue(reader, BitSeq)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: BitList) {.
|
|
|
|
raises: [IOError, Defect].} =
|
2021-03-23 22:50:18 +00:00
|
|
|
writeValue(writer, BitSeq value)
|
|
|
|
|
2021-09-27 08:14:43 +00:00
|
|
|
## BitArray
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var BitArray) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
hexToByteArray(readValue(reader, string), value.bytes)
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
|
|
|
"A BitArray value should be a valid hex string")
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: BitArray) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(value.bytes))
|
|
|
|
|
2021-11-02 18:23:31 +00:00
|
|
|
## BlockHash
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var BlockHash) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
hexToByteArray(reader.readValue(string), distinctBase(value))
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
|
|
|
"BlockHash value should be a valid hex string")
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: BlockHash) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(distinctBase(value)))
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
## Eth2Digest
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var Eth2Digest) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
hexToByteArray(reader.readValue(string), value.data)
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
|
|
|
"Eth2Digest value should be a valid hex string")
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: Eth2Digest) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(value.data))
|
|
|
|
|
2021-05-20 10:44:13 +00:00
|
|
|
## BloomLogs
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var BloomLogs) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
hexToByteArray(reader.readValue(string), value.data)
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
|
|
|
"BloomLogs value should be a valid hex string")
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: BloomLogs) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(value.data))
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
## HashArray
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var HashArray) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
readValue(reader, value.data)
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: HashArray) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, value.data)
|
|
|
|
|
|
|
|
## HashList
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var HashList) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
readValue(reader, value.data)
|
2021-09-27 09:24:58 +00:00
|
|
|
value.resetCache()
|
2021-03-23 22:50:18 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: HashList) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, value.data)
|
|
|
|
|
|
|
|
## Eth1Address
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], value: var Eth1Address) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
hexToByteArray(reader.readValue(string), distinctBase(value))
|
|
|
|
except ValueError:
|
|
|
|
raiseUnexpectedValue(reader,
|
|
|
|
"Eth1Address value should be a valid hex string")
|
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: Eth1Address) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(distinctBase(value)))
|
|
|
|
|
2022-01-20 12:31:55 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: GraffitiBytes)
|
|
|
|
{.raises: [IOError, Defect].} =
|
|
|
|
writeValue(writer, hexOriginal(distinctBase(value)))
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson], T: type GraffitiBytes): T
|
|
|
|
{.raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
init(GraffitiBytes, reader.readValue(string))
|
|
|
|
except ValueError as err:
|
|
|
|
reader.raiseUnexpectedValue err.msg
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
## Version
|
2022-01-08 20:06:34 +00:00
|
|
|
proc readValue*(
|
|
|
|
reader: var JsonReader[RestJson],
|
|
|
|
value: var (Version | ForkDigest | DomainType | GraffitiBytes)) {.
|
2021-03-23 22:50:18 +00:00
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
try:
|
|
|
|
hexToByteArray(reader.readValue(string), distinctBase(value))
|
|
|
|
except ValueError:
|
2022-01-08 20:06:34 +00:00
|
|
|
raiseUnexpectedValue(
|
|
|
|
reader, "Expected a valid hex string with " & $value.len() & " bytes")
|
2021-03-23 22:50:18 +00:00
|
|
|
|
2021-08-27 09:00:06 +00:00
|
|
|
## ForkedBeaconBlock
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson],
|
|
|
|
value: var ForkedBeaconBlock) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
var
|
|
|
|
version: Option[BeaconBlockFork]
|
|
|
|
data: Option[JsonString]
|
|
|
|
|
|
|
|
for fieldName in readObjectFields(reader):
|
|
|
|
case fieldName
|
|
|
|
of "version":
|
|
|
|
if version.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple version fields found",
|
|
|
|
"ForkedBeaconBlock")
|
|
|
|
let vres = reader.readValue(string)
|
|
|
|
case vres
|
|
|
|
of "phase0":
|
|
|
|
version = some(BeaconBlockFork.Phase0)
|
|
|
|
of "altair":
|
|
|
|
version = some(BeaconBlockFork.Altair)
|
2021-09-27 14:22:58 +00:00
|
|
|
of "merge":
|
2022-01-04 09:45:38 +00:00
|
|
|
version = some(BeaconBlockFork.Bellatrix)
|
2021-08-27 09:00:06 +00:00
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue("Incorrect version field value")
|
|
|
|
of "data":
|
|
|
|
if data.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple data fields found",
|
|
|
|
"ForkedBeaconBlock")
|
|
|
|
data = some(reader.readValue(JsonString))
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedField(fieldName, "ForkedBeaconBlock")
|
|
|
|
|
|
|
|
if version.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field version is missing")
|
|
|
|
if data.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field data is missing")
|
|
|
|
|
|
|
|
case version.get():
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
some(RestJson.decode(string(data.get()), phase0.BeaconBlock,
|
|
|
|
requireAllFields = true))
|
|
|
|
except SerializationError:
|
|
|
|
none[phase0.BeaconBlock]()
|
|
|
|
if res.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Incorrect phase0 block format")
|
|
|
|
value = ForkedBeaconBlock.init(res.get())
|
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
some(RestJson.decode(string(data.get()), altair.BeaconBlock,
|
|
|
|
requireAllFields = true))
|
|
|
|
except SerializationError:
|
|
|
|
none[altair.BeaconBlock]()
|
|
|
|
if res.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Incorrect altair block format")
|
|
|
|
value = ForkedBeaconBlock.init(res.get())
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Bellatrix:
|
2021-09-27 14:22:58 +00:00
|
|
|
let res =
|
|
|
|
try:
|
2022-01-06 11:25:35 +00:00
|
|
|
some(RestJson.decode(string(data.get()), bellatrix.BeaconBlock,
|
2021-09-27 14:22:58 +00:00
|
|
|
requireAllFields = true))
|
|
|
|
except SerializationError:
|
2022-01-06 11:25:35 +00:00
|
|
|
none[bellatrix.BeaconBlock]()
|
2021-09-27 14:22:58 +00:00
|
|
|
if res.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Incorrect merge block format")
|
|
|
|
value = ForkedBeaconBlock.init(res.get())
|
2021-08-27 09:00:06 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: ForkedBeaconBlock) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writer.beginRecord()
|
|
|
|
case value.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
writer.writeField("version", "phase0")
|
2021-10-18 16:37:27 +00:00
|
|
|
writer.writeField("data", value.phase0Data)
|
2021-08-27 09:00:06 +00:00
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
writer.writeField("version", "altair")
|
2021-10-18 16:37:27 +00:00
|
|
|
writer.writeField("data", value.altairData)
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Bellatrix:
|
2021-09-27 14:22:58 +00:00
|
|
|
writer.writeField("version", "merge")
|
|
|
|
when false:
|
|
|
|
# TODO SerializationError
|
2021-10-18 16:37:27 +00:00
|
|
|
writer.writeField("data", value.mergeData)
|
2021-08-27 09:00:06 +00:00
|
|
|
writer.endRecord()
|
|
|
|
|
|
|
|
## ForkedSignedBeaconBlock
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson],
|
|
|
|
value: var ForkedSignedBeaconBlock) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
var
|
|
|
|
version: Option[BeaconBlockFork]
|
|
|
|
data: Option[JsonString]
|
|
|
|
|
|
|
|
for fieldName in readObjectFields(reader):
|
|
|
|
case fieldName
|
|
|
|
of "version":
|
|
|
|
if version.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple version fields found",
|
|
|
|
"ForkedSignedBeaconBlock")
|
|
|
|
let vres = reader.readValue(string)
|
|
|
|
case vres
|
|
|
|
of "phase0":
|
|
|
|
version = some(BeaconBlockFork.Phase0)
|
|
|
|
of "altair":
|
|
|
|
version = some(BeaconBlockFork.Altair)
|
2021-09-27 14:22:58 +00:00
|
|
|
of "merge":
|
2022-01-04 09:45:38 +00:00
|
|
|
version = some(BeaconBlockFork.Bellatrix)
|
2021-08-27 09:00:06 +00:00
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue("Incorrect version field value")
|
|
|
|
of "data":
|
|
|
|
if data.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple data fields found",
|
|
|
|
"ForkedSignedBeaconBlock")
|
|
|
|
data = some(reader.readValue(JsonString))
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedField(fieldName, "ForkedSignedBeaconBlock")
|
|
|
|
|
|
|
|
if version.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field version is missing")
|
|
|
|
if data.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field data is missing")
|
|
|
|
|
|
|
|
case version.get():
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
some(RestJson.decode(string(data.get()), phase0.SignedBeaconBlock,
|
|
|
|
requireAllFields = true))
|
|
|
|
except SerializationError:
|
|
|
|
none[phase0.SignedBeaconBlock]()
|
|
|
|
if res.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Incorrect phase0 block format")
|
|
|
|
value = ForkedSignedBeaconBlock.init(res.get())
|
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
let res =
|
|
|
|
try:
|
|
|
|
some(RestJson.decode(string(data.get()), altair.SignedBeaconBlock,
|
|
|
|
requireAllFields = true))
|
|
|
|
except SerializationError:
|
|
|
|
none[altair.SignedBeaconBlock]()
|
|
|
|
if res.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Incorrect altair block format")
|
|
|
|
value = ForkedSignedBeaconBlock.init(res.get())
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Bellatrix:
|
2021-09-27 14:22:58 +00:00
|
|
|
let res =
|
|
|
|
try:
|
2022-01-06 11:25:35 +00:00
|
|
|
some(RestJson.decode(string(data.get()), bellatrix.SignedBeaconBlock,
|
2021-09-27 14:22:58 +00:00
|
|
|
requireAllFields = true))
|
|
|
|
except SerializationError:
|
2022-01-06 11:25:35 +00:00
|
|
|
none[bellatrix.SignedBeaconBlock]()
|
2021-09-27 14:22:58 +00:00
|
|
|
if res.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Incorrect merge block format")
|
|
|
|
value = ForkedSignedBeaconBlock.init(res.get())
|
2022-01-17 09:27:08 +00:00
|
|
|
withBlck(value):
|
|
|
|
blck.root = hash_tree_root(blck.message)
|
2021-08-27 09:00:06 +00:00
|
|
|
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson],
|
|
|
|
value: ForkedSignedBeaconBlock) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writer.beginRecord()
|
|
|
|
case value.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
writer.writeField("version", "phase0")
|
2021-10-18 16:37:27 +00:00
|
|
|
writer.writeField("data", value.phase0Data)
|
2021-08-27 09:00:06 +00:00
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
writer.writeField("version", "altair")
|
2021-10-18 16:37:27 +00:00
|
|
|
writer.writeField("data", value.altairData)
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconBlockFork.Bellatrix:
|
2021-09-27 14:22:58 +00:00
|
|
|
writer.writeField("version", "merge")
|
|
|
|
when false:
|
|
|
|
# TODO SerializationError
|
2021-10-18 16:37:27 +00:00
|
|
|
writer.writeField("data", value.mergeData)
|
2021-08-27 09:00:06 +00:00
|
|
|
writer.endRecord()
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
# ForkedHashedBeaconState is used where a `ForkedBeaconState` normally would
|
|
|
|
# be used, mainly because caching the hash early on is easier to do
|
2021-09-16 13:32:32 +00:00
|
|
|
proc readValue*(reader: var JsonReader[RestJson],
|
2021-11-05 07:34:34 +00:00
|
|
|
value: var ForkedHashedBeaconState) {.
|
2021-09-16 13:32:32 +00:00
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
var
|
|
|
|
version: Option[BeaconStateFork]
|
|
|
|
data: Option[JsonString]
|
|
|
|
|
|
|
|
for fieldName in readObjectFields(reader):
|
|
|
|
case fieldName
|
|
|
|
of "version":
|
|
|
|
if version.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple version fields found",
|
|
|
|
"ForkedBeaconState")
|
|
|
|
let vres = reader.readValue(string)
|
2021-10-18 16:37:27 +00:00
|
|
|
version = case vres
|
|
|
|
of "phase0": some(BeaconStateFork.Phase0)
|
|
|
|
of "altair": some(BeaconStateFork.Altair)
|
2022-01-04 09:45:38 +00:00
|
|
|
of "merge": some(BeaconStateFork.Bellatrix)
|
2021-10-18 16:37:27 +00:00
|
|
|
else: reader.raiseUnexpectedValue("Incorrect version field value")
|
2021-09-16 13:32:32 +00:00
|
|
|
of "data":
|
|
|
|
if data.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple data fields found",
|
|
|
|
"ForkedBeaconState")
|
|
|
|
data = some(reader.readValue(JsonString))
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedField(fieldName, "ForkedBeaconState")
|
|
|
|
|
|
|
|
if version.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field version is missing")
|
|
|
|
if data.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field data is missing")
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
# Use a temporary to avoid stack instances and `value` mutation in case of
|
|
|
|
# exception
|
|
|
|
let
|
|
|
|
tmp = (ref ForkedHashedBeaconState)(kind: version.get())
|
|
|
|
|
|
|
|
template toValue(field: untyped) =
|
|
|
|
if tmp[].kind == value.kind:
|
|
|
|
assign(value.field, tmp[].field)
|
|
|
|
else:
|
|
|
|
value = tmp[] # slow, but rare (hopefully)
|
2022-01-06 07:38:40 +00:00
|
|
|
value.field.root = hash_tree_root(value.field.data)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2021-09-16 13:32:32 +00:00
|
|
|
case version.get():
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Phase0:
|
2021-11-05 07:34:34 +00:00
|
|
|
try:
|
|
|
|
tmp[].phase0Data.data = RestJson.decode(
|
|
|
|
string(data.get()), phase0.BeaconState, requireAllFields = true)
|
|
|
|
except SerializationError:
|
2021-09-16 13:32:32 +00:00
|
|
|
reader.raiseUnexpectedValue("Incorrect phase0 beacon state format")
|
2021-11-05 07:34:34 +00:00
|
|
|
|
|
|
|
toValue(phase0Data)
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Altair:
|
2021-11-05 07:34:34 +00:00
|
|
|
try:
|
|
|
|
tmp[].altairData.data = RestJson.decode(
|
|
|
|
string(data.get()), altair.BeaconState, requireAllFields = true)
|
|
|
|
except SerializationError:
|
2021-09-16 13:32:32 +00:00
|
|
|
reader.raiseUnexpectedValue("Incorrect altair beacon state format")
|
2021-11-05 07:34:34 +00:00
|
|
|
|
|
|
|
toValue(altairData)
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
2021-11-05 07:34:34 +00:00
|
|
|
try:
|
|
|
|
tmp[].mergeData.data = RestJson.decode(
|
2022-01-06 11:25:35 +00:00
|
|
|
string(data.get()), bellatrix.BeaconState, requireAllFields = true)
|
2021-11-05 07:34:34 +00:00
|
|
|
except SerializationError:
|
|
|
|
reader.raiseUnexpectedValue("Incorrect altair beacon state format")
|
|
|
|
toValue(mergeData)
|
2021-09-16 13:32:32 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson], value: ForkedHashedBeaconState) {.
|
2021-09-16 13:32:32 +00:00
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
writer.beginRecord()
|
2021-10-18 16:37:27 +00:00
|
|
|
case value.kind
|
|
|
|
of BeaconStateFork.Phase0:
|
2021-09-16 13:32:32 +00:00
|
|
|
writer.writeField("version", "phase0")
|
2021-11-05 07:34:34 +00:00
|
|
|
writer.writeField("data", value.phase0Data.data)
|
2021-10-18 16:37:27 +00:00
|
|
|
of BeaconStateFork.Altair:
|
2021-09-16 13:32:32 +00:00
|
|
|
writer.writeField("version", "altair")
|
2021-11-05 07:34:34 +00:00
|
|
|
writer.writeField("data", value.altairData.data)
|
2022-01-04 09:45:38 +00:00
|
|
|
of BeaconStateFork.Bellatrix:
|
2021-09-27 14:22:58 +00:00
|
|
|
writer.writeField("version", "merge")
|
|
|
|
when false:
|
|
|
|
# TODO SerializationError
|
2021-11-05 07:34:34 +00:00
|
|
|
writer.writeField("data", value.mergeData.data)
|
2021-09-16 13:32:32 +00:00
|
|
|
writer.endRecord()
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
# Web3SignerRequest
|
|
|
|
proc writeValue*(writer: var JsonWriter[RestJson],
|
|
|
|
value: Web3SignerRequest) {.
|
|
|
|
raises: [IOError, Defect].} =
|
|
|
|
case value.kind
|
|
|
|
of Web3SignerRequestKind.AggregationSlot:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "AGGREGATION_SLOT")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("aggregation_slot", value.aggregationSlot)
|
|
|
|
of Web3SignerRequestKind.AggregateAndProof:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "AGGREGATE_AND_PROOF")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("aggregate_and_proof", value.aggregateAndProof)
|
|
|
|
of Web3SignerRequestKind.Attestation:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "ATTESTATION")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("attestation", value.attestation)
|
|
|
|
of Web3SignerRequestKind.Block:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "BLOCK")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("block", value.blck)
|
|
|
|
of Web3SignerRequestKind.BlockV2:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "BLOCK_V2")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("beacon_block", value.beaconBlock)
|
|
|
|
of Web3SignerRequestKind.Deposit:
|
|
|
|
writer.writeField("type", "DEPOSIT")
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("deposit", value.deposit)
|
|
|
|
of Web3SignerRequestKind.RandaoReveal:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "RANDAO_REVEAL")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("randao_reveal", value.randaoReveal)
|
|
|
|
of Web3SignerRequestKind.VoluntaryExit:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "VOLUNTARY_EXIT")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("voluntary_exit", value.voluntaryExit)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeMessage:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "SYNC_COMMITTEE_MESSAGE")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("sync_committee_message", value.syncCommitteeMessage)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeSelectionProof:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "SYNC_COMMITTEE_SELECTION_PROOF")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("sync_aggregator_selection_data",
|
|
|
|
value.syncAggregatorSelectionData)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeContributionAndProof:
|
|
|
|
doAssert(value.forkInfo.isSome(),
|
|
|
|
"forkInfo should be set for this type of request")
|
|
|
|
writer.writeField("type", "SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF")
|
|
|
|
writer.writeField("fork_info", value.forkInfo.get())
|
|
|
|
if isSome(value.signingRoot):
|
|
|
|
writer.writeField("signingRoot", value.signingRoot)
|
|
|
|
writer.writeField("contribution_and_proof",
|
|
|
|
value.syncCommitteeContributionAndProof)
|
|
|
|
|
|
|
|
proc readValue*(reader: var JsonReader[RestJson],
|
|
|
|
value: var Web3SignerRequest) {.
|
|
|
|
raises: [IOError, SerializationError, Defect].} =
|
|
|
|
var
|
|
|
|
requestKind: Option[Web3SignerRequestKind]
|
|
|
|
forkInfo: Option[Web3SignerForkInfo]
|
|
|
|
signingRoot: Option[Eth2Digest]
|
|
|
|
data: Option[JsonString]
|
|
|
|
dataName: string
|
|
|
|
|
|
|
|
for fieldName in readObjectFields(reader):
|
|
|
|
case fieldName
|
|
|
|
of "type":
|
|
|
|
if requestKind.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple `type` fields found",
|
|
|
|
"Web3SignerRequest")
|
|
|
|
let vres = reader.readValue(string)
|
|
|
|
requestKind = some(
|
|
|
|
case vres
|
|
|
|
of "AGGREGATION_SLOT":
|
|
|
|
Web3SignerRequestKind.AggregationSlot
|
|
|
|
of "AGGREGATE_AND_PROOF":
|
|
|
|
Web3SignerRequestKind.AggregateAndProof
|
|
|
|
of "ATTESTATION":
|
|
|
|
Web3SignerRequestKind.Attestation
|
|
|
|
of "BLOCK":
|
|
|
|
Web3SignerRequestKind.Block
|
|
|
|
of "BLOCK_V2":
|
|
|
|
Web3SignerRequestKind.BlockV2
|
|
|
|
of "DEPOSIT":
|
|
|
|
Web3SignerRequestKind.Deposit
|
|
|
|
of "RANDAO_REVEAL":
|
|
|
|
Web3SignerRequestKind.RandaoReveal
|
|
|
|
of "VOLUNTARY_EXIT":
|
|
|
|
Web3SignerRequestKind.VoluntaryExit
|
|
|
|
of "SYNC_COMMITTEE_MESSAGE":
|
|
|
|
Web3SignerRequestKind.SyncCommitteeMessage
|
|
|
|
of "SYNC_COMMITTEE_SELECTION_PROOF":
|
|
|
|
Web3SignerRequestKind.SyncCommitteeSelectionProof
|
|
|
|
of "SYNC_COMMITTEE_CONTRIBUTION_AND_PROOF":
|
|
|
|
Web3SignerRequestKind.SyncCommitteeContributionAndProof
|
|
|
|
else:
|
|
|
|
reader.raiseUnexpectedValue("Unexpected `type` value")
|
|
|
|
)
|
|
|
|
of "fork_info":
|
|
|
|
if forkInfo.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple `fork_info` fields found",
|
|
|
|
"Web3SignerRequest")
|
|
|
|
forkInfo = some(reader.readValue(Web3SignerForkInfo))
|
|
|
|
of "signingRoot":
|
|
|
|
if signingRoot.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple `signingRoot` fields found",
|
|
|
|
"Web3SignerRequest")
|
|
|
|
signingRoot = some(reader.readValue(Eth2Digest))
|
|
|
|
of "aggregation_slot", "aggregate_and_proof", "block", "beacon_block",
|
|
|
|
"randao_reveal", "voluntary_exit", "sync_committee_message",
|
|
|
|
"sync_aggregator_selection_data", "contribution_and_proof":
|
|
|
|
if data.isSome():
|
|
|
|
reader.raiseUnexpectedField("Multiple data fields found",
|
|
|
|
"Web3SignerRequest")
|
|
|
|
dataName = fieldName
|
|
|
|
data = some(reader.readValue(JsonString))
|
|
|
|
else:
|
|
|
|
# We ignore all unknown fields.
|
|
|
|
discard
|
|
|
|
|
|
|
|
if requestKind.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `type` is missing")
|
|
|
|
|
|
|
|
value =
|
|
|
|
case requestKind.get()
|
|
|
|
of Web3SignerRequestKind.AggregationSlot:
|
|
|
|
if dataName != "aggregation_slot":
|
|
|
|
reader.raiseUnexpectedValue("Field `aggregation_slot` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(Web3SignerAggregationSlotData,
|
|
|
|
data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `aggregation_slot` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(kind: Web3SignerRequestKind.AggregationSlot,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, aggregationSlot: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.AggregateAndProof:
|
|
|
|
if dataName != "aggregate_and_proof":
|
|
|
|
reader.raiseUnexpectedValue("Field `aggregate_and_proof` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(AggregateAndProof, data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `aggregate_and_proof` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.AggregateAndProof,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, aggregateAndProof: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.Attestation:
|
|
|
|
if dataName != "attestation":
|
|
|
|
reader.raiseUnexpectedValue("Field `attestation` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(AttestationData, data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `attestation` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.Attestation,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, attestation: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.Block:
|
|
|
|
if dataName != "block":
|
|
|
|
reader.raiseUnexpectedValue("Field `block` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(phase0.BeaconBlock, data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `block` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.Block,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, blck: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.BlockV2:
|
|
|
|
if dataName != "beacon_block":
|
|
|
|
reader.raiseUnexpectedValue("Field `beacon_block` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(ForkedBeaconBlock, data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `beacon_block` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.BlockV2,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, beaconBlock: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.Deposit:
|
|
|
|
if dataName != "deposit":
|
|
|
|
reader.raiseUnexpectedValue("Field `deposit` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(Web3SignerDepositData, data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `deposit` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.Deposit,
|
|
|
|
signingRoot: signingRoot, deposit: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.RandaoReveal:
|
|
|
|
if dataName != "randao_reveal":
|
|
|
|
reader.raiseUnexpectedValue("Field `randao_reveal` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(Web3SignerRandaoRevealData, data.get(),
|
|
|
|
true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `randao_reveal` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.RandaoReveal,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, randaoReveal: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.VoluntaryExit:
|
|
|
|
if dataName != "voluntary_exit":
|
|
|
|
reader.raiseUnexpectedValue("Field `voluntary_exit` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(VoluntaryExit, data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `voluntary_exit` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.VoluntaryExit,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot, voluntaryExit: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeMessage:
|
|
|
|
if dataName != "sync_committee_message":
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Field `sync_committee_message` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(Web3SignerSyncCommitteeMessageData,
|
|
|
|
data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `sync_committee_message` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.SyncCommitteeMessage,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot,
|
|
|
|
syncCommitteeMessage: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeSelectionProof:
|
|
|
|
if dataName != "sync_aggregator_selection_data":
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Field `sync_aggregator_selection_data` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(SyncAggregatorSelectionData,
|
|
|
|
data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `sync_aggregator_selection_data` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.SyncCommitteeSelectionProof,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot,
|
|
|
|
syncAggregatorSelectionData: data
|
|
|
|
)
|
|
|
|
of Web3SignerRequestKind.SyncCommitteeContributionAndProof:
|
|
|
|
if dataName != "contribution_and_proof":
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Field `contribution_and_proof` is missing")
|
|
|
|
if forkInfo.isNone():
|
|
|
|
reader.raiseUnexpectedValue("Field `fork_info` is missing")
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
let res = decodeJsonString(ContributionAndProof,
|
|
|
|
data.get(), true)
|
|
|
|
if res.isErr():
|
|
|
|
reader.raiseUnexpectedValue(
|
|
|
|
"Incorrect field `contribution_and_proof` format")
|
|
|
|
res.get()
|
|
|
|
Web3SignerRequest(
|
|
|
|
kind: Web3SignerRequestKind.SyncCommitteeContributionAndProof,
|
|
|
|
forkInfo: forkInfo, signingRoot: signingRoot,
|
|
|
|
syncCommitteeContributionAndProof: data
|
|
|
|
)
|
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
proc parseRoot(value: string): Result[Eth2Digest, cstring] =
|
|
|
|
try:
|
|
|
|
ok(Eth2Digest(data: hexToByteArray[32](value)))
|
|
|
|
except ValueError:
|
|
|
|
err("Unable to decode root value")
|
|
|
|
|
2021-03-23 22:50:18 +00:00
|
|
|
proc decodeBody*[T](t: typedesc[T],
|
|
|
|
body: ContentBody): Result[T, cstring] =
|
|
|
|
if body.contentType != "application/json":
|
|
|
|
return err("Unsupported content type")
|
|
|
|
let data =
|
|
|
|
try:
|
|
|
|
RestJson.decode(cast[string](body.data), T)
|
2021-06-29 15:09:29 +00:00
|
|
|
except SerializationError:
|
2021-03-23 22:50:18 +00:00
|
|
|
return err("Unable to deserialize data")
|
2021-06-29 15:09:29 +00:00
|
|
|
except CatchableError:
|
2021-03-23 22:50:18 +00:00
|
|
|
return err("Unexpected deserialization error")
|
|
|
|
ok(data)
|
2021-04-12 16:05:13 +00:00
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc encodeBytes*[T: EncodeTypes](value: T,
|
2021-08-23 10:41:48 +00:00
|
|
|
contentType: string): RestResult[seq[byte]] =
|
2021-07-13 11:15:07 +00:00
|
|
|
case contentType
|
|
|
|
of "application/json":
|
2021-08-23 10:41:48 +00:00
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.writeValue(value)
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except IOError:
|
|
|
|
return err("Input/output error")
|
|
|
|
except SerializationError:
|
|
|
|
return err("Serialization error")
|
|
|
|
ok(data)
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
|
|
|
err("Content-Type not supported")
|
|
|
|
|
|
|
|
proc encodeBytes*[T: EncodeArrays](value: T,
|
|
|
|
contentType: string): RestResult[seq[byte]] =
|
|
|
|
case contentType
|
|
|
|
of "application/json":
|
2021-08-23 10:41:48 +00:00
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
try:
|
|
|
|
var stream = memoryOutput()
|
|
|
|
var writer = JsonWriter[RestJson].init(stream)
|
|
|
|
writer.writeArray(value)
|
|
|
|
stream.getOutput(seq[byte])
|
|
|
|
except IOError:
|
|
|
|
return err("Input/output error")
|
|
|
|
except SerializationError:
|
|
|
|
return err("Serialization error")
|
|
|
|
ok(data)
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
|
|
|
err("Content-Type not supported")
|
|
|
|
|
|
|
|
proc decodeBytes*[T: DecodeTypes](t: typedesc[T], value: openarray[byte],
|
|
|
|
contentType: string): RestResult[T] =
|
2021-10-20 09:58:38 +00:00
|
|
|
const isExtensibleType = t is ExtensibleDecodeTypes
|
2021-07-13 11:15:07 +00:00
|
|
|
case contentType
|
|
|
|
of "application/json":
|
2021-08-03 15:17:11 +00:00
|
|
|
try:
|
2021-10-20 09:58:38 +00:00
|
|
|
ok RestJson.decode(value, T, allowUnknownFields = isExtensibleType)
|
2021-12-05 17:32:41 +00:00
|
|
|
except SerializationError:
|
2021-08-03 15:17:11 +00:00
|
|
|
err("Serialization error")
|
2021-07-13 11:15:07 +00:00
|
|
|
else:
|
|
|
|
err("Content-Type not supported")
|
|
|
|
|
2021-09-27 18:31:11 +00:00
|
|
|
proc decodeBytes*[T: SszDecodeTypes](t: typedesc[T], value: openarray[byte],
|
2021-11-12 22:29:28 +00:00
|
|
|
contentType: string, updateRoot = true): RestResult[T] =
|
2021-09-27 18:31:11 +00:00
|
|
|
case contentType
|
|
|
|
of "application/octet-stream":
|
|
|
|
try:
|
2021-11-12 22:29:28 +00:00
|
|
|
var v: RestResult[T]
|
|
|
|
v.ok(T()) # This optimistically avoids an expensive genericAssign
|
|
|
|
readSszBytes(value, v.get(), updateRoot)
|
|
|
|
v
|
2021-09-27 18:31:11 +00:00
|
|
|
except SerializationError as exc:
|
|
|
|
err("Serialization error")
|
|
|
|
else:
|
|
|
|
err("Content-Type not supported")
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc encodeString*(value: string): RestResult[string] =
|
|
|
|
ok(value)
|
|
|
|
|
2021-10-20 16:32:46 +00:00
|
|
|
proc encodeString*(value: Epoch|Slot|CommitteeIndex|SyncSubcommitteeIndex): RestResult[string] =
|
2021-07-13 11:15:07 +00:00
|
|
|
ok(Base10.toString(uint64(value)))
|
|
|
|
|
|
|
|
proc encodeString*(value: ValidatorSig): RestResult[string] =
|
|
|
|
ok(hexOriginal(toRaw(value)))
|
|
|
|
|
|
|
|
proc encodeString*(value: GraffitiBytes): RestResult[string] =
|
|
|
|
ok(hexOriginal(distinctBase(value)))
|
|
|
|
|
|
|
|
proc encodeString*(value: Eth2Digest): RestResult[string] =
|
|
|
|
ok(hexOriginal(value.data))
|
|
|
|
|
|
|
|
proc encodeString*(value: ValidatorIdent): RestResult[string] =
|
|
|
|
case value.kind
|
|
|
|
of ValidatorQueryKind.Index:
|
|
|
|
ok(Base10.toString(uint64(value.index)))
|
|
|
|
of ValidatorQueryKind.Key:
|
|
|
|
ok(hexOriginal(toRaw(value.key)))
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
proc encodeString*(value: ValidatorPubKey): RestResult[string] =
|
|
|
|
ok(hexOriginal(toRaw(value)))
|
|
|
|
|
2021-07-13 11:15:07 +00:00
|
|
|
proc encodeString*(value: StateIdent): RestResult[string] =
|
|
|
|
case value.kind
|
|
|
|
of StateQueryKind.Slot:
|
|
|
|
ok(Base10.toString(uint64(value.slot)))
|
|
|
|
of StateQueryKind.Root:
|
|
|
|
ok(hexOriginal(value.root.data))
|
|
|
|
of StateQueryKind.Named:
|
|
|
|
case value.value
|
|
|
|
of StateIdentType.Head:
|
|
|
|
ok("head")
|
|
|
|
of StateIdentType.Genesis:
|
|
|
|
ok("genesis")
|
|
|
|
of StateIdentType.Finalized:
|
|
|
|
ok("finalized")
|
|
|
|
of StateIdentType.Justified:
|
|
|
|
ok("justified")
|
2021-08-03 15:17:11 +00:00
|
|
|
|
|
|
|
proc encodeString*(value: BlockIdent): RestResult[string] =
|
|
|
|
case value.kind
|
|
|
|
of BlockQueryKind.Slot:
|
|
|
|
ok(Base10.toString(uint64(value.slot)))
|
|
|
|
of BlockQueryKind.Root:
|
|
|
|
ok(hexOriginal(value.root.data))
|
|
|
|
of BlockQueryKind.Named:
|
|
|
|
case value.value
|
|
|
|
of BlockIdentType.Head:
|
|
|
|
ok("head")
|
|
|
|
of BlockIdentType.Genesis:
|
|
|
|
ok("genesis")
|
|
|
|
of BlockIdentType.Finalized:
|
|
|
|
ok("finalized")
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[PeerStateKind],
|
|
|
|
value: string): Result[PeerStateKind, cstring] =
|
|
|
|
case value
|
|
|
|
of "disconnected":
|
|
|
|
ok(PeerStateKind.Disconnected)
|
|
|
|
of "connecting":
|
|
|
|
ok(PeerStateKind.Connecting)
|
|
|
|
of "connected":
|
|
|
|
ok(PeerStateKind.Connected)
|
|
|
|
of "disconnecting":
|
|
|
|
ok(PeerStateKind.Disconnecting)
|
|
|
|
else:
|
|
|
|
err("Incorrect peer's state value")
|
|
|
|
|
|
|
|
proc encodeString*(value: PeerStateKind): Result[string, cstring] =
|
|
|
|
case value
|
|
|
|
of PeerStateKind.Disconnected:
|
|
|
|
ok("disconnected")
|
|
|
|
of PeerStateKind.Connecting:
|
|
|
|
ok("connecting")
|
|
|
|
of PeerStateKind.Connected:
|
|
|
|
ok("connected")
|
|
|
|
of PeerStateKind.Disconnecting:
|
|
|
|
ok("disconnecting")
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[PeerDirectKind],
|
|
|
|
value: string): Result[PeerDirectKind, cstring] =
|
|
|
|
case value
|
|
|
|
of "inbound":
|
|
|
|
ok(PeerDirectKind.Inbound)
|
|
|
|
of "outbound":
|
|
|
|
ok(PeerDirectKind.Outbound)
|
|
|
|
else:
|
|
|
|
err("Incorrect peer's direction value")
|
|
|
|
|
|
|
|
proc encodeString*(value: PeerDirectKind): Result[string, cstring] =
|
|
|
|
case value
|
|
|
|
of PeerDirectKind.Inbound:
|
|
|
|
ok("inbound")
|
|
|
|
of PeerDirectKind.Outbound:
|
|
|
|
ok("outbound")
|
|
|
|
|
|
|
|
proc encodeString*(peerid: PeerID): Result[string, cstring] =
|
|
|
|
ok($peerid)
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[EventTopic],
|
|
|
|
value: string): Result[EventTopic, cstring] =
|
|
|
|
case value
|
|
|
|
of "head":
|
|
|
|
ok(EventTopic.Head)
|
|
|
|
of "block":
|
|
|
|
ok(EventTopic.Block)
|
|
|
|
of "attestation":
|
|
|
|
ok(EventTopic.Attestation)
|
|
|
|
of "voluntary_exit":
|
|
|
|
ok(EventTopic.VoluntaryExit)
|
|
|
|
of "finalized_checkpoint":
|
|
|
|
ok(EventTopic.FinalizedCheckpoint)
|
|
|
|
of "chain_reorg":
|
|
|
|
ok(EventTopic.ChainReorg)
|
2021-09-22 12:17:15 +00:00
|
|
|
of "contribution_and_proof":
|
|
|
|
ok(EventTopic.ContributionAndProof)
|
2021-08-03 15:17:11 +00:00
|
|
|
else:
|
|
|
|
err("Incorrect event's topic value")
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[ValidatorSig],
|
|
|
|
value: string): Result[ValidatorSig, cstring] =
|
|
|
|
if len(value) != ValidatorSigSize + 2:
|
|
|
|
return err("Incorrect validator signature value length")
|
|
|
|
if value[0] != '0' and value[1] != 'x':
|
|
|
|
return err("Incorrect validator signature encoding")
|
|
|
|
ValidatorSig.fromHex(value)
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
proc decodeString*(t: typedesc[ValidatorPubKey],
|
|
|
|
value: string): Result[ValidatorPubKey, cstring] =
|
|
|
|
if len(value) != ValidatorKeySize + 2:
|
|
|
|
return err("Incorrect validator's key value length")
|
|
|
|
if value[0] != '0' and value[1] != 'x':
|
|
|
|
err("Incorrect validator's key encoding")
|
|
|
|
else:
|
|
|
|
ValidatorPubKey.fromHex(value)
|
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
proc decodeString*(t: typedesc[GraffitiBytes],
|
|
|
|
value: string): Result[GraffitiBytes, cstring] =
|
|
|
|
try:
|
|
|
|
ok(GraffitiBytes.init(value))
|
|
|
|
except ValueError:
|
|
|
|
err("Unable to decode graffiti value")
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[string],
|
|
|
|
value: string): Result[string, cstring] =
|
|
|
|
ok(value)
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[Slot], value: string): Result[Slot, cstring] =
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(Slot(res))
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[Epoch], value: string): Result[Epoch, cstring] =
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(Epoch(res))
|
|
|
|
|
2021-09-23 22:13:25 +00:00
|
|
|
proc decodeString*(t: typedesc[uint64],
|
|
|
|
value: string): Result[uint64, cstring] =
|
|
|
|
Base10.decode(uint64, value)
|
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
proc decodeString*(t: typedesc[StateIdent],
|
|
|
|
value: string): Result[StateIdent, cstring] =
|
|
|
|
if len(value) > 2:
|
|
|
|
if (value[0] == '0') and (value[1] == 'x'):
|
|
|
|
if len(value) != RootHashSize + 2:
|
|
|
|
err("Incorrect state root value length")
|
|
|
|
else:
|
|
|
|
let res = ? parseRoot(value)
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Root, root: res))
|
|
|
|
elif (value[0] in DecimalSet) and (value[1] in DecimalSet):
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Slot, slot: Slot(res)))
|
|
|
|
else:
|
|
|
|
case value
|
|
|
|
of "head":
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Named,
|
|
|
|
value: StateIdentType.Head))
|
|
|
|
of "genesis":
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Named,
|
|
|
|
value: StateIdentType.Genesis))
|
|
|
|
of "finalized":
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Named,
|
|
|
|
value: StateIdentType.Finalized))
|
|
|
|
of "justified":
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Named,
|
|
|
|
value: StateIdentType.Justified))
|
|
|
|
else:
|
|
|
|
err("Incorrect state identifier value")
|
|
|
|
else:
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(StateIdent(kind: StateQueryKind.Slot, slot: Slot(res)))
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[BlockIdent],
|
|
|
|
value: string): Result[BlockIdent, cstring] =
|
|
|
|
if len(value) > 2:
|
|
|
|
if (value[0] == '0') and (value[1] == 'x'):
|
|
|
|
if len(value) != RootHashSize + 2:
|
|
|
|
err("Incorrect block root value length")
|
|
|
|
else:
|
|
|
|
let res = ? parseRoot(value)
|
|
|
|
ok(BlockIdent(kind: BlockQueryKind.Root, root: res))
|
|
|
|
elif (value[0] in DecimalSet) and (value[1] in DecimalSet):
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(BlockIdent(kind: BlockQueryKind.Slot, slot: Slot(res)))
|
|
|
|
else:
|
|
|
|
case value
|
|
|
|
of "head":
|
|
|
|
ok(BlockIdent(kind: BlockQueryKind.Named,
|
|
|
|
value: BlockIdentType.Head))
|
|
|
|
of "genesis":
|
|
|
|
ok(BlockIdent(kind: BlockQueryKind.Named,
|
|
|
|
value: BlockIdentType.Genesis))
|
|
|
|
of "finalized":
|
|
|
|
ok(BlockIdent(kind: BlockQueryKind.Named,
|
|
|
|
value: BlockIdentType.Finalized))
|
|
|
|
else:
|
|
|
|
err("Incorrect block identifier value")
|
|
|
|
else:
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(BlockIdent(kind: BlockQueryKind.Slot, slot: Slot(res)))
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[ValidatorIdent],
|
|
|
|
value: string): Result[ValidatorIdent, cstring] =
|
|
|
|
if len(value) > 2:
|
|
|
|
if (value[0] == '0') and (value[1] == 'x'):
|
|
|
|
if len(value) != ValidatorKeySize + 2:
|
|
|
|
err("Incorrect validator's key value length")
|
|
|
|
else:
|
|
|
|
let res = ? ValidatorPubKey.fromHex(value)
|
|
|
|
ok(ValidatorIdent(kind: ValidatorQueryKind.Key,
|
|
|
|
key: res))
|
|
|
|
elif (value[0] in DecimalSet) and (value[1] in DecimalSet):
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(ValidatorIdent(kind: ValidatorQueryKind.Index,
|
|
|
|
index: RestValidatorIndex(res)))
|
|
|
|
else:
|
|
|
|
err("Incorrect validator identifier value")
|
|
|
|
else:
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
ok(ValidatorIdent(kind: ValidatorQueryKind.Index,
|
|
|
|
index: RestValidatorIndex(res)))
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[PeerID],
|
|
|
|
value: string): Result[PeerID, cstring] =
|
|
|
|
PeerID.init(value)
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[CommitteeIndex],
|
|
|
|
value: string): Result[CommitteeIndex, cstring] =
|
|
|
|
let res = ? Base10.decode(uint64, value)
|
2022-01-08 23:28:49 +00:00
|
|
|
CommitteeIndex.init(res)
|
2021-08-03 15:17:11 +00:00
|
|
|
|
2021-10-20 16:32:46 +00:00
|
|
|
proc decodeString*(t: typedesc[SyncSubcommitteeIndex],
|
|
|
|
value: string): Result[SyncSubcommitteeIndex, cstring] =
|
2022-01-08 23:28:49 +00:00
|
|
|
let res = ? Base10.decode(uint64, value)
|
|
|
|
SyncSubcommitteeIndex.init(res)
|
2021-10-19 17:44:05 +00:00
|
|
|
|
2021-08-03 15:17:11 +00:00
|
|
|
proc decodeString*(t: typedesc[Eth2Digest],
|
|
|
|
value: string): Result[Eth2Digest, cstring] =
|
|
|
|
if len(value) != RootHashSize + 2:
|
|
|
|
return err("Incorrect root value length")
|
|
|
|
if value[0] != '0' and value[1] != 'x':
|
|
|
|
return err("Incorrect root value encoding")
|
|
|
|
parseRoot(value)
|
|
|
|
|
|
|
|
proc decodeString*(t: typedesc[ValidatorFilter],
|
|
|
|
value: string): Result[ValidatorFilter, cstring] =
|
|
|
|
case value
|
|
|
|
of "pending_initialized":
|
|
|
|
ok({ValidatorFilterKind.PendingInitialized})
|
|
|
|
of "pending_queued":
|
|
|
|
ok({ValidatorFilterKind.PendingQueued})
|
|
|
|
of "active_ongoing":
|
|
|
|
ok({ValidatorFilterKind.ActiveOngoing})
|
|
|
|
of "active_exiting":
|
|
|
|
ok({ValidatorFilterKind.ActiveExiting})
|
|
|
|
of "active_slashed":
|
|
|
|
ok({ValidatorFilterKind.ActiveSlashed})
|
|
|
|
of "exited_unslashed":
|
|
|
|
ok({ValidatorFilterKind.ExitedUnslashed})
|
|
|
|
of "exited_slashed":
|
|
|
|
ok({ValidatorFilterKind.ExitedSlashed})
|
|
|
|
of "withdrawal_possible":
|
|
|
|
ok({ValidatorFilterKind.WithdrawalPossible})
|
|
|
|
of "withdrawal_done":
|
|
|
|
ok({ValidatorFilterKind.WithdrawalDone})
|
|
|
|
of "pending":
|
|
|
|
ok({
|
|
|
|
ValidatorFilterKind.PendingInitialized,
|
|
|
|
ValidatorFilterKind.PendingQueued
|
|
|
|
})
|
|
|
|
of "active":
|
|
|
|
ok({
|
|
|
|
ValidatorFilterKind.ActiveOngoing,
|
|
|
|
ValidatorFilterKind.ActiveExiting,
|
|
|
|
ValidatorFilterKind.ActiveSlashed
|
|
|
|
})
|
|
|
|
of "exited":
|
|
|
|
ok({
|
|
|
|
ValidatorFilterKind.ExitedUnslashed,
|
|
|
|
ValidatorFilterKind.ExitedSlashed
|
|
|
|
})
|
|
|
|
of "withdrawal":
|
|
|
|
ok({
|
|
|
|
ValidatorFilterKind.WithdrawalPossible,
|
|
|
|
ValidatorFilterKind.WithdrawalDone
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
err("Incorrect validator state identifier value")
|