ibc_testkit/hosts/
mock.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use alloc::vec::Vec;

use bon::Builder;
use ibc::core::client::types::Height;
use ibc::core::host::types::identifiers::ChainId;
use ibc::core::primitives::Timestamp;

use super::{TestBlock, TestHeader, TestHost};
use crate::testapp::ibc::clients::mock::client_state::MockClientState;
use crate::testapp::ibc::clients::mock::consensus_state::MockConsensusState;
use crate::testapp::ibc::clients::mock::header::MockHeader;

#[derive(Debug, Builder)]
pub struct MockHost {
    /// Unique identifier for the chain.
    #[builder(default = ChainId::new("mock-0").expect("Never fails"))]
    pub chain_id: ChainId,
    /// The chain of blocks underlying this context.
    #[builder(default)]
    pub history: Vec<MockHeader>,
}

impl Default for MockHost {
    fn default() -> Self {
        Self::builder().build()
    }
}

impl TestHost for MockHost {
    type Block = MockHeader;
    type ClientState = MockClientState;
    type BlockParams = ();
    type LightClientParams = ();

    fn history(&self) -> &Vec<Self::Block> {
        &self.history
    }

    fn push_block(&mut self, block: Self::Block) {
        self.history.push(block);
    }

    fn generate_block(
        &self,
        _commitment_root: Vec<u8>,
        height: u64,
        timestamp: Timestamp,
        _params: &Self::BlockParams,
    ) -> Self::Block {
        MockHeader {
            height: Height::new(self.chain_id.revision_number(), height).expect("Never fails"),
            timestamp,
        }
    }

    fn generate_client_state(
        &self,
        latest_height: &Height,
        _params: &Self::LightClientParams,
    ) -> Self::ClientState {
        MockClientState::new(self.get_block(latest_height).expect("height exists"))
    }
}

impl TestBlock for MockHeader {
    type Header = Self;

    fn height(&self) -> Height {
        self.height
    }

    fn timestamp(&self) -> Timestamp {
        self.timestamp
    }

    fn into_header_with_trusted(self, _trusted_block: &Self) -> Self::Header {
        self
    }
}

impl From<MockHeader> for MockConsensusState {
    fn from(block: MockHeader) -> Self {
        Self::new(block)
    }
}

impl TestHeader for MockHeader {
    type ConsensusState = MockConsensusState;

    fn height(&self) -> Height {
        self.height
    }

    fn timestamp(&self) -> Timestamp {
        self.timestamp
    }
}