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
/*!
   Constructs for running test cases with two chains,
   together with the relayer setup with chain handles and foreign clients.
*/

use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::CreateOptions as ClientOptions;
use tracing::info;

use crate::bootstrap::binary::chain::{bootstrap_chains_with_full_nodes, BootstrapClientOptions};
use crate::error::Error;
use crate::framework::base::{HasOverrides, TestConfigOverride};
use crate::framework::binary::ics::InterchainSecurityChainTest;
use crate::framework::binary::node::{
    run_binary_node_test, run_single_node_test, BinaryNodeTest, NodeConfigOverride,
    NodeGenesisOverride,
};
use crate::framework::supervisor::{RunWithSupervisor, SupervisorOverride};
use crate::relayer::driver::RelayerDriver;
use crate::types::binary::chains::{ConnectedChains, DropChainHandle};
use crate::types::config::TestConfig;
use crate::types::env::write_env;
use crate::types::single::node::FullNode;
use crate::util::suspend::hang_on_error;

/**
   Runs a test case that implements [`BinaryChainTest`], with
   the test case being executed twice, with the second time having the
   position of the two chains flipped.
*/
pub fn run_two_way_binary_chain_test<Test, Overrides>(test: &Test) -> Result<(), Error>
where
    Test: BinaryChainTest,
    Test: HasOverrides<Overrides = Overrides>,
    Overrides: NodeConfigOverride
        + NodeGenesisOverride
        + RelayerConfigOverride
        + ClientOptionsOverride
        + SupervisorOverride
        + TestConfigOverride,
{
    run_binary_chain_test(&RunTwoWayBinaryChainTest::new(test))
}

/**
   Runs a test case that implements [`BinaryChainTest`].
*/
pub fn run_binary_chain_test<Test, Overrides>(test: &Test) -> Result<(), Error>
where
    Test: BinaryChainTest,
    Test: HasOverrides<Overrides = Overrides>,
    Overrides: NodeConfigOverride
        + NodeGenesisOverride
        + RelayerConfigOverride
        + ClientOptionsOverride
        + SupervisorOverride
        + TestConfigOverride,
{
    run_binary_node_test(&RunBinaryChainTest::new(&RunWithSupervisor::new(test)))
}

/**
   Runs a test case that implements [`BinaryChainTest`], with
   the test case being executed with a single chain that is connected
   to itself.
*/
pub fn run_self_connected_binary_chain_test<Test, Overrides>(test: &Test) -> Result<(), Error>
where
    Test: BinaryChainTest,
    Test: HasOverrides<Overrides = Overrides>,
    Overrides: NodeConfigOverride
        + NodeGenesisOverride
        + RelayerConfigOverride
        + ClientOptionsOverride
        + TestConfigOverride,
{
    run_single_node_test(&RunBinaryChainTest::new(test))
}

/**
   This trait is implemented for test cases that need to have two
   full nodes running together with the relayer setup with chain
   handles and foreign clients.

   Test writers can use this to implement test cases that only
   need the chains and relayers setup without the connection or
   channel handshake.
*/
pub trait BinaryChainTest {
    /// Test runner
    fn run<ChainA: ChainHandle, ChainB: ChainHandle>(
        &self,
        config: &TestConfig,
        relayer: RelayerDriver,
        chains: ConnectedChains<ChainA, ChainB>,
    ) -> Result<(), Error>;
}

/**
   An internal trait that can be implemented by test cases to override the
   relayer config before the relayer gets initialized.

   This is called by [`RunBinaryChainTest`] after the
   full nodes are running and before the relayer is initialized.

   Test writers should implement
   [`TestOverrides`](crate::framework::overrides::TestOverrides)
   for their test cases instead of implementing this trait directly.
*/
pub trait RelayerConfigOverride {
    /// Modify the relayer config
    fn modify_relayer_config(&self, config: &mut Config);
}

/// An internal trait that can be implemented by test cases to override the
/// settings for the foreign clients bootstrapped for the test.
///
/// The default implementation returns the settings for a client
/// connecting two Cosmos chains with no customizations.
/// Test writers should implement [`TestOverrides`]
/// for their test cases instead of implementing this trait directly.
///
/// [`TestOverrides`]: crate::framework::overrides::TestOverrides
///
pub trait ClientOptionsOverride {
    fn client_options_a_to_b(&self) -> ClientOptions {
        Default::default()
    }

    fn client_options_b_to_a(&self) -> ClientOptions {
        Default::default()
    }
}

/**
   A wrapper type that lifts a test case that implements [`BinaryChainTest`]
   into a test case the implements [`BinaryNodeTest`].
*/
pub struct RunBinaryChainTest<'a, Test> {
    /// Inner test
    pub test: &'a Test,
}

/**
   A wrapper type that lifts a test case that implements [`BinaryChainTest`]
   into a test case the implements [`BinaryChainTest`].

   During execution, the underlying [`BinaryChainTest`] is run twice, with
   the second time having the position of the two chains flipped.
*/
pub struct RunTwoWayBinaryChainTest<'a, Test> {
    /// Inner test
    pub test: &'a Test,
}

/**
   A wrapper type that lifts a test case that implements [`BinaryChainTest`]
   into a test case that implements [`BinaryNodeTest`].

   During execution, the test case is given a [`ConnectedChains`] with a
   single underlying chain that is connected to itself.
*/
pub struct RunSelfConnectedBinaryChainTest<'a, Test> {
    /// Inner test
    pub test: &'a Test,
}

impl<'a, Test> RunBinaryChainTest<'a, Test>
where
    Test: BinaryChainTest,
{
    /// Create a new [`RunBinaryChainTest`]
    pub fn new(test: &'a Test) -> Self {
        Self { test }
    }
}

