fuel-relayer 0.9.1

Fuel Relayer
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use crate::{abi, config};
use anyhow::anyhow;
use ethers_contract::EthEvent;
use ethers_core::{
    abi::RawLog,
    types::{Log, U256},
};
use fuel_core_interfaces::{
    common::fuel_types::{Address, AssetId, Bytes32, Word},
    model::DepositCoin,
};

/// This is going to be superseded with MessageLog: https://github.com/FuelLabs/fuel-core/issues/366
#[derive(Debug, Clone, PartialEq)]
pub struct AssetDepositLog {
    pub account: Address,
    pub token: AssetId,
    pub amount: Word,
    pub precision_factor: u8,
    pub block_number: u32,
    pub deposit_nonce: Bytes32,
}

impl From<&AssetDepositLog> for DepositCoin {
    fn from(asset: &AssetDepositLog) -> Self {
        Self {
            owner: asset.account,
            amount: asset.amount,
            asset_id: asset.token, // TODO should this be hash of token_id and precision factor
            nonce: asset.deposit_nonce,
            deposited_da_height: asset.block_number,
            fuel_block_spend: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum EthEventLog {
    AssetDeposit(AssetDepositLog),
    // save it in validator set
    ValidatorRegistration {
        staking_key: Address,
        consensus_key: Address,
    },
    // remove it from validator set
    ValidatorUnregistration {
        staking_key: Address,
    },
    // do nothing. maybe used it for stats or info data.
    Deposit {
        depositor: Address, // It is 24bytes address from ethereum
        amount: Word,
    },
    // remove all delegations
    Withdrawal {
        withdrawer: Address, // It is 24bytes address from ethereum
        amount: Word,
    },
    // remove old delegations, delegate to new validators.
    Delegation {
        delegator: Address, // It is 24bytes address from ethereum
        delegates: Vec<Address>,
        amounts: Vec<u64>,
    },
    FuelBlockCommited {
        block_root: Bytes32,
        height: Word,
    },
    Unknown,
}

/// block_number(32bits) | precisionFactor(8bits) | depositNonce(256bits)
/// data is packet as three 256bit/32bytes values
const ASSET_DEPOSIT_DATA_LEN: usize = 32 + 32 + 32;

impl TryFrom<&Log> for EthEventLog {
    type Error = anyhow::Error;

    fn try_from(log: &Log) -> Result<Self, Self::Error> {
        if log.topics.is_empty() {
            return Err(anyhow!("Topic list is empty"));
        }

        let log = match log.topics[0] {
            n if n == *config::ETH_LOG_ASSET_DEPOSIT => {
                if log.topics.len() != 4 {
                    return Err(anyhow!("Malformed topics for AssetDeposit"));
                }
                let account = unsafe { Address::from_slice_unchecked(log.topics[1].as_ref()) };
                let token = unsafe { AssetId::from_slice_unchecked(log.topics[2].as_ref()) };

                if !log.topics[3][..24].iter().all(|&b| b == 0) {
                    return Err(anyhow!(
                        "Malformed amount for AssetDeposit. Amount bigger then u64",
                    ));
                }
                let amount = <[u8; 8]>::try_from(&log.topics[3][24..])
                    .map(u64::from_be_bytes)
                    .expect("We have checked slice bounds");

                // data is contains: block_number(32bits) | precisionFactor(8bits) | depositNonce(256bits)
                let data = &log.data.0;

                if data.len() != ASSET_DEPOSIT_DATA_LEN {
                    return Err(anyhow!(
                        "Malformed data length for AssetDeposit: {}",
                        data.len()
                    ));
                }
                if !data[..28].iter().all(|&b| b == 0) {
                    return Err(anyhow!(
                        "Malformed amount for AssetDeposit. Amount bigger then u64",
                    ));
                }

                let block_number = <[u8; 4]>::try_from(&data[28..32])
                    .map(u32::from_be_bytes)
                    .expect("We have checked slice bounds");

                if !data[32..63].iter().all(|&b| b == 0) {
                    return Err(anyhow!(
                        "Malformed amount for AssetDeposit. Amount bigger then u64",
                    ));
                }

                let precision_factor = data[63];

                let deposit_nonce = unsafe { Bytes32::from_slice_unchecked(&data[64..]) };

                Self::AssetDeposit(AssetDepositLog {
                    block_number,
                    account,
                    amount,
                    token,
                    precision_factor,
                    deposit_nonce,
                })
            }
            n if n == *config::ETH_LOG_VALIDATOR_REGISTRATION => {
                if log.topics.len() != 3 {
                    return Err(anyhow!("Malformed topics for ValidatorRegistration"));
                }
                let staking_key = unsafe { Address::from_slice_unchecked(log.topics[1].as_ref()) };
                let consensus_key =
                    unsafe { Address::from_slice_unchecked(log.topics[2].as_ref()) };

                Self::ValidatorRegistration {
                    staking_key,
                    consensus_key,
                }
            }
            n if n == *config::ETH_LOG_VALIDATOR_UNREGISTRATION => {
                if log.topics.len() != 2 {
                    return Err(anyhow!("Malformed topics for ValidatorUnregistration"));
                }
                let staking_key = unsafe { Address::from_slice_unchecked(log.topics[1].as_ref()) };

                Self::ValidatorUnregistration { staking_key }
            }
            n if n == *config::ETH_LOG_DEPOSIT => {
                if log.topics.len() != 3 {
                    return Err(anyhow!("Malformed topics for ValidatorRegistration"));
                }
                let depositor = unsafe { Address::from_slice_unchecked(log.topics[1].as_ref()) };
                let amount = unsafe { Bytes32::from_slice_unchecked(log.topics[2].as_ref()) };

                let amount = <[u8; 8]>::try_from(&amount[24..])
                    .map(u64::from_be_bytes)
                    .expect("We have checked slice bounds");

                Self::Deposit { depositor, amount }
            }
            n if n == *config::ETH_LOG_WITHDRAWAL => {
                if log.topics.len() != 3 {
                    return Err(anyhow!("Malformed topics for Withdrawal"));
                }
                let withdrawer = unsafe { Address::from_slice_unchecked(log.topics[1].as_ref()) };
                let amount = unsafe { Bytes32::from_slice_unchecked(log.topics[2].as_ref()) };

                let amount = <[u8; 8]>::try_from(&amount[24..])
                    .map(u64::from_be_bytes)
                    .expect("We have checked slice bounds");

                Self::Withdrawal { withdrawer, amount }
            }
            n if n == *config::ETH_LOG_DELEGATION => {
                if log.topics.len() != 2 {
                    return Err(anyhow!("Malformed topics for Delegation"));
                }

                let raw_log = RawLog {
                    topics: log.topics.clone(),
                    data: log.data.to_vec(),
                };

                let delegation = abi::validators::DelegationFilter::decode_log(&raw_log)?;
                let mut delegator = Address::zeroed();
                delegator[12..].copy_from_slice(delegation.delegator.as_bytes());

                Self::Delegation {
                    delegator,
                    delegates: delegation
                        .delegates
                        .into_iter()
                        .map(|b| {
                            let mut delegates = Address::zeroed();
                            delegates[12..].copy_from_slice(b.as_ref());
                            delegates
                        })
                        .collect(),
                    amounts: delegation
                        .amounts
                        .into_iter()
                        .map(|amount| {
                            if amount > U256::from(u64::MAX) {
                                u64::MAX
                            } else {
                                amount.as_u64()
                            }
                        })
                        .collect(),
                }
            }
            n if n == *config::ETH_FUEL_BLOCK_COMMITED => {
                if log.topics.len() != 3 {
                    return Err(anyhow!("Malformed topics for FuelBlockCommited"));
                }
                let block_root = unsafe { Bytes32::from_slice_unchecked(log.topics[1].as_ref()) };

                let height = <[u8; 4]>::try_from(&log.topics[2][28..])
                    .map(u32::from_be_bytes)
                    .expect("Slice bounds are predefined") as u64;

                Self::FuelBlockCommited { block_root, height }
            }
            _ => Self::Unknown,
        };

        Ok(log)
    }
}

#[cfg(test)]
pub mod tests {

    use bytes::{Bytes, BytesMut};
    use ethers_core::types::{Bytes as EthersBytes, H160, H256, U64};
    use rand::rngs::StdRng;
    use rand::{Rng, SeedableRng};

    use super::*;
    use crate::config;

    pub fn eth_log_validator_registration(
        eth_block: u64,
        staking_key: Address,
        consensus_key: Address,
    ) -> Log {
        log_default(
            eth_block,
            vec![
                *config::ETH_LOG_VALIDATOR_REGISTRATION,
                H256::from_slice(staking_key.as_ref()),
                H256::from_slice(consensus_key.as_ref()),
            ],
            Bytes::new(),
        )
    }

    pub fn eth_log_validator_unregistration(eth_block: u64, staking_key: Address) -> Log {
        log_default(
            eth_block,
            vec![
                *config::ETH_LOG_VALIDATOR_UNREGISTRATION,
                H256::from_slice(staking_key.as_ref()),
            ],
            Bytes::new(),
        )
    }

    pub fn eth_log_deposit(eth_block: u64, depositor: Address, amount: Word) -> Log {
        log_default(
            eth_block,
            vec![
                *config::ETH_LOG_DEPOSIT,
                H256::from_slice(depositor.as_ref()),
                H256::from_low_u64_be(amount),
            ],
            Bytes::new(),
        )
    }

    pub fn eth_log_withdrawal(eth_block: u64, withdrawer: Address, amount: Word) -> Log {
        log_default(
            eth_block,
            vec![
                *config::ETH_LOG_WITHDRAWAL,
                H256::from_slice(withdrawer.as_ref()),
                H256::from_low_u64_be(amount),
            ],
            Bytes::new(),
        )
    }

    pub fn eth_log_delegation(
        eth_block: u64,
        mut delegator: Address,
        mut delegates: Vec<Address>,
        amounts: Vec<u64>,
    ) -> Log {
        delegator.iter_mut().take(12).for_each(|i| *i = 0);

        delegates
            .iter_mut()
            .for_each(|delegate| delegate.iter_mut().take(12).for_each(|i| *i = 0));

        let mut data: Vec<u8> = Vec::new();
        let mut del_data: Vec<u8> = Vec::new();

        del_data.extend(H256::from_low_u64_be(delegates.len() as u64).as_ref());
        // append offsets
        let offset = delegates.len() * 32;
        for i in 0..delegates.len() {
            del_data.extend(H256::from_low_u64_be((offset + i * 64) as u64).as_ref());
        }
        // 20 is size of eth address
        let size = H256::from_low_u64_be(20);
        for delegate in delegates {
            del_data.extend(size.as_ref());
            let mut bytes = [0u8; 32];
            bytes[..20].copy_from_slice(&delegate.as_ref()[12..]);
            del_data.extend(&bytes);
        }

        data.extend(H256::from_low_u64_be(64).as_ref());
        data.extend(H256::from_low_u64_be(64 + del_data.len() as u64).as_ref());

        data.extend(del_data);

        data.extend(H256::from_low_u64_be(amounts.len() as u64).as_ref());
        for amount in amounts {
            data.extend(H256::from_low_u64_be(amount).as_ref());
        }

        let data = BytesMut::from_iter(data.into_iter()).freeze();

        log_default(
            eth_block,
            vec![
                *config::ETH_LOG_DELEGATION,
                H256::from_slice(delegator.as_ref()),
            ],
            data,
        )
    }

    pub fn eth_log_asset_deposit(
        eth_block: u64,
        account: Address,
        token: AssetId,
        block_number: u32,
        amount: Word,
        deposit_nonce: Bytes32,
        precision_factor: u8,
    ) -> Log {
        //block_number(32bits) | precision_factor(256bits) | depositNonce(256bits)
        // 32+32+32
        let mut b = BytesMut::new();
        b.resize(ASSET_DEPOSIT_DATA_LEN, 0);
        //let mut b: [u8; 68] = [0; 68];
        b[28..32].copy_from_slice(&block_number.to_be_bytes());
        // 4..28 are zeroes
        b[63] = precision_factor;
        b[64..96].copy_from_slice(deposit_nonce.as_ref());
        log_default(
            eth_block,
            vec![
                *config::ETH_LOG_ASSET_DEPOSIT,
                H256::from_slice(account.as_ref()),
                H256::from_slice(token.as_ref()),
                H256::from_low_u64_be(amount),
            ],
            b.freeze(),
        )
    }

    pub fn eth_log_fuel_block_commited(
        eth_block: u64,
        block_root: Bytes32,
        fuel_height: u32,
    ) -> Log {
        let t = log_default(
            eth_block,
            vec![
                *config::ETH_FUEL_BLOCK_COMMITED,
                H256::from_slice(block_root.as_ref()),
                H256::from_low_u64_be(fuel_height as u64),
            ],
            Bytes::new(),
        );
        t
    }

    fn log_default(eth_block: u64, topics: Vec<H256>, data: Bytes) -> Log {
        Log {
            address: H160::zero(), // we dont check or use this
            topics,
            data: EthersBytes(data),
            block_hash: None,
            block_number: Some(U64([eth_block])),
            transaction_hash: None,
            transaction_index: None,
            log_index: None,
            transaction_log_index: None,
            log_type: None,
            removed: Some(false),
        }
    }

    #[test]
    fn eth_event_validator_unregistration_try_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let staking_key = rng.gen();

        let log = eth_log_validator_unregistration(eth_block, staking_key);
        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );
        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::ValidatorUnregistration { staking_key },
            "Decoded log does not match data we encoded"
        );
    }

