Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifications for ./crypto #4677

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions beacon_node/lighthouse_network/src/rpc/codec/ssz_snappy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ mod tests {
SignedBeaconBlock::from_block(full_block, Signature::empty())
}

fn default_blob_sidecar() -> Arc<BlobSidecar<Spec>> {
fn empty_blob_sidecar() -> Arc<BlobSidecar<Spec>> {
Arc::new(BlobSidecar::empty())
}

Expand Down Expand Up @@ -1048,21 +1048,21 @@ mod tests {
assert_eq!(
encode_then_decode_response(
SupportedProtocol::BlobsByRangeV1,
RPCCodedResponse::Success(RPCResponse::BlobsByRange(default_blob_sidecar())),
RPCCodedResponse::Success(RPCResponse::BlobsByRange(empty_blob_sidecar())),
ForkName::Deneb,
&chain_spec
),
Ok(Some(RPCResponse::BlobsByRange(default_blob_sidecar()))),
Ok(Some(RPCResponse::BlobsByRange(empty_blob_sidecar()))),
);

assert_eq!(
encode_then_decode_response(
SupportedProtocol::BlobsByRootV1,
RPCCodedResponse::Success(RPCResponse::BlobsByRoot(default_blob_sidecar())),
RPCCodedResponse::Success(RPCResponse::BlobsByRoot(empty_blob_sidecar())),
ForkName::Deneb,
&chain_spec
),
Ok(Some(RPCResponse::BlobsByRoot(default_blob_sidecar()))),
Ok(Some(RPCResponse::BlobsByRoot(empty_blob_sidecar()))),
);
}

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/lighthouse_network/tests/rpc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn test_blobs_by_range_chunked_rpc() {
});

// BlocksByRange Response
let blob = BlobSidecar::<E>::default();
let blob = BlobSidecar::<E>::empty();

let rpc_response = Response::BlobsByRange(Some(Arc::new(blob)));

Expand Down
2 changes: 1 addition & 1 deletion consensus/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ harness = false
[dependencies]
merkle_proof = { path = "../../consensus/merkle_proof" }
bls = { path = "../../crypto/bls", features = ["arbitrary"] }
kzg = { path = "../../crypto/kzg", features = ["arbitrary"] }
kzg = { path = "../../crypto/kzg" }
compare_fields = { path = "../../common/compare_fields" }
compare_fields_derive = { path = "../../common/compare_fields_derive" }
eth2_interop_keypairs = { path = "../../common/eth2_interop_keypairs" }
Expand Down
15 changes: 11 additions & 4 deletions consensus/types/src/blob_sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl Ord for BlobIdentifier {
Encode,
Decode,
TreeHash,
Default,
TestRandom,
Derivative,
arbitrary::Arbitrary,
Expand Down Expand Up @@ -120,7 +119,16 @@ impl<T: EthSpec> BlobSidecar<T> {
}

pub fn empty() -> Self {
Self::default()
Self {
block_root: Hash256::zero(),
index: 0,
slot: Slot::new(0),
block_parent_root: Hash256::zero(),
proposer_index: 0,
blob: Blob::<T>::default(),
kzg_commitment: KzgCommitment::empty_for_testing(),
kzg_proof: KzgProof::empty(),
}
}

pub fn random_valid<R: Rng>(rng: &mut R, kzg: &Kzg<T::Kzg>) -> Result<Self, String> {
Expand Down Expand Up @@ -154,7 +162,7 @@ impl<T: EthSpec> BlobSidecar<T> {
blob,
kzg_commitment: commitment,
kzg_proof: proof,
..Default::default()
..Self::empty()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool trick! didn't know we could use arbitrary functions to fill remaining fields.

})
}

