From 0bfc933c504ea924dfaa3f136a1de4651d79f31a Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 6 Sep 2023 05:49:57 +1000 Subject: [PATCH] Deneb review suggestions (2) (#4680) * Serde deny unknown fields * Require 0x prefix --- .../src/cases/kzg_blob_to_kzg_commitment.rs | 3 ++- .../src/cases/kzg_compute_blob_kzg_proof.rs | 3 ++- .../ef_tests/src/cases/kzg_compute_kzg_proof.rs | 3 ++- .../src/cases/kzg_verify_blob_kzg_proof.rs | 16 ++++++++++++---- .../src/cases/kzg_verify_blob_kzg_proof_batch.rs | 3 ++- .../ef_tests/src/cases/kzg_verify_kzg_proof.rs | 3 ++- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/testing/ef_tests/src/cases/kzg_blob_to_kzg_commitment.rs b/testing/ef_tests/src/cases/kzg_blob_to_kzg_commitment.rs index d26d57de3..bb37b4790 100644 --- a/testing/ef_tests/src/cases/kzg_blob_to_kzg_commitment.rs +++ b/testing/ef_tests/src/cases/kzg_blob_to_kzg_commitment.rs @@ -6,12 +6,13 @@ use serde_derive::Deserialize; use std::marker::PhantomData; #[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] pub struct KZGBlobToKZGCommitmentInput { pub blob: String, } #[derive(Debug, Clone, Deserialize)] -#[serde(bound = "E: EthSpec")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] pub struct KZGBlobToKZGCommitment { pub input: KZGBlobToKZGCommitmentInput, pub output: Option, diff --git a/testing/ef_tests/src/cases/kzg_compute_blob_kzg_proof.rs b/testing/ef_tests/src/cases/kzg_compute_blob_kzg_proof.rs index 598fd2e78..59929678d 100644 --- a/testing/ef_tests/src/cases/kzg_compute_blob_kzg_proof.rs +++ b/testing/ef_tests/src/cases/kzg_compute_blob_kzg_proof.rs @@ -6,13 +6,14 @@ use serde_derive::Deserialize; use std::marker::PhantomData; #[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] pub struct KZGComputeBlobKZGProofInput { pub blob: String, pub commitment: String, } #[derive(Debug, Clone, Deserialize)] -#[serde(bound = "E: EthSpec")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] pub struct KZGComputeBlobKZGProof { pub input: KZGComputeBlobKZGProofInput, pub output: Option, diff --git a/testing/ef_tests/src/cases/kzg_compute_kzg_proof.rs b/testing/ef_tests/src/cases/kzg_compute_kzg_proof.rs index 7483bed04..1c9ca6a58 100644 --- a/testing/ef_tests/src/cases/kzg_compute_kzg_proof.rs +++ b/testing/ef_tests/src/cases/kzg_compute_kzg_proof.rs @@ -13,13 +13,14 @@ pub fn parse_point(point: &str) -> Result { } #[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] pub struct KZGComputeKZGProofInput { pub blob: String, pub z: String, } #[derive(Debug, Clone, Deserialize)] -#[serde(bound = "E: EthSpec")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] pub struct KZGComputeKZGProof { pub input: KZGComputeKZGProofInput, pub output: Option<(String, Hash256)>, diff --git a/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof.rs b/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof.rs index 11bb20478..a813efdb7 100644 --- a/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof.rs +++ b/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof.rs @@ -16,7 +16,7 @@ pub fn get_kzg() -> Result, Error> { } pub fn parse_proof(proof: &str) -> Result { - hex::decode(&proof[2..]) + hex::decode(strip_0x(proof)?) .map_err(|e| Error::FailedToParseTest(format!("Failed to parse proof: {:?}", e))) .and_then(|bytes| { bytes @@ -27,7 +27,7 @@ pub fn parse_proof(proof: &str) -> Result { } pub fn parse_commitment(commitment: &str) -> Result { - hex::decode(&commitment[2..]) + hex::decode(strip_0x(commitment)?) .map_err(|e| Error::FailedToParseTest(format!("Failed to parse commitment: {:?}", e))) .and_then(|bytes| { bytes.try_into().map_err(|e| { @@ -38,7 +38,7 @@ pub fn parse_commitment(commitment: &str) -> Result { } pub fn parse_blob(blob: &str) -> Result, Error> { - hex::decode(&blob[2..]) + hex::decode(strip_0x(blob)?) .map_err(|e| Error::FailedToParseTest(format!("Failed to parse blob: {:?}", e))) .and_then(|bytes| { Blob::::new(bytes) @@ -46,7 +46,15 @@ pub fn parse_blob(blob: &str) -> Result, Error> { }) } +fn strip_0x(s: &str) -> Result<&str, Error> { + s.strip_prefix("0x").ok_or(Error::FailedToParseTest(format!( + "Hex is missing 0x prefix: {}", + s + ))) +} + #[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] pub struct KZGVerifyBlobKZGProofInput { pub blob: String, pub commitment: String, @@ -54,7 +62,7 @@ pub struct KZGVerifyBlobKZGProofInput { } #[derive(Debug, Clone, Deserialize)] -#[serde(bound = "E: EthSpec")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] pub struct KZGVerifyBlobKZGProof { pub input: KZGVerifyBlobKZGProofInput, pub output: Option, diff --git a/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof_batch.rs b/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof_batch.rs index 90dc1614b..5e402e8da 100644 --- a/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof_batch.rs +++ b/testing/ef_tests/src/cases/kzg_verify_blob_kzg_proof_batch.rs @@ -5,6 +5,7 @@ use serde_derive::Deserialize; use std::marker::PhantomData; #[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] pub struct KZGVerifyBlobKZGProofBatchInput { pub blobs: Vec, pub commitments: Vec, @@ -12,7 +13,7 @@ pub struct KZGVerifyBlobKZGProofBatchInput { } #[derive(Debug, Clone, Deserialize)] -#[serde(bound = "E: EthSpec")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] pub struct KZGVerifyBlobKZGProofBatch { pub input: KZGVerifyBlobKZGProofBatchInput, pub output: Option, diff --git a/testing/ef_tests/src/cases/kzg_verify_kzg_proof.rs b/testing/ef_tests/src/cases/kzg_verify_kzg_proof.rs index a6acbec18..aafd99909 100644 --- a/testing/ef_tests/src/cases/kzg_verify_kzg_proof.rs +++ b/testing/ef_tests/src/cases/kzg_verify_kzg_proof.rs @@ -5,6 +5,7 @@ use serde_derive::Deserialize; use std::marker::PhantomData; #[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields)] pub struct KZGVerifyKZGProofInput { pub commitment: String, pub z: String, @@ -13,7 +14,7 @@ pub struct KZGVerifyKZGProofInput { } #[derive(Debug, Clone, Deserialize)] -#[serde(bound = "E: EthSpec")] +#[serde(bound = "E: EthSpec", deny_unknown_fields)] pub struct KZGVerifyKZGProof { pub input: KZGVerifyKZGProofInput, pub output: Option,