Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/eth2util/src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use pluto_eth2api::spec::phase0::{Root, Slot};
use pluto_ssz::{HashWalker, Hasher, HasherError};

/// Hashing error.
#[derive(Debug, thiserror::Error)]
pub enum HashError {
/// Failed to hash the slot root.
#[error("hash epoch: {0}")]
HashEpoch(#[from] HasherError),
}

/// Result type for hash helpers.
type Result<T> = std::result::Result<T, HashError>;

/// Returns the SSZ hash root of the slot.
pub fn slot_hash_root(slot: Slot) -> Result<Root> {
let mut hasher = Hasher::default();
let index = hasher.index();

hasher.put_uint64(slot)?;
hasher.merkleize(index)?;

Ok(hasher.hash_root()?)
}
Comment on lines +16 to +24
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need this helper, tree_hash::TreeHash should implement the tree_hash_root for Slot which is u64 already.

use tree_hash::TreeHash;

let tree_hash_root = slot.tree_hash_root().0;


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_slot_hash_root() {
let resp = slot_hash_root(2).expect("hash slot");
assert_eq!(
hex::encode(resp),
"0200000000000000000000000000000000000000000000000000000000000000"
);
}
}
3 changes: 3 additions & 0 deletions crates/eth2util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub mod rlp;
/// EIP712 utilities.
pub mod eip712;

/// SSZ hash helpers.
pub mod hash;

/// Network utilities.
pub mod network;

Expand Down
Loading