casper-node 2.0.3

The Casper blockchain node
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
#![allow(clippy::boxed_local)] // We use boxed locals to pass on event data unchanged.

//! Reactor core.
//!
//! Any long running instance of the node application uses an event-dispatch pattern: Events are
//! generated and stored on an event queue, then processed one-by-one. This process happens inside
//! the reactor, which also exclusively holds the state of the application besides pending events:
//!
//! 1. The reactor pops a reactor event off the event queue (called a
//!    [`Scheduler`](type.Scheduler.html)).
//! 2. The event is dispatched by the reactor via [`Reactor::dispatch_event`]. Since the reactor
//!    holds mutable state, it can grant any component that processes an event mutable, exclusive
//!    access to its state.
//! 3. Once the [(synchronous)](`crate::components::Component::handle_event`) event processing has
//!    completed, the component returns an [`effect`](crate::effect).
//! 4. The reactor spawns a task that executes these effects and possibly schedules more events.
//! 5. go to 1.
//!
//! For descriptions of events and instructions on how to create effects, see the
//! [`effect`](super::effect) module.
//!
//! # Reactors
//!
//! There is no single reactor, but rather a reactor for each application type, since it defines
//! which components are used and how they are wired up. The reactor defines the state by being a
//! `struct` of components, their initialization through [`Reactor::new`] and event dispatching to
//! components via [`Reactor::dispatch_event`].
//!
//! With all these set up, a reactor can be executed using a [`Runner`], either in a step-wise
//! manner using [`Runner::crank`] or indefinitely using [`Runner::run`].

mod event_queue_metrics;
pub(crate) mod main_reactor;
mod queue_kind;

use std::{
    any,
    collections::HashMap,
    env,
    fmt::{Debug, Display},
    io::Write,
    num::NonZeroU64,
    str::FromStr,
    sync::{atomic::Ordering, Arc},
};

use datasize::DataSize;
use erased_serde::Serialize as ErasedSerialize;
#[cfg(test)]
use fake_instant::FakeClock;
#[cfg(test)]
use futures::future::BoxFuture;
use futures::FutureExt;
use once_cell::sync::Lazy;
use prometheus::{self, Histogram, HistogramOpts, IntCounter, IntGauge, Registry};
use quanta::{Clock, IntoNanoseconds};
use serde::Serialize;
use signal_hook::consts::signal::{SIGINT, SIGQUIT, SIGTERM};
use stats_alloc::{Stats, INSTRUMENTED_SYSTEM};
use tokio::time::{Duration, Instant};
use tracing::{debug_span, error, info, instrument, trace, warn, Instrument, Span};

#[cfg(test)]
use crate::components::ComponentState;
#[cfg(test)]
use casper_types::testing::TestRng;
use casper_types::{
    Block, BlockHeader, Chainspec, ChainspecRawBytes, FinalitySignature, Transaction,
};

#[cfg(target_os = "linux")]
use utils::rlimit::{Limit, OpenFiles, ResourceLimit};

#[cfg(test)]
use crate::testing::{network::NetworkedReactor, ConditionCheckReactor};
use crate::{
    components::{
        block_accumulator,
        fetcher::{self, FetchItem},
        network::{blocklist::BlocklistJustification, Identity as NetworkIdentity},
        transaction_acceptor,
    },
    effect::{
        announcements::{ControlAnnouncement, PeerBehaviorAnnouncement, QueueDumpFormat},
        incoming::NetResponse,
        Effect, EffectBuilder, EffectExt, Effects,
    },
    failpoints::FailpointActivation,
    types::{BlockExecutionResultsOrChunk, ExitCode, LegacyDeploy, NodeId, SyncLeap, TrieOrChunk},
    unregister_metric,
    utils::{self, SharedFlag, WeightedRoundRobin},
    NodeRng, TERMINATION_REQUESTED,
};
use casper_storage::block_store::types::ApprovalsHashes;
pub(crate) use queue_kind::QueueKind;

/// Default threshold for when an event is considered slow.  Can be overridden by setting the env
/// var `CL_EVENT_MAX_MICROSECS=<MICROSECONDS>`.
const DEFAULT_DISPATCH_EVENT_THRESHOLD: Duration = Duration::from_secs(1);
const DISPATCH_EVENT_THRESHOLD_ENV_VAR: &str = "CL_EVENT_MAX_MICROSECS";
#[cfg(test)]
const POLL_INTERVAL: Duration = Duration::from_millis(10);