Expand Down Expand Up @@ -198,7 +206,6 @@ impl<T: EthSpec> BlobSidecar<T> {
Encode,
Decode,
TreeHash,
Default,
TestRandom,
Derivative,
arbitrary::Arbitrary,
Expand Down
4 changes: 2 additions & 2 deletions crypto/kzg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ hex = "0.4.2"
ethereum_hashing = "1.0.0-beta.2"
c-kzg = { git = "https://github.com/ethereum/c-kzg-4844", rev = "fa3c62989527073fdce8b2138bb27a52bb2407c5" , features = ["mainnet-spec"]}
c_kzg_min = { package = "c-kzg", git = "https://github.com/ethereum//c-kzg-4844", rev = "fa3c62989527073fdce8b2138bb27a52bb2407c5", features = ["minimal-spec"], optional = true }
arbitrary = { version = "1.0", features = ["derive"], optional = true }
arbitrary = { version = "1.0", features = ["derive"] }

[features]
# TODO(deneb): enabled by default for convenience, would need more cfg magic to disable
default = ["c_kzg_min"]
minimal-spec = ["c_kzg_min"]
minimal-spec = ["c_kzg_min"]
36 changes: 8 additions & 28 deletions crypto/kzg/src/kzg_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
use tree_hash::{Hash256, PackedEncoding, TreeHash};

pub const BLOB_COMMITMENT_VERSION_KZG: u8 = 0x01;
pub const VERSIONED_HASH_VERSION_KZG: u8 = 0x01;

#[derive(Derivative, Clone, Copy, Encode, Decode)]
#[derivative(PartialEq, Eq, Hash)]
Expand All @@ -19,9 +19,13 @@ pub struct KzgCommitment(pub [u8; c_kzg::BYTES_PER_COMMITMENT]);
impl KzgCommitment {
pub fn calculate_versioned_hash(&self) -> Hash256 {
let mut versioned_hash = hash_fixed(&self.0);
versioned_hash[0] = BLOB_COMMITMENT_VERSION_KZG;
versioned_hash[0] = VERSIONED_HASH_VERSION_KZG;
Hash256::from_slice(versioned_hash.as_slice())
}

pub fn empty_for_testing() -> Self {
KzgCommitment([0; c_kzg::BYTES_PER_COMMITMENT])
}
}

impl From<KzgCommitment> for c_kzg::Bytes48 {
Expand All @@ -42,12 +46,6 @@ impl Display for KzgCommitment {
}
}

impl Default for KzgCommitment {
fn default() -> Self {
KzgCommitment([0; BYTES_PER_COMMITMENT])
}
}

impl TreeHash for KzgCommitment {
fn tree_hash_type() -> tree_hash::TreeHashType {
<[u8; BYTES_PER_COMMITMENT] as TreeHash>::tree_hash_type()
Expand Down Expand Up @@ -80,25 +78,8 @@ impl<'de> Deserialize<'de> for KzgCommitment {
where
D: Deserializer<'de>,
{
pub struct StringVisitor;

impl<'de> serde::de::Visitor<'de> for StringVisitor {
type Value = String;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a hex string with 0x prefix")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(value.to_string())
}
}

let string = deserializer.deserialize_str(StringVisitor)?;
<Self as std::str::FromStr>::from_str(&string).map_err(serde::de::Error::custom)
let string = String::deserialize(deserializer)?;
Self::from_str(&string).map_err(serde::de::Error::custom)
}
}

Expand Down Expand Up @@ -131,7 +112,6 @@ impl Debug for KzgCommitment {
}
}

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary<'_> for KzgCommitment {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BYTES_PER_COMMITMENT];
Expand Down
28 changes: 2 additions & 26 deletions crypto/kzg/src/kzg_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ impl fmt::Display for KzgProof {
}
}

impl Default for KzgProof {
fn default() -> Self {
KzgProof([0; BYTES_PER_PROOF])
}
}

impl From<[u8; BYTES_PER_PROOF]> for KzgProof {
fn from(bytes: [u8; BYTES_PER_PROOF]) -> Self {
Self(bytes)
Expand Down Expand Up @@ -87,25 +81,8 @@ impl<'de> Deserialize<'de> for KzgProof {
where
D: Deserializer<'de>,
{
pub struct StringVisitor;

impl<'de> serde::de::Visitor<'de> for StringVisitor {
type Value = String;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a hex string with 0x prefix")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(value.to_string())
}
}

let string = deserializer.deserialize_str(StringVisitor)?;
<Self as std::str::FromStr>::from_str(&string).map_err(serde::de::Error::custom)
let string = String::deserialize(deserializer)?;
Self::from_str(&string).map_err(serde::de::Error::custom)
}
}

Expand Down Expand Up @@ -138,7 +115,6 @@ impl Debug for KzgProof {
}
}

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary<'_> for KzgProof {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
let mut bytes = [0u8; BYTES_PER_PROOF];
Expand Down
Loading