From 0fde4c61f374fcf2e0761eb847fc62592e362be6 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Mon, 28 Oct 2024 16:11:20 +0200 Subject: [PATCH] Minor cleanups --- crates/subspace-core-primitives/src/hashes.rs | 12 ++--- crates/subspace-core-primitives/src/lib.rs | 18 +++---- crates/subspace-core-primitives/src/pos.rs | 50 +++++++------------ .../subspace-core-primitives/src/solutions.rs | 26 ++++++---- 4 files changed, 43 insertions(+), 63 deletions(-) diff --git a/crates/subspace-core-primitives/src/hashes.rs b/crates/subspace-core-primitives/src/hashes.rs index 358940d1d5..29e702763e 100644 --- a/crates/subspace-core-primitives/src/hashes.rs +++ b/crates/subspace-core-primitives/src/hashes.rs @@ -18,7 +18,7 @@ use crate::ScalarBytes; use core::array::TryFromSliceError; use core::fmt; -use derive_more::{AsMut, AsRef, Deref, DerefMut, From}; +use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Into}; use hex::FromHex; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -36,6 +36,7 @@ use serde::{Deserialize, Serialize}; PartialOrd, Hash, From, + Into, AsRef, AsMut, Deref, @@ -47,7 +48,7 @@ use serde::{Deserialize, Serialize}; )] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", serde(transparent))] -pub struct Blake3Hash(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); +pub struct Blake3Hash(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Blake3Hash::SIZE]); impl fmt::Debug for Blake3Hash { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -97,13 +98,6 @@ impl TryFrom<&[u8]> for Blake3Hash { } } -impl From for [u8; Blake3Hash::SIZE] { - #[inline] - fn from(value: Blake3Hash) -> Self { - value.0 - } -} - impl Blake3Hash { /// Size of BLAKE3 hash output (in bytes). pub const SIZE: usize = 32; diff --git a/crates/subspace-core-primitives/src/lib.rs b/crates/subspace-core-primitives/src/lib.rs index cf20d5f7c9..1d786c34ed 100644 --- a/crates/subspace-core-primitives/src/lib.rs +++ b/crates/subspace-core-primitives/src/lib.rs @@ -55,9 +55,6 @@ const_assert!(core::mem::size_of::() >= core::mem::size_of::()); /// Signing context used for creating reward signatures by farmers. pub const REWARD_SIGNING_CONTEXT: &[u8] = b"subspace_reward"; -/// Byte length of a randomness type. -pub const RANDOMNESS_LENGTH: usize = 32; - /// Type of randomness. #[derive( Debug, @@ -75,7 +72,7 @@ pub const RANDOMNESS_LENGTH: usize = 32; MaxEncodedLen, )] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Randomness(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; RANDOMNESS_LENGTH]); +pub struct Randomness(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Randomness::SIZE]); impl AsRef<[u8]> for Randomness { #[inline] @@ -92,6 +89,9 @@ impl AsMut<[u8]> for Randomness { } impl Randomness { + /// Size of randomness (in bytes). + pub const SIZE: usize = 32; + /// Derive global slot challenge from global randomness. // TODO: Separate type for global challenge pub fn derive_global_challenge(&self, slot: SlotNumber) -> Blake3Hash { @@ -129,9 +129,10 @@ pub type BlockWeight = u128; TypeInfo, Deref, From, + Into, )] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct PublicKey(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); +pub struct PublicKey(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; PublicKey::SIZE]); impl fmt::Display for PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -139,13 +140,6 @@ impl fmt::Display for PublicKey { } } -impl From for [u8; PublicKey::SIZE] { - #[inline] - fn from(value: PublicKey) -> Self { - value.0 - } -} - impl AsRef<[u8]> for PublicKey { #[inline] fn as_ref(&self) -> &[u8] { diff --git a/crates/subspace-core-primitives/src/pos.rs b/crates/subspace-core-primitives/src/pos.rs index decf9cc857..d1586ca8b9 100644 --- a/crates/subspace-core-primitives/src/pos.rs +++ b/crates/subspace-core-primitives/src/pos.rs @@ -4,27 +4,13 @@ mod serde; use crate::hashes::{blake3_hash, Blake3Hash}; -use derive_more::{Deref, DerefMut, From}; +use derive_more::{Deref, DerefMut, From, Into}; use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; /// Proof of space seed. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref)] -pub struct PosSeed([u8; Self::SIZE]); - -impl From<[u8; PosSeed::SIZE]> for PosSeed { - #[inline] - fn from(value: [u8; Self::SIZE]) -> Self { - Self(value) - } -} - -impl From for [u8; PosSeed::SIZE] { - #[inline] - fn from(value: PosSeed) -> Self { - value.0 - } -} +#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref, From, Into)] +pub struct PosSeed([u8; PosSeed::SIZE]); impl PosSeed { /// Size of proof of space seed in bytes. @@ -33,23 +19,21 @@ impl PosSeed { /// Proof of space proof bytes. #[derive( - Debug, Copy, Clone, Eq, PartialEq, Deref, DerefMut, Encode, Decode, TypeInfo, MaxEncodedLen, + Debug, + Copy, + Clone, + Eq, + PartialEq, + Deref, + DerefMut, + From, + Into, + Encode, + Decode, + TypeInfo, + MaxEncodedLen, )] -pub struct PosProof([u8; Self::SIZE]); - -impl From<[u8; PosProof::SIZE]> for PosProof { - #[inline] - fn from(value: [u8; Self::SIZE]) -> Self { - Self(value) - } -} - -impl From for [u8; PosProof::SIZE] { - #[inline] - fn from(value: PosProof) -> Self { - value.0 - } -} +pub struct PosProof([u8; PosProof::SIZE]); impl Default for PosProof { #[inline] diff --git a/crates/subspace-core-primitives/src/solutions.rs b/crates/subspace-core-primitives/src/solutions.rs index 837c78505f..c3af44e6e2 100644 --- a/crates/subspace-core-primitives/src/solutions.rs +++ b/crates/subspace-core-primitives/src/solutions.rs @@ -60,17 +60,25 @@ const_assert!(solution_range_to_pieces(pieces_to_solution_range(5, (1, 6)), (1, /// A Ristretto Schnorr signature as bytes produced by `schnorrkel` crate. #[derive( - Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo, Deref, From, + Debug, + Copy, + Clone, + PartialEq, + Eq, + Ord, + PartialOrd, + Hash, + Encode, + Decode, + TypeInfo, + Deref, + From, + Into, )] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct RewardSignature(#[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; Self::SIZE]); - -impl From for [u8; RewardSignature::SIZE] { - #[inline] - fn from(value: RewardSignature) -> Self { - value.0 - } -} +pub struct RewardSignature( + #[cfg_attr(feature = "serde", serde(with = "hex"))] [u8; RewardSignature::SIZE], +); impl AsRef<[u8]> for RewardSignature { #[inline]