static DISPATCH_EVENT_THRESHOLD: Lazy<Duration> = Lazy::new(|| {
    env::var(DISPATCH_EVENT_THRESHOLD_ENV_VAR)
        .map(|threshold_str| {
            let threshold_microsecs = u64::from_str(&threshold_str).unwrap_or_else(|error| {
                panic!(
                    "can't parse env var {}={} as a u64: {}",
                    DISPATCH_EVENT_THRESHOLD_ENV_VAR, threshold_str, error
                )
            });
            Duration::from_micros(threshold_microsecs)
        })
        .unwrap_or_else(|_| DEFAULT_DISPATCH_EVENT_THRESHOLD)
});

#[cfg(target_os = "linux")]
/// The desired limit for open files.
const TARGET_OPEN_FILES_LIMIT: Limit = 64_000;

#[cfg(target_os = "linux")]
/// Adjusts the maximum number of open file handles upwards towards the hard limit.
fn adjust_open_files_limit() {
    // Ensure we have reasonable ulimits.
    match ResourceLimit::<OpenFiles>::get() {
        Err(err) => {
            warn!(%err, "could not retrieve open files limit");
        }

        Ok(current_limit) => {
            if current_limit.current() < TARGET_OPEN_FILES_LIMIT {
                let best_possible = if current_limit.max() < TARGET_OPEN_FILES_LIMIT {
                    warn!(
                        wanted = TARGET_OPEN_FILES_LIMIT,
                        hard_limit = current_limit.max(),
                        "settling for lower open files limit due to hard limit"
                    );
                    current_limit.max()
                } else {
                    TARGET_OPEN_FILES_LIMIT
                };

                let new_limit = ResourceLimit::<OpenFiles>::fixed(best_possible);
                if let Err(err) = new_limit.set() {
                    warn!(%err, current=current_limit.current(), target=best_possible, "did not succeed in raising open files limit")
                } else {
                    tracing::debug!(?new_limit, "successfully increased open files limit");
                }
            } else {
                tracing::debug!(
                    ?current_limit,
                    "not changing open files limit, already sufficient"
                );
            }
        }
    }
}

#[cfg(not(target_os = "linux"))]
/// File handle limit adjustment shim.
fn adjust_open_files_limit() {
    info!("not on linux, not adjusting open files limit");
}

/// Event scheduler
///
/// The scheduler is a combination of multiple event queues that are polled in a specific order. It
/// is the central hook for any part of the program that schedules events directly.
///
/// Components rarely use this, but use a bound `EventQueueHandle` instead.
///
/// Schedule tuples contain an optional ancestor ID and the actual event. The ancestor ID indicates
/// which potential previous event resulted in the event being created.
pub(crate) type Scheduler<Ev> = WeightedRoundRobin<(Option<NonZeroU64>, Ev), QueueKind>;

/// Event queue handle
///
/// The event queue handle is how almost all parts of the application interact with the reactor
/// outside of the normal event loop. It gives different parts a chance to schedule messages that
/// stem from things like external IO.
#[derive(DataSize, Debug)]
pub(crate) struct EventQueueHandle<REv>
where
    REv: 'static,
{
    /// A reference to the scheduler of the event queue.
    scheduler: &'static Scheduler<REv>,
    /// Flag indicating whether or not the reactor processing this event queue is shutting down.
    is_shutting_down: SharedFlag,
}

// Implement `Clone` and `Copy` manually, as `derive` will make it depend on `R` and `Ev` otherwise.
impl<REv> Clone for EventQueueHandle<REv> {
    fn clone(&self) -> Self {
        *self
    }
}
impl<REv> Copy for EventQueueHandle<REv> {}

impl<REv> EventQueueHandle<REv> {
    /// Creates a new event queue handle.
    pub(crate) fn new(scheduler: &'static Scheduler<REv>, is_shutting_down: SharedFlag) -> Self {
        EventQueueHandle {
            scheduler,
            is_shutting_down,
        }
    }

    /// Creates a new event queue handle that is not connected to a shutdown flag.
    ///
    /// This method is used in tests, where we are never disabling shutdown warnings anyway.
    #[cfg(test)]
    pub(crate) fn without_shutdown(scheduler: &'static Scheduler<REv>) -> Self {
        EventQueueHandle::new(scheduler, SharedFlag::global_shared())
    }

    /// Schedule an event on a specific queue.
    ///
    /// The scheduled event will not have an ancestor.
    pub(crate) async fn schedule<Ev>(self, event: Ev, queue_kind: QueueKind)
    where
        REv: From<Ev>,
    {
        self.schedule_with_ancestor(None, event, queue_kind).await;
    }

    /// Schedule an event on a specific queue.
    pub(crate) async fn schedule_with_ancestor<Ev>(
        self,
        ancestor: Option<NonZeroU64>,
        event: Ev,
        queue_kind: QueueKind,
    ) where
        REv: From<Ev>,
    {
        self.scheduler
            .push((ancestor, event.into()), queue_kind)
            .await;
    }

