Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Feb 29, 2024
1 parent f9be068 commit a2bf476
Show file tree
Hide file tree
Showing 21 changed files with 750 additions and 576 deletions.
2 changes: 1 addition & 1 deletion ibc-testkit/src/fixtures/core/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use alloc::fmt::Debug;
use core::cmp::min;
use core::ops::{Add, Sub};
use core::time::Duration;
use tendermint::Time;

use basecoin_store::context::ProvableStore;
use ibc::core::client::types::Height;
use ibc::core::host::types::identifiers::ChainId;
use ibc::core::primitives::prelude::*;
use ibc::core::primitives::Timestamp;
use tendermint::Time;
use typed_builder::TypedBuilder;

use crate::hosts::TestHost;
Expand Down
229 changes: 97 additions & 132 deletions ibc-testkit/src/testapp/ibc/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,152 +529,117 @@ mod tests {
use ibc::core::channel::types::error::{ChannelError, PacketError};
use ibc::core::channel::types::packet::Packet;
use ibc::core::channel::types::Version;
use ibc::core::host::types::identifiers::ChainId;
use ibc::core::primitives::Signer;
use ibc::core::router::module::Module;
use ibc::core::router::types::module::{ModuleExtras, ModuleId};

use super::*;
use crate::fixtures::core::channel::PacketConfig;
use crate::fixtures::core::signer::dummy_bech32_account;
use crate::hosts::mockhost::MockHost;
use crate::hosts::tenderminthost::TendermintHost;
use crate::testapp::ibc::core::router::MockRouter;

#[test]
fn test_history_manipulation() {
pub struct Test {
fn test_history_manipulation_mock() {
pub struct Test<H: TestHost> {
name: String,
ctx: MockContext,
ctx: MockContext<H>,
}
let cv = 1; // The version to use for all chains.

let mock_chain_id = ChainId::new(&format!("mockgaia-{cv}")).unwrap();

let tests: Vec<Test> = vec![
Test {
name: "Empty history, small pruning window".to_string(),
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: 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: 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: 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: 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: 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: 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: 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: 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: 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(),
},
];

for mut test in tests {
// All tests should yield a valid context after initialization.
assert!(
test.ctx.validate().is_ok(),
"failed in test {} while validating context {:?}",
test.name,
test.ctx
);

let current_height = test.ctx.latest_height();

// After advancing the chain's height, the context should still be valid.
test.ctx.advance_host_chain_height();
assert!(
test.ctx.validate().is_ok(),
"failed in test {} while validating context {:?}",
test.name,
test.ctx
);

let next_height = current_height.increment();
assert_eq!(
test.ctx.latest_height(),
next_height,
"failed while increasing height for context {:?}",
test.ctx
);

assert_eq!(
test.ctx
.host_block(&current_height)
.expect("Never fails")
.height(),
current_height,
"failed while fetching height {:?} of context {:?}",
current_height,
test.ctx
);
fn run_tests<H: TestHost>(sub_title: &str) {
let cv = 1; // The version to use for all chains.
let mock_chain_id = ChainId::new(&format!("mockgaia-{cv}")).unwrap();

let tests: Vec<Test<H>> = vec![
Test {
name: "Empty history, small pruning window".to_string(),
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: "Large pruning window".to_string(),
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: "Small pruning window".to_string(),
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: "Small pruning window, small starting height".to_string(),
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: "Large pruning window, large starting height".to_string(),
ctx: MockContextConfig::builder()
.host_id(mock_chain_id.clone())
.max_history_size(50)
.latest_height(Height::new(cv, 2000).expect("Never fails"))
.build(),
},
];

for mut test in tests {
// All tests should yield a valid context after initialization.
assert!(
test.ctx.validate().is_ok(),
"failed in test [{}] {} while validating context {:?}",
sub_title,
test.name,
test.ctx
);

let current_height = test.ctx.latest_height();

// After advancing the chain's height, the context should still be valid.
test.ctx.advance_host_chain_height();
assert!(
test.ctx.validate().is_ok(),
"failed in test [{}] {} while validating context {:?}",
sub_title,
test.name,
test.ctx
);

let next_height = current_height.increment();
assert_eq!(
test.ctx.latest_height(),
next_height,
"failed while increasing height for context {:?}",
test.ctx
);

assert_eq!(
test.ctx
.host_block(&current_height)
.expect("Never fails")
.height(),
current_height,
"failed while fetching height {:?} of context {:?}",
current_height,
test.ctx
);
}
}

run_tests::<MockHost>("Mock Host");
run_tests::<TendermintHost>("Synthetic TM Host");
}

#[test]
Expand Down
7 changes: 4 additions & 3 deletions ibc-testkit/tests/core/ics02_client/create_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ibc_testkit::fixtures::clients::tendermint::{
dummy_tendermint_header, dummy_tm_client_state_from_header,
};
use ibc_testkit::fixtures::core::signer::dummy_account_id;
use ibc_testkit::hosts::mockhost::MockHost;
use ibc_testkit::testapp::ibc::clients::mock::client_state::{
client_type as mock_client_type, MockClientState,
};
Expand All @@ -24,7 +25,7 @@ use test_log::test;

#[test]
fn test_create_client_ok() {
let mut ctx = MockContext::default();
let mut ctx = MockContext::<MockHost>::default();
let mut router = MockRouter::new_with_transfer();
let signer = dummy_account_id();
let height = Height::new(0, 42).unwrap();
Expand Down Expand Up @@ -57,7 +58,7 @@ fn test_create_client_ok() {
fn test_tm_create_client_ok() {
let signer = dummy_account_id();

let mut ctx = MockContext::default();
let mut ctx = MockContext::<MockHost>::default();

let mut router = MockRouter::new_with_transfer();

Expand Down Expand Up @@ -93,7 +94,7 @@ fn test_tm_create_client_ok() {
fn test_invalid_frozen_tm_client_creation() {
let signer = dummy_account_id();

let ctx = MockContext::default();
let ctx = MockContext::<MockHost>::default();

let router = MockRouter::new_with_transfer();

Expand Down
Loading

0 comments on commit a2bf476

Please sign in to comment.