namada_node 0.251.4

Namada node library code
Documentation
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
pub mod control;
pub mod events;
pub mod test_tools;

use std::ops::ControlFlow;

use async_trait::async_trait;
use ethabi::Address;
use ethbridge_events::{EventKind, event_codecs};
use itertools::Either;
use namada_sdk::control_flow::time::{Constant, Duration, Instant, Sleep};
use namada_sdk::eth_bridge::ethers::providers::{Http, Middleware, Provider};
use namada_sdk::eth_bridge::oracle::config::Config;
use namada_sdk::eth_bridge::{SyncStatus, eth_syncing_status_timeout, ethers};
use namada_sdk::ethereum_events::EthereumEvent;
use namada_sdk::{ethereum_structs, hints};
use num256::Uint256;
use thiserror::Error;
use tokio::sync::mpsc::Sender as BoundedSender;
use tokio::sync::mpsc::error::TryRecvError;
use tokio::task::LocalSet;

use self::events::PendingEvent;
use super::abortable::AbortableSpawner;
use crate::oracle::control::Command;

/// The default amount of time the oracle will wait between processing blocks
const DEFAULT_BACKOFF: Duration = Duration::from_millis(500);
const DEFAULT_CEILING: Duration = Duration::from_secs(30);

#[derive(Error, Debug)]
pub enum Error {
    #[error("Ethereum node has fallen out of sync")]
    FallenBehind,
    #[error(
        "Couldn't check for events ({0} from {1}) with the RPC endpoint: {2}"
    )]
    CheckEvents(String, Address, String),
    #[error("Could not send all bridge events ({0} from {1}) to the shell")]
    Channel(String, Address),
    #[error(
        "Need more confirmations for oracle to continue processing blocks."
    )]
    MoreConfirmations,
    #[error("The Ethereum oracle timed out")]
    Timeout,
}

/// Convert values to [`ethabi`] Ethereum event logs.
pub trait IntoEthAbiLog {
    /// Convert an Ethereum event log to the corresponding
    /// [`ethabi`] type.
    fn into_ethabi_log(self) -> ethabi::RawLog;
}

impl IntoEthAbiLog for ethers::types::Log {
    #[inline]
    fn into_ethabi_log(self) -> ethabi::RawLog {
        self.into()
    }
}

impl IntoEthAbiLog for ethabi::RawLog {
    #[inline]
    fn into_ethabi_log(self) -> ethabi::RawLog {
        self
    }
}

/// Client implementations that speak a subset of the
/// Geth JSONRPC protocol.
#[async_trait(?Send)]
pub trait RpcClient {
    /// Ethereum event log.
    type Log: IntoEthAbiLog;

    /// Instantiate a new client, pointing to the
    /// given RPC url.
    fn new_client(rpc_url: &str) -> Self
    where
        Self: Sized;

    /// Query a block for Ethereum events from a given ABI type
    /// and contract address.
    async fn check_events_in_block(
        &self,
        block: ethereum_structs::BlockHeight,
        address: Address,
        abi_signature: &str,
    ) -> Result<Vec<Self::Log>, Error>;

    /// Check if the fullnode we are connected to is syncing or is up
    /// to date with the Ethereum (an return the block height).
    ///
    /// Note that the syncing call may return false inaccurately. In
    /// that case, we must check if the block number is 0 or not.
    async fn syncing(
        &self,
        last_processed_block: Option<&ethereum_structs::BlockHeight>,
        backoff: Duration,
        deadline: Instant,
    ) -> Result<SyncStatus, Error>;

    /// Given its current state, check if this RPC client
    /// may recover from the given [`enum@Error`].
    fn may_recover(&self, error: &Error) -> bool;
}

#[async_trait(?Send)]
impl RpcClient for Provider<Http> {
    type Log = ethers::types::Log;

    #[inline]
    fn new_client(url: &str) -> Self
    where
        Self: Sized,
    {
        Provider::<Http>::try_from(url).expect("Invalid Ethereum RPC url")
    }

    async fn check_events_in_block(
        &self,
        block: ethereum_structs::BlockHeight,
        contract_address: Address,
        abi_signature: &str,
    ) -> Result<Vec<Self::Log>, Error> {
        let height = {
            let n: Uint256 = block.into();
            let n: u64 =
                n.0.try_into().expect("Ethereum block number overflow");
            n
        };
        self.get_logs(
            &ethers::types::Filter::new()
                .from_block(height)
                .to_block(height)
                .event(abi_signature)
                .address(contract_address),
        )
        .await
        .map_err(|error| {
            Error::CheckEvents(
                abi_signature.into(),
                contract_address,
                error.to_string(),
            )
        })
    }

