Fix REST JSON KzgCommitment encoding/decoding to use hex strings per spec (#5522)

This commit is contained in:
tersec 2023-10-24 15:22:59 +00:00 committed by GitHub
parent 468ee6a7ed
commit 5aa5095e17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 5 deletions

View File

@ -428,12 +428,13 @@ OK: 12/12 Fail: 0/12 Skip: 0/12
+ prune states OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
## REST JSON decoding
## REST JSON encoding and decoding
```diff
+ DenebSignedBlockContents OK
+ KzgCommitment OK
+ RestPublishedSignedBlockContents OK
```
OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 3/3 Fail: 0/3 Skip: 0/3
## Remove keystore testing suite
```diff
+ Many remotes OK
@ -711,4 +712,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9
---TOTAL---
OK: 400/405 Fail: 0/405 Skip: 5/405
OK: 401/406 Fail: 0/406 Skip: 5/406

View File

@ -950,6 +950,21 @@ proc writeValue*(
) {.raises: [IOError].} =
writeValue(writer, hexOriginal(distinctBase(value)))
## KzgCommitment
# https://github.com/ethereum/beacon-APIs/blob/d934a03187729635bef06ca7f3c067645c3eab15/types/primitive.yaml#L135-L140
proc readValue*(reader: var JsonReader[RestJson], value: var KzgCommitment) {.
raises: [IOError, SerializationError].} =
try:
hexToByteArray(reader.readValue(string), distinctBase(value))
except ValueError:
raiseUnexpectedValue(reader,
"KzgCommitment value should be a valid hex string")
proc writeValue*(
writer: var JsonWriter[RestJson], value: KzgCommitment
) {.raises: [IOError].} =
writeValue(writer, hexOriginal(distinctBase(value)))
## GraffitiBytes
proc writeValue*(
writer: var JsonWriter[RestJson], value: GraffitiBytes
@ -1821,7 +1836,7 @@ proc readValue*(reader: var JsonReader[RestJson],
value = RestPublishedSignedBlockContents(
kind: ConsensusFork.Deneb,
denebData: DenebSignedBlockContents(
# Constructed to be interally consistent
# Constructed to be internally consistent
signed_block: signed_message.get().distinctBase.denebData,
signed_blob_sidecars: signed_blob_sidecars.get()
)

View File

@ -203,7 +203,16 @@ const denebSignedContents = """
}
"""
suite "REST JSON decoding":
# Can't be in same namespace as some other KZG-related fromHex overloads due to
# https://github.com/nim-lang/Nim/issues/22861
from stew/byteutils import hexToByteArray
func fromHex(T: typedesc[KzgCommitment], s: string): T {.
raises: [ValueError].} =
var res: T
hexToByteArray(s, res)
res
suite "REST JSON encoding and decoding":
test "DenebSignedBlockContents":
check: hash_tree_root(RestJson.decode(
denebSignedContents, DenebSignedBlockContents, requireAllFields = true,
@ -216,3 +225,33 @@ suite "REST JSON decoding":
requireAllFields = true, allowUnknownFields = true).denebData) ==
Eth2Digest.fromHex(
"0x6b9fce0e35ee7af9b061f244706c4eda43c16e9dcc5b1cc817ed0671f49d16a8")
test "KzgCommitment":
let
zeroString =
"\"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\""
randString =
"\"0xe2822fdd03685968091c79b1f81d17ed646196c920baecf927a6abbe45cd2d930a692e85ff5d96ebe36d99a57c74d5cb\""
zeroKzgCommitment = KzgCommitment.fromHex(
"0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
randKzgCommitment = KzgCommitment.fromHex(
"0xe2822fdd03685968091c79b1f81d17ed646196c920baecf927a6abbe45cd2d930a692e85ff5d96ebe36d99a57c74d5cb")
check:
RestJson.decode(
zeroString, KzgCommitment, requireAllFields = true,
allowUnknownFields = true) == zeroKzgCommitment
RestJson.decode(
zeroString, KzgCommitment, requireAllFields = true,
allowUnknownFields = true) != randKzgCommitment
RestJson.decode(
randString, KzgCommitment, requireAllFields = true,
allowUnknownFields = true) != zeroKzgCommitment
RestJson.decode(
randString, KzgCommitment, requireAllFields = true,
allowUnknownFields = true) == randKzgCommitment
RestJson.encode(zeroKzgCommitment) == zeroString
RestJson.encode(zeroKzgCommitment) != randString
RestJson.encode(randKzgCommitment) != zeroString
RestJson.encode(randKzgCommitment) == randString