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
use crate::core::ics02_client::client_consensus::AnyConsensusState;
use crate::core::ics02_client::client_state::AnyClientState;
use crate::core::ics02_client::client_type::ClientType;
use crate::core::ics02_client::error::{Error, ErrorDetail};
use crate::core::ics02_client::handler::ClientResult::{self, Create, Update, Upgrade};
use crate::core::ics24_host::identifier::ClientId;
use crate::Height;
pub trait ClientReader {
fn client_type(&self, client_id: &ClientId) -> Result<ClientType, Error>;
fn client_state(&self, client_id: &ClientId) -> Result<AnyClientState, Error>;
fn consensus_state(
&self,
client_id: &ClientId,
height: Height,
) -> Result<AnyConsensusState, Error>;
fn maybe_consensus_state(
&self,
client_id: &ClientId,
height: Height,
) -> Result<Option<AnyConsensusState>, Error> {
match self.consensus_state(client_id, height) {
Ok(cs) => Ok(Some(cs)),
Err(e) => match e.detail() {
ErrorDetail::ConsensusStateNotFound(_) => Ok(None),
_ => Err(e),
},
}
}
fn next_consensus_state(
&self,
client_id: &ClientId,
height: Height,
) -> Result<Option<AnyConsensusState>, Error>;
fn prev_consensus_state(
&self,
client_id: &ClientId,
height: Height,
) -> Result<Option<AnyConsensusState>, Error>;
fn client_counter(&self) -> Result<u64, Error>;
}
pub trait ClientKeeper {
fn store_client_result(&mut self, handler_res: ClientResult) -> Result<(), Error> {
match handler_res {
Create(res) => {
let client_id = res.client_id.clone();
self.store_client_type(client_id.clone(), res.client_type)?;
self.store_client_state(client_id.clone(), res.client_state.clone())?;
self.store_consensus_state(
client_id,
res.client_state.latest_height(),
res.consensus_state,
)?;
self.increase_client_counter();
Ok(())
}
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(())
}
Upgrade(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(())
}
}
}
fn store_client_type(
&mut self,
client_id: ClientId,
client_type: ClientType,
) -> Result<(), Error>;
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>;
fn increase_client_counter(&mut self);
}