    async fn syncing(
        &self,
        last_processed_block: Option<&ethereum_structs::BlockHeight>,
        backoff: Duration,
        deadline: Instant,
    ) -> Result<SyncStatus, Error> {
        match eth_syncing_status_timeout(self, backoff, deadline)
            .await
            .map_err(|_| Error::Timeout)?
        {
            s @ SyncStatus::Syncing => Ok(s),
            SyncStatus::AtHeight(height) => match last_processed_block {
                Some(last) if <&Uint256>::from(last) < &height => {
                    Ok(SyncStatus::AtHeight(height))
                }
                None => Ok(SyncStatus::AtHeight(height)),
                _ => Err(Error::FallenBehind),
            },
        }
    }

    #[inline(always)]
    fn may_recover(&self, error: &Error) -> bool {
        !matches!(
            error,
            Error::Timeout | Error::Channel(_, _) | Error::CheckEvents(_, _, _)
        )
    }
}

/// A client that can talk to geth and parse
/// and relay events relevant to Namada to the
/// ledger process
pub struct Oracle<C = Provider<Http>> {
    /// The client that talks to the Ethereum fullnode
    client: C,
    /// A channel for sending processed and confirmed
    /// events to the ledger process
    sender: BoundedSender<EthereumEvent>,
    /// The most recently processed block is recorded here.
    last_processed_block: last_processed_block::Sender,
    /// How long the oracle should wait between checking blocks
    backoff: Duration,
    /// How long the oracle should allow the fullnode to be unresponsive
    ceiling: Duration,
    /// A channel for controlling and configuring the oracle.
    control: control::Receiver,
}

impl<C: RpcClient> Oracle<C> {
    /// Construct a new [`Oracle`]. Note that it can not do anything until it
    /// has been sent a configuration via the passed in `control` channel.
    pub fn new(
        client_or_url: Either<C, &str>,
        sender: BoundedSender<EthereumEvent>,
        last_processed_block: last_processed_block::Sender,
        backoff: Duration,
        ceiling: Duration,
        control: control::Receiver,
    ) -> Self {
        Self {
            client: match client_or_url {
                Either::Left(client) => client,
                Either::Right(url) => C::new_client(url),
            },
            sender,
            backoff,
            ceiling,
            last_processed_block,
            control,
        }
    }

    /// Send a series of [`EthereumEvent`]s to the Namada
    /// ledger. Returns a boolean indicating that all sent
    /// successfully. If false is returned, the receiver
    /// has hung up.
    ///
    /// N.B. this will block if the internal channel buffer
    /// is full.
    async fn send(&self, events: Vec<EthereumEvent>) -> bool {
        if self.sender.is_closed() {
            return false;
        }
        for event in events.into_iter() {
            if self.sender.send(event).await.is_err() {
                return false;
            }
        }
        true
    }

    /// Check if a new config has been sent from the Shell.
    fn update_config(&mut self) -> Option<Config> {
        match self.control.try_recv() {
            Ok(Command::UpdateConfig(config)) => Some(config),
            Err(TryRecvError::Disconnected) => panic!(
                "The Ethereum oracle command channel has unexpectedly hung up."
            ),
            _ => None,
        }
    }

    /// If the bridge has been deactivated, block here until a new
    /// config is passed that reactivates the bridge
    async fn wait_on_reactivation(&mut self) -> Config {
        loop {
            if let Some(Command::UpdateConfig(c)) = self.control.recv().await {
                if c.active {
                    return c;
                }
            }
        }
    }
}

/// Block until an initial configuration is received via the command channel.
/// Returns the initial config once received, or `None` if the command channel
/// is closed.
async fn await_initial_configuration(
    receiver: &mut control::Receiver,
) -> Option<Config> {
    match receiver.recv().await {
        Some(Command::UpdateConfig(config)) => Some(config),
        _ => None,
    }
}

