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
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use fixed_hash::construct_fixed_hash;
#[cfg(feature = "serde")]
use impl_serde::serialize as bytes;
const SIZE: usize = 48;
construct_fixed_hash! {
pub struct H384(SIZE);
}
pub type ValidatorId = H384;
#[cfg(feature = "serde")]
impl Serialize for H384 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
bytes::serialize(&self.0, serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for H384 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
bytes::deserialize_check_len(deserializer, bytes::ExpectedLen::Exact(SIZE))
.map(|x| H384::from_slice(&x))
}
}
#[cfg(feature = "parity-codec")]
impl codec::Encode for H384 {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}
#[cfg(feature = "parity-codec")]
impl codec::Decode for H384 {
fn decode<I: codec::Input>(input: &mut I) -> Option<Self> {
<[u8; SIZE] as codec::Decode>::decode(input).map(H384)
}
}
impl ssz::Encode for H384 {
fn encode_to<W: ::ssz::Output>(&self, dest: &mut W) {
dest.write(self.as_ref())
}
}
impl ssz::Decode for H384 {
fn decode_as<I: ::ssz::Input>(input: &mut I) -> Option<(Self, usize)> {
let mut vec = [0u8; SIZE];
if input.read(&mut vec[..SIZE]) != SIZE {
None
} else {
Some((H384::from(&vec), SIZE))
}
}
}
impl ssz::Prefixable for H384 {
fn prefixed() -> bool {
false
}
}
impl ssz::Composite for H384 { }
impl ssz::Hashable for H384 {
fn hash<H: ::hash_db::Hasher>(&self) -> H::Out {
ssz::hash::merkleize::<H>(ssz::hash::chunkify(self.as_ref()))
}
}
impl Into<primitive_types::H256> for H384 {
fn into(self) -> primitive_types::H256 {
primitive_types::H256::from_slice(&self[0..32])
}
}