Skip to content

Commit

Permalink
Merge pull request #3272 from autonomys/debug-print-as-hex
Browse files Browse the repository at this point in the history
Debug print many core data structures as hex
  • Loading branch information
nazar-pc authored Dec 2, 2024
2 parents 2be1461 + 9ea2b2d commit 7d92855
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 57 deletions.
34 changes: 19 additions & 15 deletions crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,16 @@ pub const REWARD_SIGNING_CONTEXT: &[u8] = b"subspace_reward";

/// Type of randomness.
#[derive(
Debug,
Default,
Copy,
Clone,
Eq,
PartialEq,
From,
Into,
Deref,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Default, Copy, Clone, Eq, PartialEq, From, Into, Deref, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
pub struct Randomness([u8; Randomness::SIZE]);

impl fmt::Debug for Randomness {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -156,7 +150,6 @@ pub type BlockWeight = u128;

/// A Ristretto Schnorr public key as bytes produced by `schnorrkel` crate.
#[derive(
Debug,
Default,
Copy,
Clone,
Expand All @@ -174,6 +167,12 @@ pub type BlockWeight = u128;
)]
pub struct PublicKey([u8; PublicKey::SIZE]);

impl fmt::Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -239,7 +238,6 @@ impl PublicKey {

/// Single BLS12-381 scalar with big-endian representation, not guaranteed to be valid
#[derive(
Debug,
Default,
Copy,
Clone,
Expand All @@ -262,6 +260,12 @@ impl PublicKey {
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct ScalarBytes([u8; ScalarBytes::FULL_BYTES]);

impl fmt::Debug for ScalarBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

impl ScalarBytes {
/// How many full bytes can be stored in BLS12-381 scalar (for instance before encoding). It is
/// actually 254 bits, but bits are mut harder to work with and likely not worth it.
Expand Down
47 changes: 39 additions & 8 deletions crates/subspace-core-primitives/src/pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,16 @@ impl PieceOffset {
/// Raw record contained within recorded history segment before archiving is applied.
///
/// NOTE: This is a stack-allocated data structure and can cause stack overflow!
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref, DerefMut)]
#[derive(Copy, Clone, Eq, PartialEq, Deref, DerefMut)]
#[repr(transparent)]
pub struct RawRecord([[u8; ScalarBytes::SAFE_BYTES]; Self::NUM_CHUNKS]);

impl fmt::Debug for RawRecord {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0.as_flattened()))
}
}

impl Default for RawRecord {
#[inline]
fn default() -> Self {
Expand Down Expand Up @@ -407,10 +413,16 @@ impl RawRecord {
/// Record contained within a piece.
///
/// NOTE: This is a stack-allocated data structure and can cause stack overflow!
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref, DerefMut)]
#[derive(Copy, Clone, Eq, PartialEq, Deref, DerefMut)]
#[repr(transparent)]
pub struct Record([[u8; ScalarBytes::FULL_BYTES]; Self::NUM_CHUNKS]);

impl fmt::Debug for Record {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0.as_flattened()))
}
}

impl Default for Record {
#[inline]
fn default() -> Self {
Expand Down Expand Up @@ -601,7 +613,6 @@ impl Record {

/// Record commitment contained within a piece.
#[derive(
Debug,
Copy,
Clone,
Eq,
Expand All @@ -618,6 +629,12 @@ impl Record {
)]
pub struct RecordCommitment([u8; RecordCommitment::SIZE]);

impl fmt::Debug for RecordCommitment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -731,7 +748,6 @@ impl RecordCommitment {

/// Record witness contained within a piece.
#[derive(
Debug,
Copy,
Clone,
Eq,
Expand All @@ -748,6 +764,12 @@ impl RecordCommitment {
)]
pub struct RecordWitness([u8; RecordWitness::SIZE]);

impl fmt::Debug for RecordWitness {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -859,12 +881,17 @@ impl RecordWitness {
pub const SIZE: usize = 48;
}

#[derive(Debug)]
enum CowBytes {
Shared(Bytes),
Owned(BytesMut),
}

impl fmt::Debug for CowBytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.as_ref()))
}
}