/// Set up an Oracle and run the process where the Oracle
/// processes and forwards Ethereum events to the ledger
pub fn run_oracle<C: RpcClient>(
    url: impl AsRef<str>,
    sender: BoundedSender<EthereumEvent>,
    control: control::Receiver,
    last_processed_block: last_processed_block::Sender,
    spawner: &mut AbortableSpawner,
) {
    let url = url.as_ref().to_owned();
    spawner
        .abortable("Ethereum Oracle", move |aborter| {
            let rt = tokio::runtime::Handle::current();
            rt.block_on(async move {
                LocalSet::new()
                    .run_until(async move {
                        tracing::info!(
                            ?url,
                            "Ethereum event oracle is starting"
                        );

                        let oracle = Oracle::<C>::new(
                            Either::Right(&url),
                            sender,
                            last_processed_block,
                            DEFAULT_BACKOFF,
                            DEFAULT_CEILING,
                            control,
                        );
                        run_oracle_aux(oracle).await;

                        tracing::info!(
                            ?url,
                            "Ethereum event oracle is no longer running"
                        );
                    })
                    .await
            });

            drop(aborter);
            Ok(())
        })
        .spawn_blocking();
}

/// Determine what action to take after attempting to
/// process events contained in an Ethereum block.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum ProcessEventAction {
    /// No events could be processed at this time, so we must keep
    /// polling for new events.
    ContinuePollingEvents,
    /// Some error occurred while processing Ethereum events in
    /// the current height. We must halt the oracle.
    HaltOracle,
    /// The current Ethereum block height has been processed.
    /// We must advance to the next Ethereum height.
    ProceedToNextBlock,
}

impl ProcessEventAction {
    /// Check whether the action commands a new block to be processed.
    #[inline]
    #[allow(dead_code)]
    pub fn process_new_block(&self) -> bool {
        matches!(self, Self::ProceedToNextBlock)
    }
}

impl ProcessEventAction {
    /// Handles the requested oracle action, translating it to a format
    /// understood by the set of [`Sleep`] abstractions.
    fn handle(self) -> ControlFlow<Result<(), ()>, ()> {
        match self {
            ProcessEventAction::ContinuePollingEvents => {
                ControlFlow::Continue(())
            }
            ProcessEventAction::HaltOracle => ControlFlow::Break(Err(())),
            ProcessEventAction::ProceedToNextBlock => {
                ControlFlow::Break(Ok(()))
            }
        }
    }
}

/// Tentatively process a batch of Ethereum events.
pub(crate) async fn try_process_eth_events<C: RpcClient>(
    oracle: &Oracle<C>,
    config: &Config,
    next_block_to_process: &ethereum_structs::BlockHeight,
) -> ProcessEventAction {
    process_events_in_block(next_block_to_process, oracle, config)
        .await
        .map_or_else(
            |error| {
                if oracle.client.may_recover(&error) {
                    tracing::debug!(
                        %error,
                        block = ?next_block_to_process,
                        "Error while trying to process Ethereum block"
                    );
                    ProcessEventAction::ContinuePollingEvents
                } else {
                    tracing::error!(
                        reason = %error,
                        block = ?next_block_to_process,
                        "The Ethereum oracle has disconnected"
                    );
                    ProcessEventAction::HaltOracle
                }
            },
            |()| ProcessEventAction::ProceedToNextBlock,
        )
}

/// Given an oracle, watch for new Ethereum events, processing
/// them into Namada native types.
///
/// It also checks that once the specified number of confirmations
/// is reached, an event is forwarded to the ledger process
async fn run_oracle_aux<C: RpcClient>(mut oracle: Oracle<C>) {
    tracing::info!("Oracle is awaiting initial configuration");
    let mut config =
        match await_initial_configuration(&mut oracle.control).await {
            Some(config) => {
                tracing::info!(
                    "Oracle received initial configuration - {:?}",
                    config
                );
                config
            }
            None => {
                tracing::debug!(
                    "Oracle control channel was closed before the oracle \
                     could be configured"
                );
                return;
            }
        };

    let mut next_block_to_process = config.start_block.clone();

    loop {
        tracing::info!(
            ?next_block_to_process,
            "Checking Ethereum block for bridge events"
        );
        let res = Sleep { strategy: Constant(oracle.backoff) }.run(|| async {
            tokio::select! {
                action = try_process_eth_events(&oracle, &config, &next_block_to_process) => {
                    action.handle()
                },
                _ = oracle.sender.closed() => {
                    tracing::info!(
                        "Ethereum oracle can not send events to the ledger; the \
                        receiver has hung up. Shutting down"
                    );
                    ControlFlow::Break(Err(()))
                }
            }
        })
        .await;

        if hints::unlikely(res.is_err()) {
            break;
        }

        oracle
            .last_processed_block
            .send_replace(Some(next_block_to_process.clone()));
        // check if a new config has been sent.
        if let Some(new_config) = oracle.update_config() {
            config = new_config;
        }
        if !config.active {
            config = oracle.wait_on_reactivation().await;
        }
        next_block_to_process = next_block_to_process.next();
    }
}

