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
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use crate::block_synchronizer::{BlockHeader, BlockSynchronizeResult, Command};
use fastcrypto::Hash;
use prometheus::IntGauge;
use std::collections::HashMap;
use tokio::sync::oneshot;
use types::{metered_channel, CertificateDigest};

#[derive(Debug)]
enum Core {
    SynchronizeBlockHeaders {
        block_ids: Vec<CertificateDigest>,
        times: u32,
        result: Vec<BlockSynchronizeResult<BlockHeader>>,
        ready: oneshot::Sender<()>,
    },
    SynchronizeBlockPayload {
        block_ids: Vec<CertificateDigest>,
        times: u32,
        result: Vec<BlockSynchronizeResult<BlockHeader>>,
        ready: oneshot::Sender<()>,
    },
    AssertExpectations {
        ready: oneshot::Sender<()>,
    },
}

struct MockBlockSynchronizerCore {
    /// A map that holds the expected requests for sync block headers and their
    /// stubbed response.
    block_headers_expected_requests:
        HashMap<Vec<CertificateDigest>, (u32, Vec<BlockSynchronizeResult<BlockHeader>>)>,

    /// A map that holds the expected requests for sync block payload and their
    /// stubbed response.
    block_payload_expected_requests:
        HashMap<Vec<CertificateDigest>, (u32, Vec<BlockSynchronizeResult<BlockHeader>>)>,

    /// Channel to receive the messages that are supposed to be sent to the
    /// block synchronizer.
    rx_commands: metered_channel::Receiver<Command>,

    /// Channel to receive the commands to mock the requests.
    rx_core: metered_channel::Receiver<Core>,
}

impl MockBlockSynchronizerCore {
    async fn run(&mut self) {
        loop {
            tokio::select! {
                Some(command) = self.rx_commands.recv() => {
                    match command {
                        Command::SynchronizeRange { .. } => {
                            todo!("MockBlockSynchronizerCore for Command::SynchronizeRange is unimplemented!")
                        }
                        Command::SynchronizeBlockHeaders { block_ids, respond_to } => {
                            let (times, results) = self
                                .block_headers_expected_requests
                                .remove(&block_ids)
                                .unwrap_or_else(||panic!("{}", format!("Unexpected call received for SynchronizeBlockHeaders, {:?}", block_ids).as_str()))
                                .to_owned();

                            if times > 1 {
                                self.block_headers_expected_requests.insert(block_ids, (times - 1, results.clone()));
                            }

                            for result in results {
                                respond_to.send(result).await.expect("Couldn't send message");
                            }
                        }
                        Command::SynchronizeBlockPayload { certificates, respond_to } => {
                            let block_ids = certificates.into_iter().map(|c|c.digest()).collect();
                            let (times, results) = self
                                .block_payload_expected_requests
                                .remove(&block_ids)
                                .unwrap_or_else(||panic!("{}", format!("Unexpected call received for SynchronizeBlockPayload, {:?}", block_ids).as_str()))
                                .to_owned();

                            if times > 1 {
                                self.block_payload_expected_requests.insert(block_ids, (times - 1, results.clone()));
                            }

                            for result in results {
                                respond_to.send(result).await.expect("Couldn't send message");
                            }
                        }
                    }
                }
                Some(command) = self.rx_core.recv() => {
                    match command {
                        Core::SynchronizeBlockHeaders {
                            block_ids,
                            times,
                            result,
                            ready,
                        } => {
                            self.block_headers_expected_requests.insert(block_ids, (times, result));
                            ready.send(()).expect("Failed to send ready message");
                        },
                        Core::SynchronizeBlockPayload {
                            block_ids,
                            times,
                            result,
                            ready,
                        } => {
                            self.block_payload_expected_requests.insert(block_ids, (times, result));
                            ready.send(()).expect("Failed to send ready message");
                        },
                        Core::AssertExpectations {ready} => {
                            self.assert_expectations();
                            ready.send(()).expect("Failed to send ready message");
                        }
                    }
                }
            }
        }
    }

    fn assert_expectations(&self) {
        let mut result: String = "".to_string();

        for (ids, results) in &self.block_headers_expected_requests {
            result.push_str(
                format!(
                    "SynchronizeBlockHeaders, ids={:?}, results={:?}",
                    ids, results
                )
                .as_str(),
            );
        }

        for (ids, results) in &self.block_payload_expected_requests {
            result.push_str(
                format!(
                    "SynchronizeBlockPayload, ids={:?}, results={:?}",
                    ids, results
                )
                .as_str(),
            );
        }

        if !result.is_empty() {
            panic!(
                "There are expected calls that haven't been fulfilled \n\n {}",
                result
            );
        }
    }
}

/// A mock helper for the BlockSynchronizer to help us mock the responses
/// eliminating the need to wire in the actual BlockSynchronizer when needed
/// for other components.
pub struct MockBlockSynchronizer {
    tx_core: metered_channel::Sender<Core>,
}

impl MockBlockSynchronizer {
    pub fn new(rx_commands: metered_channel::Receiver<Command>) -> Self {
        let mock_counter = IntGauge::new("MOCK_COUNTER", "mock counter").unwrap();
        let (tx_core, rx_core) = metered_channel::channel(1, &mock_counter);

        let mut core = MockBlockSynchronizerCore {
            block_headers_expected_requests: HashMap::new(),
            block_payload_expected_requests: HashMap::new(),
            rx_commands,
            rx_core,
        };

        tokio::spawn(async move {
            core.run().await;
        });

        Self { tx_core }
    }

    /// A simple method that allow us to mock responses for the
    /// SynchronizeBlockHeaders requests.
    /// `block_ids`: The block_ids we expect
    /// `results`: The results we would like to respond with
    /// `times`: How many times we should expect to be called.
    pub async fn expect_synchronize_block_headers(
        &self,
        block_ids: Vec<CertificateDigest>,
        result: Vec<BlockSynchronizeResult<BlockHeader>>,
        times: u32,
    ) {
        let (tx, rx) = oneshot::channel();
        self.tx_core
            .send(Core::SynchronizeBlockHeaders {
                block_ids,
                times,
                result,
                ready: tx,
            })
            .await
            .expect("Failed to send mock expectation");

        Self::await_channel(rx).await;
    }

    /// A method that allow us to mock responses for the
    /// SynchronizeBlockPayload requests. It has to be noted that we use
    /// the block_ids as a way to identify the expected certificates for
    /// the request since that on its own suffice to identify them.
    /// `block_ids`: The block_ids we expect
    /// `results`: The results we would like to respond with
    /// `times`: How many times we should expect to be called.
    pub async fn expect_synchronize_block_payload(
        &self,
        block_ids: Vec<CertificateDigest>,
        result: Vec<BlockSynchronizeResult<BlockHeader>>,
        times: u32,
    ) {
        let (tx, rx) = oneshot::channel();
        self.tx_core
            .send(Core::SynchronizeBlockPayload {
                block_ids,
                times,
                result,
                ready: tx,
            })
            .await
            .expect("Failed to send mock expectation");

        Self::await_channel(rx).await;
    }

    /// Asserts that all the expectations have been fulfilled and no
    /// expectation has been left without having been called.
    pub async fn assert_expectations(&self) {
        let (tx, rx) = oneshot::channel();
        self.tx_core
            .send(Core::AssertExpectations { ready: tx })
            .await
            .expect("Failed to assert expectations");

        Self::await_channel(rx).await;
    }

    /// Helper method to wait on a oneshot receiver channel
    /// and avoid printing the error. We expect when the
    /// MockBlockSynchronizerCore panics to violently close
    /// the provided oneshot channel. To ensure that the
    /// current thread will panic, we are handling the error
    /// case and we also print an empty message to avoid
    /// printing the receive error.
    async fn await_channel(rx: oneshot::Receiver<()>) {
        if rx.await.is_err() {
            panic!("");
        }
    }
}