impl<'a, Test> RunTwoWayBinaryChainTest<'a, Test>
where
    Test: BinaryChainTest,
{
    /// Create a new [`RunTwoWayBinaryChainTest`]
    pub fn new(test: &'a Test) -> Self {
        Self { test }
    }
}

impl<'a, Test> RunSelfConnectedBinaryChainTest<'a, Test>
where
    Test: BinaryChainTest,
{
    /// Create a new [`RunSelfConnectedBinaryChainTest`]
    pub fn new(test: &'a Test) -> Self {
        Self { test }
    }
}

impl<'a, Test, Overrides> BinaryNodeTest for RunBinaryChainTest<'a, Test>
where
    Test: BinaryChainTest,
    Test: HasOverrides<Overrides = Overrides>,
    Overrides: RelayerConfigOverride + ClientOptionsOverride,
{
    fn run(&self, config: &TestConfig, node_a: FullNode, node_b: FullNode) -> Result<(), Error> {
        let overrides = self.test.get_overrides();

        let bootstrap_options = BootstrapClientOptions::default()
            .client_options_a_to_b(overrides.client_options_a_to_b())
            .client_options_b_to_a(overrides.client_options_b_to_a())
            .bootstrap_with_random_ids(config.bootstrap_with_random_ids);

        let (relayer, chains) = bootstrap_chains_with_full_nodes(
            config,
            node_a,
            node_b,
            bootstrap_options,
            |config| {
                overrides.modify_relayer_config(config);
            },
        )?;

        let env_path = config.chain_store_dir.join("binary-chains.env");

        write_env(&env_path, &(&relayer, &chains))?;

        info!("written chains environment to {}", env_path.display());

        let _drop_handle_a = DropChainHandle(chains.handle_a.clone());
        let _drop_handle_b = DropChainHandle(chains.handle_b.clone());

        self.test.run(config, relayer, chains)?;

        Ok(())
    }
}

impl<'a, Test, Overrides> InterchainSecurityChainTest for RunBinaryChainTest<'a, Test>
where
    Test: BinaryChainTest,
    Test: HasOverrides<Overrides = Overrides>,
    Overrides: RelayerConfigOverride + ClientOptionsOverride,
{
    fn run(&self, config: &TestConfig, node_a: FullNode, node_b: FullNode) -> Result<(), Error> {
        let overrides = self.test.get_overrides();

        let bootstrap_options = BootstrapClientOptions::default()
            .client_options_a_to_b(overrides.client_options_a_to_b())
            .client_options_b_to_a(overrides.client_options_b_to_a())
            .bootstrap_with_random_ids(config.bootstrap_with_random_ids);

        let (relayer, chains) = bootstrap_chains_with_full_nodes(
            config,
            node_a,
            node_b,
            bootstrap_options,
            |config| {
                overrides.modify_relayer_config(config);
            },
        )?;

        let env_path = config.chain_store_dir.join("binary-chains.env");

        write_env(&env_path, &(&relayer, &chains))?;

        info!("written chains environment to {}", env_path.display());

        let _drop_handle_a = DropChainHandle(chains.handle_a.clone());
        let _drop_handle_b = DropChainHandle(chains.handle_b.clone());

        self.test.run(config, relayer, chains)?;

        Ok(())
    }
}

impl<'a, Test: BinaryChainTest> BinaryChainTest for RunTwoWayBinaryChainTest<'a, Test> {
    fn run<ChainA: ChainHandle, ChainB: ChainHandle>(
        &self,
        config: &TestConfig,
        relayer: RelayerDriver,
        chains: ConnectedChains<ChainA, ChainB>,
    ) -> Result<(), Error> {
        info!(
            "running two-way chain test, from {} to {}",
            chains.chain_id_a(),
            chains.chain_id_b(),
        );

        self.test.run(config, relayer.clone(), chains.clone())?;

        info!(
            "running two-way chain test in the opposite direction, from {} to {}",
            chains.chain_id_b(),
            chains.chain_id_a(),
        );

        let chains = chains.flip();

        self.test.run(config, relayer, chains)?;

        Ok(())
    }
}

impl<'a, Test, Overrides> BinaryChainTest for RunWithSupervisor<'a, Test>
where
    Test: BinaryChainTest,
    Test: HasOverrides<Overrides = Overrides>,
    Overrides: SupervisorOverride,
{
    fn run<ChainA: ChainHandle, ChainB: ChainHandle>(
        &self,
        config: &TestConfig,
        relayer: RelayerDriver,
        chains: ConnectedChains<ChainA, ChainB>,
    ) -> Result<(), Error> {
        if self.get_overrides().should_spawn_supervisor() {
            relayer
                .clone()
                .with_supervisor(|| self.test.run(config, relayer, chains))
        } else {
            hang_on_error(config.hang_on_fail, || {
                self.test.run(config, relayer, chains)
            })
        }
    }
}

impl<'a, Test, Overrides> HasOverrides for RunBinaryChainTest<'a, Test>
where
    Test: HasOverrides<Overrides = Overrides>,
{
    type Overrides = Overrides;

    fn get_overrides(&self) -> &Self::Overrides {
        self.test.get_overrides()
    }
}

impl<'a, Test, Overrides> HasOverrides for RunTwoWayBinaryChainTest<'a, Test>
where
    Test: HasOverrides<Overrides = Overrides>,
{
    type Overrides = Overrides;

    fn get_overrides(&self) -> &Self::Overrides {
        self.test.get_overrides()
    }
}