Skip to content

Commit

Permalink
Merge #2219
Browse files Browse the repository at this point in the history
2219: add debug to hashes r=damip a=damip



Co-authored-by: damip <[email protected]>
  • Loading branch information
bors[bot] and damip authored Jan 27, 2022
2 parents 249cc0c + 21e5a7b commit 2ba81c7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
8 changes: 7 additions & 1 deletion massa-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::settings::HASH_SIZE_BYTES;
use bitcoin_hashes;
use std::{convert::TryInto, str::FromStr};

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Hash)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Hash)]
pub struct Hash(bitcoin_hashes::sha256::Hash);

impl std::fmt::Display for Hash {
Expand All @@ -14,6 +14,12 @@ impl std::fmt::Display for Hash {
}
}

impl std::fmt::Debug for Hash {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.to_bs58_check())
}
}

impl Hash {
/// Compute a hash from data.
///
Expand Down
8 changes: 7 additions & 1 deletion massa-models/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::convert::TryInto;
use std::fmt::Formatter;
use std::str::FromStr;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct BlockId(pub Hash);

impl PreHashed for BlockId {}
Expand All @@ -29,6 +29,12 @@ impl std::fmt::Display for BlockId {
}
}

impl std::fmt::Debug for BlockId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.to_bs58_check())
}
}

impl FromStr for BlockId {
type Err = ModelsError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
8 changes: 7 additions & 1 deletion massa-models/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use massa_signature::PublicKey;
use serde::{Deserialize, Serialize};

/// NodeId wraps a public key to uniquely identify a node.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[derive(Clone, Copy, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct NodeId(pub PublicKey);

impl std::fmt::Display for NodeId {
Expand All @@ -13,6 +13,12 @@ impl std::fmt::Display for NodeId {
}
}

impl std::fmt::Debug for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.to_bs58_check())
}
}

impl std::str::FromStr for NodeId {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down
9 changes: 8 additions & 1 deletion massa-models/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::convert::TryInto;
use std::fmt::Formatter;
use std::{ops::RangeInclusive, str::FromStr};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct OperationId(Hash);

impl std::fmt::Display for OperationId {
Expand All @@ -27,6 +27,12 @@ impl std::fmt::Display for OperationId {
}
}

impl std::fmt::Debug for OperationId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0.to_bs58_check())
}
}

impl FromStr for OperationId {
type Err = ModelsError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand All @@ -50,6 +56,7 @@ impl OperationId {
Hash::from_bytes(data).map_err(|_| ModelsError::HashError)?,
))
}

pub fn from_bs58_check(data: &str) -> Result<OperationId, ModelsError> {
Ok(OperationId(
Hash::from_bs58_check(data).map_err(|_| ModelsError::HashError)?,
Expand Down

0 comments on commit 2ba81c7

Please sign in to comment.