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
/*!
    Helper functions for bootstrapping a connection between two chains.
*/

use core::time::Duration;
use eyre::{eyre, Report as Error};
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::config::default::connection_delay as default_connection_delay;
use ibc_relayer::connection::{Connection, ConnectionSide};
use ibc_relayer_types::timestamp::ZERO_DURATION;
use tracing::{debug, info};

use crate::relayer::connection::TaggedConnectionExt;
use crate::types::binary::client::ClientIdPair;
use crate::types::binary::connection::ConnectedConnection;
use crate::types::binary::foreign_client::ForeignClientPair;
use crate::types::id::TaggedClientIdRef;
use crate::util::random::random_u64_range;

pub struct BootstrapConnectionOptions {
    pub connection_delay: Duration,
    pub pad_connection_id_a: u64,
    pub pad_connection_id_b: u64,
}

/**
   Create a new [`ConnectedConnection`] using the foreign clients with
   initialized client IDs.
*/
pub fn bootstrap_connection<ChainA: ChainHandle, ChainB: ChainHandle>(
    foreign_clients: &ForeignClientPair<ChainA, ChainB>,
    options: BootstrapConnectionOptions,
) -> Result<ConnectedConnection<ChainA, ChainB>, Error> {
    let chain_a = foreign_clients.handle_a();
    let chain_b = foreign_clients.handle_b();

    let client_id_a = foreign_clients.client_id_a();
    let client_id_b = foreign_clients.client_id_b();

    pad_connection_id(
        &chain_a,
        &chain_b,
        &client_id_a,
        &client_id_b,
        options.pad_connection_id_a,
    )?;
    pad_connection_id(
        &chain_b,
        &chain_a,
        &client_id_b,
        &client_id_a,
        options.pad_connection_id_b,
    )?;

    let connection = Connection::new(
        foreign_clients.client_b_to_a.clone(),
        foreign_clients.client_a_to_b.clone(),
        options.connection_delay,
    )?;

    let connection_id_a = connection
        .tagged_connection_id_a()
        .ok_or_else(|| eyre!("expected connection id to present"))?
        .cloned();

    let connection_id_b = connection
        .tagged_connection_id_b()
        .ok_or_else(|| eyre!("expected connection id to present"))?
        .cloned();

    info!(
        "created new chain/client/connection from {}/{}/{} to {}/{}/{}",
        chain_a.id(),
        client_id_a,
        connection_id_a,
        chain_b.id(),
        client_id_b,
        connection_id_b,
    );

    let connected_connection = ConnectedConnection::new(
        ClientIdPair::new(client_id_a.cloned(), client_id_b.cloned()),
        connection,
        connection_id_a,
        connection_id_b,
    );

    Ok(connected_connection)
}

/**
   Create a random number of dummy connection IDs so that the bootstrapped
   connection ID is random instead of being always `connection-0`.

   This would help us catch bugs where the connection IDs are used at
   the wrong side of the chain, but still got accepted because the
   connection IDs on both sides are the same.
*/
pub fn pad_connection_id<ChainA: ChainHandle, ChainB: ChainHandle>(
    chain_a: &ChainA,
    chain_b: &ChainB,
    client_id_a: &TaggedClientIdRef<ChainA, ChainB>,
    client_id_b: &TaggedClientIdRef<ChainB, ChainA>,
    pad_count: u64,
) -> Result<(), Error> {
    for i in 0..pad_count {
        debug!(
            "creating new connection id {} on chain {}",
            i + 1,
            chain_a.id()
        );

        let connection: Connection<ChainB, ChainA> = Connection {
            delay_period: ZERO_DURATION,
            a_side: ConnectionSide::new(chain_b.clone(), client_id_b.cloned().into_value(), None),
            b_side: ConnectionSide::new(chain_a.clone(), client_id_a.cloned().into_value(), None),
        };

        connection.build_conn_init_and_send()?;
    }

    Ok(())
}

impl Default for BootstrapConnectionOptions {
    fn default() -> Self {
        Self {
            connection_delay: default_connection_delay(),
            pad_connection_id_a: 0,
            pad_connection_id_b: 0,
        }
    }
}

impl BootstrapConnectionOptions {
    pub fn connection_delay(mut self, connection_delay: Duration) -> Self {
        self.connection_delay = connection_delay;
        self
    }

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

        self
    }
}