    #[test]
    fn eth_event_validator_registration_try_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let staking_key = rng.gen();
        let consensus_key = rng.gen();

        let log = eth_log_validator_registration(eth_block, staking_key, consensus_key);
        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );
        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::ValidatorRegistration {
                staking_key,
                consensus_key
            },
            "Decoded log does not match data we encoded"
        );
    }

    #[test]
    fn eth_event_withdrawal_try_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let withdrawer = rng.gen();
        let amount = rng.gen();

        let log = eth_log_withdrawal(eth_block, withdrawer, amount);
        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );
        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::Withdrawal { withdrawer, amount },
            "Decoded log does not match data we encoded"
        );
    }

    #[test]
    fn eth_event_deposit_try_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let depositor = rng.gen();
        let amount = rng.gen();

        let log = eth_log_deposit(eth_block, depositor, amount);
        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );
        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::Deposit { depositor, amount },
            "Decoded log does not match data we encoded"
        );
    }

    #[test]
    fn eth_event_delegate_try_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let mut delegator: Address = rng.gen();
        delegator.iter_mut().take(12).for_each(|i| *i = 0);
        let mut delegate1: Address = rng.gen();
        delegate1.iter_mut().take(12).for_each(|i| *i = 0);
        let mut delegate2: Address = rng.gen();
        delegate2.iter_mut().take(12).for_each(|i| *i = 0);

        let log = eth_log_delegation(
            eth_block,
            delegator,
            vec![delegate1, delegate2],
            vec![10, 20],
        );

        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );

        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::Delegation {
                delegator,
                delegates: vec![delegate1, delegate2],
                amounts: vec![10, 20],
            },
            "Decoded log does not match data we encoded"
        );
    }

    #[test]
    fn eth_event_fuel_block_comit_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let block_root = rng.gen();
        let height: u32 = rng.gen();

        let log = eth_log_fuel_block_commited(eth_block, block_root, height);
        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );
        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        let height = height as u64;
        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::FuelBlockCommited { block_root, height },
            "Decoded log does not match data we encoded"
        );
    }

    #[test]
    fn eth_event_asset_deposit_try_from_log() {
        let rng = &mut StdRng::seed_from_u64(2322u64);
        let eth_block = rng.gen();
        let account = rng.gen();
        let token = rng.gen();
        let block_number: u32 = rng.gen();
        let amount = rng.gen();
        let deposit_nonce = rng.gen();
        let precision_factor = rng.gen();

        let log = eth_log_asset_deposit(
            eth_block,
            account,
            token,
            block_number,
            amount,
            deposit_nonce,
            precision_factor,
        );
        assert_eq!(
            Some(U64([eth_block])),
            log.block_number,
            "Block number not set"
        );
        let fuel_log = EthEventLog::try_from(&log);
        assert!(fuel_log.is_ok(), "Parsing error:{:?}", fuel_log);

        assert_eq!(
            fuel_log.unwrap(),
            EthEventLog::AssetDeposit(AssetDepositLog {
                account,
                token,
                block_number,
                precision_factor,
                amount,
                deposit_nonce
            }),
            "Decoded log does not match data we encoded"
        );
    }
}