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
/*!
    Helper functions for bootstrapping two relayer chain handles
    with connected foreign clients.
*/

use eyre::Report as Error;
use ibc_relayer::chain::handle::{ChainHandle, CountingAndCachingChainHandle};
use ibc_relayer::config::Config;
use ibc_relayer::error::ErrorDetail as RelayerErrorDetail;
use ibc_relayer::foreign_client::{
    extract_client_id, CreateOptions as ClientOptions, ForeignClient,
};
use ibc_relayer::keyring::errors::ErrorDetail as KeyringErrorDetail;
use ibc_relayer::registry::SharedRegistry;
use ibc_relayer_types::core::ics24_host::identifier::ClientId;
use std::path::Path;
use std::time::Duration;
use std::{fs, thread};
use tracing::{debug, info};

use crate::relayer::driver::RelayerDriver;
use crate::types::binary::chains::ConnectedChains;
use crate::types::binary::foreign_client::ForeignClientPair;
use crate::types::config::TestConfig;
use crate::types::single::node::FullNode;
use crate::types::tagged::*;
use crate::types::wallet::{TestWallets, Wallet};
use crate::util::random::random_u64_range;

#[derive(Default)]
pub struct BootstrapClientOptions {
    pub client_options_a_to_b: ClientOptions,
    pub client_options_b_to_a: ClientOptions,
    pub pad_client_id_a_to_b: u64,
    pub pad_client_id_b_to_a: u64,
}

/// Bootstraps two relayer chain handles with connected foreign clients.
///
/// Returns a tuple consisting of the [`RelayerDriver`] and a
/// [`ConnectedChains`] object that contains the given
/// full nodes together with the corresponding two [`ChainHandle`]s and
/// [`ForeignClient`]s.
pub fn bootstrap_chains_with_full_nodes(
    test_config: &TestConfig,
    node_a: FullNode,
    node_b: FullNode,
    options: BootstrapClientOptions,
    config_modifier: impl FnOnce(&mut Config),
) -> Result<
    (
        RelayerDriver,
        ConnectedChains<impl ChainHandle, impl ChainHandle>,
    ),
    Error,
> {
    let mut config = Config::default();

    add_chain_config(&mut config, &node_a, test_config, 0)?;
    add_chain_config(&mut config, &node_b, test_config, 1)?;

    config_modifier(&mut config);

    let config_path = test_config.chain_store_dir.join("relayer-config.toml");

    save_relayer_config(&config, &config_path)?;

    let registry = new_registry(config.clone());

    // Pass in unique closure expressions `||{}` as the first argument so that
    // the returned chains are considered different types by Rust.
    // See [`spawn_chain_handle`] for more details.
    let handle_a = spawn_chain_handle(|| {}, &registry, &node_a)?;
    let handle_b = spawn_chain_handle(|| {}, &registry, &node_b)?;

    // Wait for the chain handles to be spawned
    thread::sleep(Duration::from_secs(10));

    pad_client_ids(&handle_a, &handle_b, options.pad_client_id_a_to_b)?;
    pad_client_ids(&handle_b, &handle_a, options.pad_client_id_b_to_a)?;

    let foreign_clients = bootstrap_foreign_client_pair(&handle_a, &handle_b, options)?;

    let relayer = RelayerDriver {
        config_path,
        config,
        registry,
        hang_on_fail: test_config.hang_on_fail,
    };

    let chains = ConnectedChains::new(
        handle_a,
        handle_b,
        MonoTagged::new(node_a),
        MonoTagged::new(node_b),
        foreign_clients,
    );

    Ok((relayer, chains))
}

/// Bootstraps two relayer chain handles with connected foreign clients.
///
/// Returns a tuple consisting of the [`RelayerDriver`] and a
/// [`ConnectedChains`] object that contains the given
/// full nodes together with the corresponding two [`ChainHandle`]s and
/// [`ForeignClient`]s.
///
/// This method gives the caller a way to modify the relayer configuration
/// that is pre-generated from the configurations of the full nodes.
pub fn bootstrap_foreign_client_pair<ChainA: ChainHandle, ChainB: ChainHandle>(
    chain_a: &ChainA,
    chain_b: &ChainB,
    options: BootstrapClientOptions,
) -> Result<ForeignClientPair<ChainA, ChainB>, Error> {
    let client_a_to_b = bootstrap_foreign_client(chain_a, chain_b, options.client_options_a_to_b)?;
    let client_b_to_a = bootstrap_foreign_client(chain_b, chain_a, options.client_options_b_to_a)?;
    Ok(ForeignClientPair::new(client_a_to_b, client_b_to_a))
}

pub fn bootstrap_foreign_client<ChainA: ChainHandle, ChainB: ChainHandle>(
    chain_a: &ChainA,
    chain_b: &ChainB,
    client_options: ClientOptions,
) -> Result<ForeignClient<ChainB, ChainA>, Error> {
    let foreign_client =
        ForeignClient::restore(ClientId::default(), chain_b.clone(), chain_a.clone());

    let event = foreign_client.build_create_client_and_send(client_options)?;
    let client_id = extract_client_id(&event.event)?.clone();

    info!(
        "created foreign client from chain {} to chain {} with client id {} on chain {}",
        chain_a.id(),
        chain_b.id(),
        client_id,
        chain_b.id()
    );

    Ok(ForeignClient::restore(
        client_id,
        chain_b.clone(),
        chain_a.clone(),
    ))
}