    /// Returns number of events in each of the scheduler's queues.
    pub(crate) fn event_queues_counts(&self) -> HashMap<QueueKind, usize> {
        self.scheduler.event_queues_counts()
    }

    /// Returns whether the associated reactor is currently shutting down.
    pub(crate) fn shutdown_flag(&self) -> SharedFlag {
        self.is_shutting_down
    }
}

/// Reactor core.
///
/// Any reactor should implement this trait and be executed by the `reactor::run` function.
pub(crate) trait Reactor: Sized {
    // Note: We've gone for the `Sized` bound here, since we return an instance in `new`. As an
    // alternative, `new` could return a boxed instance instead, removing this requirement.

    /// Event type associated with reactor.
    ///
    /// Defines what kind of event the reactor processes.
    type Event: ReactorEvent + Display;

    /// A configuration for the reactor
    type Config;

    /// The error type returned by the reactor.
    type Error: Send + 'static;

    /// Dispatches an event on the reactor.
    ///
    /// This function is typically only called by the reactor itself to dispatch an event. It is
    /// safe to call regardless, but will cause the event to skip the queue and things like
    /// accounting.
    fn dispatch_event(
        &mut self,
        effect_builder: EffectBuilder<Self::Event>,
        rng: &mut NodeRng,
        event: Self::Event,
    ) -> Effects<Self::Event>;

    /// Creates a new instance of the reactor.
    ///
    /// This method creates the full state, which consists of all components, and returns a reactor
    /// instance along with the effects that the components generated upon instantiation.
    ///
    /// If any instantiation fails, an error is returned.
    fn new(
        cfg: Self::Config,
        chainspec: Arc<Chainspec>,
        chainspec_raw_bytes: Arc<ChainspecRawBytes>,
        network_identity: NetworkIdentity,
        registry: &Registry,
        event_queue: EventQueueHandle<Self::Event>,
        rng: &mut NodeRng,
    ) -> Result<(Self, Effects<Self::Event>), Self::Error>;

    /// Instructs the reactor to update performance metrics, if any.
    fn update_metrics(&mut self, _event_queue_handle: EventQueueHandle<Self::Event>) {}

    /// Activate/deactivate a failpoint.
    fn activate_failpoint(&mut self, _activation: &FailpointActivation) {
        // Default is to ignore the failpoint. If failpoint support is enabled for a reactor, route
        // the activation to the respective components here.
    }

    /// Returns the state of a named components.
    ///
    /// May return `None` if the component cannot be found, or if the reactor does not support
    /// querying component states.
    #[allow(dead_code)]
    #[cfg(test)]
    fn get_component_state(&self, _name: &str) -> Option<&ComponentState> {
        None
    }
}

/// A reactor event type.
pub(crate) trait ReactorEvent: Send + Debug + From<ControlAnnouncement> + 'static {
    /// Returns `true` if the event is a control announcement variant.
    fn is_control(&self) -> bool;

    /// Converts the event into a control announcement without copying.
    ///
    /// Note that this function must return `Some` if and only `is_control` returns `true`.
    fn try_into_control(self) -> Option<ControlAnnouncement>;

    /// Returns a cheap but human-readable description of the event.
    fn description(&self) -> &'static str {
        "anonymous event"
    }
}

/// A drop-like trait for `async` compatible drop-and-wait.
///
/// Shuts down a type by explicitly freeing resources, but allowing to wait on cleanup to complete.
#[cfg(test)]
pub(crate) trait Finalize: Sized {
    /// Runs cleanup code and waits for a shutdown to complete.
    ///
    /// This function must always be optional and a way to wait for all resources to be freed, not
    /// mandatory for cleanup!
    fn finalize(self) -> BoxFuture<'static, ()> {
        async move {}.boxed()
    }
}

/// Represents memory statistics in bytes.
struct AllocatedMem {
    /// Total allocated memory in bytes.
    allocated: u64,
    /// Total consumed memory in bytes.
    consumed: u64,
    /// Total system memory in bytes.
    total: u64,
}

/// A runner for a reactor.
///
/// The runner manages a reactor's event queue and reactor itself and can run it either continuously
/// or in a step-by-step manner.
#[derive(Debug)]
pub(crate) struct Runner<R>
where
    R: Reactor,
{
    /// The scheduler used for the reactor.
    scheduler: &'static Scheduler<R::Event>,

    /// The reactor instance itself.
    reactor: R,

    /// Counter for events, to aid tracing.
    current_event_id: u64,

    /// Timestamp of last reactor metrics update.
    last_metrics: Instant,

    /// Metrics for the runner.
    metrics: RunnerMetrics,

    /// Check if we need to update reactor metrics every this many events.
    event_metrics_threshold: u64,

    /// Only update reactor metrics if at least this much time has passed.
    event_metrics_min_delay: Duration,

    /// An accurate, possible TSC-supporting clock.
    clock: Clock,

    /// Flag indicating the reactor is being shut down.
    is_shutting_down: SharedFlag,
}

