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
use tendermint::time::Time;
use tendermint_testgen::light_block::TmLightBlock;
use tendermint_testgen::{Generator, LightBlock as TestgenLightBlock};
use time::OffsetDateTime;
use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TMConsensusState;
use crate::clients::ics07_tendermint::header::Header as TMHeader;
use crate::core::ics02_client::client_consensus::AnyConsensusState;
use crate::core::ics02_client::header::AnyHeader;
use crate::core::ics24_host::identifier::ChainId;
use crate::mock::header::MockHeader;
use crate::prelude::*;
use crate::timestamp::Timestamp;
use crate::Height;
#[derive(Clone, Debug, Copy)]
pub enum HostType {
Mock,
SyntheticTendermint,
}
#[derive(Clone, Debug)]
pub enum HostBlock {
Mock(MockHeader),
SyntheticTendermint(Box<TmLightBlock>),
}
impl HostBlock {
pub fn height(&self) -> Height {
match self {
HostBlock::Mock(header) => header.height(),
HostBlock::SyntheticTendermint(light_block) => Height::new(
ChainId::chain_version(light_block.signed_header.header.chain_id.as_str()),
light_block.signed_header.header.height.value(),
),
}
}
pub fn generate_block(chain_id: ChainId, chain_type: HostType, height: u64) -> HostBlock {
match chain_type {
HostType::Mock => HostBlock::Mock(MockHeader {
height: Height::new(chain_id.version(), height),
timestamp: Timestamp::now(),
}),
HostType::SyntheticTendermint => {
HostBlock::SyntheticTendermint(Box::new(Self::generate_tm_block(chain_id, height)))
}
}
}
pub fn generate_tm_block(chain_id: ChainId, height: u64) -> TmLightBlock {
let time: Time = OffsetDateTime::now_utc().try_into().unwrap();
TestgenLightBlock::new_default_with_time_and_chain_id(chain_id.to_string(), time, height)
.generate()
.unwrap()
}
}
impl From<TmLightBlock> for AnyConsensusState {
fn from(light_block: TmLightBlock) -> Self {
let cs = TMConsensusState::from(light_block.signed_header.header);
AnyConsensusState::Tendermint(cs)
}
}
impl From<HostBlock> for AnyConsensusState {
fn from(any_block: HostBlock) -> Self {
match any_block {
HostBlock::Mock(mock_header) => mock_header.into(),
HostBlock::SyntheticTendermint(light_block) => (*light_block).into(),
}
}
}
impl From<HostBlock> for AnyHeader {
fn from(any_block: HostBlock) -> Self {
match any_block {
HostBlock::Mock(mock_header) => mock_header.into(),
HostBlock::SyntheticTendermint(light_block_box) => {
AnyHeader::Tendermint((*light_block_box).into())
}
}
}
}
impl From<TmLightBlock> for TMHeader {
fn from(light_block: TmLightBlock) -> Self {
TMHeader {
signed_header: light_block.signed_header,
validator_set: light_block.validators,
trusted_height: Default::default(),
trusted_validator_set: light_block.next_validators,
}
}
}