/// Checks if the given block has any events relating to the bridge, and if so,
/// sends them to the oracle's `sender` channel
async fn process_events_in_block<C: RpcClient>(
    block_to_process: &ethereum_structs::BlockHeight,
    oracle: &Oracle<C>,
    config: &Config,
) -> Result<(), Error> {
    let mut queue: Vec<PendingEvent> = vec![];
    let pending = &mut queue;
    // update the latest block height

    let last_processed_block_ref = oracle.last_processed_block.borrow();
    let last_processed_block = last_processed_block_ref.as_ref();
    let backoff = oracle.backoff;
    #[allow(clippy::arithmetic_side_effects)]
    let deadline = Instant::now() + oracle.ceiling;
    let latest_block = match oracle
        .client
        .syncing(last_processed_block, backoff, deadline)
        .await?
    {
        SyncStatus::AtHeight(height) => height,
        SyncStatus::Syncing => return Err(Error::FallenBehind),
    }
    .into();
    let minimum_latest_block = block_to_process.clone().unchecked_add(
        ethereum_structs::BlockHeight::from(config.min_confirmations),
    );
    if minimum_latest_block > latest_block {
        tracing::debug!(
            ?block_to_process,
            ?latest_block,
            ?minimum_latest_block,
            "Waiting for enough Ethereum blocks to be synced"
        );
        return Err(Error::MoreConfirmations);
    }
    tracing::debug!(
        ?block_to_process,
        ?latest_block,
        "Got latest Ethereum block height"
    );
    // check for events in Ethereum blocks that have reached the minimum number
    // of confirmations
    for codec in event_codecs() {
        let sig = codec.event_signature();
        let addr: Address = match codec.kind() {
            EventKind::Bridge => config.bridge_contract.into(),
        };
        tracing::debug!(
            ?block_to_process,
            ?addr,
            ?sig,
            "Checking for bridge events"
        );
        // fetch the events for matching the given signature
        let mut events = {
            let logs = oracle
                .client
                .check_events_in_block(block_to_process.clone(), addr, &sig)
                .await?;
            if !logs.is_empty() {
                tracing::info!(
                    ?block_to_process,
                    ?addr,
                    ?sig,
                    n_events = logs.len(),
                    "Found bridge events in Ethereum block"
                )
            }
            logs.into_iter()
                .map(IntoEthAbiLog::into_ethabi_log)
                .filter_map(|log| {
                    match PendingEvent::decode(
                        codec,
                        block_to_process.clone().into(),
                        &log,
                        u64::from(config.min_confirmations).into(),
                    ) {
                        Ok(event) => Some(event),
                        Err(error) => {
                            tracing::error!(
                                ?error,
                                ?block_to_process,
                                ?addr,
                                ?sig,
                                "Couldn't decode event: {:#?}",
                                log
                            );
                            None
                        }
                    }
                })
                .collect()
        };
        pending.append(&mut events);
        if !pending.is_empty() {
            tracing::info!(
                ?block_to_process,
                ?addr,
                ?sig,
                pending = pending.len(),
                "There are Ethereum events pending"
            );
        }
        let confirmed = process_queue(&latest_block, pending);
        if !confirmed.is_empty() {
            tracing::info!(
                ?block_to_process,
                ?addr,
                ?sig,
                pending = pending.len(),
                confirmed = confirmed.len(),
                min_confirmations = ?config.min_confirmations,
                "Some events that have reached the minimum number of \
                 confirmations and will be sent onwards"
            );
        }
        if !oracle.send(confirmed).await {
            return Err(Error::Channel(sig.into(), addr));
        }
    }
    Ok(())
}

/// Check which events in the queue have reached their
/// required number of confirmations and remove them
/// from the queue of pending events
fn process_queue(
    latest_block: &Uint256,
    pending: &mut Vec<PendingEvent>,
) -> Vec<EthereumEvent> {
    let mut pending_tmp: Vec<PendingEvent> = Vec::with_capacity(pending.len());
    std::mem::swap(&mut pending_tmp, pending);
    let mut confirmed = vec![];
    for item in pending_tmp.into_iter() {
        if item.is_confirmed(latest_block) {
            confirmed.push(item.event);
        } else {
            pending.push(item);
        }
    }
    confirmed
}