/// Metric data for the Runner
#[derive(Debug)]
struct RunnerMetrics {
    /// Total number of events processed.
    events: IntCounter,
    /// Histogram of how long it took to dispatch an event.
    event_dispatch_duration: Histogram,
    /// Total allocated RAM in bytes, as reported by stats_alloc.
    allocated_ram_bytes: IntGauge,
    /// Total consumed RAM in bytes, as reported by sys-info.
    consumed_ram_bytes: IntGauge,
    /// Total system RAM in bytes, as reported by sys-info.
    total_ram_bytes: IntGauge,
    /// Handle to the metrics registry, in case we need to unregister.
    registry: Registry,
}

impl RunnerMetrics {
    /// Create and register new runner metrics.
    fn new(registry: &Registry) -> Result<Self, prometheus::Error> {
        let events = IntCounter::new(
            "runner_events",
            "running total count of events handled by this reactor",
        )?;

        // Create an event dispatch histogram, putting extra emphasis on the area between 1-10 us.
        let event_dispatch_duration = Histogram::with_opts(
            HistogramOpts::new(
                "event_dispatch_duration",
                "time in nanoseconds to dispatch an event",
            )
            .buckets(vec![
                100.0,
                500.0,
                1_000.0,
                5_000.0,
                10_000.0,
                20_000.0,
                50_000.0,
                100_000.0,
                200_000.0,
                300_000.0,
                400_000.0,
                500_000.0,
                600_000.0,
                700_000.0,
                800_000.0,
                900_000.0,
                1_000_000.0,
                2_000_000.0,
                5_000_000.0,
            ]),
        )?;

        let allocated_ram_bytes =
            IntGauge::new("allocated_ram_bytes", "total allocated ram in bytes")?;
        let consumed_ram_bytes =
            IntGauge::new("consumed_ram_bytes", "total consumed ram in bytes")?;
        let total_ram_bytes = IntGauge::new("total_ram_bytes", "total system ram in bytes")?;

        registry.register(Box::new(events.clone()))?;
        registry.register(Box::new(event_dispatch_duration.clone()))?;
        registry.register(Box::new(allocated_ram_bytes.clone()))?;
        registry.register(Box::new(consumed_ram_bytes.clone()))?;
        registry.register(Box::new(total_ram_bytes.clone()))?;

        Ok(RunnerMetrics {
            events,
            event_dispatch_duration,
            registry: registry.clone(),
            allocated_ram_bytes,
            consumed_ram_bytes,
            total_ram_bytes,
        })
    }
}

impl Drop for RunnerMetrics {
    fn drop(&mut self) {
        unregister_metric!(self.registry, self.events);
        unregister_metric!(self.registry, self.event_dispatch_duration);
        unregister_metric!(self.registry, self.allocated_ram_bytes);
        unregister_metric!(self.registry, self.consumed_ram_bytes);
        unregister_metric!(self.registry, self.total_ram_bytes);
    }
}

