Skip to content

Commit

Permalink
Fix unknown fields should be ignored when parsing liveness responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Sep 11, 2023
1 parent ff176a1 commit 49b3615
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,9 @@ template unrecognizedFieldWarning =
trace "JSON field not recognized by the current version of Nimbus. Consider upgrading",
fieldName, typeName = typetraits.name(typeof value)

template unrecognizedFieldIgnore =
discard readValue(reader, JsonString)

## ForkedBeaconBlock
template prepareForkedBlockReading(
reader: var JsonReader[RestJson],
Expand Down Expand Up @@ -2767,7 +2770,7 @@ proc readValue*(reader: var JsonReader[RestJson],
"Multiple `active` fields found", "RestActivityItem")
active = some(reader.readValue(bool))
else:
discard
unrecognizedFieldIgnore()

if index.isNone():
reader.raiseUnexpectedValue("Missing or empty `index` value")
Expand Down Expand Up @@ -2807,7 +2810,7 @@ proc readValue*(reader: var JsonReader[RestJson],
"Multiple `is_live` fields found", "RestLivenessItem")
isLive = some(reader.readValue(bool))
else:
discard
unrecognizedFieldIgnore()

if index.isNone():
reader.raiseUnexpectedValue("Missing or empty `index` value")
Expand Down Expand Up @@ -2935,8 +2938,7 @@ proc readValue*(reader: var JsonReader[RestJson],
"RestErrorMessage")
stacktraces = some(reader.readValue(seq[string]))
else:
# We ignore all additional fields.
discard reader.readValue(JsonString)
unrecognizedFieldIgnore()

if code.isNone():
reader.raiseUnexpectedValue("Missing or invalid `code` value")
Expand Down
12 changes: 12 additions & 0 deletions tests/test_validator_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,15 @@ suite "Validator Client test suite":
check:
res.isOk()
res.get().data == expect

let vector = stringToBytes(
"{\"data\":[{\"index\":\"100000\",\"epoch\":\"202919\",\"is_live\":true}]}")

let contentType = getContentType("application/json").tryGet()
let res = decodeBytes(
GetValidatorsLivenessResponse, vector, Opt.some(contentType))
check:
res.isOk()
len(res.get().data) == 1
res.get().data[0].index == 100000
res.get().data[0].is_live == true

0 comments on commit 49b3615

Please sign in to comment.