pub mod last_processed_block {
    //! Functionality to do with publishing which blocks we have processed.
    use namada_sdk::ethereum_structs;
    use tokio::sync::watch;

    pub type Sender = watch::Sender<Option<ethereum_structs::BlockHeight>>;
    pub type Receiver = watch::Receiver<Option<ethereum_structs::BlockHeight>>;

    /// Construct a [`tokio::sync::watch`] channel to publish the most recently
    /// processed block. Until the live oracle processes its first block, this
    /// will be `None`.
    pub fn channel() -> (Sender, Receiver) {
        watch::channel(None)
    }
}

#[cfg(test)]
mod test_oracle {
    use std::num::NonZeroU64;

    use ethbridge_bridge_events::{TransferToChainFilter, TransferToErcFilter};
    use namada_sdk::address::testing::gen_established_address;
    use namada_sdk::eth_bridge::ethers::types::H160;
    use namada_sdk::eth_bridge::structs::Erc20Transfer;
    use namada_sdk::ethereum_events::{EthAddress, TransferToEthereum};
    use namada_sdk::hash::Hash;
    use tokio::sync::oneshot::channel;
    use tokio::time::timeout;

    use super::*;
    use crate::ethereum_oracle::test_tools::event_log::GetLog;
    use crate::ethereum_oracle::test_tools::mock_web3_client::{
        TestCmd, TestOracle, Web3Client, Web3Controller, event_signature,
    };

    /// The data returned from setting up a test
    struct TestPackage {
        oracle: TestOracle,
        controller: Web3Controller,
        eth_recv: tokio::sync::mpsc::Receiver<EthereumEvent>,
        control_sender: control::Sender,
        blocks_processed_recv: tokio::sync::mpsc::UnboundedReceiver<Uint256>,
    }

    /// Helper function that starts running the oracle in a new thread, and
    /// initializes it with a simple default configuration that is appropriate
    /// for tests.
    async fn start_with_default_config(
        oracle: TestOracle,
        control_sender: &mut control::Sender,
        config: Config,
    ) -> tokio::task::JoinHandle<()> {
        let handle = tokio::task::spawn_blocking(move || {
            let rt = tokio::runtime::Handle::current();
            rt.block_on(async move {
                LocalSet::new()
                    .run_until(async move {
                        run_oracle_aux(oracle).await;
                    })
                    .await
            });
        });
        control_sender
            .try_send(control::Command::UpdateConfig(config))
            .unwrap();
        handle
    }

    /// Set up an oracle with a mock web3 client that we can control
    fn setup() -> TestPackage {
        let (blocks_processed_recv, client) = Web3Client::setup();
        let (eth_sender, eth_receiver) = tokio::sync::mpsc::channel(1000);
        let (last_processed_block_sender, _) = last_processed_block::channel();
        let (control_sender, control_receiver) = control::channel();
        let controller = client.controller();
        TestPackage {
            oracle: TestOracle {
                client,
                sender: eth_sender,
                last_processed_block: last_processed_block_sender,
                // backoff should be short for tests so that they run faster
                backoff: Duration::from_millis(5),
                ceiling: DEFAULT_CEILING,
                control: control_receiver,
            },
            controller,
            eth_recv: eth_receiver,
            control_sender,
            blocks_processed_recv,
        }
    }

    /// Test that if the fullnode stops, the oracle
    /// shuts down, even if the web3 client is unresponsive
    #[tokio::test]
    async fn test_shutdown() {
        let TestPackage {
            oracle,
            eth_recv,
            controller,
            mut control_sender,
            ..
        } = setup();
        let oracle = start_with_default_config(
            oracle,
            &mut control_sender,
            Config::default(),
        )
        .await;
        controller.apply_cmd(TestCmd::Unresponsive);
        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that if no logs are received from the web3
    /// client, no events are sent out
    #[tokio::test]
    async fn test_no_logs_no_op() {
        let TestPackage {
            oracle,
            mut eth_recv,
            controller,
            blocks_processed_recv: _processed,
            mut control_sender,
        } = setup();
        let oracle = start_with_default_config(
            oracle,
            &mut control_sender,
            Config::default(),
        )
        .await;
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(150u32)));

