Skip to content

Commit

Permalink
imp(ibc-testkit): deprecate MockContext multiple init methods (#1047)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
rnbguy and Farhad-Shabani authored Jan 19, 2024
1 parent ccef193 commit ee92645
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 190 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ibc-testkit] Deprecate `MockContext::new*` in favor of `MockContextConfig`.
([\#1042](https://github.com/cosmos/ibc-rs/issues/1042))
2 changes: 2 additions & 0 deletions ibc-testkit/src/fixtures/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct MockContextConfig {
#[builder(default = HostType::Mock)]
host_type: HostType,

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

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

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

#[builder(default = Timestamp::now())]
Expand Down
47 changes: 24 additions & 23 deletions ibc-testkit/src/relayer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ mod tests {
use tracing::debug;

use super::RelayerContext;
use crate::fixtures::core::context::MockContextConfig;
use crate::hosts::block::{HostBlock, HostType};
use crate::relayer::context::ClientId;
use crate::relayer::error::RelayerError;
use crate::testapp::ibc::clients::mock::client_state::client_type as mock_client_type;
use crate::testapp::ibc::core::router::MockRouter;
use crate::testapp::ibc::core::types::MockContext;

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

// Create two mock contexts, one for each chain.
let mut ctx_a =
MockContext::new(chain_id_a.clone(), HostType::Mock, 5, chain_a_start_height)
.with_client_parametrized_with_chain_id(
chain_id_b.clone(),
&client_on_a_for_b,
client_on_a_for_b_height,
Some(tm_client_type()), // The target host chain (B) is synthetic TM.
Some(client_on_a_for_b_height),
);
let mut ctx_a = MockContextConfig::builder()
.host_id(chain_id_a.clone())
.latest_height(chain_a_start_height)
.build()
.with_client_parametrized_with_chain_id(
chain_id_b.clone(),
&client_on_a_for_b,
client_on_a_for_b_height,
Some(tm_client_type()), // The target host chain (B) is synthetic TM.
Some(client_on_a_for_b_height),
);
// dummy; not actually used in client updates
let mut router_a = MockRouter::new_with_transfer();

let mut ctx_b = MockContext::new(
chain_id_b,
HostType::SyntheticTendermint,
5,
chain_b_start_height,
)
.with_client_parametrized_with_chain_id(
chain_id_a,
&client_on_b_for_a,
client_on_b_for_a_height,
Some(mock_client_type()), // The target host chain is mock.
Some(client_on_b_for_a_height),
);
let mut ctx_b = MockContextConfig::builder()
.host_id(chain_id_b)
.host_type(HostType::SyntheticTendermint)
.latest_height(chain_b_start_height)
.build()
.with_client_parametrized_with_chain_id(
chain_id_a,
&client_on_b_for_a,
client_on_b_for_a_height,
Some(mock_client_type()), // The target host chain is mock.
Some(client_on_b_for_a_height),
);
// dummy; not actually used in client updates
let mut router_b = MockRouter::new_with_transfer();

Expand Down
131 changes: 65 additions & 66 deletions ibc-testkit/src/testapp/ibc/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use super::client_ctx::{MockClientRecord, PortChannelIdMap};
use crate::fixtures::clients::tendermint::{
dummy_tm_client_state_from_header, ClientStateConfig as TmClientStateConfig,
};
use crate::fixtures::core::context::MockContextConfig;
use crate::hosts::block::{HostBlock, HostType};
use crate::relayer::error::RelayerError;
use crate::testapp::ibc::clients::mock::client_state::{
Expand Down Expand Up @@ -147,12 +148,7 @@ pub struct MockClientConfig {
/// creation of new domain objects.
impl Default for MockContext {
fn default() -> Self {
Self::new(
ChainId::new("mockgaia-0").expect("Never fails"),
HostType::Mock,
5,
Height::new(0, 5).expect("Never fails"),
)
MockContextConfig::builder().build()
}
}

Expand Down Expand Up @@ -183,6 +179,10 @@ impl MockContext {
/// the chain maintain in its history, which also determines the pruning window. Parameter
/// `latest_height` determines the current height of the chain. This context
/// has support to emulate two type of underlying chains: Mock or SyntheticTendermint.
#[deprecated(
since = "0.49.2",
note = "Please use `MockContextConfig::builder().build()` instead"
)]
pub fn new(
host_id: ChainId,
host_type: HostType,
Expand Down Expand Up @@ -239,6 +239,10 @@ impl MockContext {
/// Note: the validator history is used accordingly for current validator set and next validator set.
/// `validator_history[i]` and `validator_history[i+1]` is i'th block's current and next validator set.
/// The number of blocks will be `validator_history.len() - 1` due to the above.
#[deprecated(
since = "0.49.2",
note = "Please use `MockContextConfig::builder().build()` instead"
)]
pub fn new_with_validator_history(
host_id: ChainId,
host_type: HostType,
Expand Down Expand Up @@ -816,93 +820,88 @@ mod tests {
let tests: Vec<Test> = vec![
Test {
name: "Empty history, small pruning window".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::Mock,
2,
Height::new(cv, 1).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.max_history_size(2)
.latest_height(Height::new(cv, 1).expect("Never fails"))
.build(),
},
Test {
name: "[Synthetic TM host] Empty history, small pruning window".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::SyntheticTendermint,
2,
Height::new(cv, 1).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.host_type(HostType::SyntheticTendermint)
.max_history_size(2)
.latest_height(Height::new(cv, 1).expect("Never fails"))
.build(),
},
Test {
name: "Large pruning window".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::Mock,
30,
Height::new(cv, 2).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.max_history_size(30)
.latest_height(Height::new(cv, 2).expect("Never fails"))
.build(),
},
Test {
name: "[Synthetic TM host] Large pruning window".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::SyntheticTendermint,
30,
Height::new(cv, 2).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.host_type(HostType::SyntheticTendermint)
.max_history_size(30)
.latest_height(Height::new(cv, 2).expect("Never fails"))
.build(),
},
Test {
name: "Small pruning window".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::Mock,
3,
Height::new(cv, 30).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.max_history_size(3)
.latest_height(Height::new(cv, 30).expect("Never fails"))
.build(),
},
Test {
name: "[Synthetic TM host] Small pruning window".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::SyntheticTendermint,
3,
Height::new(cv, 30).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.host_type(HostType::SyntheticTendermint)
.max_history_size(3)
.latest_height(Height::new(cv, 30).expect("Never fails"))
.build(),
},
Test {
name: "Small pruning window, small starting height".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::Mock,
3,
Height::new(cv, 2).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.max_history_size(3)
.latest_height(Height::new(cv, 2).expect("Never fails"))
.build(),
},
Test {
name: "[Synthetic TM host] Small pruning window, small starting height".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::SyntheticTendermint,
3,
Height::new(cv, 2).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.host_type(HostType::SyntheticTendermint)
.max_history_size(3)
.latest_height(Height::new(cv, 2).expect("Never fails"))
.build(),
},
Test {
name: "Large pruning window, large starting height".to_string(),
ctx: MockContext::new(
mock_chain_id.clone(),
HostType::Mock,
50,
Height::new(cv, 2000).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.max_history_size(50)
.latest_height(Height::new(cv, 2000).expect("Never fails"))
.build(),
},
Test {
name: "[Synthetic TM host] Large pruning window, large starting height".to_string(),
ctx: MockContext::new(
mock_chain_id,
HostType::SyntheticTendermint,
50,
Height::new(cv, 2000).expect("Never fails"),
),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id)
.host_type(HostType::SyntheticTendermint)
.max_history_size(50)
.latest_height(Height::new(cv, 2000).expect("Never fails"))
.build(),
},
];

Expand Down
Loading

0 comments on commit ee92645

Please sign in to comment.