ibc_testkit/hosts/
mock.rs

1use alloc::vec::Vec;
2
3use bon::Builder;
4use ibc::core::client::types::Height;
5use ibc::core::host::types::identifiers::ChainId;
6use ibc::core::primitives::Timestamp;
7
8use super::{TestBlock, TestHeader, TestHost};
9use crate::testapp::ibc::clients::mock::client_state::MockClientState;
10use crate::testapp::ibc::clients::mock::consensus_state::MockConsensusState;
11use crate::testapp::ibc::clients::mock::header::MockHeader;
12
13#[derive(Debug, Builder)]
14pub struct MockHost {
15    /// Unique identifier for the chain.
16    #[builder(default = ChainId::new("mock-0").expect("Never fails"))]
17    pub chain_id: ChainId,
18    /// The chain of blocks underlying this context.
19    #[builder(default)]
20    pub history: Vec<MockHeader>,
21}
22
23impl Default for MockHost {
24    fn default() -> Self {
25        Self::builder().build()
26    }
27}
28
29impl TestHost for MockHost {
30    type Block = MockHeader;
31    type ClientState = MockClientState;
32    type BlockParams = ();
33    type LightClientParams = ();
34
35    fn history(&self) -> &Vec<Self::Block> {
36        &self.history
37    }
38
39    fn push_block(&mut self, block: Self::Block) {
40        self.history.push(block);
41    }
42
43    fn generate_block(
44        &self,
45        _commitment_root: Vec<u8>,
46        height: u64,
47        timestamp: Timestamp,
48        _params: &Self::BlockParams,
49    ) -> Self::Block {
50        MockHeader {
51            height: Height::new(self.chain_id.revision_number(), height).expect("Never fails"),
52            timestamp,
53        }
54    }
55
56    fn generate_client_state(
57        &self,
58        latest_height: &Height,
59        _params: &Self::LightClientParams,
60    ) -> Self::ClientState {
61        MockClientState::new(self.get_block(latest_height).expect("height exists"))
62    }
63}
64
65impl TestBlock for MockHeader {
66    type Header = Self;
67
68    fn height(&self) -> Height {
69        self.height
70    }
71
72    fn timestamp(&self) -> Timestamp {
73        self.timestamp
74    }
75
76    fn into_header_with_trusted(self, _trusted_block: &Self) -> Self::Header {
77        self
78    }
79}
80
81impl From<MockHeader> for MockConsensusState {
82    fn from(block: MockHeader) -> Self {
83        Self::new(block)
84    }
85}
86
87impl TestHeader for MockHeader {
88    type ConsensusState = MockConsensusState;
89
90    fn height(&self) -> Height {
91        self.height
92    }
93
94    fn timestamp(&self) -> Timestamp {
95        self.timestamp
96    }
97}