ibc_testkit/fixtures/core/
context.rs

1use alloc::fmt::Debug;
2use core::time::Duration;
3
4use basecoin_store::context::ProvableStore;
5use bon::builder;
6use ibc::core::client::context::client_state::ClientStateValidation;
7use ibc::core::client::types::Height;
8use ibc::core::primitives::prelude::*;
9use ibc::core::primitives::Timestamp;
10
11use crate::context::StoreGenericTestContext;
12use crate::hosts::{HostClientState, TestBlock, TestHost};
13use crate::testapp::ibc::core::router::MockRouter;
14use crate::testapp::ibc::core::types::{MockIbcStore, DEFAULT_BLOCK_TIME_SECS};
15use crate::utils::year_2023;
16
17/// Returns a dummy [`StoreGenericTestContext`], for testing purposes only!
18#[builder]
19pub fn dummy_store_generic_test_context<S, H>(
20    #[builder(default)] host: H,
21    #[builder(default = Duration::from_secs(DEFAULT_BLOCK_TIME_SECS))] block_time: Duration,
22    #[builder(default = year_2023())] latest_timestamp: Timestamp,
23    #[builder(default)] block_params_history: Vec<H::BlockParams>,
24    #[builder(default = Height::new(0, 5).expect("Never fails"))] latest_height: Height,
25) -> StoreGenericTestContext<S, H>
26where
27    S: ProvableStore + Debug + Default,
28    H: TestHost,
29    HostClientState<H>: ClientStateValidation<MockIbcStore<S>>,
30{
31    assert_ne!(
32        latest_height.revision_height(),
33        0,
34        "The chain must have a non-zero revision_height"
35    );
36
37    // timestamp at height 1
38    let genesis_timestamp = (latest_timestamp
39        - (block_time * u32::try_from(latest_height.revision_height() - 1).expect("no overflow")))
40    .expect("no underflow");
41
42    let mut context = StoreGenericTestContext {
43        multi_store: Default::default(),
44        host,
45        ibc_store: MockIbcStore::new(latest_height.revision_number(), Default::default()),
46        ibc_router: MockRouter::new_with_transfer(),
47    };
48
49    // store is at height 0; no block
50
51    context.advance_genesis_height(genesis_timestamp, &Default::default());
52
53    // store is at height 1; one block
54
55    context = context.advance_block_up_to_height(
56        latest_height
57            .sub(block_params_history.len() as u64)
58            .expect("no error"),
59    );
60
61    for block_params in block_params_history {
62        context.advance_block_height_with_params(block_time, &block_params);
63    }
64
65    assert_eq!(
66        context.host.latest_block().height(),
67        latest_height,
68        "The latest height in the host must match the latest height in the context"
69    );
70
71    assert_eq!(
72        context.host.latest_block().timestamp(),
73        latest_timestamp,
74        "The latest timestamp in the host must match the latest timestamp in the context"
75    );
76
77    context
78}