Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Oct 28, 2024
1 parent 7483aee commit 0fde4c6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 63 deletions.
12 changes: 3 additions & 9 deletions crates/subspace-core-primitives/src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +36,7 @@ use serde::{Deserialize, Serialize};
PartialOrd,
Hash,
From,
Into,
AsRef,
AsMut,
Deref,
Expand All @@ -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 {
Expand Down Expand Up @@ -97,13 +98,6 @@ impl TryFrom<&[u8]> for Blake3Hash {
}
}

impl From<Blake3Hash> 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;
Expand Down
18 changes: 6 additions & 12 deletions crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ const_assert!(core::mem::size_of::<usize>() >= core::mem::size_of::<u32>());
/// 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,
Expand All @@ -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]
Expand All @@ -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 {
Expand Down Expand Up @@ -129,23 +129,17 @@ 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 {
write!(f, "{}", hex::encode(self.0))
}
}

impl From<PublicKey> for [u8; PublicKey::SIZE] {
#[inline]
fn from(value: PublicKey) -> Self {
value.0
}
}

impl AsRef<[u8]> for PublicKey {
#[inline]
fn as_ref(&self) -> &[u8] {
Expand Down
50 changes: 17 additions & 33 deletions crates/subspace-core-primitives/src/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PosSeed> 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.
Expand All @@ -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<PosProof> for [u8; PosProof::SIZE] {
#[inline]
fn from(value: PosProof) -> Self {
value.0
}
}
pub struct PosProof([u8; PosProof::SIZE]);

impl Default for PosProof {
#[inline]
Expand Down
26 changes: 17 additions & 9 deletions crates/subspace-core-primitives/src/solutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RewardSignature> 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]
Expand Down

0 comments on commit 0fde4c6

Please sign in to comment.