-
Notifications
You must be signed in to change notification settings - Fork 87
/
client_ctx.rs
342 lines (309 loc) · 11.1 KB
/
client_ctx.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use core::fmt::Debug;
use basecoin_store::context::{ProvableStore, Store};
use basecoin_store::types::Height as StoreHeight;
use ibc::clients::tendermint::context::{
CommonContext as TmCommonContext, ValidationContext as TmValidationContext,
};
use ibc::core::client::context::{ClientExecutionContext, ClientValidationContext};
use ibc::core::client::types::error::ClientError;
use ibc::core::client::types::Height;
use ibc::core::handler::types::error::ContextError;
use ibc::core::host::types::identifiers::{ChannelId, ClientId, PortId};
use ibc::core::host::types::path::{
ClientConsensusStatePath, ClientStatePath, ClientUpdateHeightPath, ClientUpdateTimePath, Path,
};
use ibc::core::host::ValidationContext;
use ibc::core::primitives::Timestamp;
use ibc::primitives::prelude::*;
use crate::testapp::ibc::clients::mock::client_state::MockClientContext;
use crate::testapp::ibc::clients::{AnyClientState, AnyConsensusState};
use crate::testapp::ibc::core::types::MockGenericContext;
pub type PortChannelIdMap<V> = BTreeMap<PortId, BTreeMap<ChannelId, V>>;
/// A mock of an IBC client record as it is stored in a mock context.
/// For testing ICS02 handlers mostly, cf. `MockClientContext`.
#[derive(Clone, Debug)]
pub struct MockClientRecord {
/// The client state (representing only the latest height at the moment).
pub client_state: Option<AnyClientState>,
/// Mapping of heights to consensus states for this client.
pub consensus_states: BTreeMap<Height, AnyConsensusState>,
}
impl<S> MockClientContext for MockGenericContext<S>
where
S: ProvableStore + Debug,
{
type ConversionError = &'static str;
type AnyConsensusState = AnyConsensusState;
fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
ValidationContext::host_timestamp(self)
}
fn host_height(&self) -> Result<Height, ContextError> {
ValidationContext::host_height(self)
}
fn consensus_state(
&self,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Self::AnyConsensusState, ContextError> {
ValidationContext::consensus_state(self, client_cons_state_path)
}
}
impl<S> ClientValidationContext for MockGenericContext<S>
where
S: ProvableStore + Debug,
{
/// Returns the time and height when the client state for the given
/// [`ClientId`] was updated with a header for the given [`Height`]
fn update_meta(
&self,
client_id: &ClientId,
height: &Height,
) -> Result<(Timestamp, Height), ContextError> {
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
let processed_timestamp = self
.ibc_store
.client_processed_times
.get(StoreHeight::Pending, &client_update_time_path)
.ok_or(ClientError::UpdateMetaDataNotFound {
client_id: client_id.clone(),
height: *height,
})?;
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
let processed_height = self
.ibc_store
.client_processed_heights
.get(StoreHeight::Pending, &client_update_height_path)
.ok_or(ClientError::UpdateMetaDataNotFound {
client_id: client_id.clone(),
height: *height,
})?;
Ok((processed_timestamp, processed_height))
}
}
impl<S> ClientExecutionContext for MockGenericContext<S>
where
S: ProvableStore + Debug,
{
type V = Self;
type AnyClientState = AnyClientState;
type AnyConsensusState = AnyConsensusState;
/// Called upon successful client creation and update
fn store_client_state(
&mut self,
client_state_path: ClientStatePath,
client_state: Self::AnyClientState,
) -> Result<(), ContextError> {
self.ibc_store
.client_state_store
.set(client_state_path.clone(), client_state)
.map_err(|_| ClientError::Other {
description: "Client state store error".to_string(),
})?;
Ok(())
}
/// Called upon successful client creation and update
fn store_consensus_state(
&mut self,
consensus_state_path: ClientConsensusStatePath,
consensus_state: Self::AnyConsensusState,
) -> Result<(), ContextError> {
self.ibc_store
.consensus_state_store
.set(consensus_state_path, consensus_state)
.map_err(|_| ClientError::Other {
description: "Consensus state store error".to_string(),
})?;
Ok(())
}
/// Called upon successful client update. Implementations are expected to
/// use this to record the time and height at which this update (or header)
/// was processed.
fn store_update_meta(
&mut self,
client_id: ClientId,
height: Height,
host_timestamp: Timestamp,
host_height: Height,
) -> Result<(), ContextError> {
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.ibc_store
.client_processed_times
.set(client_update_time_path, host_timestamp)
.map_err(|_| ClientError::Other {
description: "store update error".into(),
})?;
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.ibc_store
.client_processed_heights
.set(client_update_height_path, host_height)
.map_err(|_| ClientError::Other {
description: "store update error".into(),
})?;
Ok(())
}
/// Delete the update metadata associated with the client at the specified
/// height.
fn delete_update_meta(
&mut self,
client_id: ClientId,
height: Height,
) -> Result<(), ContextError> {
let client_update_time_path = ClientUpdateTimePath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.ibc_store
.client_processed_times
.delete(client_update_time_path);
let client_update_height_path = ClientUpdateHeightPath::new(
client_id.clone(),
height.revision_number(),
height.revision_height(),
);
self.ibc_store
.client_processed_heights
.delete(client_update_height_path);
Ok(())
}
fn delete_consensus_state(
&mut self,
consensus_state_path: ClientConsensusStatePath,
) -> Result<(), ContextError> {
self.ibc_store
.consensus_state_store
.delete(consensus_state_path);
Ok(())
}
}
impl<S> TmCommonContext for MockGenericContext<S>
where
S: ProvableStore + Debug,
{
type ConversionError = &'static str;
type AnyConsensusState = AnyConsensusState;
fn host_timestamp(&self) -> Result<Timestamp, ContextError> {
ValidationContext::host_timestamp(self)
}
fn host_height(&self) -> Result<Height, ContextError> {
ValidationContext::host_height(self)
}
fn consensus_state(
&self,
client_cons_state_path: &ClientConsensusStatePath,
) -> Result<Self::AnyConsensusState, ContextError> {
ValidationContext::consensus_state(self, client_cons_state_path)
}
fn consensus_state_heights(&self, client_id: &ClientId) -> Result<Vec<Height>, ContextError> {
let path = format!("clients/{}/consensusStates", client_id)
.try_into()
.map_err(|_| ClientError::Other {
description: "Invalid consensus state path".into(),
})?;
self.ibc_store
.consensus_state_store
.get_keys(&path)
.into_iter()
.flat_map(|path| {
if let Ok(Path::ClientConsensusState(consensus_path)) = path.try_into() {
Some(consensus_path)
} else {
None
}
})
.map(|consensus_path| {
let height = Height::new(
consensus_path.revision_number,
consensus_path.revision_height,
)?;
Ok(height)
})
.collect()
}
}
impl<S> TmValidationContext for MockGenericContext<S>
where
S: ProvableStore + Debug,
{
fn next_consensus_state(
&self,
client_id: &ClientId,
height: &Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
let path = format!("clients/{client_id}/consensusStates")
.try_into()
.unwrap(); // safety - path must be valid since ClientId and height are valid Identifiers
let keys = self.ibc_store.store.get_keys(&path);
let found_path = keys.into_iter().find_map(|path| {
if let Ok(Path::ClientConsensusState(path)) = path.try_into() {
if height
< &Height::new(path.revision_number, path.revision_height).expect("no error")
{
return Some(path);
}
}
None
});
if let Some(path) = found_path {
let consensus_state = self
.ibc_store
.consensus_state_store
.get(StoreHeight::Pending, &path)
.ok_or(ClientError::ConsensusStateNotFound {
client_id: client_id.clone(),
height: *height,
})?;
Ok(Some(consensus_state))
} else {
Ok(None)
}
}
fn prev_consensus_state(
&self,
client_id: &ClientId,
height: &Height,
) -> Result<Option<Self::AnyConsensusState>, ContextError> {
let path = format!("clients/{client_id}/consensusStates")
.try_into()
.unwrap(); // safety - path must be valid since ClientId and height are valid Identifiers
let keys = self.ibc_store.store.get_keys(&path);
let found_path = keys.into_iter().rev().find_map(|path| {
if let Ok(Path::ClientConsensusState(path)) = path.try_into() {
if height
> &Height::new(path.revision_number, path.revision_height).expect("no error")
{
return Some(path);
}
}
None
});
if let Some(path) = found_path {
let consensus_state = self
.ibc_store
.consensus_state_store
.get(StoreHeight::Pending, &path)
.ok_or(ClientError::ConsensusStateNotFound {
client_id: client_id.clone(),
height: *height,
})?;
Ok(Some(consensus_state))
} else {
Ok(None)
}
}
}