impl PartialEq for CowBytes {
fn eq(&self, other: &Self) -> bool {
self.as_ref().eq(other.as_ref())
Expand Down Expand Up @@ -1162,12 +1189,16 @@ impl Piece {
/// Internally piece contains a record and corresponding witness that together with segment
/// commitment of the segment this piece belongs to can be used to verify that a piece belongs to
/// the actual archival history of the blockchain.
#[derive(
Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deref, DerefMut, AsRef, AsMut,
)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deref, DerefMut, AsRef, AsMut)]
#[repr(transparent)]
pub struct PieceArray([u8; Piece::SIZE]);

impl fmt::Debug for PieceArray {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

impl Default for PieceArray {
#[inline]
fn default() -> Self {
Expand Down
29 changes: 15 additions & 14 deletions crates/subspace-core-primitives/src/pos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Proof of space-related data structures.
use crate::hashes::{blake3_hash, Blake3Hash};
use core::fmt;
use derive_more::{Deref, DerefMut, From, Into};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
Expand All @@ -12,32 +13,32 @@ use serde::{Deserializer, Serializer};
use serde_big_array::BigArray;

/// Proof of space seed.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref, From, Into)]
#[derive(Copy, Clone, Eq, PartialEq, Deref, From, Into)]
pub struct PosSeed([u8; PosSeed::SIZE]);

impl fmt::Debug for PosSeed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

impl PosSeed {
/// Size of proof of space seed in bytes.
pub const SIZE: usize = 32;
}

/// Proof of space proof bytes.
#[derive(
Debug,
Copy,
Clone,
Eq,
PartialEq,
Deref,
DerefMut,
From,
Into,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Copy, Clone, Eq, PartialEq, Deref, DerefMut, From, Into, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
pub struct PosProof([u8; PosProof::SIZE]);

impl fmt::Debug for PosProof {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down
21 changes: 18 additions & 3 deletions crates/subspace-core-primitives/src/pot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use serde::{Deserializer, Serializer};

/// Proof of time key(input to the encryption).
#[derive(
Debug,
Default,
Copy,
Clone,
Expand All @@ -33,6 +32,12 @@ use serde::{Deserializer, Serializer};
)]
pub struct PotKey([u8; Self::SIZE]);

impl fmt::Debug for PotKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -98,7 +103,6 @@ impl PotKey {

/// Proof of time seed
#[derive(
Debug,
Default,
Copy,
Clone,
Expand All @@ -117,6 +121,12 @@ impl PotKey {
)]
pub struct PotSeed([u8; Self::SIZE]);

impl fmt::Debug for PotSeed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -187,7 +197,6 @@ impl PotSeed {

/// Proof of time output, can be intermediate checkpoint or final slot output
#[derive(
Debug,
Default,
Copy,
Clone,
Expand All @@ -206,6 +215,12 @@ impl PotSeed {
)]
pub struct PotOutput([u8; Self::SIZE]);

impl fmt::Debug for PotOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down
17 changes: 15 additions & 2 deletions crates/subspace-core-primitives/src/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::BlockNumber;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use core::array::TryFromSliceError;
use core::fmt;
use core::iter::Step;
use core::num::NonZeroU64;
use derive_more::{
Expand Down Expand Up @@ -134,7 +135,6 @@ impl SegmentIndex {

/// Segment commitment contained within segment header.
#[derive(
Debug,
Copy,
Clone,
Eq,
Expand All @@ -152,6 +152,12 @@ impl SegmentIndex {
#[repr(transparent)]
pub struct SegmentCommitment([u8; SegmentCommitment::SIZE]);

impl fmt::Debug for SegmentCommitment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex::encode(self.0))
}
}

#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
Expand Down Expand Up @@ -422,10 +428,17 @@ impl SegmentHeader {
/// Recorded history segment before archiving is applied.
///
/// NOTE: This is a stack-allocated data structure and can cause stack overflow!
#[derive(Debug, Copy, Clone, Eq, PartialEq, Deref, DerefMut)]
#[derive(Copy, Clone, Eq, PartialEq, Deref, DerefMut)]
#[repr(transparent)]
pub struct RecordedHistorySegment([RawRecord; Self::NUM_RAW_RECORDS]);

impl fmt::Debug for RecordedHistorySegment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RecordedHistorySegment")
.finish_non_exhaustive()
}
}

impl Default for RecordedHistorySegment {
#[inline]
fn default() -> Self {
Expand Down
Loading

0 comments on commit 7d92855

Please sign in to comment.