-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathcontext.rs
130 lines (110 loc) · 4.93 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
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
//! ICS4 (channel) context. The two traits `ConnectionReader` and `ConnectionKeeper` define
//! the interface that any host chain must implement to be able to process any `ChannelMsg`.
//! TODO make "ADR 004: IBC protocol implementation" for more details.
//!
use crate::ics02_client::client_def::{AnyClientState, AnyConsensusState};
use crate::ics03_connection::connection::ConnectionEnd;
use crate::ics04_channel::channel::{ChannelEnd, State};
use crate::ics04_channel::error::Error;
use crate::ics04_channel::handler::ChannelResult;
use crate::ics04_channel::version::{get_compatible_versions, pick_version};
use crate::ics05_port::capabilities::Capability;
use crate::ics24_host::identifier::{ChannelId, ConnectionId, PortId};
use crate::Height;
/// A context supplying all the necessary read-only dependencies for processing any `ChannelMsg`.
pub trait ChannelReader {
/// Returns the ChannelEnd for the given identifier `chan_id`.
fn channel_end(&self, port_channel_id: &(PortId, ChannelId)) -> Option<ChannelEnd>;
/// Returns the ConnectionState for the given identifier `connection_id`.
fn connection_state(&self, connection_id: &ConnectionId) -> Option<ConnectionEnd>;
fn connection_channels(&self, cid: &ConnectionId) -> Option<Vec<(PortId, ChannelId)>>;
fn channel_client_state(&self, port_channel_id: &(PortId, ChannelId))
-> Option<AnyClientState>;
fn channel_consensus_state(
&self,
port_channel_id: &(PortId, ChannelId),
height: Height,
) -> Option<AnyConsensusState>;
fn port_capability(&self, port_id: &PortId) -> Option<Capability>;
fn capability_authentification(&self, port_id: &PortId, cap: &Capability) -> bool;
/// Function required by ICS 04. Returns the list of all possible versions that the channels handshake protocol supports.
fn get_compatible_versions(&self) -> Vec<String> {
get_compatible_versions()
}
/// Function required by ICS 04. Returns one version out of the supplied list of versions, which the channel handshake protocol prefers.
fn pick_version(
&self,
supported_versions: Vec<String>,
counterparty_candidate_versions: Vec<String>,
) -> Result<String, Error> {
pick_version(supported_versions, counterparty_candidate_versions)
}
}
/// A context supplying all the necessary write-only dependencies (i.e., storage writing facility)
/// for processing any `ChannelMsg`.
pub trait ChannelKeeper {
fn store_channel_result(&mut self, result: ChannelResult) -> Result<(), Error> {
match result.channel_end.state() {
State::Init => {
let channel_id = self.next_channel_id();
self.store_channel(
&(result.port_id.clone(), channel_id.clone()),
&result.channel_end,
)?;
self.store_nextsequence_send(&(result.port_id.clone(), channel_id.clone()), 1)?;
self.store_nextsequence_recv(&(result.port_id.clone(), channel_id.clone()), 1)?;
self.store_nextsequence_ack(&(result.port_id.clone(), channel_id.clone()), 1)?;
self.store_connection_channels(
&result.channel_end.connection_hops()[0].clone(),
&(result.port_id.clone(), channel_id.clone()),
)?;
}
State::TryOpen => {
self.store_channel(
&(result.port_id, result.channel_id.clone().unwrap()),
&result.channel_end,
)?;
// TODO: If this is the first time the handler processed this channel, associate the
// channel end to its client identifier.
// self.store_channel_to_connection(
// &(result.port_id,result.channel_id),
// &result.... ,
// )?;
}
_ => {
self.store_channel(
&(result.port_id, result.channel_id.clone().unwrap()),
&result.channel_end,
)?;
}
}
Ok(())
}
fn next_channel_id(&mut self) -> ChannelId;
fn store_connection_channels(
&mut self,
conn_id: &ConnectionId,
port_channel_id: &(PortId, ChannelId),
) -> Result<(), Error>;
/// Stores the given channel_end at a path associated with the port_id and channel_id.
fn store_channel(
&mut self,
port_channel_id: &(PortId, ChannelId),
channel_end: &ChannelEnd,
) -> Result<(), Error>;
fn store_nextsequence_send(
&mut self,
port_channel_id: &(PortId, ChannelId),
seq: u64,
) -> Result<(), Error>;
fn store_nextsequence_recv(
&mut self,
port_channel_id: &(PortId, ChannelId),
seq: u64,
) -> Result<(), Error>;
fn store_nextsequence_ack(
&mut self,
port_channel_id: &(PortId, ChannelId),
seq: u64,
) -> Result<(), Error>;
}