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
use core::time::Duration;
use eyre::eyre;
use ibc::core::ics03_connection::connection::State as ConnectionState;
use ibc::core::ics03_connection::connection::{ConnectionEnd, IdentifiedConnectionEnd};
use ibc::timestamp::ZERO_DURATION;
use ibc::Height;
use ibc_relayer::chain::handle::ChainHandle;
use ibc_relayer::chain::requests::QueryConnectionRequest;
use ibc_relayer::connection::{extract_connection_id, Connection, ConnectionSide};
use crate::error::Error;
use crate::types::id::{TaggedClientIdRef, TaggedConnectionId, TaggedConnectionIdRef};
use crate::types::tagged::DualTagged;
use crate::util::retry::assert_eventually_succeed;
pub trait TaggedConnectionExt<ChainA: ChainHandle, ChainB: ChainHandle> {
fn tagged_connection_id_a(&self) -> Option<TaggedConnectionIdRef<ChainA, ChainB>>;
fn tagged_connection_id_b(&self) -> Option<TaggedConnectionIdRef<ChainB, ChainA>>;
}
pub trait TaggedConnectionEndExt<ChainA, ChainB> {
fn tagged_counterparty_connection_id(&self) -> Option<TaggedConnectionId<ChainB, ChainA>>;
}
impl<ChainA: ChainHandle, ChainB: ChainHandle> TaggedConnectionExt<ChainA, ChainB>
for Connection<ChainA, ChainB>
{
fn tagged_connection_id_a(&self) -> Option<TaggedConnectionIdRef<ChainA, ChainB>> {
self.a_side.connection_id().map(DualTagged::new)
}
fn tagged_connection_id_b(&self) -> Option<TaggedConnectionIdRef<ChainB, ChainA>> {
self.b_side.connection_id().map(DualTagged::new)
}
}
impl<ChainA, ChainB> TaggedConnectionEndExt<ChainA, ChainB>
for DualTagged<ChainA, ChainB, ConnectionEnd>
{
fn tagged_counterparty_connection_id(&self) -> Option<TaggedConnectionId<ChainB, ChainA>> {
self.contra_map(|c| c.counterparty().connection_id.clone())
.transpose()
}
}
pub fn init_connection<ChainA: ChainHandle, ChainB: ChainHandle>(
handle_a: &ChainA,
handle_b: &ChainB,
client_id_a: &TaggedClientIdRef<ChainA, ChainB>,
client_id_b: &TaggedClientIdRef<ChainB, ChainA>,
) -> Result<
(
TaggedConnectionId<ChainB, ChainA>,
Connection<ChainB, ChainA>,
),
Error,
> {
let connection = Connection {
delay_period: ZERO_DURATION,
a_side: ConnectionSide::new(handle_a.clone(), (*client_id_a.value()).clone(), None),
b_side: ConnectionSide::new(handle_b.clone(), (*client_id_b.value()).clone(), None),
};
let event = connection.build_conn_init_and_send()?;
let connection_id = extract_connection_id(&event)?.clone();
let connection2 = Connection::restore_from_event(handle_b.clone(), handle_a.clone(), event)?;
Ok((DualTagged::new(connection_id), connection2))
}
pub fn query_connection_end<ChainA: ChainHandle, ChainB>(
handle: &ChainA,
connection_id: &TaggedConnectionIdRef<ChainA, ChainB>,
) -> Result<DualTagged<ChainA, ChainB, ConnectionEnd>, Error> {
let connection_end = handle.query_connection(QueryConnectionRequest {
connection_id: connection_id.into_value().clone(),
height: Height::zero(),
})?;
Ok(DualTagged::new(connection_end))
}
pub fn query_identified_connection_end<ChainA: ChainHandle, ChainB>(
handle: &ChainA,
connection_id: TaggedConnectionIdRef<ChainA, ChainB>,
) -> Result<DualTagged<ChainA, ChainB, IdentifiedConnectionEnd>, Error> {
let connection_end = handle.query_connection(QueryConnectionRequest {
connection_id: connection_id.into_value().clone(),
height: Height::zero(),
})?;
Ok(DualTagged::new(IdentifiedConnectionEnd::new(
connection_id.into_value().clone(),
connection_end,
)))
}
pub fn assert_eventually_connection_established<ChainA: ChainHandle, ChainB: ChainHandle>(
handle_a: &ChainA,
handle_b: &ChainB,
connection_id_a: &TaggedConnectionIdRef<ChainA, ChainB>,
) -> Result<TaggedConnectionId<ChainB, ChainA>, Error> {
assert_eventually_succeed(
"connection should eventually established",
20,
Duration::from_secs(1),
|| {
let connection_end_a = query_connection_end(handle_a, connection_id_a)?;
if !connection_end_a
.value()
.state_matches(&ConnectionState::Open)
{
return Err(Error::generic(eyre!(
"expected connection end A to be in open state"
)));
}
let connection_id_b = connection_end_a
.tagged_counterparty_connection_id()
.ok_or_else(|| {
eyre!("expected counterparty connection id to present on open connection")
})?;
let connection_end_b = query_connection_end(handle_b, &connection_id_b.as_ref())?;
if !connection_end_b
.value()
.state_matches(&ConnectionState::Open)
{
return Err(Error::generic(eyre!(
"expected connection end B to be in open state"
)));
}
Ok(connection_id_b)
},
)
}