-
Notifications
You must be signed in to change notification settings - Fork 49
/
lib.rs
388 lines (307 loc) · 11.7 KB
/
lib.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! prototype module for bridging in ethereum pow blockchain, including mainet and ropsten
#![recursion_limit = "128"]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;
use codec::{Decode, Encode};
use frame_support::{decl_event, decl_module, decl_storage, ensure, traits::Get};
use frame_system::{self as system, ensure_root, ensure_signed};
use sp_runtime::RuntimeDebug;
use sp_std::vec::Vec;
use ethash::{EthereumPatch, LightDAG};
use merkle_patricia_trie::{trie::Trie, MerklePatriciaTrie, Proof};
use sp_eth_primitives::{
header::EthHeader, pow::EthashPartial, pow::EthashSeal, receipt::Receipt, EthBlockNumber, H256, U256,
};
type DAG = LightDAG<EthereumPatch>;
pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type EthNetwork: Get<u64>;
}
/// Familial details concerning a block
#[derive(Default, Clone, Copy, Eq, PartialEq, Encode, Decode)]
pub struct HeaderInfo {
/// Total difficulty of the block and all its parents
pub total_difficulty: U256,
/// Parent hash of the header
pub parent_hash: H256,
/// Block number
pub number: EthBlockNumber,
}
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct EthReceiptProof {
pub index: u64,
pub proof: Vec<u8>,
pub header_hash: H256,
}
decl_storage! {
trait Store for Module<T: Trait> as EthRelay {
/// Anchor block that works as genesis block
pub GenesisHeader get(fn begin_header): Option<EthHeader>;
/// Hash of best block header
pub BestHeaderHash get(fn best_header_hash): H256;
pub CanonicalHeaderHashOf get(canonical_header_hash_of): map u64 => H256;
pub HeaderOf get(header_of): map H256 => Option<EthHeader>;
pub HeaderInfoOf get(header_info_of): map H256 => Option<HeaderInfo>;
/// Number of blocks finality
pub NumberOfBlocksFinality get(number_of_blocks_finality) config(): u64 = 30;
pub NumberOfBlocksSafe get(number_of_blocks_safe) config(): u64 = 10;
pub CheckAuthorities get(fn check_authorities) config(): bool = true;
pub Authorities get(fn authorities) config(): Vec<T::AccountId>;
}
add_extra_genesis {
config(header): Option<Vec<u8>>;
config(genesis_difficulty): u64;
build(|config| {
if let Some(h) = &config.header {
let header: EthHeader = rlp::decode(&h).expect("Deserialize Genesis Header - FAILED");
// Discard the result even it fail.
let _ = <Module<T>>::init_genesis_header(&header,config.genesis_difficulty);
}
});
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call
where
origin: T::Origin
{
fn deposit_event() = default;
// TODO: Just for easy testing.
pub fn reset_genesis_header(origin, header: EthHeader, genesis_difficulty: u64) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
ensure!(Self::authorities().contains(&relayer), "Account - NO PRIVILEGES");
}
Self::init_genesis_header(&header, genesis_difficulty)?;
<Module<T>>::deposit_event(RawEvent::SetGenesisHeader(relayer, header, genesis_difficulty));
}
pub fn relay_header(origin, header: EthHeader) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
ensure!(Self::authorities().contains(&relayer), "Account - NO PRIVILEGES");
}
let header_hash = header.hash();
ensure!(! HeaderInfoOf::get(&header_hash).is_some(), "The header is already known.");
// let best_header_hash = Self::best_header_hash();
// if self.best_header_hash == Default::default() {
// Self::maybe_store_header(&header)?;
// }
Self::verify_header(&header)?;
Self::maybe_store_header(&header)?;
<Module<T>>::deposit_event(RawEvent::RelayHeader(relayer, header));
}
pub fn check_receipt(origin, proof_record: EthReceiptProof) {
let relayer = ensure_signed(origin)?;
if Self::check_authorities() {
ensure!(Self::authorities().contains(&relayer), "Account - NO PRIVILEGES");
}
let verified_receipt = Self::verify_receipt(&proof_record)?;
<Module<T>>::deposit_event(RawEvent::VerifyProof(relayer, verified_receipt, proof_record));
}
pub fn add_authority(origin, who: T::AccountId) {
let _me = ensure_root(origin)?;
if !Self::authorities().contains(&who) {
<Authorities<T>>::mutate(|l| l.push(who.clone()));
<Module<T>>::deposit_event(RawEvent::AddAuthority(who));
}
}
pub fn remove_authority(origin, who: T::AccountId) {
let _me = ensure_root(origin)?;
if let Some(i) = Self::authorities()
.into_iter()
.position(|who_| who_ == who) {
<Authorities<T>>::mutate(|l| l.remove(i));
<Module<T>>::deposit_event(RawEvent::RemoveAuthority(who));
}
}
pub fn toggle_check_authorities(origin) {
let _me = ensure_root(origin)?;
CheckAuthorities::put(!Self::check_authorities());
<Module<T>>::deposit_event(RawEvent::ToggleCheckAuthorities(Self::check_authorities()));
}
}
}
decl_event! {
pub enum Event<T>
where
<T as system::Trait>::AccountId
{
SetGenesisHeader(AccountId, EthHeader, u64),
RelayHeader(AccountId, EthHeader),
VerifyProof(AccountId, Receipt, EthReceiptProof),
AddAuthority(AccountId),
RemoveAuthority(AccountId),
ToggleCheckAuthorities(bool),
}
}
/// Handler for selecting the genesis validator set.
pub trait VerifyEthReceipts {
fn verify_receipt(proof_record: &EthReceiptProof) -> Result<Receipt, &'static str>;
}
impl<T: Trait> Module<T> {
pub fn init_genesis_header(header: &EthHeader, genesis_difficulty: u64) -> Result<(), &'static str> {
let header_hash = header.hash();
ensure!(header_hash == header.re_compute_hash(), "Header Hash - MISMATCHED");
let block_number = header.number();
HeaderOf::insert(&header_hash, header);
// initialize header info, including total difficulty.
HeaderInfoOf::insert(
&header_hash,
HeaderInfo {
parent_hash: *header.parent_hash(),
total_difficulty: genesis_difficulty.into(),
number: block_number,
},
);
// Initialize the the best hash.
BestHeaderHash::mutate(|hash| {
*hash = header_hash;
});
CanonicalHeaderHashOf::insert(block_number, &header_hash);
// Removing headers with larger numbers, if there are.
let mut number = block_number + 1;
loop {
// If the current block hash is 0 (unlikely), or the previous hash matches the
// current hash, then we chains converged and can stop now.
if !CanonicalHeaderHashOf::exists(&number) {
break;
}
CanonicalHeaderHashOf::remove(&number);
number += 1;
}
GenesisHeader::put(header.clone());
Ok(())
}
/// 1. proof of difficulty
/// 2. proof of pow (mixhash)
/// 3. challenge
fn verify_header(header: &EthHeader) -> Result<(), &'static str> {
ensure!(header.hash() == header.re_compute_hash(), "Header Hash - MISMATCHED");
let parent_hash = header.parent_hash();
let number = header.number();
ensure!(
number >= Self::begin_header().ok_or("Begin Header - NOT EXISTED")?.number(),
"Block Number - TOO SMALL",
);
// There must be a corresponding parent hash
let prev_header = Self::header_of(parent_hash).ok_or("Previous Header - NOT EXISTED")?;
ensure!((prev_header.number() + 1) == number, "Block Number - MISMATCHED");
// check difficulty
let ethash_params = match T::EthNetwork::get() {
0 => EthashPartial::production(),
1 => EthashPartial::ropsten_testnet(),
_ => EthashPartial::production(), // others
};
ethash_params.verify_block_basic(header)?;
// verify difficulty
let difficulty = ethash_params.calculate_difficulty(header, &prev_header);
ensure!(difficulty == *header.difficulty(), "Verify Difficulty - FAILED");
// verify mixhash
match T::EthNetwork::get() {
1 => {
// TODO: Ropsten have issues, do not verify mixhash
}
_ => {
let seal = EthashSeal::parse_seal(header.seal())?;
let light_dag = DAG::new(number.into());
let partial_header_hash = header.bare_hash();
let mix_hash = light_dag.hashimoto(partial_header_hash, seal.nonce).0;
if mix_hash != seal.mix_hash {
return Err("Mixhash - MISMATCHED");
}
}
};
Ok(())
}
fn maybe_store_header(header: &EthHeader) -> Result<(), &'static str> {
let best_header_info =
Self::header_info_of(Self::best_header_hash()).ok_or("Best Header Detail - NOT EXISTED")?;
if best_header_info.number > header.number + Self::number_of_blocks_finality() {
return Err("Header Too Old: It's too late to add this block header.");
}
let header_hash = header.hash();
HeaderOf::insert(header_hash, header);
let parent_total_difficulty = Self::header_info_of(header.parent_hash())
.ok_or("Previous Header Detail - NOT EXISTED")?
.total_difficulty;
let block_number = header.number();
let header_info = HeaderInfo {
number: block_number,
parent_hash: *header.parent_hash(),
total_difficulty: parent_total_difficulty + header.difficulty(),
};
HeaderInfoOf::insert(&header_hash, header_info);
// Check total difficulty and re-org if necessary.
if header_info.total_difficulty > best_header_info.total_difficulty
|| (header_info.total_difficulty == best_header_info.total_difficulty
&& header.difficulty % 2 == U256::default())
{
// The new header is the tip of the new canonical chain.
// We need to update hashes of the canonical chain to match the new header.
// If the new header has a lower number than the previous header, we need to cleaning
// it going forward.
if best_header_info.number > header_info.number {
for number in header_info.number + 1..=best_header_info.number {
CanonicalHeaderHashOf::remove(&number);
}
}
// Replacing the global best header hash.
BestHeaderHash::mutate(|hash| {
*hash = header_hash;
});
CanonicalHeaderHashOf::insert(&header_info.number, &header_hash);
// Replacing past hashes until we converge into the same parent.
// Starting from the parent hash.
let mut number = header.number - 1;
let mut current_hash = header_info.parent_hash;
loop {
let prev_value = CanonicalHeaderHashOf::get(&number);
// If the current block hash is 0 (unlikely), or the previous hash matches the
// current hash, then we chains converged and can stop now.
if number == 0 || prev_value == current_hash {
break;
}
CanonicalHeaderHashOf::insert(&number, ¤t_hash);
// Check if there is an info to get the parent hash
if let Some(info) = HeaderInfoOf::get(¤t_hash) {
current_hash = info.parent_hash;
} else {
break;
}
number -= 1;
}
}
Ok(())
}
// TODO: Economic model design required for the relay
fn _punish(_who: &T::AccountId) -> Result<(), &'static str> {
unimplemented!()
}
}
impl<T: Trait> VerifyEthReceipts for Module<T> {
/// confirm that the block hash is right
/// get the receipt MPT trie root from the block header
/// Using receipt MPT trie root to verify the proof and index etc.
fn verify_receipt(proof_record: &EthReceiptProof) -> Result<Receipt, &'static str> {
let info = Self::header_info_of(&proof_record.header_hash).ok_or("Header - NOT EXISTED")?;
let canonical_hash = Self::canonical_header_hash_of(info.number);
if proof_record.header_hash != canonical_hash {
return Err("Header - NOT CANONICAL");
}
let best_info = Self::header_info_of(Self::best_header_hash()).ok_or("Header - Best Header Not Found")?;
if best_info.number < info.number + Self::number_of_blocks_safe() {
return Err("Header - NOT SAFE");
}
let header = Self::header_of(&proof_record.header_hash).ok_or("Header - NOT EXISTED")?;
let proof: Proof = rlp::decode(&proof_record.proof).map_err(|_| "Rlp Decode - FAILED")?;
let key = rlp::encode(&proof_record.index);
let value = MerklePatriciaTrie::verify_proof(header.receipts_root().0.to_vec(), &key, proof)
.map_err(|_| "Verify Proof - FAILED")?
.ok_or("Trie Key - NOT EXISTED")?;
let receipt = rlp::decode(&value).map_err(|_| "Deserialize Receipt - FAILED")?;
Ok(receipt)
}
}