forked from sigp/lighthouse
/
execution_block_hash.rs
101 lines (81 loc) · 2.32 KB
/
execution_block_hash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use crate::test_utils::TestRandom;
use crate::Hash256;
use rand::RngCore;
use serde_derive::{Deserialize, Serialize};
use ssz::{Decode, DecodeError, Encode};
use std::fmt;
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(transparent)]
pub struct ExecutionBlockHash(Hash256);
impl ExecutionBlockHash {
pub fn zero() -> Self {
Self(Hash256::zero())
}
pub fn repeat_byte(b: u8) -> Self {
Self(Hash256::repeat_byte(b))
}
pub fn from_root(root: Hash256) -> Self {
Self(root)
}
pub fn into_root(self) -> Hash256 {
self.0
}
}
impl Encode for ExecutionBlockHash {
fn is_ssz_fixed_len() -> bool {
<Hash256 as Encode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Hash256 as Encode>::ssz_fixed_len()
}
fn ssz_bytes_len(&self) -> usize {
self.0.ssz_bytes_len()
}
fn ssz_append(&self, buf: &mut Vec<u8>) {
self.0.ssz_append(buf)
}
}
impl Decode for ExecutionBlockHash {
fn is_ssz_fixed_len() -> bool {
<Hash256 as Decode>::is_ssz_fixed_len()
}
fn ssz_fixed_len() -> usize {
<Hash256 as Decode>::ssz_fixed_len()
}
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
Hash256::from_ssz_bytes(bytes).map(Self)
}
}
impl tree_hash::TreeHash for ExecutionBlockHash {
fn tree_hash_type() -> tree_hash::TreeHashType {
Hash256::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.0.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
Hash256::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> tree_hash::Hash256 {
self.0.tree_hash_root()
}
}
impl TestRandom for ExecutionBlockHash {
fn random_for_test(rng: &mut impl RngCore) -> Self {
Self(Hash256::random_for_test(rng))
}
}
impl std::str::FromStr for ExecutionBlockHash {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Hash256::from_str(s)
.map(Self)
.map_err(|e| format!("{:?}", e))
}
}
impl fmt::Display for ExecutionBlockHash {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}