impl<R> Runner<R>
where
    R: Reactor,
    R::Event: Serialize,
    R::Error: From<prometheus::Error>,
{
    /// Creates a new runner from a given configuration, using existing metrics.
    #[instrument(
        "init",
        level = "debug",
        skip_all,
        fields(node_id = %NodeId::from(&network_identity))
    )]
    pub(crate) async fn with_metrics(
        cfg: R::Config,
        chainspec: Arc<Chainspec>,
        chainspec_raw_bytes: Arc<ChainspecRawBytes>,
        network_identity: NetworkIdentity,
        rng: &mut NodeRng,
        registry: &Registry,
    ) -> Result<Self, R::Error> {
        adjust_open_files_limit();

        let event_size = size_of::<R::Event>();

        // Check if the event is of a reasonable size. This only emits a runtime warning at startup
        // right now, since storage size of events is not an issue per se, but copying might be
        // expensive if events get too large.
        if event_size > 16 * size_of::<usize>() {
            warn!(
                %event_size, type_name = ?any::type_name::<R::Event>(),
                "large event size, consider reducing it or boxing"
            );
        }

        let event_queue_dump_threshold =
            env::var("CL_EVENT_QUEUE_DUMP_THRESHOLD").map_or(None, |s| s.parse::<usize>().ok());

        let scheduler = utils::leak(Scheduler::new(
            QueueKind::weights(),
            event_queue_dump_threshold,
        ));
        let is_shutting_down = SharedFlag::new();
        let event_queue = EventQueueHandle::new(scheduler, is_shutting_down);
        let (reactor, initial_effects) = R::new(
            cfg,
            chainspec,
            chainspec_raw_bytes,
            network_identity,
            registry,
            event_queue,
            rng,
        )?;

        info!(
            "Reactor: with_metrics has: {} initial_effects",
            initial_effects.len()
        );
        // Run all effects from component instantiation.
        process_effects(None, scheduler, initial_effects, QueueKind::Regular)
            .instrument(debug_span!("process initial effects"))
            .await;

        info!("reactor main loop is ready");

        Ok(Runner {
            scheduler,
            reactor,
            current_event_id: 1,
            metrics: RunnerMetrics::new(registry)?,
            last_metrics: Instant::now(),
            event_metrics_min_delay: Duration::from_secs(30),
            event_metrics_threshold: 1000,
            clock: Clock::new(),
            is_shutting_down,
        })
    }

    /// Processes a single event on the event queue.
    ///
    /// Returns `Some(exit_code)` if processing should stop.
    #[instrument("dispatch", level = "debug", fields(a, ev = self.current_event_id), skip(self, rng))]
    pub(crate) async fn crank(&mut self, rng: &mut NodeRng) -> Option<ExitCode> {
        self.metrics.events.inc();

        let event_queue = EventQueueHandle::new(self.scheduler, self.is_shutting_down);
        let effect_builder = EffectBuilder::new(event_queue);

        // Update metrics like memory usage and event queue sizes.
        if self.current_event_id % self.event_metrics_threshold == 0 {
            // We update metrics on the first very event as well to get a good baseline.
            if self.last_metrics.elapsed() >= self.event_metrics_min_delay {
                self.reactor.update_metrics(event_queue);

                // Use a fresh timestamp. This skews the metrics collection interval a little bit,
                // but ensures that if metrics collection time explodes, we are guaranteed a full
                // `event_metrics_min_delay` of event processing.
                self.last_metrics = Instant::now();
            }

            if let Some(AllocatedMem {
                allocated,
                consumed,
                total,
            }) = Self::get_allocated_memory()
            {
                trace!(%allocated, %total, "memory allocated");
                self.metrics.allocated_ram_bytes.set(allocated as i64);
                self.metrics.consumed_ram_bytes.set(consumed as i64);
                self.metrics.total_ram_bytes.set(total as i64);
            }
        }

        let ((ancestor, event), queue_kind) = self.scheduler.pop().await;
        trace!(%event, %queue_kind, "current");
        let event_desc = event.description();

        // Create another span for tracing the processing of one event.
        Span::current().record("ev", self.current_event_id);

        // If we know the ancestor of an event, record it.
        if let Some(ancestor) = ancestor {
            Span::current().record("a", ancestor.get());
        }

        // Dispatch the event, then execute the resulting effect.
        let start = self.clock.start();

        let (effects, maybe_exit_code, queue_kind) = if event.is_control() {
            // We've received a control event, which will _not_ be handled by the reactor.
            match event.try_into_control() {
                None => {
                    // If `as_control().is_some()` is true, but `try_into_control` fails, the trait
                    // is implemented incorrectly.
                    error!(
                        "event::as_control succeeded, but try_into_control failed. this is a bug"
                    );

                    // We ignore the event.
                    (Effects::new(), None, QueueKind::Control)
                }
                Some(ControlAnnouncement::ShutdownDueToUserRequest) => (
                    Effects::new(),
                    Some(ExitCode::CleanExitDontRestart),
                    QueueKind::Control,
                ),
                Some(ControlAnnouncement::ShutdownForUpgrade) => {
                    (Effects::new(), Some(ExitCode::Success), QueueKind::Control)
                }
                Some(ControlAnnouncement::ShutdownAfterCatchingUp) => (
                    Effects::new(),
                    Some(ExitCode::CleanExitDontRestart),
                    QueueKind::Control,
                ),
                Some(ControlAnnouncement::FatalError { file, line, msg }) => {
                    error!(%file, %line, %msg, "fatal error via control announcement");
                    (Effects::new(), Some(ExitCode::Abort), QueueKind::Control)
                }
                Some(ControlAnnouncement::QueueDumpRequest {
                    dump_format,
                    finished,
                }) => {
                    match dump_format {
                        QueueDumpFormat::Serde(mut ser) => {
                            self.scheduler
                                .dump(move |queue_dump| {
                                    if let Err(err) =
                                        queue_dump.erased_serialize(&mut ser.as_serializer())
                                    {
                                        warn!(%err, "queue dump failed to serialize");
                                    }
                                })
                                .await;
                        }
                        QueueDumpFormat::Debug(ref file) => {
                            match file.try_clone() {
                                Ok(mut local_file) => {
                                    self.scheduler
                                        .dump(move |queue_dump| {
                                            write!(&mut local_file, "{:?}", queue_dump)
                                                .and_then(|_| local_file.flush())
                                                .map_err(|err| {
                                                    warn!(
                                                        ?err,
                                                        "failed to write/flush queue dump using debug format"
                                                    );
                                                })
                                                .ok();
                                        })
                                        .await;
                                }
                                Err(err) => warn!(
                                    %err,
                                    "could not create clone of temporary file for queue debug dump"
                                ),
                            };
                        }
                    }

                    // Notify requester that we finished writing the queue dump.
                    finished.respond(()).await;

                    // Do nothing on queue dump otherwise.
                    (Default::default(), None, QueueKind::Control)
                }
                Some(ControlAnnouncement::ActivateFailpoint { activation }) => {
                    self.reactor.activate_failpoint(&activation);

                    // No other effects, calling the method is all we had to do.
                    (Effects::new(), None, QueueKind::Control)
                }
            }
        } else {
            (
                self.reactor.dispatch_event(effect_builder, rng, event),
                None,
                queue_kind,
            )
        };

        let end = self.clock.end();

        // Warn if processing took a long time, record to histogram.
        let delta = self.clock.delta(start, end);
        if delta > *DISPATCH_EVENT_THRESHOLD {
            warn!(%event_desc, ns = delta.into_nanos(), "event took very long to dispatch");
        }
        self.metrics
            .event_dispatch_duration
            .observe(delta.into_nanos() as f64);

        // Run effects, with the current event ID as the ancestor for resulting set of events.
        process_effects(
            NonZeroU64::new(self.current_event_id),
            self.scheduler,
            effects,
            queue_kind,
        )
        .in_current_span()
        .await;

        self.current_event_id += 1;

        maybe_exit_code
    }

    /// Gets both the allocated and total memory from sys-info + jemalloc
    fn get_allocated_memory() -> Option<AllocatedMem> {
        let mem_info = match sys_info::mem_info() {
            Ok(mem_info) => mem_info,
            Err(error) => {
                warn!(%error, "unable to get mem_info using sys-info");
                return None;
            }
        };

        // mem_info gives us kilobytes
        let total = mem_info.total * 1024;
        let consumed = total - (mem_info.avail * 1024);

        let Stats {
            allocations: _,
            deallocations: _,
            reallocations: _,
            bytes_allocated,
            bytes_deallocated,
            bytes_reallocated: _,
        } = INSTRUMENTED_SYSTEM.stats();

        Some(AllocatedMem {
            allocated: bytes_allocated.saturating_sub(bytes_deallocated) as u64,
            consumed,
            total,
        })
    }

    /// Runs the reactor until `self.crank` returns `Some` or we get interrupted by a termination
    /// signal.
    pub(crate) async fn run(&mut self, rng: &mut NodeRng) -> ExitCode {
        loop {
            match TERMINATION_REQUESTED.load(Ordering::SeqCst) as i32 {
                0 => {
                    if let Some(exit_code) = self.crank(rng).await {
                        self.is_shutting_down.set();
                        break exit_code;
                    }
                }
                SIGINT => {
                    self.is_shutting_down.set();
                    break ExitCode::SigInt;
                }
                SIGQUIT => {
                    self.is_shutting_down.set();
                    break ExitCode::SigQuit;
                }
                SIGTERM => {
                    self.is_shutting_down.set();
                    break ExitCode::SigTerm;
                }
                _ => error!("should be unreachable - bug in signal handler"),
            }
        }
    }
}

