-
Notifications
You must be signed in to change notification settings - Fork 9
/
coordinator.go
556 lines (468 loc) · 18.7 KB
/
coordinator.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package testing
import (
"fmt"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/core/04-channel/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
ibctesting "github.com/cosmos/cosmos-sdk/x/ibc/testing"
"github.com/datachainlab/fabric-ibc/example"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)
var (
ChainIDPrefix = "testchain"
globalStartTime = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
timeIncrement = time.Second * 5
)
type Coordinator struct {
t *testing.T
Chains map[string]TestChainI
}
func NewCoordinator(t *testing.T, n int, mspID string, txSignMode TxSignMode) *Coordinator {
chains := make(map[string]TestChainI)
for i := 0; i < n; i++ {
chainID := ibctesting.GetChainID(i)
chains[chainID] = NewTestFabricChain(t, chainID, mspID, txSignMode)
}
return &Coordinator{
t: t,
Chains: chains,
}
}
// Setup constructs a TM client, connection, and channel on both chains provided. It will
// fail if any error occurs. The clientID's, TestConnections, and TestChannels are returned
// for both chains. The channels created are connected to the ibc-transfer application.
func (coord *Coordinator) Setup(
chainA, chainB TestChainI, order channeltypes.Order,
) (string, string, *ibctesting.TestConnection, *ibctesting.TestConnection, ibctesting.TestChannel, ibctesting.TestChannel) {
clientA, clientB, connA, connB := coord.SetupClientConnections(chainA, chainB, Fabric)
// channels can also be referenced through the returned connections
channelA, channelB := coord.CreateMockChannels(chainA, chainB, connA, connB, order)
return clientA, clientB, connA, connB, channelA, channelB
}
// SetupClientConnections is a helper function to create clients and the appropriate
// connections on both the source and counterparty chain. It assumes the caller does not
// anticipate any errors.
func (coord *Coordinator) SetupClientConnections(
chainA, chainB TestChainI,
clientType string,
) (string, string, *ibctesting.TestConnection, *ibctesting.TestConnection) {
clientA, clientB := coord.SetupClients(chainA, chainB, clientType)
connA, connB := coord.CreateConnection(chainA, chainB, clientA, clientB)
return clientA, clientB, connA, connB
}
// SetupClients is a helper function to create clients on both chains. It assumes the
// caller does not anticipate any errors.
func (coord *Coordinator) SetupClients(
chainA, chainB TestChainI,
clientType string,
) (string, string) {
clientA, err := coord.CreateClient(chainA, chainB, clientType)
require.NoError(coord.t, err)
clientB, err := coord.CreateClient(chainB, chainA, clientType)
require.NoError(coord.t, err)
return clientA, clientB
}
// CreateClient creates a counterparty client on the source chain and returns the clientID.
func (coord *Coordinator) CreateClient(
source, counterparty TestChainI,
clientType string,
) (clientID string, err error) {
coord.CommitBlock(source, counterparty)
clientID = source.NewClientID(counterparty.GetChainID())
switch clientType {
// case Tendermint:
// err = source.(*ibctesting.TestChain).CreateTMClient(counterparty, clientID)
// case SoloMachine:
// err = source.(*TestSolomachineChain).CreateClient(counterparty, clientID)
case Fabric:
err = source.(*TestChain).CreateClient(counterparty, clientID)
default:
err = fmt.Errorf("client type %s is not supported", clientType)
}
if err != nil {
return "", err
}
coord.IncrementTime()
return clientID, nil
}
// UpdateClient updates a counterparty client on the source chain.
func (coord *Coordinator) UpdateClient(
source, counterparty TestChainI,
clientID string,
clientType string,
) (err error) {
coord.CommitBlock(source, counterparty)
switch clientType {
// case Tendermint:
// err = source.UpdateTMClient(counterparty, clientID)
// case SoloMachine:
// err = source.(*TestSolomachineChain).UpdateClient(counterparty, clientID)
case Fabric:
err = source.(*TestChain).UpdateClient(counterparty, clientID)
default:
err = fmt.Errorf("client type %s is not supported", clientType)
}
if err != nil {
return err
}
coord.IncrementTime()
return nil
}
// CreateConnection constructs and executes connection handshake messages in order to create
// OPEN channels on chainA and chainB. The connection information of for chainA and chainB
// are returned within a TestConnection struct. The function expects the connections to be
// successfully opened otherwise testing will fail.
func (coord *Coordinator) CreateConnection(
chainA, chainB TestChainI,
clientA, clientB string,
) (*ibctesting.TestConnection, *ibctesting.TestConnection) {
connA, connB, err := coord.ConnOpenInit(chainA, chainB, clientA, clientB)
require.NoError(coord.t, err)
err = coord.ConnOpenTry(chainB, chainA, connB, connA)
require.NoError(coord.t, err)
err = coord.ConnOpenAck(chainA, chainB, connA, connB)
require.NoError(coord.t, err)
err = coord.ConnOpenConfirm(chainB, chainA, connB, connA)
require.NoError(coord.t, err)
return connA, connB
}
// CreateTransferChannels constructs and executes channel handshake messages to create OPEN
// ibc-transfer channels on chainA and chainB. The function expects the channels to be
// successfully opened otherwise testing will fail.
func (coord *Coordinator) CreateTransferChannels(
chainA, chainB TestChainI,
connA, connB *ibctesting.TestConnection,
order channeltypes.Order,
) (ibctesting.TestChannel, ibctesting.TestChannel) {
return coord.CreateChannel(chainA, chainB, connA, connB, TransferPort, TransferPort, order)
}
// CreateChannel constructs and executes channel handshake messages in order to create
// OPEN channels on chainA and chainB. The function expects the channels to be successfully
// opened otherwise testing will fail.
func (coord *Coordinator) CreateChannel(
chainA, chainB TestChainI,
connA, connB *ibctesting.TestConnection,
sourcePortID, counterpartyPortID string,
order channeltypes.Order,
) (ibctesting.TestChannel, ibctesting.TestChannel) {
channelA, channelB, err := coord.ChanOpenInit(chainA, chainB, connA, connB, sourcePortID, counterpartyPortID, order)
require.NoError(coord.t, err)
err = coord.ChanOpenTry(chainB, chainA, channelB, channelA, connB, order)
require.NoError(coord.t, err)
err = coord.ChanOpenAck(chainA, chainB, channelA, channelB)
require.NoError(coord.t, err)
err = coord.ChanOpenConfirm(chainB, chainA, channelB, channelA)
require.NoError(coord.t, err)
return channelA, channelB
}
// CreateMockChannels constructs and executes channel handshake messages to create OPEN
// channels that use a mock application module that returns nil on all callbacks. This
// function is expects the channels to be successfully opened otherwise testing will
// fail.
func (coord *Coordinator) CreateMockChannels(
chainA, chainB TestChainI,
connA, connB *ibctesting.TestConnection,
order channeltypes.Order,
) (ibctesting.TestChannel, ibctesting.TestChannel) {
return coord.CreateChannel(chainA, chainB, connA, connB, MockPort, MockPort, order)
}
// ConnOpenInit initializes a connection on the source chain with the state INIT
// using the OpenInit handshake call.
//
// NOTE: The counterparty testing connection will be created even if it is not created in the
// application state.
func (coord *Coordinator) ConnOpenInit(
source, counterparty TestChainI,
clientID, counterpartyClientID string,
) (*ibctesting.TestConnection, *ibctesting.TestConnection, error) {
sourceConnection := source.AddTestConnection(clientID, counterpartyClientID)
counterpartyConnection := counterparty.AddTestConnection(counterpartyClientID, clientID)
// initialize connection on source
if err := source.ConnectionOpenInit(counterparty, sourceConnection, counterpartyConnection); err != nil {
return sourceConnection, counterpartyConnection, err
}
coord.IncrementTime()
// update source client on counterparty connection
if err := coord.UpdateClient(
counterparty, source,
counterpartyClientID, counterparty.Type(),
); err != nil {
return sourceConnection, counterpartyConnection, err
}
return sourceConnection, counterpartyConnection, nil
}
// ConnOpenTry initializes a connection on the source chain with the state TRYOPEN
// using the OpenTry handshake call.
func (coord *Coordinator) ConnOpenTry(
source, counterparty TestChainI,
sourceConnection, counterpartyConnection *ibctesting.TestConnection,
) error {
// initialize TRYOPEN connection on source
if err := source.ConnectionOpenTry(counterparty, sourceConnection, counterpartyConnection); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
counterpartyConnection.ClientID, counterparty.Type(),
)
}
// ConnOpenAck initializes a connection on the source chain with the state OPEN
// using the OpenAck handshake call.
func (coord *Coordinator) ConnOpenAck(
source, counterparty TestChainI,
sourceConnection, counterpartyConnection *ibctesting.TestConnection,
) error {
// set OPEN connection on source using OpenAck
if err := source.ConnectionOpenAck(counterparty, sourceConnection, counterpartyConnection); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
counterpartyConnection.ClientID, counterparty.Type(),
)
}
// ConnOpenConfirm initializes a connection on the source chain with the state OPEN
// using the OpenConfirm handshake call.
func (coord *Coordinator) ConnOpenConfirm(
source, counterparty TestChainI,
sourceConnection, counterpartyConnection *ibctesting.TestConnection,
) error {
if err := source.ConnectionOpenConfirm(counterparty, sourceConnection, counterpartyConnection); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
counterpartyConnection.ClientID, counterparty.Type(),
)
}
// ChanOpenInit initializes a channel on the source chain with the state INIT
// using the OpenInit handshake call.
//
// NOTE: The counterparty testing channel will be created even if it is not created in the
// application state.
func (coord *Coordinator) ChanOpenInit(
source, counterparty TestChainI,
connection, counterpartyConnection *ibctesting.TestConnection,
sourcePortID, counterpartyPortID string,
order channeltypes.Order,
) (ibctesting.TestChannel, ibctesting.TestChannel, error) {
sourceChannel := connection.AddTestChannel(sourcePortID)
counterpartyChannel := counterpartyConnection.AddTestChannel(counterpartyPortID)
// NOTE: only creation of a capability for a transfer or mock port is supported
// Other applications must bind to the port in InitGenesis or modify this code.
source.CreatePortCapability(sourceChannel.PortID)
coord.IncrementTime()
// initialize channel on source
if err := source.ChanOpenInit(sourceChannel, counterpartyChannel, order, connection.ID); err != nil {
return sourceChannel, counterpartyChannel, err
}
coord.IncrementTime()
// update source client on counterparty connection
if err := coord.UpdateClient(
counterparty, source,
counterpartyConnection.ClientID, counterparty.Type(),
); err != nil {
return sourceChannel, counterpartyChannel, err
}
return sourceChannel, counterpartyChannel, nil
}
// ChanOpenTry initializes a channel on the source chain with the state TRYOPEN
// using the OpenTry handshake call.
func (coord *Coordinator) ChanOpenTry(
source, counterparty TestChainI,
sourceChannel, counterpartyChannel ibctesting.TestChannel,
connection *ibctesting.TestConnection,
order channeltypes.Order,
) error {
// create a capability
source.CreatePortCapability(sourceChannel.PortID)
// initialize channel on source
if err := source.ChanOpenTry(counterparty, sourceChannel, counterpartyChannel, order, connection.ID); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
connection.CounterpartyClientID, counterparty.Type(),
)
}
// ChanOpenAck initializes a channel on the source chain with the state OPEN
// using the OpenAck handshake call.
func (coord *Coordinator) ChanOpenAck(
source, counterparty TestChainI,
sourceChannel, counterpartyChannel ibctesting.TestChannel,
) error {
if err := source.ChanOpenAck(counterparty, sourceChannel, counterpartyChannel); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
sourceChannel.CounterpartyClientID, counterparty.Type(),
)
}
// ChanOpenConfirm initializes a channel on the source chain with the state OPEN
// using the OpenConfirm handshake call.
func (coord *Coordinator) ChanOpenConfirm(
source, counterparty TestChainI,
sourceChannel, counterpartyChannel ibctesting.TestChannel,
) error {
if err := source.ChanOpenConfirm(counterparty, sourceChannel, counterpartyChannel); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
sourceChannel.CounterpartyClientID, counterparty.Type(),
)
}
// ChanCloseInit closes a channel on the source chain resulting in the channels state
// being set to CLOSED.
//
// NOTE: does not work with ibc-transfer module
func (coord *Coordinator) ChanCloseInit(
source, counterparty TestChainI,
channel ibctesting.TestChannel,
) error {
if err := source.ChanCloseInit(counterparty, channel); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
channel.CounterpartyClientID, counterparty.Type(),
)
}
// SetChannelClosed sets a channel state to CLOSED.
func (coord *Coordinator) SetChannelClosed(
source, counterparty TestChainI,
testChannel ibctesting.TestChannel,
) error {
channel := source.GetChannel(testChannel)
channel.State = channeltypes.CLOSED
switch source.Type() {
case Tendermint:
source.GetApp().(*simapp.SimApp).IBCKeeper.ChannelKeeper.SetChannel(source.GetContext(), testChannel.PortID, testChannel.ID, channel)
case Fabric:
source.GetApp().(*example.IBCApp).IBCKeeper.ChannelKeeper.SetChannel(source.GetContext(), testChannel.PortID, testChannel.ID, channel)
}
coord.CommitBlock(source)
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
testChannel.CounterpartyClientID, counterparty.Type(),
)
}
// RelayPacket receives a channel packet on counterparty, queries the ack
// and acknowledges the packet on source. The clients are updated as needed.
func (coord *Coordinator) RelayPacket(
source, counterparty TestChainI,
sourceClient, counterpartyClient string,
packet channeltypes.Packet, ack []byte,
) error {
if err := coord.RecvPacket(source, counterparty, sourceClient, packet); err != nil {
return err
}
return coord.AcknowledgePacket(source, counterparty, counterpartyClient, packet, ack)
}
// RecvPacket receives a channel packet on the counterparty chain and updates
// the client on the source chain representing the counterparty.
func (coord *Coordinator) RecvPacket(
source, counterparty TestChainI,
sourceClient string,
packet channeltypes.Packet,
) error {
// get proof of packet commitment on source
packetKey := host.KeyPacketCommitment(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
proof, proofHeight := source.QueryProof(packetKey)
recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, counterparty.GetSenderAccount().GetAddress())
// receive on counterparty and update source client
return coord.SendMsgs(counterparty, source, sourceClient, []sdk.Msg{recvMsg})
}
// AcknowledgePacket acknowledges on the source chain the packet received on
// the counterparty chain and updates the client on the counterparty representing
// the source chain.
// TODO: add a query for the acknowledgement by events
// - https://github.com/cosmos/cosmos-sdk/issues/6509
func (coord *Coordinator) AcknowledgePacket(
source, counterparty TestChainI,
counterpartyClient string,
packet channeltypes.Packet, ack []byte,
) error {
// get proof of acknowledgement on counterparty
packetKey := host.KeyPacketAcknowledgement(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
proof, proofHeight := counterparty.QueryProof(packetKey)
ackMsg := channeltypes.NewMsgAcknowledgement(packet, ack, proof, proofHeight, source.GetSenderAccount().GetAddress())
return coord.SendMsgs(source, counterparty, counterpartyClient, []sdk.Msg{ackMsg})
}
// CommitBlock commits a block on the provided indexes and then increments the global time.
//
// CONTRACT: the passed in list of indexes must not contain duplicates
func (coord *Coordinator) CommitBlock(chains ...TestChainI) {
for _, chain := range chains {
switch chain.Type() {
case Tendermint:
chain.GetApp().(*simapp.SimApp).Commit()
chain.NextBlock()
case SoloMachine:
}
}
coord.IncrementTime()
}
// IncrementTime iterates through all the TestChain's and increments their current header time
// by 5 seconds.
//
// CONTRACT: this function must be called after every commit on any TestChain.
func (coord *Coordinator) IncrementTime() {
for _, chain := range coord.Chains {
switch chain := chain.(type) {
case *TestChain: // Fabric
chain.currentTime = chain.currentTime.Add(time.Second)
chain.Stub.GetTxTimestampReturns(×tamppb.Timestamp{Seconds: chain.currentTime.Unix()}, nil)
// case *ibctesting.TestChain: // Tendermint
// chain.CurrentHeader.Time = chain.CurrentHeader.Time.Add(timeIncrement)
// chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader})
default:
panic("not implemented error")
}
}
}
// GetChain returns the TestChain using the given chainID and returns an error if it does
// not exist.
func (coord *Coordinator) GetChain(chainID string) TestChainI {
chain, found := coord.Chains[chainID]
require.True(coord.t, found, fmt.Sprintf("%s chain does not exist", chainID))
return chain
}
// SendMsg delivers a single provided message to the chain. The counterparty
// client is update with the new source consensus state.
func (coord *Coordinator) SendMsg(source, counterparty TestChainI, counterpartyClientID string, msg sdk.Msg) error {
return coord.SendMsgs(source, counterparty, counterpartyClientID, []sdk.Msg{msg})
}
// SendMsgs delivers the provided messages to the chain. The counterparty
// client is updated with the new source consensus state.
func (coord *Coordinator) SendMsgs(source, counterparty TestChainI, counterpartyClientID string, msgs []sdk.Msg) error {
if _, err := source.SendMsgs(msgs...); err != nil {
return err
}
coord.IncrementTime()
// update source client on counterparty connection
return coord.UpdateClient(
counterparty, source,
counterpartyClientID, counterparty.Type(),
)
}