Skip to content

Commit 8289081

Browse files
imp(ibc-testkit): deprecate MockContext multiple init methods (#1047)
* deprecate default arg init methods in MockContext * refactor tests * add changelog * chore: trim redundant method calls, opting for default values --------- Co-authored-by: Farhad Shabani <farhad.shabani@gmail.com>
1 parent 7a37d48 commit 8289081

File tree

7 files changed

+186
-190
lines changed

7 files changed

+186
-190
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [ibc-testkit] Deprecate `MockContext::new*` in favor of `MockContextConfig`.
2+
([\#1042](https://github.com/cosmos/ibc-rs/issues/1042))

ibc-testkit/src/fixtures/core/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct MockContextConfig {
2121
#[builder(default = HostType::Mock)]
2222
host_type: HostType,
2323

24+
#[builder(default = ChainId::new("mockgaia-0").expect("Never fails"))]
2425
host_id: ChainId,
2526

2627
#[builder(default = Duration::from_secs(DEFAULT_BLOCK_TIME_SECS))]
@@ -33,6 +34,7 @@ pub struct MockContextConfig {
3334
#[builder(default, setter(strip_option))]
3435
validator_set_history: Option<Vec<Vec<TestgenValidator>>>,
3536

37+
#[builder(default = Height::new(0, 5).expect("Never fails"))]
3638
latest_height: Height,
3739

3840
#[builder(default = Timestamp::now())]

ibc-testkit/src/relayer/context.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ mod tests {
5252
use tracing::debug;
5353

5454
use super::RelayerContext;
55+
use crate::fixtures::core::context::MockContextConfig;
5556
use crate::hosts::block::{HostBlock, HostType};
5657
use crate::relayer::context::ClientId;
5758
use crate::relayer::error::RelayerError;
5859
use crate::testapp::ibc::clients::mock::client_state::client_type as mock_client_type;
5960
use crate::testapp::ibc::core::router::MockRouter;
60-
use crate::testapp::ibc::core::types::MockContext;
6161

6262
/// Builds a `ClientMsg::UpdateClient` for a client with id `client_id` running on the `dest`
6363
/// context, assuming that the latest header on the source context is `src_header`.
@@ -121,31 +121,32 @@ mod tests {
121121
let chain_id_b = ChainId::new("mockgaiaB-1").unwrap();
122122

123123
// Create two mock contexts, one for each chain.
124-
let mut ctx_a =
125-
MockContext::new(chain_id_a.clone(), HostType::Mock, 5, chain_a_start_height)
126-
.with_client_parametrized_with_chain_id(
127-
chain_id_b.clone(),
128-
&client_on_a_for_b,
129-
client_on_a_for_b_height,
130-
Some(tm_client_type()), // The target host chain (B) is synthetic TM.
131-
Some(client_on_a_for_b_height),
132-
);
124+
let mut ctx_a = MockContextConfig::builder()
125+
.host_id(chain_id_a.clone())
126+
.latest_height(chain_a_start_height)
127+
.build()
128+
.with_client_parametrized_with_chain_id(
129+
chain_id_b.clone(),
130+
&client_on_a_for_b,
131+
client_on_a_for_b_height,
132+
Some(tm_client_type()), // The target host chain (B) is synthetic TM.
133+
Some(client_on_a_for_b_height),
134+
);
133135
// dummy; not actually used in client updates
134136
let mut router_a = MockRouter::new_with_transfer();
135137

136-
let mut ctx_b = MockContext::new(
137-
chain_id_b,
138-
HostType::SyntheticTendermint,
139-
5,
140-
chain_b_start_height,
141-
)
142-
.with_client_parametrized_with_chain_id(
143-
chain_id_a,
144-
&client_on_b_for_a,
145-
client_on_b_for_a_height,
146-
Some(mock_client_type()), // The target host chain is mock.
147-
Some(client_on_b_for_a_height),
148-
);
138+
let mut ctx_b = MockContextConfig::builder()
139+
.host_id(chain_id_b)
140+
.host_type(HostType::SyntheticTendermint)
141+
.latest_height(chain_b_start_height)
142+
.build()
143+
.with_client_parametrized_with_chain_id(
144+
chain_id_a,
145+
&client_on_b_for_a,
146+
client_on_b_for_a_height,
147+
Some(mock_client_type()), // The target host chain is mock.
148+
Some(client_on_b_for_a_height),
149+
);
149150
// dummy; not actually used in client updates
150151
let mut router_b = MockRouter::new_with_transfer();
151152

ibc-testkit/src/testapp/ibc/core/types.rs

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use super::client_ctx::{MockClientRecord, PortChannelIdMap};
3333
use crate::fixtures::clients::tendermint::{
3434
dummy_tm_client_state_from_header, ClientStateConfig as TmClientStateConfig,
3535
};
36+
use crate::fixtures::core::context::MockContextConfig;
3637
use crate::hosts::block::{HostBlock, HostType};
3738
use crate::relayer::error::RelayerError;
3839
use crate::testapp::ibc::clients::mock::client_state::{
@@ -147,12 +148,7 @@ pub struct MockClientConfig {
147148
/// creation of new domain objects.
148149
impl Default for MockContext {
149150
fn default() -> Self {
150-
Self::new(
151-
ChainId::new("mockgaia-0").expect("Never fails"),
152-
HostType::Mock,
153-
5,
154-
Height::new(0, 5).expect("Never fails"),
155-
)
151+
MockContextConfig::builder().build()
156152
}
157153
}
158154

@@ -183,6 +179,10 @@ impl MockContext {
183179
/// the chain maintain in its history, which also determines the pruning window. Parameter
184180
/// `latest_height` determines the current height of the chain. This context
185181
/// has support to emulate two type of underlying chains: Mock or SyntheticTendermint.
182+
#[deprecated(
183+
since = "0.49.2",
184+
note = "Please use `MockContextConfig::builder().build()` instead"
185+
)]
186186
pub fn new(
187187
host_id: ChainId,
188188
host_type: HostType,
@@ -239,6 +239,10 @@ impl MockContext {
239239
/// Note: the validator history is used accordingly for current validator set and next validator set.
240240
/// `validator_history[i]` and `validator_history[i+1]` is i'th block's current and next validator set.
241241
/// The number of blocks will be `validator_history.len() - 1` due to the above.
242+
#[deprecated(
243+
since = "0.49.2",
244+
note = "Please use `MockContextConfig::builder().build()` instead"
245+
)]
242246
pub fn new_with_validator_history(
243247
host_id: ChainId,
244248
host_type: HostType,
@@ -816,93 +820,88 @@ mod tests {
816820
let tests: Vec<Test> = vec![
817821
Test {
818822
name: "Empty history, small pruning window".to_string(),
819-
ctx: MockContext::new(
820-
mock_chain_id.clone(),
821-
HostType::Mock,
822-
2,
823-
Height::new(cv, 1).expect("Never fails"),
824-
),
823+
ctx: MockContextConfig::builder()
824+
.host_id(mock_chain_id.clone())
825+
.max_history_size(2)
826+
.latest_height(Height::new(cv, 1).expect("Never fails"))
827+
.build(),
825828
},
826829
Test {
827830
name: "[Synthetic TM host] Empty history, small pruning window".to_string(),
828-
ctx: MockContext::new(
829-
mock_chain_id.clone(),
830-
HostType::SyntheticTendermint,
831-
2,
832-
Height::new(cv, 1).expect("Never fails"),
833-
),
831+
ctx: MockContextConfig::builder()
832+
.host_id(mock_chain_id.clone())
833+
.host_type(HostType::SyntheticTendermint)
834+
.max_history_size(2)
835+
.latest_height(Height::new(cv, 1).expect("Never fails"))
836+
.build(),
834837
},
835838
Test {
836839
name: "Large pruning window".to_string(),
837-
ctx: MockContext::new(
838-
mock_chain_id.clone(),
839-
HostType::Mock,
840-
30,
841-
Height::new(cv, 2).expect("Never fails"),
842-
),
840+
ctx: MockContextConfig::builder()
841+
.host_id(mock_chain_id.clone())
842+
.max_history_size(30)
843+
.latest_height(Height::new(cv, 2).expect("Never fails"))
844+
.build(),
843845
},
844846
Test {
845847
name: "[Synthetic TM host] Large pruning window".to_string(),
846-
ctx: MockContext::new(
847-
mock_chain_id.clone(),
848-
HostType::SyntheticTendermint,
849-
30,
850-
Height::new(cv, 2).expect("Never fails"),
851-
),
848+
ctx: MockContextConfig::builder()
849+
.host_id(mock_chain_id.clone())
850+
.host_type(HostType::SyntheticTendermint)
851+
.max_history_size(30)
852+
.latest_height(Height::new(cv, 2).expect("Never fails"))
853+
.build(),
852854
},
853855
Test {
854856
name: "Small pruning window".to_string(),
855-
ctx: MockContext::new(
856-
mock_chain_id.clone(),
857-
HostType::Mock,
858-
3,
859-
Height::new(cv, 30).expect("Never fails"),
860-
),
857+
ctx: MockContextConfig::builder()
858+
.host_id(mock_chain_id.clone())
859+
.max_history_size(3)
860+
.latest_height(Height::new(cv, 30).expect("Never fails"))
861+
.build(),
861862
},
862863
Test {
863864
name: "[Synthetic TM host] Small pruning window".to_string(),
864-
ctx: MockContext::new(
865-
mock_chain_id.clone(),
866-
HostType::SyntheticTendermint,
867-
3,
868-
Height::new(cv, 30).expect("Never fails"),
869-
),
865+
ctx: MockContextConfig::builder()
866+
.host_id(mock_chain_id.clone())
867+
.host_type(HostType::SyntheticTendermint)
868+
.max_history_size(3)
869+
.latest_height(Height::new(cv, 30).expect("Never fails"))
870+
.build(),
870871
},
871872
Test {
872873
name: "Small pruning window, small starting height".to_string(),
873-
ctx: MockContext::new(
874-
mock_chain_id.clone(),
875-
HostType::Mock,
876-
3,
877-
Height::new(cv, 2).expect("Never fails"),
878-
),
874+
ctx: MockContextConfig::builder()
875+
.host_id(mock_chain_id.clone())
876+
.max_history_size(3)
877+
.latest_height(Height::new(cv, 2).expect("Never fails"))
878+
.build(),
879879
},
880880
Test {
881881
name: "[Synthetic TM host] Small pruning window, small starting height".to_string(),
882-
ctx: MockContext::new(
883-
mock_chain_id.clone(),
884-
HostType::SyntheticTendermint,
885-
3,
886-
Height::new(cv, 2).expect("Never fails"),
887-
),
882+
ctx: MockContextConfig::builder()
883+
.host_id(mock_chain_id.clone())
884+
.host_type(HostType::SyntheticTendermint)
885+
.max_history_size(3)
886+
.latest_height(Height::new(cv, 2).expect("Never fails"))
887+
.build(),
888888
},
889889
Test {
890890
name: "Large pruning window, large starting height".to_string(),
891-
ctx: MockContext::new(
892-
mock_chain_id.clone(),
893-
HostType::Mock,
894-
50,
895-
Height::new(cv, 2000).expect("Never fails"),
896-
),
891+
ctx: MockContextConfig::builder()
892+
.host_id(mock_chain_id.clone())
893+
.max_history_size(50)
894+
.latest_height(Height::new(cv, 2000).expect("Never fails"))
895+
.build(),
897896
},
898897
Test {
899898
name: "[Synthetic TM host] Large pruning window, large starting height".to_string(),
900-
ctx: MockContext::new(
901-
mock_chain_id,
902-
HostType::SyntheticTendermint,
903-
50,
904-
Height::new(cv, 2000).expect("Never fails"),
905-
),
899+
ctx: MockContextConfig::builder()
900+
.host_id(mock_chain_id)
901+
.host_type(HostType::SyntheticTendermint)
902+
.max_history_size(50)
903+
.latest_height(Height::new(cv, 2000).expect("Never fails"))
904+
.build(),
906905
},
907906
];
908907

0 commit comments

Comments
 (0)