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
/*!
   Constructs for implementing overrides for test cases.
*/

use core::time::Duration;
use ibc_relayer::config::default::connection_delay as default_connection_delay;
use ibc_relayer::config::Config;
use ibc_relayer::foreign_client::CreateOptions as ClientOptions;
use ibc_relayer_types::core::ics04_channel::channel::Ordering;
use ibc_relayer_types::core::ics04_channel::version::Version;
use ibc_relayer_types::core::ics24_host::identifier::PortId;

use crate::error::Error;
use crate::framework::base::HasOverrides;
use crate::framework::base::TestConfigOverride;
use crate::framework::binary::chain::{ClientOptionsOverride, RelayerConfigOverride};
use crate::framework::binary::channel::{
    ChannelOrderOverride, ChannelVersionOverride, PortsOverride,
};
use crate::framework::binary::connection::ConnectionDelayOverride;
use crate::framework::binary::node::{NodeConfigOverride, NodeGenesisOverride};
use crate::framework::nary::channel::PortsOverride as NaryPortsOverride;
use crate::framework::supervisor::SupervisorOverride;
use crate::types::config::TestConfig;

/**
   This trait should be implemented for all test cases to allow overriding
   some parts of the behavior during the test setup.

   Since all methods in this trait have default implementation, test cases
   that do not need any override can have an empty implementation body for
   this trait.

   The trait provides generic implementation of the specialized traits such as
   [`RelayerConfigOverride`]. As a result, it is sufficient for test
   writers to only implement this trait instead of implementing the
   numerous override traits.

   When a new override trait is defined, the same trait method should
   also be defined inside this trait with a default method body.
*/
pub trait TestOverrides {
    fn modify_test_config(&self, _config: &mut TestConfig) {}

    /**
        Modify the full node config before the chain gets initialized.

        The config is in the dynamic-typed [`toml::Value`] format, as we do not
        want to model the full format of the node config in Rust. Test authors
        can use the helper methods in [`chain::config`](crate::chain::config)
        to modify common config fields.

        Implemented for [`NodeConfigOverride`].
    */
    fn modify_node_config(&self, _config: &mut toml::Value) -> Result<(), Error> {
        Ok(())
    }

    /**
        Modify the genesis file before the chain gets initialized.

        The config is in the dynamic-typed [`serde_json::Value`] format, as we do not
        want to model the full format of the genesis file in Rust.

        Implemented for [`NodeGenesisOverride`].
    */
    fn modify_genesis_file(&self, _genesis: &mut serde_json::Value) -> Result<(), Error> {
        Ok(())
    }

    /**
       Modify the relayer config before initializing the relayer. Does no
       modification by default.

       Implemented for [`RelayerConfigOverride`].
    */
    fn modify_relayer_config(&self, _config: &mut Config) {
        // No modification by default
    }

    /// Returns the settings for the foreign client on the first chain for the
    /// second chain. The defaults are for a client connecting two Cosmos chains
    /// with no custom settings.
    fn client_options_a_to_b(&self) -> ClientOptions {
        Default::default()
    }

    /// Returns the settings for the foreign client on the second chain for the
    /// first chain. The defaults are for a client connecting two Cosmos chains
    /// with no custom settings.
    fn client_options_b_to_a(&self) -> ClientOptions {
        Default::default()
    }

    fn should_spawn_supervisor(&self) -> bool {
        true
    }

    /**
       Return the connection delay used for creating connections as [`Duration`].
       Defaults to zero.

       Implemented for [`ConnectionDelayOverride`].
    */
    fn connection_delay(&self) -> Duration {
        default_connection_delay()
    }

    /**
       Return the port ID used for creating the channel for the first chain.
       Returns the "transfer" port by default.

       Implemented for [`PortsOverride`].
    */
    fn channel_port_a(&self) -> PortId {
        PortId::transfer()
    }

    /**
       Return the port ID used for creating the channel for the second chain.
       Returns the "transfer" port by default.

       Implemented for [`PortsOverride`].
    */
    fn channel_port_b(&self) -> PortId {
        PortId::transfer()
    }

    /**
       Return the channel ordering used for creating channels as [`Ordering`].
       Defaults to [`Ordering::Unordered`].

       Implemented for [`ChannelOrderOverride`].
    */
    fn channel_order(&self) -> Ordering {
        Ordering::Unordered
    }

    /**
       Return the channel version used for creating channels as [`Version`].
       Defaults to [`Version::ics20()`].

       Implemented for [`ChannelVersionOverride`].
    */
    fn channel_version(&self) -> Version {
        Version::ics20()
    }
}

impl<Test: TestOverrides> HasOverrides for Test {
    type Overrides = Self;

    fn get_overrides(&self) -> &Self {
        self
    }
}

impl<Test: TestOverrides> TestConfigOverride for Test {
    fn modify_test_config(&self, config: &mut TestConfig) {
        TestOverrides::modify_test_config(self, config)
    }
}

impl<Test: TestOverrides> NodeConfigOverride for Test {
    fn modify_node_config(&self, config: &mut toml::Value) -> Result<(), Error> {
        TestOverrides::modify_node_config(self, config)
    }
}

impl<Test: TestOverrides> NodeGenesisOverride for Test {
    fn modify_genesis_file(&self, genesis: &mut serde_json::Value) -> Result<(), Error> {
        TestOverrides::modify_genesis_file(self, genesis)
    }
}

impl<Test: TestOverrides> RelayerConfigOverride for Test {
    fn modify_relayer_config(&self, config: &mut Config) {
        TestOverrides::modify_relayer_config(self, config)
    }
}

impl<Test: TestOverrides> ClientOptionsOverride for Test {
    fn client_options_a_to_b(&self) -> ClientOptions {
        TestOverrides::client_options_a_to_b(self)
    }

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

impl<Test: TestOverrides> SupervisorOverride for Test {
    fn should_spawn_supervisor(&self) -> bool {
        TestOverrides::should_spawn_supervisor(self)
    }
}

impl<Test: TestOverrides> ConnectionDelayOverride for Test {
    fn connection_delay(&self) -> Duration {
        TestOverrides::connection_delay(self)
    }
}

impl<Test: TestOverrides> PortsOverride for Test {
    fn channel_port_a(&self) -> PortId {
        TestOverrides::channel_port_a(self)
    }

    fn channel_port_b(&self) -> PortId {
        TestOverrides::channel_port_b(self)
    }
}

impl<Test: TestOverrides> ChannelOrderOverride for Test {
    fn channel_order(&self) -> Ordering {
        TestOverrides::channel_order(self)
    }
}

impl<Test: TestOverrides> ChannelVersionOverride for Test {
    fn channel_version(&self) -> Version {
        TestOverrides::channel_version(self)
    }
}

impl<Test: TestOverrides> NaryPortsOverride<2> for Test {
    fn channel_ports(&self) -> [[PortId; 2]; 2] {
        let port_a = self.channel_port_a();
        let port_b = self.channel_port_b();

        [[port_a.clone(), port_b.clone()], [port_b, port_a]]
    }
}