#[cfg(test)]
#[derive(Eq, PartialEq, Debug)]
pub(crate) enum TryCrankOutcome {
    NoEventsToProcess,
    ProcessedAnEvent,
    ShouldExit(ExitCode),
    Exited,
}

#[cfg(test)]
impl<R> Runner<R>
where
    R: Reactor,
    R::Event: Serialize,
    R::Error: From<prometheus::Error>,
{
    /// Creates a new runner from a given configuration.
    ///
    /// Creates a metrics registry that is only going to be used in this runner.
    pub(crate) async fn new(
        cfg: R::Config,
        chainspec: Arc<Chainspec>,
        chainspec_raw_bytes: Arc<ChainspecRawBytes>,
        rng: &mut NodeRng,
    ) -> Result<Self, R::Error> {
        // Instantiate a new registry for metrics for this reactor.
        let registry = Registry::new();
        let network_identity = NetworkIdentity::with_generated_certs().unwrap();
        Self::with_metrics(
            cfg,
            chainspec,
            chainspec_raw_bytes,
            network_identity,
            rng,
            &registry,
        )
        .await
    }

    /// Create an instance of an `EffectBuilder`.
    #[cfg(test)]
    pub(crate) fn effect_builder(&self) -> EffectBuilder<R::Event> {
        let event_queue = EventQueueHandle::new(self.scheduler, self.is_shutting_down);
        EffectBuilder::new(event_queue)
    }

    /// Inject (schedule then process) effects created via a call to `create_effects` which is
    /// itself passed an instance of an `EffectBuilder`.
    #[cfg(test)]
    pub(crate) async fn process_injected_effects<F>(&mut self, create_effects: F)
    where
        F: FnOnce(EffectBuilder<R::Event>) -> Effects<R::Event>,
    {
        use tracing::{debug_span, Instrument};

        let event_queue = EventQueueHandle::new(self.scheduler, self.is_shutting_down);
        let effect_builder = EffectBuilder::new(event_queue);

        let effects = create_effects(effect_builder);

        process_effects(None, self.scheduler, effects, QueueKind::Regular)
            .instrument(debug_span!(
                "process injected effects",
                ev = self.current_event_id
            ))
            .await
    }

    /// Processes a single event if there is one and we haven't previously handled an exit code.
    pub(crate) async fn try_crank(&mut self, rng: &mut NodeRng) -> TryCrankOutcome {
        if self.is_shutting_down.is_set() {
            TryCrankOutcome::Exited
        } else if self.scheduler.item_count() == 0 {
            TryCrankOutcome::NoEventsToProcess
        } else {
            match self.crank(rng).await {
                Some(exit_code) => {
                    self.is_shutting_down.set();
                    TryCrankOutcome::ShouldExit(exit_code)
                }
                None => TryCrankOutcome::ProcessedAnEvent,
            }
        }
    }

    /// Returns a reference to the reactor.
    pub(crate) fn reactor(&self) -> &R {
        &self.reactor
    }

    /// Returns a mutable reference to the reactor.
    pub(crate) fn reactor_mut(&mut self) -> &mut R {
        &mut self.reactor
    }

    /// Shuts down a reactor, sealing and draining the entire queue before returning it.
    pub(crate) async fn drain_into_inner(self) -> R {
        self.is_shutting_down.set();
        self.scheduler.seal();
        for (ancestor, event) in self.scheduler.drain_queues().await {
            tracing::debug!(?ancestor, %event, "drained event");
        }
        self.reactor
    }
}

