forked from cosmos/ibc-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.rs
104 lines (86 loc) · 4.34 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
//! ICS3 (connection) context. The two traits `ConnectionReader` and `ConnectionKeeper` define
//! the interface that any host chain must implement to be able to process any `ConnectionMsg`.
//! See "ADR 003: IBC protocol implementation" for more details.
use crate::ics02_client::client_consensus::AnyConsensusState;
use crate::ics02_client::client_state::AnyClientState;
use crate::ics03_connection::connection::ConnectionEnd;
use crate::ics03_connection::error::Error;
use crate::ics03_connection::handler::{ConnectionIdState, ConnectionResult};
use crate::ics03_connection::version::{get_compatible_versions, pick_version, Version};
use crate::ics23_commitment::commitment::CommitmentPrefix;
use crate::ics24_host::identifier::{ClientId, ConnectionId};
use crate::prelude::*;
use crate::Height;
/// A context supplying all the necessary read-only dependencies for processing any `ConnectionMsg`.
pub trait ConnectionReader {
/// Returns the ConnectionEnd for the given identifier `conn_id`.
fn connection_end(&self, conn_id: &ConnectionId) -> Result<ConnectionEnd, Error>;
/// Returns the ClientState for the given identifier `client_id`.
fn client_state(&self, client_id: &ClientId) -> Result<AnyClientState, Error>;
/// Returns the current height of the local chain.
fn host_current_height(&self) -> Height;
/// Returns the oldest height available on the local chain.
fn host_oldest_height(&self) -> Height;
/// Returns the prefix that the local chain uses in the KV store.
fn commitment_prefix(&self) -> CommitmentPrefix;
/// Returns the ConsensusState that the given client stores at a specific height.
fn client_consensus_state(
&self,
client_id: &ClientId,
height: Height,
) -> Result<AnyConsensusState, Error>;
/// Returns the ConsensusState of the host (local) chain at a specific height.
fn host_consensus_state(&self, height: Height) -> Result<AnyConsensusState, Error>;
/// Function required by ICS 03. Returns the list of all possible versions that the connection
/// handshake protocol supports.
fn get_compatible_versions(&self) -> Vec<Version> {
get_compatible_versions()
}
/// Function required by ICS 03. Returns one version out of the supplied list of versions, which the
/// connection handshake protocol prefers.
fn pick_version(
&self,
supported_versions: Vec<Version>,
counterparty_candidate_versions: Vec<Version>,
) -> Result<Version, Error> {
pick_version(supported_versions, counterparty_candidate_versions)
}
/// Returns a counter on how many connections have been created thus far.
/// The value of this counter should increase only via method
/// `ConnectionKeeper::increase_connection_counter`.
fn connection_counter(&self) -> Result<u64, Error>;
}
/// A context supplying all the necessary write-only dependencies (i.e., storage writing facility)
/// for processing any `ConnectionMsg`.
pub trait ConnectionKeeper {
fn store_connection_result(&mut self, result: ConnectionResult) -> Result<(), Error> {
self.store_connection(result.connection_id.clone(), &result.connection_end)?;
// If we generated an identifier, increase the counter & associate this new identifier
// with the client id.
if matches!(result.connection_id_state, ConnectionIdState::Generated) {
self.increase_connection_counter();
// Also associate the connection end to its client identifier.
self.store_connection_to_client(
result.connection_id.clone(),
result.connection_end.client_id(),
)?;
}
Ok(())
}
/// Stores the given connection_end at a path associated with the connection_id.
fn store_connection(
&mut self,
connection_id: ConnectionId,
connection_end: &ConnectionEnd,
) -> Result<(), Error>;
/// Stores the given connection_id at a path associated with the client_id.
fn store_connection_to_client(
&mut self,
connection_id: ConnectionId,
client_id: &ClientId,
) -> Result<(), Error>;
/// Called upon connection identifier creation (Init or Try process).
/// Increases the counter which keeps track of how many connections have been created.
/// Should never fail.
fn increase_connection_counter(&mut self);
}