-
Notifications
You must be signed in to change notification settings - Fork 329
/
context.rs
69 lines (62 loc) · 2.49 KB
/
context.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
//! ICS2 (client) context. The two traits `ClientReader` and `ClientKeeper` define the interface
//! that any host chain must implement to be able to process any `ClientMsg`. See
//! "ADR 003: IBC protocol implementation" for more details.
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error::Error;
use crate::ics02_client::handler::ClientResult::{Create, Update};
use crate::ics02_client::handler::{ClientEvent, ClientResult};
use crate::ics24_host::identifier::ClientId;
use crate::Height;
/// Defines the read-only part of ICS2 (client functions) context.
pub trait ClientReader {
fn client_type(&self, client_id: &ClientId) -> Option<ClientType>;
fn client_state(&self, client_id: &ClientId) -> Option<AnyClientState>;
fn consensus_state(&self, client_id: &ClientId, height: Height) -> Option<AnyConsensusState>;
}
/// Defines the write-only part of ICS2 (client functions) context.
pub trait ClientKeeper {
fn store_client_type(
&mut self,
client_id: ClientId,
client_type: ClientType,
) -> Result<(), Error>;
fn store_client_result(
&mut self,
handler_res: ClientResult,
) -> Result<Vec<ClientEvent>, Error> {
match handler_res {
Create(res) => {
let client_id = self.next_client_id();
self.store_client_state(client_id.clone(), res.client_state.clone())?;
self.store_consensus_state(
client_id.clone(),
res.client_state.latest_height(),
res.consensus_state,
)?;
Ok(vec![ClientEvent::ClientCreated(client_id)])
}
Update(res) => {
self.store_client_state(res.client_id.clone(), res.client_state.clone())?;
self.store_consensus_state(
res.client_id.clone(),
res.client_state.latest_height(),
res.consensus_state,
)?;
Ok(vec![ClientEvent::ClientUpdated(res.client_id)])
}
}
}
fn next_client_id(&mut self) -> ClientId;
fn store_client_state(
&mut self,
client_id: ClientId,
client_state: AnyClientState,
) -> Result<(), Error>;
fn store_consensus_state(
&mut self,
client_id: ClientId,
height: Height,
consensus_state: AnyConsensusState,
) -> Result<(), Error>;
}