From 41dfc9ed91f3fa2847b5b5767a14b0f2e47b0604 Mon Sep 17 00:00:00 2001 From: tersec Date: Wed, 25 Oct 2023 21:50:59 +0000 Subject: [PATCH] add `KZGProof` and `Blob` REST JSON serializations (#5526) * add KZGProof and Blob REST JSON serializations * error summary updates * copyright notice * consistent integer types for Nim 2.0 --- AllTests-mainnet.md | 10 +-- .../eth2_apis/eth2_rest_serialization.nim | 25 +++++-- tests/simulation/restapi.sh | 13 ++++ tests/test_rest_json_serialization.nim | 69 ++++++++++++++++++- 4 files changed, 107 insertions(+), 10 deletions(-) diff --git a/AllTests-mainnet.md b/AllTests-mainnet.md index cad55eab8..48daf1f08 100644 --- a/AllTests-mainnet.md +++ b/AllTests-mainnet.md @@ -430,11 +430,13 @@ OK: 12/12 Fail: 0/12 Skip: 0/12 OK: 1/1 Fail: 0/1 Skip: 0/1 ## REST JSON encoding and decoding ```diff -+ DenebSignedBlockContents OK ++ Blob OK ++ DenebSignedBlockContents decoding OK + KzgCommitment OK -+ RestPublishedSignedBlockContents OK ++ KzgProof OK ++ RestPublishedSignedBlockContents decoding OK ``` -OK: 3/3 Fail: 0/3 Skip: 0/3 +OK: 5/5 Fail: 0/5 Skip: 0/5 ## Remove keystore testing suite ```diff + Many remotes OK @@ -712,4 +714,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2 OK: 9/9 Fail: 0/9 Skip: 0/9 ---TOTAL--- -OK: 401/406 Fail: 0/406 Skip: 5/406 +OK: 403/408 Fail: 0/408 Skip: 5/408 diff --git a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim index eed41b03a..5498610c4 100644 --- a/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim +++ b/beacon_chain/spec/eth2_apis/eth2_rest_serialization.nim @@ -950,9 +950,26 @@ 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) {. +## Blob +## https://github.com/ethereum/beacon-APIs/blob/v2.4.2/types/primitive.yaml#L129-L133 +proc readValue*(reader: var JsonReader[RestJson], value: var Blob) {. + raises: [IOError, SerializationError].} = + try: + hexToByteArray(reader.readValue(string), distinctBase(value)) + except ValueError: + raiseUnexpectedValue(reader, + "Blob value should be a valid hex string") + +proc writeValue*( + writer: var JsonWriter[RestJson], value: Blob +) {.raises: [IOError].} = + writeValue(writer, hexOriginal(distinctBase(value))) + +## KzgCommitment and KzgProof; both are the same type, but this makes it +## explicit. +## https://github.com/ethereum/beacon-APIs/blob/v2.4.2/types/primitive.yaml#L135-L146 +proc readValue*(reader: var JsonReader[RestJson], + value: var (KzgCommitment|KzgProof)) {. raises: [IOError, SerializationError].} = try: hexToByteArray(reader.readValue(string), distinctBase(value)) @@ -961,7 +978,7 @@ proc readValue*(reader: var JsonReader[RestJson], value: var KzgCommitment) {. "KzgCommitment value should be a valid hex string") proc writeValue*( - writer: var JsonWriter[RestJson], value: KzgCommitment + writer: var JsonWriter[RestJson], value: KzgCommitment | KzgProof ) {.raises: [IOError].} = writeValue(writer, hexOriginal(distinctBase(value))) diff --git a/tests/simulation/restapi.sh b/tests/simulation/restapi.sh index aaac05a2a..7298fd30d 100755 --- a/tests/simulation/restapi.sh +++ b/tests/simulation/restapi.sh @@ -1,4 +1,11 @@ #!/usr/bin/env bash +# +# beacon_chain +# Copyright (c) 2021-2023 Status Research & Development GmbH +# 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. set -e @@ -196,6 +203,8 @@ DEPOSIT_CONTRACT_BLOCK="0x000000000000000000000000000000000000000000000000000000 echo Wrote $RUNTIME_CONFIG_FILE: +# DENEB_FORK_EPOCH must be non-FAR_FUTURE_EPOCH to trigger creation of blob +# sidecar database table. tee "$RUNTIME_CONFIG_FILE" <