#[cfg(test)]
impl<R> Runner<ConditionCheckReactor<R>>
where
    R: Reactor + NetworkedReactor,
    R::Event: Serialize,
    R::Error: From<prometheus::Error>,
{
    /// Cranks the runner until `condition` is true or until `within` has elapsed.
    ///
    /// Returns `true` if `condition` has been met within the specified timeout.
    ///
    /// Panics if cranking causes the node to return an exit code.
    pub(crate) async fn crank_until<F>(&mut self, rng: &mut TestRng, condition: F, within: Duration)
    where
        F: Fn(&R::Event) -> bool + Send + 'static,
    {
        self.reactor.set_condition_checker(Box::new(condition));

        tokio::time::timeout(within, self.crank_and_check_indefinitely(rng))
            .await
            .unwrap_or_else(|_| {
                panic!(
                    "Runner::crank_until() timed out after {}s on node {}",
                    within.as_secs_f64(),
                    self.reactor.inner().node_id()
                )
            })
    }

    async fn crank_and_check_indefinitely(&mut self, rng: &mut TestRng) {
        loop {
            match self.try_crank(rng).await {
                TryCrankOutcome::NoEventsToProcess => {
                    FakeClock::advance_time(POLL_INTERVAL.as_millis() as u64);
                    tokio::time::sleep(POLL_INTERVAL).await;
                    continue;
                }
                TryCrankOutcome::ProcessedAnEvent => {}
                TryCrankOutcome::ShouldExit(exit_code) => {
                    panic!("should not exit: {:?}", exit_code)
                }
                TryCrankOutcome::Exited => unreachable!(),
            }

            if self.reactor.condition_result() {
                info!("{} met condition", self.reactor.inner().node_id());
                return;
            }
        }
    }
}

/// Spawns tasks that will process the given effects.
///
/// Result events from processing the events will be scheduled with the given ancestor.
async fn process_effects<Ev>(
    ancestor: Option<NonZeroU64>,
    scheduler: &'static Scheduler<Ev>,
    effects: Effects<Ev>,
    queue_kind: QueueKind,
) where
    Ev: Send + 'static,
{
    for effect in effects {
        tokio::spawn(async move {
            for event in effect.await {
                scheduler.push((ancestor, event), queue_kind).await;
            }
        });
    }
}

