Deneb review suggestions (2) (#4680)

* Serde deny unknown fields

* Require 0x prefix
This commit is contained in:
Paul Hauner 2023-09-06 05:49:57 +10:00 committed by GitHub
parent e783a40e01
commit 0bfc933c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 9 deletions

View File

@ -6,12 +6,13 @@ use serde_derive::Deserialize;
use std::marker::PhantomData; use std::marker::PhantomData;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KZGBlobToKZGCommitmentInput { pub struct KZGBlobToKZGCommitmentInput {
pub blob: String, pub blob: String,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec", deny_unknown_fields)]
pub struct KZGBlobToKZGCommitment<E: EthSpec> { pub struct KZGBlobToKZGCommitment<E: EthSpec> {
pub input: KZGBlobToKZGCommitmentInput, pub input: KZGBlobToKZGCommitmentInput,
pub output: Option<String>, pub output: Option<String>,

View File

@ -6,13 +6,14 @@ use serde_derive::Deserialize;
use std::marker::PhantomData; use std::marker::PhantomData;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KZGComputeBlobKZGProofInput { pub struct KZGComputeBlobKZGProofInput {
pub blob: String, pub blob: String,
pub commitment: String, pub commitment: String,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec", deny_unknown_fields)]
pub struct KZGComputeBlobKZGProof<E: EthSpec> { pub struct KZGComputeBlobKZGProof<E: EthSpec> {
pub input: KZGComputeBlobKZGProofInput, pub input: KZGComputeBlobKZGProofInput,
pub output: Option<String>, pub output: Option<String>,

View File

@ -13,13 +13,14 @@ pub fn parse_point(point: &str) -> Result<Hash256, Error> {
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KZGComputeKZGProofInput { pub struct KZGComputeKZGProofInput {
pub blob: String, pub blob: String,
pub z: String, pub z: String,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec", deny_unknown_fields)]
pub struct KZGComputeKZGProof<E: EthSpec> { pub struct KZGComputeKZGProof<E: EthSpec> {
pub input: KZGComputeKZGProofInput, pub input: KZGComputeKZGProofInput,
pub output: Option<(String, Hash256)>, pub output: Option<(String, Hash256)>,

View File

@ -16,7 +16,7 @@ pub fn get_kzg<P: KzgPreset>() -> Result<Kzg<P>, Error> {
} }
pub fn parse_proof(proof: &str) -> Result<KzgProof, Error> { pub fn parse_proof(proof: &str) -> Result<KzgProof, Error> {
hex::decode(&proof[2..]) hex::decode(strip_0x(proof)?)
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse proof: {:?}", e))) .map_err(|e| Error::FailedToParseTest(format!("Failed to parse proof: {:?}", e)))
.and_then(|bytes| { .and_then(|bytes| {
bytes bytes
@ -27,7 +27,7 @@ pub fn parse_proof(proof: &str) -> Result<KzgProof, Error> {
} }
pub fn parse_commitment(commitment: &str) -> Result<KzgCommitment, Error> { pub fn parse_commitment(commitment: &str) -> Result<KzgCommitment, Error> {
hex::decode(&commitment[2..]) hex::decode(strip_0x(commitment)?)
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse commitment: {:?}", e))) .map_err(|e| Error::FailedToParseTest(format!("Failed to parse commitment: {:?}", e)))
.and_then(|bytes| { .and_then(|bytes| {
bytes.try_into().map_err(|e| { bytes.try_into().map_err(|e| {
@ -38,7 +38,7 @@ pub fn parse_commitment(commitment: &str) -> Result<KzgCommitment, Error> {
} }
pub fn parse_blob<E: EthSpec>(blob: &str) -> Result<Blob<E>, Error> { pub fn parse_blob<E: EthSpec>(blob: &str) -> Result<Blob<E>, Error> {
hex::decode(&blob[2..]) hex::decode(strip_0x(blob)?)
.map_err(|e| Error::FailedToParseTest(format!("Failed to parse blob: {:?}", e))) .map_err(|e| Error::FailedToParseTest(format!("Failed to parse blob: {:?}", e)))
.and_then(|bytes| { .and_then(|bytes| {
Blob::<E>::new(bytes) Blob::<E>::new(bytes)
@ -46,7 +46,15 @@ pub fn parse_blob<E: EthSpec>(blob: &str) -> Result<Blob<E>, 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)] #[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KZGVerifyBlobKZGProofInput { pub struct KZGVerifyBlobKZGProofInput {
pub blob: String, pub blob: String,
pub commitment: String, pub commitment: String,
@ -54,7 +62,7 @@ pub struct KZGVerifyBlobKZGProofInput {
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec", deny_unknown_fields)]
pub struct KZGVerifyBlobKZGProof<E: EthSpec> { pub struct KZGVerifyBlobKZGProof<E: EthSpec> {
pub input: KZGVerifyBlobKZGProofInput, pub input: KZGVerifyBlobKZGProofInput,
pub output: Option<bool>, pub output: Option<bool>,

View File

@ -5,6 +5,7 @@ use serde_derive::Deserialize;
use std::marker::PhantomData; use std::marker::PhantomData;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KZGVerifyBlobKZGProofBatchInput { pub struct KZGVerifyBlobKZGProofBatchInput {
pub blobs: Vec<String>, pub blobs: Vec<String>,
pub commitments: Vec<String>, pub commitments: Vec<String>,
@ -12,7 +13,7 @@ pub struct KZGVerifyBlobKZGProofBatchInput {
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec", deny_unknown_fields)]
pub struct KZGVerifyBlobKZGProofBatch<E: EthSpec> { pub struct KZGVerifyBlobKZGProofBatch<E: EthSpec> {
pub input: KZGVerifyBlobKZGProofBatchInput, pub input: KZGVerifyBlobKZGProofBatchInput,
pub output: Option<bool>, pub output: Option<bool>,

View File

@ -5,6 +5,7 @@ use serde_derive::Deserialize;
use std::marker::PhantomData; use std::marker::PhantomData;
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KZGVerifyKZGProofInput { pub struct KZGVerifyKZGProofInput {
pub commitment: String, pub commitment: String,
pub z: String, pub z: String,
@ -13,7 +14,7 @@ pub struct KZGVerifyKZGProofInput {
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")] #[serde(bound = "E: EthSpec", deny_unknown_fields)]
pub struct KZGVerifyKZGProof<E: EthSpec> { pub struct KZGVerifyKZGProof<E: EthSpec> {
pub input: KZGVerifyKZGProofInput, pub input: KZGVerifyKZGProofInput,
pub output: Option<bool>, pub output: Option<bool>,