-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ica_channel_close_test.go
336 lines (275 loc) · 10.6 KB
/
ica_channel_close_test.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package interchaintest_test
import (
"context"
"encoding/json"
"testing"
"time"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
chantypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
relayerinterchaintest "github.com/cosmos/relayer/v2/interchaintest"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
// TestScenarioICAChannelClose is very similar to the TestScenarioInterchainAccounts,
// but instead it tests manually closing the channel using the relayer CLI.
func TestScenarioICAChannelClose(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
client, network := interchaintest.DockerSetup(t)
rep := testreporter.NewNopReporter()
eRep := rep.RelayerExecReporter(t)
ctx := context.Background()
// Get both chains
nf := 0
nv := 1
cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{
{
Name: "icad",
NumValidators: &nv,
NumFullNodes: &nf,
ChainConfig: ibc.ChainConfig{
Images: []ibc.DockerImage{
{Repository: "ghcr.io/cosmos/ibc-go-icad", Version: "v0.5.0", UidGid: "1025:1025"},
},
},
},
{
Name: "icad",
NumValidators: &nv,
NumFullNodes: &nf,
ChainConfig: ibc.ChainConfig{
Images: []ibc.DockerImage{
{Repository: "ghcr.io/cosmos/ibc-go-icad", Version: "v0.5.0", UidGid: "1025:1025"},
},
},
},
})
chains, err := cf.Chains(t.Name())
require.NoError(t, err)
chain1, chain2 := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)
// Get a relayer instance
r := relayerinterchaintest.
NewRelayerFactory(relayerinterchaintest.RelayerConfig{}).
Build(t, client, network)
// Build the network; spin up the chains and configure the relayer
const pathName = "test-path"
const relayerName = "relayer"
ic := interchaintest.NewInterchain().
AddChain(chain1).
AddChain(chain2).
AddRelayer(r, relayerName).
AddLink(interchaintest.InterchainLink{
Chain1: chain1,
Chain2: chain2,
Relayer: r,
Path: pathName,
})
require.NoError(t, ic.Build(ctx, eRep, interchaintest.InterchainBuildOptions{
TestName: t.Name(),
Client: client,
NetworkID: network,
SkipPathCreation: true,
// Uncomment this to load blocks, txs, msgs, and events into sqlite db as test runs
// BlockDatabaseFile: interchaintest.DefaultBlockDatabaseFilepath(),
}))
t.Parallel()
// Fund a user account on chain1 and chain2
initBal := sdkmath.NewInt(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), initBal, chain1, chain2)
chain1User := users[0]
chain2User := users[1]
// Generate a new IBC path
err = r.GeneratePath(ctx, eRep, chain1.Config().ChainID, chain2.Config().ChainID, pathName)
require.NoError(t, err)
// Create new clients
err = r.CreateClients(ctx, eRep, pathName, ibc.CreateClientOptions{TrustingPeriod: "330h"})
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 2, chain1, chain2)
require.NoError(t, err)
// Create a new connection
err = r.CreateConnections(ctx, eRep, pathName)
require.NoError(t, err)
err = testutil.WaitForBlocks(ctx, 2, chain1, chain2)
require.NoError(t, err)
// Query for the newly created connection
connections, err := r.GetConnections(ctx, eRep, chain1.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 1, len(connections))
// Start the relayer and set the cleanup function.
err = r.StartRelayer(ctx, eRep, pathName)
require.NoError(t, err)
t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
t.Logf("an error occurred while stopping the relayer: %s", err)
}
},
)
// Register a new interchain account on chain2, on behalf of the user acc on chain1
chain1Addr := chain1User.(*cosmos.CosmosWallet).FormattedAddressWithPrefix(chain1.Config().Bech32Prefix)
registerICA := []string{
chain1.Config().Bin, "tx", "intertx", "register",
"--from", chain1Addr,
"--connection-id", connections[0].ID,
"--chain-id", chain1.Config().ChainID,
"--home", chain1.HomeDir(),
"--node", chain1.GetRPCAddress(),
"--keyring-backend", keyring.BackendTest,
"-y",
}
_, _, err = chain1.Exec(ctx, registerICA, nil)
require.NoError(t, err)
ir := cosmos.DefaultEncoding().InterfaceRegistry
c2h, err := chain2.Height(ctx)
require.NoError(t, err)
channelFound := func(found *chantypes.MsgChannelOpenConfirm) bool {
return found.PortId == "icahost"
}
// Wait for channel open confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir,
c2h, c2h+30, channelFound)
require.NoError(t, err)
// Query for the newly registered interchain account
queryICA := []string{
chain1.Config().Bin, "query", "intertx", "interchainaccounts", connections[0].ID, chain1Addr,
"--chain-id", chain1.Config().ChainID,
"--home", chain1.HomeDir(),
"--node", chain1.GetRPCAddress(),
}
stdout, _, err := chain1.Exec(ctx, queryICA, nil)
require.NoError(t, err)
icaAddr := parseInterchainAccountField(stdout)
require.NotEmpty(t, icaAddr)
// Get initial account balances
chain2Addr := chain2User.(*cosmos.CosmosWallet).FormattedAddressWithPrefix(chain2.Config().Bech32Prefix)
chain2OrigBal, err := chain2.GetBalance(ctx, chain2Addr, chain2.Config().Denom)
require.NoError(t, err)
icaOrigBal, err := chain2.GetBalance(ctx, icaAddr, chain2.Config().Denom)
require.NoError(t, err)
// Send funds to ICA from user account on chain2
transferAmount := sdkmath.NewInt(10000)
transfer := ibc.WalletAmount{
Address: icaAddr,
Denom: chain2.Config().Denom,
Amount: transferAmount,
}
err = chain2.SendFunds(ctx, chain2User.KeyName(), transfer)
require.NoError(t, err)
chain2Bal, err := chain2.GetBalance(ctx, chain2Addr, chain2.Config().Denom)
require.NoError(t, err)
require.True(t, chain2OrigBal.Sub(transferAmount).Equal(chain2Bal))
icaBal, err := chain2.GetBalance(ctx, icaAddr, chain2.Config().Denom)
require.NoError(t, err)
require.True(t, icaOrigBal.Add(transferAmount).Equal(icaBal))
// Build bank transfer msg
rawMsg, err := json.Marshal(map[string]any{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": icaAddr,
"to_address": chain2Addr,
"amount": []map[string]any{
{
"denom": chain2.Config().Denom,
"amount": transferAmount.String(),
},
},
})
require.NoError(t, err)
// Send bank transfer msg to ICA on chain2 from the user account on chain1
sendICATransfer := []string{
chain1.Config().Bin, "tx", "intertx", "submit", string(rawMsg),
"--connection-id", connections[0].ID,
"--from", chain1Addr,
"--chain-id", chain1.Config().ChainID,
"--home", chain1.HomeDir(),
"--node", chain1.GetRPCAddress(),
"--keyring-backend", keyring.BackendTest,
"-y",
}
_, _, err = chain1.Exec(ctx, sendICATransfer, nil)
require.NoError(t, err)
c1h, err := chain1.Height(ctx)
require.NoError(t, err)
ackFound := func(found *chantypes.MsgAcknowledgement) bool {
return found.Packet.Sequence == 1 &&
found.Packet.SourcePort == "icacontroller-"+chain1Addr &&
found.Packet.DestinationPort == "icahost"
}
// Wait for ack
_, err = cosmos.PollForMessage(ctx, chain1, ir,
c1h, c1h+25, ackFound)
require.NoError(t, err)
// Assert that the funds have been received by the user account on chain2
chain2Bal, err = chain2.GetBalance(ctx, chain2Addr, chain2.Config().Denom)
require.NoError(t, err)
require.True(t, chain2OrigBal.Equal(chain2Bal))
// Assert that the funds have been removed from the ICA on chain2
icaBal, err = chain2.GetBalance(ctx, icaAddr, chain2.Config().Denom)
require.NoError(t, err)
require.True(t, icaOrigBal.Equal(icaBal))
// Stop the relayer and wait for the process to terminate
err = r.StopRelayer(ctx, eRep)
require.NoError(t, err)
// Send another bank transfer msg to ICA on chain2 from the user account on chain1.
// This message should timeout and the channel will be closed when we re-start the relayer.
_, _, err = chain1.Exec(ctx, sendICATransfer, nil)
require.NoError(t, err)
// Wait for approximately one minute to allow packet timeout threshold to be hit
time.Sleep(70 * time.Second)
chain1Chans, err := r.GetChannels(ctx, eRep, chain1.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 1, len(chain1Chans))
// Close the channel using the channel close CLI method
res := r.Exec(ctx, eRep, []string{"tx", "channel-close", pathName, chain1Chans[0].ChannelID, chain1Chans[0].PortID}, nil)
require.NoError(t, res.Err)
require.Zero(t, res.ExitCode)
// Assert that the packet timed out and that the acc balances are correct
chain2Bal, err = chain2.GetBalance(ctx, chain2Addr, chain2.Config().Denom)
require.NoError(t, err)
require.True(t, chain2OrigBal.Equal(chain2Bal))
icaBal, err = chain2.GetBalance(ctx, icaAddr, chain2.Config().Denom)
require.NoError(t, err)
require.True(t, icaOrigBal.Equal(icaBal))
// Assert that the channel ends are both closed
chain1Chans, err = r.GetChannels(ctx, eRep, chain1.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 1, len(chain1Chans))
require.Subset(t, []string{"STATE_CLOSED", "Closed"}, []string{chain1Chans[0].State})
chain2Chans, err := r.GetChannels(ctx, eRep, chain2.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 1, len(chain2Chans))
require.Subset(t, []string{"STATE_CLOSED", "Closed"}, []string{chain2Chans[0].State})
// Restart the relayer for the next channel handshake
err = r.StartRelayer(ctx, eRep, pathName)
require.NoError(t, err)
// Attempt to open another channel for the same ICA
_, _, err = chain1.Exec(ctx, registerICA, nil)
require.NoError(t, err)
c2h, err = chain2.Height(ctx)
require.NoError(t, err)
// Wait for channel open confirm
_, err = cosmos.PollForMessage(ctx, chain2, ir,
c2h, c2h+40, channelFound)
require.NoError(t, err)
// Assert that a new channel has been opened and the same ICA is in use
stdout, _, err = chain1.Exec(ctx, queryICA, nil)
require.NoError(t, err)
newICA := parseInterchainAccountField(stdout)
require.NotEmpty(t, newICA)
require.Equal(t, icaAddr, newICA)
chain1Chans, err = r.GetChannels(ctx, eRep, chain1.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 2, len(chain1Chans))
require.Subset(t, []string{"STATE_OPEN", "Open"}, []string{chain1Chans[1].State})
chain2Chans, err = r.GetChannels(ctx, eRep, chain2.Config().ChainID)
require.NoError(t, err)
require.Equal(t, 2, len(chain2Chans))
require.Subset(t, []string{"STATE_OPEN", "Open"}, []string{chain2Chans[1].State})
}