/// Converts a single effect into another by wrapping it.
fn wrap_effect<Ev, REv, F>(wrap: F, effect: Effect<Ev>) -> Effect<REv>
where
    F: Fn(Ev) -> REv + Send + 'static,
    Ev: Send + 'static,
    REv: Send + 'static,
{
    // The double-boxing here is very unfortunate =(.
    (async move {
        let events = effect.await;
        events.into_iter().map(wrap).collect()
    })
    .boxed()
}

/// Converts multiple effects into another by wrapping.
pub(crate) fn wrap_effects<Ev, REv, F>(wrap: F, effects: Effects<Ev>) -> Effects<REv>
where
    F: Fn(Ev) -> REv + Send + 'static + Clone,
    Ev: Send + 'static,
    REv: Send + 'static,
{
    effects
        .into_iter()
        .map(move |effect| wrap_effect(wrap.clone(), effect))
        .collect()
}

fn handle_fetch_response<R, I>(
    reactor: &mut R,
    effect_builder: EffectBuilder<<R as Reactor>::Event>,
    rng: &mut NodeRng,
    sender: NodeId,
    serialized_item: &[u8],
) -> Effects<<R as Reactor>::Event>
where
    I: FetchItem,
    R: Reactor,
    <R as Reactor>::Event: From<fetcher::Event<I>> + From<PeerBehaviorAnnouncement>,
{
    match fetcher::Event::<I>::from_get_response_serialized_item(sender, serialized_item) {
        Some(fetcher_event) => {
            Reactor::dispatch_event(reactor, effect_builder, rng, fetcher_event.into())
        }
        None => effect_builder
            .announce_block_peer_with_justification(
                sender,
                BlocklistJustification::SentBadItem { tag: I::TAG },
            )
            .ignore(),
    }
}

fn handle_get_response<R>(
    reactor: &mut R,
    effect_builder: EffectBuilder<<R as Reactor>::Event>,
    rng: &mut NodeRng,
    sender: NodeId,
    message: Box<NetResponse>,
) -> Effects<<R as Reactor>::Event>
where
    R: Reactor,
    <R as Reactor>::Event: From<transaction_acceptor::Event>
        + From<fetcher::Event<FinalitySignature>>
        + From<fetcher::Event<Block>>
        + From<fetcher::Event<BlockHeader>>
        + From<fetcher::Event<BlockExecutionResultsOrChunk>>
        + From<fetcher::Event<LegacyDeploy>>
        + From<fetcher::Event<Transaction>>
        + From<fetcher::Event<SyncLeap>>
        + From<fetcher::Event<TrieOrChunk>>
        + From<fetcher::Event<ApprovalsHashes>>
        + From<block_accumulator::Event>
        + From<PeerBehaviorAnnouncement>,
{
    match *message {
        NetResponse::Transaction(ref serialized_item) => handle_fetch_response::<R, Transaction>(
            reactor,
            effect_builder,
            rng,
            sender,
            serialized_item,
        ),
        NetResponse::LegacyDeploy(ref serialized_item) => handle_fetch_response::<R, LegacyDeploy>(
            reactor,
            effect_builder,
            rng,
            sender,
            serialized_item,
        ),
        NetResponse::Block(ref serialized_item) => {
            handle_fetch_response::<R, Block>(reactor, effect_builder, rng, sender, serialized_item)
        }
        NetResponse::BlockHeader(ref serialized_item) => handle_fetch_response::<R, BlockHeader>(
            reactor,
            effect_builder,
            rng,
            sender,
            serialized_item,
        ),
        NetResponse::FinalitySignature(ref serialized_item) => {
            handle_fetch_response::<R, FinalitySignature>(
                reactor,
                effect_builder,
                rng,
                sender,
                serialized_item,
            )
        }
        NetResponse::SyncLeap(ref serialized_item) => handle_fetch_response::<R, SyncLeap>(
            reactor,
            effect_builder,
            rng,
            sender,
            serialized_item,
        ),
        NetResponse::ApprovalsHashes(ref serialized_item) => {
            handle_fetch_response::<R, ApprovalsHashes>(
                reactor,
                effect_builder,
                rng,
                sender,
                serialized_item,
            )
        }
        NetResponse::BlockExecutionResults(ref serialized_item) => {
            handle_fetch_response::<R, BlockExecutionResultsOrChunk>(
                reactor,
                effect_builder,
                rng,
                sender,
                serialized_item,
            )
        }
    }
}