-
Notifications
You must be signed in to change notification settings - Fork 90
/
update_client.rs
143 lines (119 loc) · 6.04 KB
/
update_client.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
use crate::prelude::*;
use tendermint_light_client_verifier::types::{TrustedBlockState, UntrustedBlockState};
use tendermint_light_client_verifier::Verifier;
use crate::clients::ics07_tendermint::consensus_state::ConsensusState as TmConsensusState;
use crate::clients::ics07_tendermint::error::{Error, IntoResult};
use crate::clients::ics07_tendermint::header::Header as TmHeader;
use crate::core::ics02_client::client_state::ClientState as Ics2ClientState;
use crate::core::ics02_client::error::ClientError;
use crate::core::ics24_host::path::ClientConsensusStatePath;
use crate::core::{ics24_host::identifier::ClientId, ValidationContext};
use super::{check_header_trusted_next_validator_set, downcast_tm_consensus_state, ClientState};
impl ClientState {
pub fn verify_header(
&self,
ctx: &dyn ValidationContext,
client_id: &ClientId,
header: TmHeader,
) -> Result<(), ClientError> {
// Checks that the header fields are valid.
header.validate_basic()?;
// The tendermint-light-client crate though works on heights that are assumed
// to have the same revision number. We ensure this here.
header.verify_chain_id_version_matches_height(&self.chain_id())?;
// Delegate to tendermint-light-client, which contains the required checks
// of the new header against the trusted consensus state.
{
let trusted_state =
{
let trusted_client_cons_state_path =
ClientConsensusStatePath::new(client_id, &header.trusted_height);
let trusted_consensus_state = downcast_tm_consensus_state(
ctx.consensus_state(&trusted_client_cons_state_path)?
.as_ref(),
)?;
check_header_trusted_next_validator_set(&header, &trusted_consensus_state)?;
TrustedBlockState {
chain_id: &self.chain_id.clone().into(),
header_time: trusted_consensus_state.timestamp,
height: header.trusted_height.revision_height().try_into().map_err(
|_| ClientError::ClientSpecific {
description: Error::InvalidHeaderHeight {
height: header.trusted_height.revision_height(),
}
.to_string(),
},
)?,
next_validators: &header.trusted_next_validator_set,
next_validators_hash: trusted_consensus_state.next_validators_hash,
}
};
let untrusted_state = UntrustedBlockState {
signed_header: &header.signed_header,
validators: &header.validator_set,
// NB: This will skip the
// VerificationPredicates::next_validators_match check for the
// untrusted state.
next_validators: None,
};
let options = self.as_light_client_options()?;
let now = ctx.host_timestamp()?.into_tm_time().unwrap();
// main header verification, delegated to the tendermint-light-client crate.
self.verifier
.verify(untrusted_state, trusted_state, &options, now)
.into_result()?;
}
Ok(())
}
pub fn check_for_misbehaviour_update_client(
&self,
ctx: &dyn ValidationContext,
client_id: &ClientId,
header: TmHeader,
) -> Result<bool, ClientError> {
let header_consensus_state = TmConsensusState::from(header.clone());
let maybe_existing_consensus_state = {
let path_at_header_height = ClientConsensusStatePath::new(client_id, &header.height());
ctx.consensus_state(&path_at_header_height).ok()
};
match maybe_existing_consensus_state {
Some(existing_consensus_state) => {
let existing_consensus_state =
downcast_tm_consensus_state(existing_consensus_state.as_ref())?;
// There is evidence of misbehaviour if the stored consensus state
// is different from the new one we received.
Ok(existing_consensus_state != header_consensus_state)
}
None => {
// If no header was previously installed, we ensure the monotonicity of timestamps.
// 1. for all headers, the new header needs to have a larger timestamp than
// the “previous header”
{
let maybe_prev_cs = ctx.prev_consensus_state(client_id, &header.height())?;
if let Some(prev_cs) = maybe_prev_cs {
// New header timestamp cannot occur *before* the
// previous consensus state's height
let prev_cs = downcast_tm_consensus_state(prev_cs.as_ref())?;
if header.signed_header.header().time <= prev_cs.timestamp {
return Ok(true);
}
}
}
// 2. if a header comes in and is not the “last” header, then we also ensure
// that its timestamp is less than the “next header”
if header.height() < self.latest_height() {
let maybe_next_cs = ctx.next_consensus_state(client_id, &header.height())?;
if let Some(next_cs) = maybe_next_cs {
// New (untrusted) header timestamp cannot occur *after* next
// consensus state's height
let next_cs = downcast_tm_consensus_state(next_cs.as_ref())?;
if header.signed_header.header().time >= next_cs.timestamp {
return Ok(true);
}
}
}
Ok(false)
}
}
}
}