move `EpochParticipationFlags` serialization to others (#4167)

The REST `writeValue/readValue` serialization functions are grouped up
with the exception of `EpochParticipationFlags`. Moving that to others.
This commit is contained in:
Etan Kissling 2022-09-23 20:29:31 +02:00 committed by GitHub
parent 77ea188c92
commit 7ac95c6ee7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 24 deletions

View File

@ -145,30 +145,6 @@ when (NimMajor, NimMinor) < (1, 4):
else:
{.push raises: [].}
proc writeValue*(writer: var JsonWriter[RestJson],
epochFlags: EpochParticipationFlags)
{.raises: [IOError, Defect].} =
for e in writer.stepwiseArrayCreation(epochFlags.asHashList):
writer.writeValue $e
proc readValue*(reader: var JsonReader[RestJson],
epochFlags: var EpochParticipationFlags)
{.raises: [SerializationError, IOError, Defect].} =
# Please note that this function won't compute the cached hash tree roots
# immediately. They will be computed on the first HTR attempt.
for e in reader.readArray(string):
let parsed = try:
parseBiggestUInt(e)
except ValueError as err:
reader.raiseUnexpectedValue("A string-encoded 8-bit usigned integer value expected")
if parsed > uint8.high:
reader.raiseUnexpectedValue("The usigned integer value should fit in 8 bits")
if not epochFlags.data.add(uint8(parsed)):
reader.raiseUnexpectedValue("The participation flags list size exceeds limit")
proc prepareJsonResponse*(t: typedesc[RestApiResponse], d: auto): seq[byte] =
let res =
block:
@ -572,6 +548,34 @@ proc readValue*(reader: var JsonReader[RestJson], value: var Epoch) {.
else:
reader.raiseUnexpectedValue($res.error())
## EpochParticipationFlags
proc writeValue*(writer: var JsonWriter[RestJson],
epochFlags: EpochParticipationFlags)
{.raises: [IOError, Defect].} =
for e in writer.stepwiseArrayCreation(epochFlags.asHashList):
writer.writeValue $e
proc readValue*(reader: var JsonReader[RestJson],
epochFlags: var EpochParticipationFlags)
{.raises: [SerializationError, IOError, Defect].} =
# Please note that this function won't compute the cached hash tree roots
# immediately. They will be computed on the first HTR attempt.
for e in reader.readArray(string):
let parsed = try:
parseBiggestUInt(e)
except ValueError as err:
reader.raiseUnexpectedValue(
"A string-encoded 8-bit usigned integer value expected")
if parsed > uint8.high:
reader.raiseUnexpectedValue(
"The usigned integer value should fit in 8 bits")
if not epochFlags.data.add(uint8(parsed)):
reader.raiseUnexpectedValue(
"The participation flags list size exceeds limit")
## ValidatorIndex
proc writeValue*(writer: var JsonWriter[RestJson], value: ValidatorIndex)
{.raises: [IOError, Defect].} =