-
Notifications
You must be signed in to change notification settings - Fork 82
/
block.rs
260 lines (232 loc) · 8.92 KB
/
block.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Host chain types and methods, used by context mock.
use core::str::FromStr;
use ibc::clients::tendermint::consensus_state::ConsensusState as TmConsensusState;
use ibc::clients::tendermint::types::proto::v1::Header as RawHeader;
use ibc::clients::tendermint::types::{Header, TENDERMINT_HEADER_TYPE_URL};
use ibc::core::client::types::error::ClientError;
use ibc::core::client::types::Height;
use ibc::core::host::types::identifiers::ChainId;
use ibc::core::primitives::prelude::*;
use ibc::core::primitives::Timestamp;
use ibc::primitives::proto::{Any, Protobuf};
use ibc::primitives::ToVec;
use tendermint::block::Header as TmHeader;
use tendermint::validator::Set as ValidatorSet;
use tendermint_testgen::light_block::TmLightBlock;
use tendermint_testgen::{
Generator, Header as TestgenHeader, LightBlock as TestgenLightBlock,
Validator as TestgenValidator,
};
use crate::testapp::ibc::clients::mock::consensus_state::MockConsensusState;
use crate::testapp::ibc::clients::mock::header::MockHeader;
use crate::testapp::ibc::clients::AnyConsensusState;
/// 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 trusted_next_validators: ValidatorSet,
pub light_block: TmLightBlock,
}
impl SyntheticTmBlock {
pub fn header(&self) -> &TmHeader {
&self.light_block.signed_header.header
}
}
impl From<SyntheticTmBlock> for Header {
fn from(light_block: SyntheticTmBlock) -> Self {
let SyntheticTmBlock {
trusted_height,
trusted_next_validators,
light_block,
} = light_block;
Self {
signed_header: light_block.signed_header,
validator_set: light_block.validators,
trusted_height,
trusted_next_validator_set: trusted_next_validators,
}
}
}
/// 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::from_str(light_block.header().chain_id.as_str())
.expect("Never fails")
.revision_number(),
light_block.header().height.value(),
)
.expect("Never fails"),
}
}
pub fn set_trusted_height(&mut self, height: Height) {
match self {
HostBlock::Mock(_) => {}
HostBlock::SyntheticTendermint(light_block) => light_block.trusted_height = height,
}
}
pub fn set_trusted_next_validators_set(&mut self, trusted_next_validators: ValidatorSet) {
match self {
HostBlock::Mock(_) => {}
HostBlock::SyntheticTendermint(light_block) => {
light_block.trusted_next_validators = trusted_next_validators
}
}
}
/// 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.revision_number(), height).expect("Never fails"),
timestamp,
})),
HostType::SyntheticTendermint => HostBlock::SyntheticTendermint(Box::new(
Self::generate_tm_block(chain_id, height, timestamp),
)),
}
}
/// Generates a new block at `height` for the given chain identifier, chain type and validator sets.
pub fn generate_block_with_validators(
chain_id: ChainId,
chain_type: HostType,
height: u64,
timestamp: Timestamp,
validators: &[TestgenValidator],
next_validators: &[TestgenValidator],
) -> HostBlock {
match chain_type {
HostType::Mock => HostBlock::Mock(Box::new(MockHeader {
height: Height::new(chain_id.revision_number(), height).expect("Never fails"),
timestamp,
})),
HostType::SyntheticTendermint => {
let light_block = TestgenLightBlock::new_default_with_header(
TestgenHeader::new(validators)
.height(height)
.chain_id(chain_id.as_str())
.next_validators(next_validators)
.time(timestamp.into_tm_time().expect("Never fails")),
)
.validators(validators)
.next_validators(next_validators)
.generate()
.expect("Never fails");
HostBlock::SyntheticTendermint(Box::new(SyntheticTmBlock {
trusted_height: Height::new(chain_id.revision_number(), 1)
.expect("Never fails"),
trusted_next_validators: light_block.next_validators.clone(),
light_block,
}))
}
}
}
pub fn generate_tm_block(
chain_id: ChainId,
height: u64,
timestamp: Timestamp,
) -> SyntheticTmBlock {
let validators = [
TestgenValidator::new("1").voting_power(50),
TestgenValidator::new("2").voting_power(50),
];
let header = TestgenHeader::new(&validators)
.height(height)
.chain_id(chain_id.as_str())
.next_validators(&validators)
.time(timestamp.into_tm_time().expect("Never fails"));
let light_block = TestgenLightBlock::new_default_with_header(header)
.generate()
.expect("Never fails");
SyntheticTmBlock {
trusted_height: Height::new(chain_id.revision_number(), 1).expect("Never fails"),
trusted_next_validators: light_block.next_validators.clone(),
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 AnyConsensusState {
fn from(light_block: SyntheticTmBlock) -> Self {
let cs = TmConsensusState::from(light_block.header().clone());
cs.into()
}
}
impl From<HostBlock> for AnyConsensusState {
fn from(any_block: HostBlock) -> Self {
match any_block {
HostBlock::Mock(mock_header) => MockConsensusState::new(*mock_header).into(),
HostBlock::SyntheticTendermint(light_block) => {
TmConsensusState::from(light_block.header().clone()).into()
}
}
}
}
impl Protobuf<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> {
let SyntheticTmBlock {
trusted_height,
trusted_next_validators,
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(trusted_next_validators.into()),
}
.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),
},
}
}
}