        let mut time = std::time::Duration::from_secs(1);
        while time > std::time::Duration::from_millis(10) {
            assert!(eth_recv.try_recv().is_err());
            time -= std::time::Duration::from_millis(10);
        }
        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that if a new block height doesn't increase,
    /// no events are sent out even if there are
    /// some in the logs.
    #[tokio::test]
    async fn test_cant_get_new_height() {
        let TestPackage {
            oracle,
            mut eth_recv,
            controller,
            blocks_processed_recv: _processed,
            mut control_sender,
        } = setup();
        let min_confirmations = 100;
        let config = Config {
            min_confirmations: NonZeroU64::try_from(min_confirmations)
                .expect("Test wasn't set up correctly"),
            ..Config::default()
        };
        let oracle =
            start_with_default_config(oracle, &mut control_sender, config)
                .await;
        // Increase height above the configured minimum confirmations
        controller.apply_cmd(TestCmd::NewHeight(min_confirmations.into()));

        let new_event = TransferToChainFilter {
            nonce: 0.into(),
            transfers: vec![],
            confirmations: 100.into(),
        }
        .get_log();
        let (sender, _) = channel();
        controller.apply_cmd(TestCmd::NewEvent {
            event_type: event_signature::<TransferToChainFilter>(),
            log: new_event,
            height: 101,
            seen: sender,
        });
        // since height is not updating, we should not receive events
        let mut time = std::time::Duration::from_secs(1);
        while time > std::time::Duration::from_millis(10) {
            assert!(eth_recv.try_recv().is_err());
            time -= std::time::Duration::from_millis(10);
        }
        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that the oracle waits until new logs
    /// are received before sending them on.
    #[tokio::test]
    async fn test_wait_on_new_logs() {
        let TestPackage {
            oracle,
            eth_recv,
            controller,
            blocks_processed_recv: _processed,
            mut control_sender,
        } = setup();
        let min_confirmations = 100;
        let config = Config {
            min_confirmations: NonZeroU64::try_from(min_confirmations)
                .expect("Test wasn't set up correctly"),
            ..Config::default()
        };
        let oracle =
            start_with_default_config(oracle, &mut control_sender, config)
                .await;
        // Increase height above the configured minimum confirmations
        controller.apply_cmd(TestCmd::NewHeight(min_confirmations.into()));

        // set the oracle to be unresponsive
        controller.apply_cmd(TestCmd::Unresponsive);
        // send a new event to the oracle
        let new_event = TransferToChainFilter {
            nonce: 0.into(),
            transfers: vec![],
            confirmations: 100.into(),
        }
        .get_log();
        let (sender, mut seen) = channel();
        controller.apply_cmd(TestCmd::NewEvent {
            event_type: event_signature::<TransferToChainFilter>(),
            log: new_event,
            height: 150,
            seen: sender,
        });
        // set the height high enough to emit the event
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(251u32)));

