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
use crate::testing::{
    benchmark_speed,
    verify::{assert_equals, Error as VerificationError},
    Speed, Thresholds,
};

mod measure;
mod node;
mod report;
mod wait;

use jormungandr_lib::time::Duration as LibsDuration;
pub use measure::*;
pub use node::{SyncNode, SyncNodeError, SyncNodeRecord};
pub use report::{MeasurementReportInterval, MeasurementReporter};
pub use wait::SyncWaitParams;

pub fn ensure_node_is_in_sync_with_others(
    target_node: &(impl SyncNode + Send),
    other_nodes: Vec<&(impl SyncNode + Send)>,
    sync_wait: Thresholds<Speed>,
    info: &str,
) -> Result<(), SyncNodeError> {
    let benchmark = benchmark_speed(info.to_owned())
        .with_thresholds(sync_wait)
        .start();

    while !benchmark.timeout_exceeded() {
        let target_node_block_height = target_node.last_block_height();

        let max_block_height: u32 = other_nodes
            .iter()
            .map(|node| node.last_block_height())
            .max()
            .expect("unable to retrieve block height from sync nodes");

        if target_node_block_height >= max_block_height {
            benchmark.stop();
            return Ok(());
        }
    }

    let other_nodes_records: Vec<SyncNodeRecord> = other_nodes
        .iter()
        .map(|x| SyncNodeRecord::new(x.alias(), x.last_block_height()))
        .collect();

    let target_node = SyncNodeRecord::new(target_node.alias(), target_node.last_block_height());

    Err(SyncNodeError::TimeoutWhenSyncingTargetNode {
        target_node,
        sync_nodes: other_nodes_records,
        timeout: benchmark.definition().thresholds().unwrap().max().into(),
    })
}

pub fn ensure_nodes_are_in_sync<A: SyncNode + ?Sized>(
    sync_wait: SyncWaitParams,
    nodes: &[&A],
) -> Result<(), VerificationError> {
    if nodes.len() < 2 {
        return Ok(());
    }

    wait_for_nodes_sync(&sync_wait);
    let duration: LibsDuration = sync_wait.wait_time().into();
    let first_node = nodes.iter().next().unwrap();

    let expected_tip = first_node.tip();
    let block_height = first_node.last_block_height();

    for node in nodes.iter().skip(1) {
        let tip = node.tip();
        assert_equals(
            &expected_tip,
            &tip,
            &format!("nodes are out of sync (different block hashes) after sync grace period: ({}) . Left node: alias: {}, content: {}, Right node: alias: {}, content: {}",
                duration,
                first_node.alias(),
                first_node.log_content(),
                node.alias(),
                node.log_content()),
        )?;
        assert_equals(
            &block_height,
            &node.last_block_height(),
            &format!("nodes are out of sync (different block height) after sync grace period: ({}) . Left node: alias: {}, content: {}, Right node: alias: {}, content: {}",
                duration,
                first_node.alias(),
                first_node.log_content(),
                node.alias(),
                node.log_content()
                ),
        )?;
    }
    Ok(())
}

pub fn wait_for_nodes_sync(sync_wait_params: &SyncWaitParams) {
    let wait_time = sync_wait_params.wait_time();
    std::thread::sleep(wait_time);
}