Skip to content

Commit

Permalink
Fix #3650 (participation format in BeaconState result is out of spec)
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Jun 19, 2022
1 parent c4383e2 commit 3856dfd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
7 changes: 6 additions & 1 deletion AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ OK: 12/12 Fail: 0/12 Skip: 0/12
+ vesion 2 single remote OK
```
OK: 3/3 Fail: 0/3 Skip: 0/3
## Serialization/deserialization [Preset: mainnet]
```diff
+ Deserialization test vectors OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
## Slashing Interchange tests [Preset: mainnet]
```diff
+ Slashing test: duplicate_pubkey_not_slashable.json OK
Expand Down Expand Up @@ -568,4 +573,4 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
OK: 9/9 Fail: 0/9 Skip: 0/9

---TOTAL---
OK: 317/322 Fail: 0/322 Skip: 5/322
OK: 318/323 Fail: 0/323 Skip: 5/323
47 changes: 45 additions & 2 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,51 @@ type
GetPhase0StateSszResponse |
GetPhase0BlockSszResponse

AltairBeaconState = altair.BeaconState
BellatrixBeaconState = bellatrix.BeaconState

{.push raises: [Defect].}

proc writePreviousEpochParticipation(writer: var JsonWriter[RestJson],
values: EpochParticipationFlags)
{.raises: [IOError, Defect].} =
for e in writer.stepwiseArrayCreation(values):
writer.writeValue $e

proc readPreviousEpochParticipation(
reader: var JsonReader[RestJson]): 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 result.data.add(uint8(parsed)):
reader.raiseUnexpectedValue("The participation flags list size exceeds limit")

RestJson.useCustomSerialization(AltairBeaconState.current_epoch_participation):
read: readPreviousEpochParticipation(reader)
write: writePreviousEpochParticipation(writer, value)

RestJson.useCustomSerialization(BellatrixBeaconState.current_epoch_participation):
read: readPreviousEpochParticipation(reader)
write: writePreviousEpochParticipation(writer, value)

RestJson.useCustomSerialization(AltairBeaconState.previous_epoch_participation):
read: readPreviousEpochParticipation(reader)
write: writePreviousEpochParticipation(writer, value)

RestJson.useCustomSerialization(BellatrixBeaconState.previous_epoch_participation):
read: readPreviousEpochParticipation(reader)
write: writePreviousEpochParticipation(writer, value)

proc prepareJsonResponse*(t: typedesc[RestApiResponse], d: auto): seq[byte] =
let res =
block:
Expand Down Expand Up @@ -1261,8 +1304,8 @@ proc readValue*(reader: var JsonReader[RestJson],
reader.raiseUnexpectedValue("Incorrect altair beacon state format")
toValue(bellatrixData)

proc writeValue*(writer: var JsonWriter[RestJson], value: ForkedHashedBeaconState) {.
raises: [IOError, Defect].} =
proc writeValue*(writer: var JsonWriter[RestJson], value: ForkedHashedBeaconState)
{.raises: [IOError, Defect].} =
writer.beginRecord()
case value.kind
of BeaconStateFork.Phase0:
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-confutils
2 changes: 1 addition & 1 deletion vendor/nim-serialization

0 comments on commit 3856dfd

Please sign in to comment.