-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection_helpers.go
292 lines (253 loc) · 7.46 KB
/
connection_helpers.go
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package ibc_test
import (
"context"
"errors"
"strings"
"testing"
"github.com/strangelove-ventures/interchaintest/v3/ibc"
"github.com/strangelove-ventures/interchaintest/v3/testreporter"
"github.com/strangelove-ventures/interchaintest/v3/testutil"
"github.com/stretchr/testify/require"
)
func generatePath(
t *testing.T,
ctx context.Context,
r ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
chainAId string,
chainBId string,
path string,
) {
err := r.GeneratePath(ctx, eRep, chainAId, chainBId, path)
require.NoError(t, err)
}
func generateICSChannel(
t *testing.T,
ctx context.Context,
r ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
icsPath string,
chainA ibc.Chain,
chainB ibc.Chain,
) {
err := r.CreateChannel(ctx, eRep, icsPath, ibc.CreateChannelOptions{
SourcePortName: "consumer",
DestPortName: "provider",
Order: ibc.Ordered,
Version: "1",
})
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 2, chainA, chainB)
require.NoError(t, err, "failed to wait for blocks")
}
func createValidator(
t *testing.T,
ctx context.Context,
r ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
chain ibc.Chain,
counterparty ibc.Chain,
) {
cmd := getCreateValidatorCmd(chain)
_, _, err := chain.Exec(ctx, cmd, nil)
require.NoError(t, err)
// Wait a bit for the VSC packet to get relayed.
err = testutil.WaitForBlocks(ctx, 2, chain, counterparty)
require.NoError(t, err, "failed to wait for blocks")
}
func linkPath(
t *testing.T,
ctx context.Context,
r ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
chainA ibc.Chain,
chainB ibc.Chain,
path string,
) {
err := r.LinkPath(ctx, eRep, path, ibc.DefaultChannelOpts(), ibc.DefaultClientOpts())
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 2, chainA, chainB)
require.NoError(t, err, "failed to wait for blocks")
}
func generateClient(
t *testing.T,
ctx context.Context,
r ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
path string,
chainA ibc.Chain,
chainB ibc.Chain,
) (string, string) {
chainAClients, _ := r.GetClients(ctx, eRep, chainA.Config().ChainID)
chainBClients, _ := r.GetClients(ctx, eRep, chainB.Config().ChainID)
err := r.CreateClients(ctx, eRep, path, ibc.CreateClientOptions{TrustingPeriod: "330h"})
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 2, chainA, chainB)
require.NoError(t, err, "failed to wait for blocks")
newChainAClients, _ := r.GetClients(ctx, eRep, chainA.Config().ChainID)
newChainBClients, _ := r.GetClients(ctx, eRep, chainB.Config().ChainID)
var newClientA, newClientB string
aClientDiff := clientDifference(chainAClients, newChainAClients)
bClientDiff := clientDifference(chainBClients, newChainBClients)
if len(aClientDiff) > 0 {
newClientA = aClientDiff[0]
} else {
newClientA = ""
}
if len(bClientDiff) > 0 {
newClientB = bClientDiff[0]
} else {
newClientB = ""
}
print("\n found client differences. new client A: ", newClientA, "b:")
return newClientA, newClientB
}
func generateConnections(
t *testing.T,
ctx context.Context,
r ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
path string,
chainA ibc.Chain,
chainB ibc.Chain,
) (string, string) {
chainAConns, _ := r.GetConnections(ctx, eRep, chainA.Config().ChainID)
chainBConns, _ := r.GetConnections(ctx, eRep, chainB.Config().ChainID)
err := r.CreateConnections(ctx, eRep, path)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 2, chainA, chainB)
require.NoError(t, err, "failed to wait for blocks")
newChainAConns, _ := r.GetConnections(ctx, eRep, chainA.Config().ChainID)
newChainBConns, _ := r.GetConnections(ctx, eRep, chainB.Config().ChainID)
newChainAConnection := connectionDifference(chainAConns, newChainAConns)
newChainBConnection := connectionDifference(chainBConns, newChainBConns)
require.NotEqual(t, 0, len(newChainAConnection), "more than one connection generated", strings.Join(newChainAConnection, " "))
require.NotEqual(t, 0, len(newChainBConnection), "more than one connection generated", strings.Join(newChainBConnection, " "))
return newChainAConnection[0], newChainBConnection[0]
}
func connectionDifference(
a []*ibc.ConnectionOutput,
b []*ibc.ConnectionOutput,
) (diff []string) {
m := make(map[string]bool)
// we first mark all existing connections
for _, item := range a {
m[item.ID] = true
}
// and append all new ones
for _, item := range b {
if _, ok := m[item.ID]; !ok {
diff = append(diff, item.ID)
}
}
return
}
func clientDifference(a, b []*ibc.ClientOutput) (diff []string) {
m := make(map[string]bool)
// we first mark all existing clients
for _, item := range a {
m[item.ClientID] = true
}
// and append all new ones
for _, item := range b {
if _, ok := m[item.ClientID]; !ok {
diff = append(diff, item.ClientID)
}
}
return
}
func printChannels(channels []ibc.ChannelOutput, chain string) {
for _, channel := range channels {
print("\n\n", chain, " channels after create channel :", channel.ChannelID, " to ", channel.Counterparty.ChannelID, "\n")
}
}
func printConnections(connections ibc.ConnectionOutputs) {
for _, connection := range connections {
print(connection.ID, "\n")
}
}
func channelDifference(oldChannels, newChannels []ibc.ChannelOutput) (diff []string) {
m := make(map[string]bool)
// we first mark all existing channels
for _, channel := range newChannels {
m[channel.ChannelID] = true
}
// then find the new ones
for _, channel := range oldChannels {
if _, ok := m[channel.ChannelID]; !ok {
diff = append(diff, channel.ChannelID)
}
}
return
}
func getPairwiseConnectionIds(
aconns ibc.ConnectionOutputs,
bconns ibc.ConnectionOutputs,
) ([]string, []string, error) {
abconnids := make([]string, 0)
baconnids := make([]string, 0)
found := false
for _, a := range aconns {
for _, b := range bconns {
if a.ClientID == b.Counterparty.ClientId &&
b.ClientID == a.Counterparty.ClientId &&
a.ID == b.Counterparty.ConnectionId &&
b.ID == a.Counterparty.ConnectionId {
found = true
abconnids = append(abconnids, a.ID)
baconnids = append(baconnids, b.ID)
}
}
}
if found {
return abconnids, baconnids, nil
} else {
return abconnids, baconnids, errors.New("no connection found")
}
}
// returns transfer channel ids
func getPairwiseTransferChannelIds(
achans []ibc.ChannelOutput,
bchans []ibc.ChannelOutput,
aToBConnId string,
bToAConnId string,
) (string, string, error) {
for _, a := range achans {
for _, b := range bchans {
if a.ChannelID == b.Counterparty.ChannelID &&
b.ChannelID == a.Counterparty.ChannelID &&
a.PortID == "transfer" &&
b.PortID == "transfer" &&
a.Ordering == "ORDER_UNORDERED" &&
b.Ordering == "ORDER_UNORDERED" &&
a.ConnectionHops[0] == aToBConnId &&
b.ConnectionHops[0] == bToAConnId {
return a.ChannelID, b.ChannelID, nil
}
}
}
return "", "", errors.New("no transfer channel found")
}
// returns ccv channel ids
func getPairwiseCCVChannelIds(
achans []ibc.ChannelOutput,
bchans []ibc.ChannelOutput,
aToBConnId string,
bToAConnId string,
) (string, string, error) {
for _, a := range achans {
for _, b := range bchans {
if a.ChannelID == b.Counterparty.ChannelID &&
b.ChannelID == a.Counterparty.ChannelID &&
a.PortID == "provider" &&
b.PortID == "consumer" &&
a.Ordering == "ORDER_ORDERED" &&
b.Ordering == "ORDER_ORDERED" &&
a.ConnectionHops[0] == aToBConnId &&
b.ConnectionHops[0] == bToAConnId {
return a.ChannelID, b.ChannelID, nil
}
}
}
return "", "", errors.New("no ccv channel found")
}