        // the event should not be emitted even though the height is large
        // enough
        let mut time = std::time::Duration::from_secs(1);
        while time > std::time::Duration::from_millis(10) {
            assert!(seen.try_recv().is_err());
            time -= std::time::Duration::from_millis(10);
        }
        // check that when web3 becomes responsive, oracle sends event
        controller.apply_cmd(TestCmd::Normal);
        seen.await.expect("Test failed");
        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that events are only sent when they
    /// reach the required number of confirmations
    #[tokio::test]
    async fn test_finality_gadget() {
        let TestPackage {
            oracle,
            mut eth_recv,
            controller,
            blocks_processed_recv: _processed,
            mut control_sender,
        } = setup();
        let min_confirmations = 100;
        let config = Config {
            min_confirmations: NonZeroU64::try_from(min_confirmations)
                .expect("Test wasn't set up correctly"),
            ..Config::default()
        };
        let oracle =
            start_with_default_config(oracle, &mut control_sender, config)
                .await;
        // Increase height above the configured minimum confirmations
        controller.apply_cmd(TestCmd::NewHeight(min_confirmations.into()));

        // confirmed after 100 blocks
        let first_event = TransferToChainFilter {
            nonce: 0.into(),
            transfers: vec![],
            confirmations: 100.into(),
        }
        .get_log();

        // confirmed after 125 blocks
        let gas_payer = gen_established_address();
        let second_event = TransferToErcFilter {
            transfers: vec![Erc20Transfer {
                amount: 0.into(),
                from: H160([0; 20]),
                to: H160([1; 20]),
                data_digest: [0; 32],
            }],
            relayer_address: gas_payer.to_string(),
            nonce: 0.into(),
        }
        .get_log();

        // send in the events to the logs
        let (sender, seen_second) = channel();
        controller.apply_cmd(TestCmd::NewEvent {
            event_type: event_signature::<TransferToErcFilter>(),
            log: second_event,
            height: 125,
            seen: sender,
        });
        let (sender, _recv) = channel();
        controller.apply_cmd(TestCmd::NewEvent {
            event_type: event_signature::<TransferToChainFilter>(),
            log: first_event,
            height: 100,
            seen: sender,
        });

        // increase block height so first event is confirmed but second is
        // not.
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(200u32)));
        // check the correct event is received
        let event = eth_recv.recv().await.expect("Test failed");
        if let EthereumEvent::TransfersToNamada { nonce, transfers } = event {
            assert_eq!(nonce, 0.into());
            assert!(transfers.is_empty());
        } else {
            panic!("Test failed, {:?}", event);
        }

        // check no other events are received
        let mut time = std::time::Duration::from_secs(1);
        while time > std::time::Duration::from_millis(10) {
            assert!(eth_recv.try_recv().is_err());
            time -= std::time::Duration::from_millis(10);
        }

        // increase block height so second event is emitted
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(225u32)));
        // wait until event is emitted
        seen_second.await.expect("Test failed");
        // increase block height so second event is confirmed
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(250u32)));
        // check correct event is received
        let event = eth_recv.recv().await.expect("Test failed");
        if let EthereumEvent::TransfersToEthereum { mut transfers, .. } = event
        {
            assert_eq!(transfers.len(), 1);
            let transfer = transfers.remove(0);
            assert_eq!(
                transfer,
                TransferToEthereum {
                    amount: Default::default(),
                    asset: EthAddress([0; 20]),
                    receiver: EthAddress([1; 20]),
                    checksum: Hash::default(),
                }
            );
        } else {
            panic!("Test failed");
        }

        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that Ethereum blocks are processed in sequence up to the latest
    /// block that has reached the minimum number of confirmations
    #[tokio::test]
    async fn test_blocks_checked_sequence() {
        let TestPackage {
            oracle,
            eth_recv,
            controller,
            mut blocks_processed_recv,
            mut control_sender,
        } = setup();
        let config = Config::default();
        let oracle = start_with_default_config(
            oracle,
            &mut control_sender,
            config.clone(),
        )
        .await;

        // set the height of the chain such that there are some blocks deep
        // enough to be considered confirmed by the oracle
        let confirmed_block_height = 9; // all blocks up to and including this block have enough confirmations
        let synced_block_height =
            u64::from(config.min_confirmations) + confirmed_block_height;
        for height in 0..synced_block_height + 1 {
            controller.apply_cmd(TestCmd::NewHeight(Uint256::from(height)));
        }
        // check that the oracle indeed processes the confirmed blocks
        for height in 0u64..confirmed_block_height + 1 {
            let block_processed = timeout(
                std::time::Duration::from_secs(3),
                blocks_processed_recv.recv(),
            )
            .await
            .expect("Timed out waiting for block to be checked")
            .unwrap();
            assert_eq!(block_processed, Uint256::from(height));
        }

        // check that the oracle hasn't yet checked any further blocks
        assert!(
            timeout(
                std::time::Duration::from_secs(1),
                blocks_processed_recv.recv()
            )
            .await
            .is_err()
        );

        // increase the height of the chain by one, and check that the oracle
        // processed the next confirmed block
        let synced_block_height = synced_block_height + 1;
        controller
            .apply_cmd(TestCmd::NewHeight(Uint256::from(synced_block_height)));

        let block_processed = timeout(
            std::time::Duration::from_secs(3),
            blocks_processed_recv.recv(),
        )
        .await
        .expect("Timed out waiting for block to be checked")
        .unwrap();
        assert_eq!(block_processed, Uint256::from(confirmed_block_height + 1));

        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that Ethereum oracle can be deactivate and reactivated
    /// via config updates.
    /// NOTE: This test can flake due to async channel race
    /// conditions.
    #[tokio::test]
    #[ignore]
    async fn test_oracle_reactivation() {
        let TestPackage {
            oracle,
            eth_recv,
            controller,
            mut blocks_processed_recv,
            mut control_sender,
        } = setup();
        let config = Config::default();
        let oracle = start_with_default_config(
            oracle,
            &mut control_sender,
            config.clone(),
        )
        .await;

        // set the height of the chain such that there are some blocks deep
        // enough to be considered confirmed by the oracle
        let confirmed_block_height = 9; // all blocks up to and including this block will have enough confirmations
        let min_confirmations = u64::from(config.min_confirmations);
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(
            min_confirmations + confirmed_block_height - 5,
        )));

        // check that the oracle indeed processes the expected blocks
        for height in 0u64..(confirmed_block_height - 4) {
            let block_processed = timeout(
                std::time::Duration::from_secs(3),
                blocks_processed_recv.recv(),
            )
            .await
            .expect("Timed out waiting for block to be checked")
            .unwrap();
            assert_eq!(block_processed, Uint256::from(height));
        }

        // Deactivate the bridge before all confirmed events are confirmed and
        // processed There is a very fine needle to thread here. A block
        // must be processed **after** this config is sent in order for
        // the updated config to be received. However, this test can
        // flake due to channel race conditions.
        control_sender
            .try_send(Command::UpdateConfig(Config {
                active: false,
                ..Default::default()
            }))
            .expect("Test failed");
        std::thread::sleep(Duration::from_secs(1));
        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(
            min_confirmations + confirmed_block_height - 4,
        )));

        let block_processed = timeout(
            std::time::Duration::from_secs(3),
            blocks_processed_recv.recv(),
        )
        .await
        .expect("Timed out waiting for block to be checked")
        .unwrap();
        assert_eq!(block_processed, Uint256::from(confirmed_block_height - 4));

        // check that the oracle hasn't yet checked any further blocks
        let res = timeout(
            std::time::Duration::from_secs(3),
            blocks_processed_recv.recv(),
        )
        .await;
        assert!(res.is_err());

        // reactivate the bridge and check that the oracle
        // processed the rest of the confirmed blocks
        control_sender
            .try_send(Command::UpdateConfig(Default::default()))
            .expect("Test failed");

        controller.apply_cmd(TestCmd::NewHeight(Uint256::from(
            min_confirmations + confirmed_block_height,
        )));
        for height in (confirmed_block_height - 3)..=confirmed_block_height {
            let block_processed = timeout(
                std::time::Duration::from_secs(3),
                blocks_processed_recv.recv(),
            )
            .await
            .expect("Timed out waiting for block to be checked")
            .unwrap();
            assert_eq!(block_processed, Uint256::from(height));
        }
        drop(eth_recv);
        oracle.await.expect("Test failed");
    }

    /// Test that if the Ethereum RPC endpoint returns a latest block that is
    /// more than one block later than the previous latest block we received, we
    /// still check all the blocks in between
    #[tokio::test]
    async fn test_all_blocks_checked() {
        let TestPackage {
            oracle,
            eth_recv,
            controller,
            mut blocks_processed_recv,
            mut control_sender,
        } = setup();
        let config = Config::default();
        let oracle = start_with_default_config(
            oracle,
            &mut control_sender,
            config.clone(),
        )
        .await;

        let confirmed_block_height = 9; // all blocks up to and including this block have enough confirmations
        let synced_block_height =
            u64::from(config.min_confirmations) + confirmed_block_height;
        controller
            .apply_cmd(TestCmd::NewHeight(Uint256::from(synced_block_height)));

        // check that the oracle has indeed processed the first `n` blocks, even
        // though the first latest block that the oracle received was not 0
        for height in 0u64..confirmed_block_height + 1 {
            let block_processed = timeout(
                std::time::Duration::from_secs(3),
                blocks_processed_recv.recv(),
            )
            .await
            .expect("Timed out waiting for block to be checked")
            .unwrap();
            assert_eq!(block_processed, Uint256::from(height));
        }

        // the next time the oracle checks, the latest block will have increased
        // by more than one
        let difference = 10;
        let synced_block_height = synced_block_height + difference;
        controller
            .apply_cmd(TestCmd::NewHeight(Uint256::from(synced_block_height)));

        // check that the oracle still checks the blocks in between
        for height in (confirmed_block_height + 1)
            ..(confirmed_block_height + difference + 1)
        {
            let block_processed = timeout(
                std::time::Duration::from_secs(3),
                blocks_processed_recv.recv(),
            )
            .await
            .expect("Timed out waiting for block to be checked")
            .unwrap();
            assert_eq!(block_processed, Uint256::from(height));
        }

        drop(eth_recv);
        oracle.await.expect("Test failed");
    }
}