pub fn pad_client_ids<ChainA: ChainHandle, ChainB: ChainHandle>(
    chain_a: &ChainA,
    chain_b: &ChainB,
    pad_count: u64,
) -> Result<(), Error> {
    let foreign_client =
        ForeignClient::restore(ClientId::default(), chain_b.clone(), chain_a.clone());

    for i in 0..pad_count {
        debug!("creating new client id {} on chain {}", i + 1, chain_b.id());
        foreign_client.build_create_client_and_send(Default::default())?;
    }

    Ok(())
}

/**
   Spawn a new chain handle using the given [`SharedRegistry`] and
   [`FullNode`].

   The function accepts a proxy type `Seed` that should be unique
   accross multiple calls so that the returned [`ChainHandle`]
   have a unique type.

   For example, the following test should fail to compile:

   ```rust,compile_fail
   # use ibc_test_framework::bootstrap::binary::chain::spawn_chain_handle;
   fn same<T>(_: T, _: T) {}

   let chain_a = spawn_chain_handle(|| {}, todo!(), todo!()).unwrap();
   let chain_b = spawn_chain_handle(|| {}, todo!(), todo!()).unwrap();
   same(chain_a, chain_b); // error: chain_a and chain_b have different types
   ```

   The reason is that Rust would give each closure expression `||{}` a
   [unique anonymous type](https://doc.rust-lang.org/reference/types/closure.html).
   When we instantiate two chains with different closure types,
   the resulting values would be considered by Rust to have different types.

   With this we can treat `chain_a` and `chain_b` having different types
   so that we do not accidentally mix them up later in the code.
*/
pub fn spawn_chain_handle<Seed>(
    _: Seed,
    registry: &SharedRegistry<impl ChainHandle>,
    node: &FullNode,
) -> Result<impl ChainHandle, Error> {
    let chain_id = &node.chain_driver.chain_id;
    let handle = registry.get_or_spawn(chain_id)?;

    add_keys_to_chain_handle(&handle, &node.wallets)?;

    Ok(handle)
}

/**
   Add a wallet key to a [`ChainHandle`]'s key store.

   Note that if the [`ChainConfig`](ibc_relayer::config::ChainConfig) is
   configured to use in-memory store only, the added key would not be
   accessible through external CLI.
*/
pub fn add_key_to_chain_handle<Chain: ChainHandle>(
    chain: &Chain,
    wallet: &Wallet,
) -> Result<(), Error> {
    let res = chain.add_key(wallet.id.0.clone(), wallet.key.clone().into());

    // Ignore error if chain handle already have the given key
    match res {
        Err(e) => match e.detail() {
            RelayerErrorDetail::KeyBase(e2) => match e2.source {
                KeyringErrorDetail::KeyAlreadyExist(_) => Ok(()),
                _ => Err(e.into()),
            },
            _ => Err(e.into()),
        },
        Ok(()) => Ok(()),
    }
}

/**
   Add multiple wallets provided in [`TestWallets`] into the
   [`ChainHandle`]'s key store.
*/
pub fn add_keys_to_chain_handle<Chain: ChainHandle>(
    chain: &Chain,
    wallets: &TestWallets,
) -> Result<(), Error> {
    add_key_to_chain_handle(chain, &wallets.relayer)?;
    add_key_to_chain_handle(chain, &wallets.user1)?;
    add_key_to_chain_handle(chain, &wallets.user2)?;

    Ok(())
}

/**
   Create a new [`SharedRegistry`] that uses [`CountingAndCachingChainHandle`]
   as the [`ChainHandle`] implementation.
*/
pub fn new_registry(config: Config) -> SharedRegistry<CountingAndCachingChainHandle> {
    <SharedRegistry<CountingAndCachingChainHandle>>::new(config)
}

/**
   Generate [`ChainConfig`](ibc_relayer::config::ChainConfig) from a running
   [`FullNode`] and add it to the relayer's [`Config`].
*/
pub fn add_chain_config(
    config: &mut Config,
    running_node: &FullNode,
    test_config: &TestConfig,
    chain_number: usize,
) -> Result<(), Error> {
    let chain_config = running_node.generate_chain_config(
        &running_node.chain_driver.chain_type,
        test_config,
        chain_number,
    )?;

    config.chains.push(chain_config);
    Ok(())
}

/**
   Save a relayer's [`Config`] to the filesystem to make it accessible
   through external CLI.
*/
pub fn save_relayer_config(config: &Config, config_path: &Path) -> Result<(), Error> {
    let config_str = toml::to_string_pretty(&config)?;

    fs::write(config_path, &config_str)?;

    info!(
        "written hermes config.toml to {}:\n{}",
        config_path.display(),
        config_str
    );

    Ok(())
}

impl BootstrapClientOptions {
    /// Overrides options for the foreign client connecting chain A to chain B.
    pub fn client_options_a_to_b(mut self, options: ClientOptions) -> Self {
        self.client_options_a_to_b = options;
        self
    }

    /// Overrides options for the foreign client connecting chain B to chain A.
    pub fn client_options_b_to_a(mut self, options: ClientOptions) -> Self {
        self.client_options_b_to_a = options;
        self
    }

    pub fn bootstrap_with_random_ids(mut self, bootstrap_with_random_ids: bool) -> Self {
        if bootstrap_with_random_ids {
            self.pad_client_id_b_to_a = random_u64_range(1, 6);
            self.pad_client_id_a_to_b = random_u64_range(1, 6);
        } else {
            self.pad_client_id_b_to_a = 0;
            self.pad_client_id_a_to_b = 1;
        }

        self
    }
}