clean up types

This commit is contained in:
realbigsean 2022-10-28 10:18:04 -04:00
parent f1a3b3b01c
commit 82eef493f3
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
6 changed files with 30 additions and 143 deletions

View File

@ -81,7 +81,26 @@ macro_rules! impl_for_lt_32byte_u8_array {
impl_for_lt_32byte_u8_array!(4); impl_for_lt_32byte_u8_array!(4);
impl_for_lt_32byte_u8_array!(32); impl_for_lt_32byte_u8_array!(32);
impl_for_lt_32byte_u8_array!(48);
impl TreeHash for [u8; 48] {
fn tree_hash_type() -> TreeHashType {
TreeHashType::Vector
}
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
unreachable!("Vector should never be packed.")
}
fn tree_hash_packing_factor() -> usize {
unreachable!("Vector should never be packed.")
}
fn tree_hash_root(&self) -> Hash256 {
let values_per_chunk = BYTES_PER_CHUNK;
let minimum_chunk_count = (48 + values_per_chunk - 1) / values_per_chunk;
merkle_root(self, minimum_chunk_count)
}
}
impl TreeHash for U128 { impl TreeHash for U128 {
fn tree_hash_type() -> TreeHashType { fn tree_hash_type() -> TreeHashType {

View File

@ -1,79 +0,0 @@
use crate::bls_field_element::BlsFieldElement;
use crate::test_utils::RngCore;
use crate::test_utils::TestRandom;
use crate::EthSpec;
use serde::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use ssz_types::VariableList;
use tree_hash::{PackedEncoding, TreeHash};
#[derive(Default, Debug, PartialEq, Hash, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Blob<T: EthSpec>(pub VariableList<BlsFieldElement, T::FieldElementsPerBlob>);
impl<T: EthSpec> TestRandom for Blob<T> {
fn random_for_test(rng: &mut impl RngCore) -> Self {
let mut res = Blob(VariableList::empty());
for _i in 0..4096 {
let slice = ethereum_types::U256([
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
rng.next_u64(),
]);
let elem = BlsFieldElement(slice);
res.0.push(elem);
}
res
}
}
impl<T: EthSpec> Encode for Blob<T> {
fn is_ssz_fixed_len() -> bool {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Encode>::ssz_fixed_len()
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
}
impl<T: EthSpec> Decode for Blob<T> {
fn is_ssz_fixed_len() -> bool {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob> as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob>>::from_ssz_bytes(bytes).map(Self)
}
}
impl<T: EthSpec> TreeHash for Blob<T> {
fn tree_hash_type() -> tree_hash::TreeHashType {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob>>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<VariableList<BlsFieldElement, T::FieldElementsPerBlob>>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}

View File

@ -1,58 +0,0 @@
use crate::Uint256;
use serde::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use tree_hash::{PackedEncoding, TreeHash};
#[derive(Default, Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BlsFieldElement(pub Uint256);
impl Encode for BlsFieldElement {
fn is_ssz_fixed_len() -> bool {
<Uint256 as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Uint256 as Encode>::ssz_fixed_len()
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
}
impl Decode for BlsFieldElement {
fn is_ssz_fixed_len() -> bool {
<Uint256 as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Uint256 as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
<Uint256 as Decode>::from_ssz_bytes(bytes).map(Self)
}
}
impl TreeHash for BlsFieldElement {
fn tree_hash_type() -> tree_hash::TreeHashType {
<Uint256>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> PackedEncoding {
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<Uint256>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}

View File

@ -3,12 +3,20 @@ use crate::*;
use derivative::Derivative; use derivative::Derivative;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode}; use ssz::{Decode, DecodeError, Encode};
use std::fmt;
use std::fmt::{Display, Formatter};
use tree_hash::{PackedEncoding, TreeHash}; use tree_hash::{PackedEncoding, TreeHash};
//TODO: is there a way around this newtype
#[derive(Derivative, Debug, Clone, Serialize, Deserialize)] #[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Hash)] #[derivative(PartialEq, Eq, Hash)]
pub struct KzgCommitment(#[serde(with = "BigArray")] [u8; 48]); pub struct KzgCommitment(#[serde(with = "BigArray")] [u8; 48]);
impl Display for KzgCommitment {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", eth2_serde_utils::hex::encode(&self.0))
}
}
impl TreeHash for KzgCommitment { impl TreeHash for KzgCommitment {
fn tree_hash_type() -> tree_hash::TreeHashType { fn tree_hash_type() -> tree_hash::TreeHashType {
<[u8; 48] as TreeHash>::tree_hash_type() <[u8; 48] as TreeHash>::tree_hash_type()

View File

@ -8,7 +8,7 @@ const KZG_PROOF_BYTES_LEN: usize = 48;
#[derive(Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)] #[derive(Debug, PartialEq, Hash, Clone, Copy, Serialize, Deserialize)]
#[serde(transparent)] #[serde(transparent)]
pub struct KzgProof(#[serde(with = "serde_kzg_proof")] pub [u8; KZG_PROOF_BYTES_LEN]); pub struct KzgProof(#[serde(with = "BigArray")] pub [u8; KZG_PROOF_BYTES_LEN]);
impl fmt::Display for KzgProof { impl fmt::Display for KzgProof {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@ -90,9 +90,7 @@ pub mod slot_data;
#[cfg(feature = "sqlite")] #[cfg(feature = "sqlite")]
pub mod sqlite; pub mod sqlite;
pub mod blob;
pub mod blobs_sidecar; pub mod blobs_sidecar;
pub mod bls_field_element;
pub mod kzg_commitment; pub mod kzg_commitment;
pub mod kzg_proof; pub mod kzg_proof;
pub mod signed_blobs_sidecar; pub mod signed_blobs_sidecar;
@ -197,7 +195,6 @@ pub type Address = H160;
pub type ForkVersion = [u8; 4]; pub type ForkVersion = [u8; 4];
pub type BLSFieldElement = Uint256; pub type BLSFieldElement = Uint256;
pub type Blob<T> = FixedVector<BLSFieldElement, <T as EthSpec>::FieldElementsPerBlob>; pub type Blob<T> = FixedVector<BLSFieldElement, <T as EthSpec>::FieldElementsPerBlob>;
pub type Polynomial<T> = VariableList<BLSFieldElement, <T as EthSpec>::FieldElementsPerBlob>;
pub use bls::{ pub use bls::{
AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey, AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey,