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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use ssz::Hashable;
use ssz_derive::Ssz;
#[cfg(feature = "serde")]
use serde_derive::{Serialize, Deserialize};
#[cfg(feature = "parity-codec")]
use codec::{Encode, Decode};
use crate::primitives::{H256, ValidatorId, Signature};
use crate::Config;
#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Eth1Data {
pub deposit_root: H256,
pub block_hash: H256,
}
impl Eth1Data {
pub fn empty() -> Self {
Self {
deposit_root: H256::default(),
block_hash: H256::default(),
}
}
}
#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Eth1DataVote {
pub eth1_data: Eth1Data,
pub vote_count: u64,
}
#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
#[ssz(no_decode)]
pub struct Deposit {
#[ssz(use_fixed)]
pub proof: Vec<H256>,
pub index: u64,
pub deposit_data: DepositData,
}
impl Deposit {
pub fn is_merkle_valid<C: Config>(&self, deposit_root: &H256, config: &C) -> bool {
let merkle = MerkleProof {
leaf: config.hash(&::ssz::Encode::encode(&self.deposit_data)),
proof: self.proof.as_ref(),
depth: config.deposit_contract_tree_depth(),
index: self.index as usize,
root: *deposit_root,
};
merkle.is_valid(config)
}
pub fn is_proof_valid<C: Config>(&self, domain: u64, config: &C) -> bool {
config.bls_verify(
&self.deposit_data.deposit_input.pubkey,
&self.deposit_data.deposit_input.truncated_hash::<C::Hasher>(),
&self.deposit_data.deposit_input.proof_of_possession,
domain
)
}
}
#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct DepositData {
pub amount: u64,
pub timestamp: u64,
pub deposit_input: DepositInput,
}
#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct DepositInput {
pub pubkey: ValidatorId,
pub withdrawal_credentials: H256,
pub proof_of_possession: Signature,
}
pub struct MerkleProof<'a> {
pub leaf: H256,
pub proof: &'a [H256],
pub root: H256,
pub depth: usize,
pub index: usize,
}
impl<'a> MerkleProof<'a> {
pub fn is_valid<C: Config>(&self, config: &C) -> bool {
if self.proof.len() != config.deposit_contract_tree_depth() {
return false
}
let mut value = self.leaf;
for i in 0..self.depth {
if (self.index / 2usize.pow(i as u32)) % 2 != 0 {
value = config.hash2(self.proof[i].as_ref(), value.as_ref());
} else {
value = config.hash2(value.as_ref(), self.proof[i].as_ref());
}
}
value == self.root
}
}