mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-29 02:33:48 +00:00
Add method to Hash256 to display shortened hashes (#9118)
#6689 Inspired by the initial implementation of #9108, credit to @chong-he. This adds an extension trait to `Hash256` and add a `short` method to provide smaller formatted hashes for logging. Co-Authored-By: Mac L <mjladson@pm.me>
This commit is contained in:
@@ -360,7 +360,7 @@ pub fn spawn_notifier<T: BeaconChainTypes>(
|
|||||||
let block_info = if current_slot > head_slot {
|
let block_info = if current_slot > head_slot {
|
||||||
" … empty".to_string()
|
" … empty".to_string()
|
||||||
} else {
|
} else {
|
||||||
head_root.to_string()
|
head_root.short().to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
let block_hash = match beacon_chain.canonical_head.head_execution_status() {
|
let block_hash = match beacon_chain.canonical_head.head_execution_status() {
|
||||||
@@ -393,7 +393,7 @@ pub fn spawn_notifier<T: BeaconChainTypes>(
|
|||||||
info!(
|
info!(
|
||||||
peers = peer_count_pretty(connected_peer_count),
|
peers = peer_count_pretty(connected_peer_count),
|
||||||
exec_hash = block_hash,
|
exec_hash = block_hash,
|
||||||
finalized_root = %finalized_checkpoint.root,
|
finalized_root = %finalized_checkpoint.root.short(),
|
||||||
finalized_epoch = %finalized_checkpoint.epoch,
|
finalized_epoch = %finalized_checkpoint.epoch,
|
||||||
epoch = %current_epoch,
|
epoch = %current_epoch,
|
||||||
block = block_info,
|
block = block_info,
|
||||||
@@ -404,7 +404,7 @@ pub fn spawn_notifier<T: BeaconChainTypes>(
|
|||||||
metrics::set_gauge(&metrics::IS_SYNCED, 0);
|
metrics::set_gauge(&metrics::IS_SYNCED, 0);
|
||||||
info!(
|
info!(
|
||||||
peers = peer_count_pretty(connected_peer_count),
|
peers = peer_count_pretty(connected_peer_count),
|
||||||
finalized_root = %finalized_checkpoint.root,
|
finalized_root = %finalized_checkpoint.root.short(),
|
||||||
finalized_epoch = %finalized_checkpoint.epoch,
|
finalized_epoch = %finalized_checkpoint.epoch,
|
||||||
%head_slot,
|
%head_slot,
|
||||||
%current_slot,
|
%current_slot,
|
||||||
|
|||||||
@@ -5,7 +5,10 @@ use rand::RngCore;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use ssz::{Decode, DecodeError, Encode};
|
use ssz::{Decode, DecodeError, Encode};
|
||||||
|
|
||||||
use crate::{core::Hash256, test_utils::TestRandom};
|
use crate::{
|
||||||
|
core::{Hash256, Hash256Ext},
|
||||||
|
test_utils::TestRandom,
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
|
||||||
#[derive(Default, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Hash)]
|
#[derive(Default, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Hash)]
|
||||||
@@ -20,13 +23,7 @@ impl fmt::Debug for ExecutionBlockHash {
|
|||||||
|
|
||||||
impl fmt::Display for ExecutionBlockHash {
|
impl fmt::Display for ExecutionBlockHash {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let hash = format!("{}", self.0);
|
self.0.short().fmt(f)
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"{}…{}",
|
|
||||||
&hash[..6],
|
|
||||||
&hash[hash.len().saturating_sub(4)..]
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,3 +49,29 @@ pub type Hash64 = alloy_primitives::B64;
|
|||||||
pub type Address = alloy_primitives::Address;
|
pub type Address = alloy_primitives::Address;
|
||||||
pub type VersionedHash = Hash256;
|
pub type VersionedHash = Hash256;
|
||||||
pub type MerkleProof = Vec<Hash256>;
|
pub type MerkleProof = Vec<Hash256>;
|
||||||
|
|
||||||
|
/// Extension trait for `Hash256` to allow us to implement additional methods on it.
|
||||||
|
pub trait Hash256Ext {
|
||||||
|
fn short(&self) -> ShortenedHash<'_>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hash256Ext for Hash256 {
|
||||||
|
fn short(&self) -> ShortenedHash<'_> {
|
||||||
|
ShortenedHash(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ShortenedHash<'a>(&'a Hash256);
|
||||||
|
|
||||||
|
impl<'a> std::fmt::Display for ShortenedHash<'a> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
let hash: &[u8; 32] = self.0.as_ref();
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
// Format as hex, padded to 2 digits per byte.
|
||||||
|
// This outputs a consistent "0x1234...abcd" format.
|
||||||
|
"0x{:02x}{:02x}…{:02x}{:02x}",
|
||||||
|
hash[0], hash[1], hash[30], hash[31]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user