-
Notifications
You must be signed in to change notification settings - Fork 92
/
host.rs
193 lines (169 loc) · 6.18 KB
/
host.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Host chain types and methods, used by context mock.
use ibc_proto::google::protobuf::Any;
use ibc_proto::ibc::lightclients::tendermint::v1::Header as RawHeader;
use ibc_proto::protobuf::Protobuf as ErasedProtobuf;
use tendermint::block::Header as TmHeader;
use tendermint_testgen::light_block::TmLightBlock;
use tendermint_testgen::{Generator, LightBlock as TestgenLightBlock};
use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState;
use crate::clients::ics07_tendermint::header::TENDERMINT_HEADER_TYPE_URL;
use crate::core::ics02_client::consensus_state::ConsensusState;
use crate::core::ics02_client::error::ClientError;
use crate::core::ics02_client::header::Header;
use crate::core::ics24_host::identifier::ChainId;
use crate::core::timestamp::Timestamp;
use crate::mock::consensus_state::MockConsensusState;
use crate::mock::header::MockHeader;
use crate::prelude::*;
use crate::Height;
/// Defines the different types of host chains that a mock context can emulate.
/// The variants are as follows:
/// - `Mock` defines that the context history consists of `MockHeader` blocks.
/// - `SyntheticTendermint`: the context has synthetically-generated Tendermint (light) blocks.
/// See also the `HostBlock` enum to get more insights into the underlying block type.
#[derive(Clone, Debug, Copy)]
pub enum HostType {
Mock,
SyntheticTendermint,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SyntheticTmBlock {
pub trusted_height: Height,
pub light_block: TmLightBlock,
}
impl SyntheticTmBlock {
pub fn header(&self) -> &TmHeader {
&self.light_block.signed_header.header
}
}
/// Depending on `HostType` (the type of host chain underlying a context mock), this enum defines
/// the type of blocks composing the history of the host chain.
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HostBlock {
Mock(Box<MockHeader>),
SyntheticTendermint(Box<SyntheticTmBlock>),
}
impl HostBlock {
/// Returns the height of a block.
pub fn height(&self) -> Height {
match self {
HostBlock::Mock(header) => header.height(),
HostBlock::SyntheticTendermint(light_block) => Height::new(
ChainId::chain_version(light_block.header().chain_id.as_str()),
light_block.header().height.value(),
)
.unwrap(),
}
}
pub fn set_trusted_height(&mut self, height: Height) {
match self {
HostBlock::Mock(_) => {}
HostBlock::SyntheticTendermint(light_block) => light_block.trusted_height = height,
}
}
/// Returns the timestamp of a block.
pub fn timestamp(&self) -> Timestamp {
match self {
HostBlock::Mock(header) => header.timestamp,
HostBlock::SyntheticTendermint(light_block) => light_block.header().time.into(),
}
}
/// Generates a new block at `height` for the given chain identifier and chain type.
pub fn generate_block(
chain_id: ChainId,
chain_type: HostType,
height: u64,
timestamp: Timestamp,
) -> HostBlock {
match chain_type {
HostType::Mock => HostBlock::Mock(Box::new(MockHeader {
height: Height::new(chain_id.version(), height).unwrap(),
timestamp,
})),
HostType::SyntheticTendermint => HostBlock::SyntheticTendermint(Box::new(
Self::generate_tm_block(chain_id, height, timestamp),
)),
}
}
pub fn generate_tm_block(
chain_id: ChainId,
height: u64,
timestamp: Timestamp,
) -> SyntheticTmBlock {
let light_block = TestgenLightBlock::new_default_with_time_and_chain_id(
chain_id.to_string(),
timestamp.into_tm_time().unwrap(),
height,
)
.generate()
.unwrap();
SyntheticTmBlock {
trusted_height: Height::new(chain_id.version(), 1).unwrap(),
light_block,
}
}
pub fn try_into_tm_block(self) -> Option<SyntheticTmBlock> {
match self {
HostBlock::Mock(_) => None,
HostBlock::SyntheticTendermint(tm_block) => Some(*tm_block),
}
}
}
impl From<SyntheticTmBlock> for Box<dyn ConsensusState> {
fn from(light_block: SyntheticTmBlock) -> Self {
let cs = TMConsensusState::from(light_block.header().clone());
cs.into_box()
}
}
impl From<HostBlock> for Box<dyn ConsensusState> {
fn from(any_block: HostBlock) -> Self {
match any_block {
HostBlock::Mock(mock_header) => MockConsensusState::new(*mock_header).into_box(),
HostBlock::SyntheticTendermint(light_block) => {
TMConsensusState::from(light_block.header().clone()).into_box()
}
}
}
}
impl ErasedProtobuf<Any> for HostBlock {}
impl TryFrom<Any> for HostBlock {
type Error = ClientError;
fn try_from(_raw: Any) -> Result<Self, Self::Error> {
todo!()
}
}
impl From<HostBlock> for Any {
fn from(value: HostBlock) -> Self {
fn encode_light_block(light_block: SyntheticTmBlock) -> Vec<u8> {
use prost::Message;
let SyntheticTmBlock {
trusted_height,
light_block,
} = light_block;
RawHeader {
signed_header: Some(light_block.signed_header.into()),
validator_set: Some(light_block.validators.into()),
trusted_height: Some(trusted_height.into()),
trusted_validators: Some(light_block.next_validators.into()),
}
.encode_to_vec()
}
match value {
HostBlock::Mock(mock_header) => (*mock_header).into(),
HostBlock::SyntheticTendermint(light_block) => Self {
type_url: TENDERMINT_HEADER_TYPE_URL.to_string(),
value: encode_light_block(*light_block),
},
}
}
}
impl Header for HostBlock {
fn height(&self) -> Height {
HostBlock::height(self)
}
fn timestamp(&self) -> Timestamp {
HostBlock::timestamp(self)
}
}