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
use core::time::Duration;
use alloc::format;
use alloc::string::{String, ToString};
use ibc_proto::google::protobuf::Any;
use crate::clients::ics07_tendermint::client_state::ClientState as TmClientState;
use crate::core::ics02_client::client_state::ClientState;
use crate::core::ics03_connection::error::ConnectionError;
use crate::core::ics23_commitment::specs::ProofSpecs;
use crate::core::ics24_host::identifier::ChainId;
use crate::Height;
use tendermint::trust_threshold::TrustThresholdFraction as TendermintTrustThresholdFraction;
pub trait ValidateSelfClientContext {
fn validate_self_tendermint_client(
&self,
client_state_of_host_on_counterparty: Any,
) -> Result<(), ConnectionError> {
let tm_client_state = TmClientState::try_from(client_state_of_host_on_counterparty)
.map_err(|_| ConnectionError::InvalidClientState {
reason: "client must be a tendermint client".to_string(),
})?;
if tm_client_state.is_frozen() {
return Err(ConnectionError::InvalidClientState {
reason: "client is frozen".to_string(),
});
}
let self_chain_id = self.chain_id();
if self_chain_id != &tm_client_state.chain_id {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"invalid chain-id. expected: {}, got: {}",
self_chain_id, tm_client_state.chain_id
),
});
}
let self_revision_number = self_chain_id.version();
if self_revision_number != tm_client_state.latest_height().revision_number() {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client is not in the same revision as the chain. expected: {}, got: {}",
self_revision_number,
tm_client_state.latest_height().revision_number()
),
});
}
if tm_client_state.latest_height() >= self.host_current_height() {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client has latest height {} greater than or equal to chain height {}",
tm_client_state.latest_height(),
self.host_current_height()
),
});
}
if self.proof_specs() != &tm_client_state.proof_specs {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"client has invalid proof specs. expected: {:?}, got: {:?}",
self.proof_specs(),
tm_client_state.proof_specs
),
});
}
let _ = {
let trust_level = tm_client_state.trust_level;
TendermintTrustThresholdFraction::new(
trust_level.numerator(),
trust_level.denominator(),
)
.map_err(|_| ConnectionError::InvalidClientState {
reason: "invalid trust level".to_string(),
})?
};
if self.unbonding_period() != tm_client_state.unbonding_period {
return Err(ConnectionError::InvalidClientState {
reason: format!(
"invalid unbonding period. expected: {:?}, got: {:?}",
self.unbonding_period(),
tm_client_state.unbonding_period,
),
});
}
if tm_client_state.unbonding_period < tm_client_state.trusting_period {
return Err(ConnectionError::InvalidClientState{ reason: format!(
"unbonding period must be greater than trusting period. unbonding period ({:?}) < trusting period ({:?})",
tm_client_state.unbonding_period,
tm_client_state.trusting_period
)});
}
if !tm_client_state.upgrade_path.is_empty()
&& self.upgrade_path() != tm_client_state.upgrade_path
{
return Err(ConnectionError::InvalidClientState {
reason: format!(
"invalid upgrade path. expected: {:?}, got: {:?}",
self.upgrade_path(),
tm_client_state.upgrade_path
),
});
}
Ok(())
}
fn chain_id(&self) -> &ChainId;
fn host_current_height(&self) -> Height;
fn proof_specs(&self) -> &ProofSpecs;
fn unbonding_period(&self) -> Duration;
fn upgrade_path(&self) -> &[String];
}