forked from strangelove-ventures/interchaintest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relayersetup.go
186 lines (151 loc) · 5.85 KB
/
relayersetup.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
package conformance
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
conntypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
)
// TestRelayerSetup contains a series of subtests that configure a relayer step-by-step.
func TestRelayerSetup(t *testing.T, ctx context.Context, cf interchaintest.ChainFactory, rf interchaintest.RelayerFactory, rep *testreporter.Reporter) {
rep.TrackTest(t)
client, network := interchaintest.DockerSetup(t)
req := require.New(rep.TestifyT(t))
chains, err := cf.Chains(t.Name())
req.NoError(err, "failed to get chains")
if len(chains) != 2 {
panic(fmt.Errorf("expected 2 chains, got %d", len(chains)))
}
c0, c1 := chains[0], chains[1]
r := rf.Build(t, client, network)
const pathName = "p"
ic := interchaintest.NewInterchain().
AddChain(c0).
AddChain(c1).
AddRelayer(r, "r").
AddLink(interchaintest.InterchainLink{
// We are adding a link here so that the interchain object creates appropriate relayer wallets,
// but we call ic.Build with SkipPathCreation=true, so the link won't be created.
Chain1: c0,
Chain2: c1,
Relayer: r,
Path: pathName,
})
eRep := rep.RelayerExecReporter(t)
req.NoError(ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
// Create relayer keys and wallets but don't create links,
// since that is what we are about to test.
SkipPathCreation: true,
}))
defer ic.Close()
// Now handle each creation step as a subtest in sequence.
// After each subtest, check t.Failed, which will be true if a subtest failed,
// to conditionally stop execution before the following subtest.
t.Run("generate path", func(t *testing.T) {
rep.TrackTest(t)
req := require.New(rep.TestifyT(t))
req.NoError(r.GeneratePath(ctx, rep.RelayerExecReporter(t), c0.Config().ChainID, c1.Config().ChainID, pathName))
})
if t.Failed() {
return
}
t.Run("create clients", func(t *testing.T) {
rep.TrackTest(t)
req := require.New(rep.TestifyT(t))
req.NoError(r.CreateClients(ctx, rep.RelayerExecReporter(t), pathName, ibc.DefaultClientOpts()))
})
if t.Failed() {
return
}
// The client isn't created immediately -- wait for two blocks to ensure the clients are ready.
req.NoError(testutil.WaitForBlocks(ctx, 2, c0, c1))
t.Run("create connections", func(t *testing.T) {
rep.TrackTest(t)
req := require.New(rep.TestifyT(t))
eRep := rep.RelayerExecReporter(t)
req.NoError(r.CreateConnections(ctx, eRep, pathName))
// Assert against the singly created connections individually.
conns0, err := r.GetConnections(ctx, eRep, c0.Config().ChainID)
req.NoError(err)
req.True(len(conns0) == 1 || len(conns0) == 2)
if len(conns0) == 2 {
// Chain might have a localhost connection. Connection IDs are sorted, so this would be at position [1].
req.Equal(exported.LocalhostConnectionID, conns0[1].ID)
}
conn0 := conns0[0]
req.NotEmpty(conn0.ID)
req.NotEmpty(conn0.ClientID)
req.Subset([]string{conntypes.OPEN.String(), "Open"}, []string{conn0.State})
conns1, err := r.GetConnections(ctx, eRep, c1.Config().ChainID)
req.NoError(err)
req.True(len(conns1) == 1 || len(conns1) == 2)
if len(conns1) == 2 {
// Chain might have a localhost connection. Connection IDs are sorted, so this would be at position [1].
req.Equal(exported.LocalhostConnectionID, conns1[1].ID)
}
conn1 := conns1[0]
req.NotEmpty(conn1.ID)
req.NotEmpty(conn1.ClientID)
req.Subset([]string{conntypes.OPEN.String(), "Open"}, []string{conn1.State})
// Now validate counterparties.
req.Equal(conn0.Counterparty.ClientId, conn1.ClientID)
req.Equal(conn0.Counterparty.ConnectionId, conn1.ID)
req.Equal(conn1.Counterparty.ClientId, conn0.ClientID)
req.Equal(conn1.Counterparty.ConnectionId, conn0.ID)
})
if t.Failed() {
return
}
t.Run("create channel", func(t *testing.T) {
rep.TrackTest(t)
req := require.New(rep.TestifyT(t))
eRep := rep.RelayerExecReporter(t)
req.NoError(r.CreateChannel(ctx, eRep, pathName, ibc.CreateChannelOptions{
SourcePortName: "transfer",
DestPortName: "transfer",
Order: ibc.Unordered,
Version: "ics20-1",
}))
// Now validate that the channels correctly report as created.
// GetChannels takes around two seconds with rly,
// so getting the channels concurrently is a measurable speedup.
eg, egCtx := errgroup.WithContext(ctx)
var channels0, channels1 []ibc.ChannelOutput
eg.Go(func() error {
var err error
channels0, err = r.GetChannels(egCtx, eRep, c0.Config().ChainID)
return err
})
eg.Go(func() error {
var err error
channels1, err = r.GetChannels(egCtx, eRep, c1.Config().ChainID)
return err
})
req.NoError(eg.Wait(), "failure retrieving channels")
req.Len(channels0, 1)
ch0 := channels0[0]
req.Len(channels1, 1)
ch1 := channels1[0]
// Piecemeal assertions against each channel.
// Not asserting against ConnectionHops or ChannelID.
req.Subset([]string{"STATE_OPEN", "Open"}, []string{ch0.State})
req.Subset([]string{"ORDER_UNORDERED", "Unordered"}, []string{ch0.Ordering})
req.Equal(ibc.ChannelCounterparty{PortID: "transfer", ChannelID: ch1.ChannelID}, ch0.Counterparty)
req.Equal("ics20-1", ch0.Version)
req.Equal("transfer", ch0.PortID)
req.Subset([]string{"STATE_OPEN", "Open"}, []string{ch1.State})
req.Subset([]string{"ORDER_UNORDERED", "Unordered"}, []string{ch1.Ordering})
req.Equal(ibc.ChannelCounterparty{PortID: "transfer", ChannelID: ch0.ChannelID}, ch1.Counterparty)
req.Equal("ics20-1", ch1.Version)
req.Equal("transfer", ch1.PortID)
})
}