-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathsolomachine.go
742 lines (614 loc) · 26.8 KB
/
solomachine.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
package ibctesting
import (
"testing"
"time"
"github.com/stretchr/testify/require"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
solomachine "github.com/cosmos/ibc-go/v7/modules/light-clients/06-solomachine"
ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
)
var (
clientIDSolomachine = "client-on-solomachine" // clientID generated on solo machine side
connectionIDSolomachine = "connection-on-solomachine" // connectionID generated on solo machine side
channelIDSolomachine = "channel-on-solomachine" // channelID generated on solo machine side
)
// DefaultSolomachineClientID is the default solo machine client id used for testing
var DefaultSolomachineClientID = "06-solomachine-0"
// Solomachine is a testing helper used to simulate a counterparty
// solo machine client.
type Solomachine struct {
t *testing.T
cdc codec.BinaryCodec
ClientID string
PrivateKeys []cryptotypes.PrivKey // keys used for signing
PublicKeys []cryptotypes.PubKey // keys used for generating solo machine pub key
PublicKey cryptotypes.PubKey // key used for verification
Sequence uint64
Time uint64
Diversifier string
}
// NewSolomachine returns a new solomachine instance with an `nKeys` amount of
// generated private/public key pairs and a sequence starting at 1. If nKeys
// is greater than 1 then a multisig public key is used.
func NewSolomachine(t *testing.T, cdc codec.BinaryCodec, clientID, diversifier string, nKeys uint64) *Solomachine {
t.Helper()
privKeys, pubKeys, pk := GenerateKeys(t, nKeys)
return &Solomachine{
t: t,
cdc: cdc,
ClientID: clientID,
PrivateKeys: privKeys,
PublicKeys: pubKeys,
PublicKey: pk,
Sequence: 1,
Time: 10,
Diversifier: diversifier,
}
}
// GenerateKeys generates a new set of secp256k1 private keys and public keys.
// If the number of keys is greater than one then the public key returned represents
// a multisig public key. The private keys are used for signing, the public
// keys are used for generating the public key and the public key is used for
// solo machine verification. The usage of secp256k1 is entirely arbitrary.
// The key type can be swapped for any key type supported by the PublicKey
// interface, if needed. The same is true for the amino based Multisignature
// public key.
func GenerateKeys(t *testing.T, n uint64) ([]cryptotypes.PrivKey, []cryptotypes.PubKey, cryptotypes.PubKey) {
t.Helper()
require.NotEqual(t, uint64(0), n, "generation of zero keys is not allowed")
privKeys := make([]cryptotypes.PrivKey, n)
pubKeys := make([]cryptotypes.PubKey, n)
for i := uint64(0); i < n; i++ {
privKeys[i] = secp256k1.GenPrivKey()
pubKeys[i] = privKeys[i].PubKey()
}
var pk cryptotypes.PubKey
if len(privKeys) > 1 {
// generate multi sig pk
pk = kmultisig.NewLegacyAminoPubKey(int(n), pubKeys)
} else {
pk = privKeys[0].PubKey()
}
return privKeys, pubKeys, pk
}
// ClientState returns a new solo machine ClientState instance.
func (solo *Solomachine) ClientState() *solomachine.ClientState {
return solomachine.NewClientState(solo.Sequence, solo.ConsensusState())
}
// ConsensusState returns a new solo machine ConsensusState instance
func (solo *Solomachine) ConsensusState() *solomachine.ConsensusState {
publicKey, err := codectypes.NewAnyWithValue(solo.PublicKey)
require.NoError(solo.t, err)
return &solomachine.ConsensusState{
PublicKey: publicKey,
Diversifier: solo.Diversifier,
Timestamp: solo.Time,
}
}
// GetHeight returns an exported.Height with Sequence as RevisionHeight
func (solo *Solomachine) GetHeight() exported.Height {
return clienttypes.NewHeight(0, solo.Sequence)
}
// CreateClient creates an on-chain client on the provided chain.
func (solo *Solomachine) CreateClient(chain *TestChain) string {
msgCreateClient, err := clienttypes.NewMsgCreateClient(solo.ClientState(), solo.ConsensusState(), chain.SenderAccount.GetAddress().String())
require.NoError(solo.t, err)
res, err := chain.SendMsgs(msgCreateClient)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
clientID, err := ParseClientIDFromEvents(res.Events)
require.NoError(solo.t, err)
return clientID
}
// UpdateClient sends a MsgUpdateClient to the provided chain and updates the given clientID.
func (solo *Solomachine) UpdateClient(chain *TestChain, clientID string) {
smHeader := solo.CreateHeader(solo.Diversifier)
msgUpdateClient, err := clienttypes.NewMsgUpdateClient(clientID, smHeader, chain.SenderAccount.GetAddress().String())
require.NoError(solo.t, err)
res, err := chain.SendMsgs(msgUpdateClient)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// CreateHeader generates a new private/public key pair and creates the
// necessary signature to construct a valid solo machine header.
// A new diversifier will be used as well
func (solo *Solomachine) CreateHeader(newDiversifier string) *solomachine.Header {
// generate new private keys and signature for header
newPrivKeys, newPubKeys, newPubKey := GenerateKeys(solo.t, uint64(len(solo.PrivateKeys)))
publicKey, err := codectypes.NewAnyWithValue(newPubKey)
require.NoError(solo.t, err)
data := &solomachine.HeaderData{
NewPubKey: publicKey,
NewDiversifier: newDiversifier,
}
dataBz, err := solo.cdc.Marshal(data)
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: []byte(solomachine.SentinelHeaderPath),
Data: dataBz,
}
bz, err := solo.cdc.Marshal(signBytes)
require.NoError(solo.t, err)
sig := solo.GenerateSignature(bz)
header := &solomachine.Header{
Timestamp: solo.Time,
Signature: sig,
NewPublicKey: publicKey,
NewDiversifier: newDiversifier,
}
// assumes successful header update
solo.Sequence++
solo.Time++
solo.PrivateKeys = newPrivKeys
solo.PublicKeys = newPubKeys
solo.PublicKey = newPubKey
solo.Diversifier = newDiversifier
return header
}
// CreateMisbehaviour constructs testing misbehaviour for the solo machine client
// by signing over two different data bytes at the same sequence.
func (solo *Solomachine) CreateMisbehaviour() *solomachine.Misbehaviour {
merklePath := solo.GetClientStatePath("counterparty")
path, err := solo.cdc.Marshal(&merklePath)
require.NoError(solo.t, err)
data, err := solo.cdc.Marshal(solo.ClientState())
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: path,
Data: data,
}
bz, err := solo.cdc.Marshal(signBytes)
require.NoError(solo.t, err)
sig := solo.GenerateSignature(bz)
signatureOne := solomachine.SignatureAndData{
Signature: sig,
Path: path,
Data: data,
Timestamp: solo.Time,
}
// misbehaviour signaturess can have different timestamps
solo.Time++
merklePath = solo.GetConsensusStatePath("counterparty", clienttypes.NewHeight(0, 1))
path, err = solo.cdc.Marshal(&merklePath)
require.NoError(solo.t, err)
data, err = solo.cdc.Marshal(solo.ConsensusState())
require.NoError(solo.t, err)
signBytes = &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: path,
Data: data,
}
bz, err = solo.cdc.Marshal(signBytes)
require.NoError(solo.t, err)
sig = solo.GenerateSignature(bz)
signatureTwo := solomachine.SignatureAndData{
Signature: sig,
Path: path,
Data: data,
Timestamp: solo.Time,
}
return &solomachine.Misbehaviour{
Sequence: solo.Sequence,
SignatureOne: &signatureOne,
SignatureTwo: &signatureTwo,
}
}
// ConnOpenInit initializes a connection on the provided chain given a solo machine clientID.
func (solo *Solomachine) ConnOpenInit(chain *TestChain, clientID string) string {
msgConnOpenInit := connectiontypes.NewMsgConnectionOpenInit(
clientID,
clientIDSolomachine, // clientID generated on solo machine side
chain.GetPrefix(), DefaultOpenInitVersion, DefaultDelayPeriod,
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgConnOpenInit)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
connectionID, err := ParseConnectionIDFromEvents(res.Events)
require.NoError(solo.t, err)
return connectionID
}
// ConnOpenAck performs the connection open ack handshake step on the tendermint chain for the associated
// solo machine client.
func (solo *Solomachine) ConnOpenAck(chain *TestChain, clientID, connectionID string) {
proofTry := solo.GenerateConnOpenTryProof(clientID, connectionID)
clientState := ibctm.NewClientState(chain.ChainID, DefaultTrustLevel, TrustingPeriod, UnbondingPeriod, MaxClockDrift, chain.LastHeader.GetHeight().(clienttypes.Height), commitmenttypes.GetSDKSpecs(), UpgradePath)
proofClient := solo.GenerateClientStateProof(clientState)
consensusState := chain.LastHeader.ConsensusState()
consensusHeight := chain.LastHeader.GetHeight()
proofConsensus := solo.GenerateConsensusStateProof(consensusState, consensusHeight)
msgConnOpenAck := connectiontypes.NewMsgConnectionOpenAck(
connectionID, connectionIDSolomachine, clientState,
proofTry, proofClient, proofConsensus,
clienttypes.ZeroHeight(), clientState.GetLatestHeight().(clienttypes.Height),
ConnectionVersion,
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgConnOpenAck)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// ChanOpenInit initializes a channel on the provided chain given a solo machine connectionID.
func (solo *Solomachine) ChanOpenInit(chain *TestChain, connectionID string) string {
msgChanOpenInit := channeltypes.NewMsgChannelOpenInit(
transfertypes.PortID,
transfertypes.Version,
channeltypes.UNORDERED,
[]string{connectionID},
transfertypes.PortID,
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgChanOpenInit)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
channelID, err := ParseChannelIDFromEvents(res.Events)
require.NoError(solo.t, err)
return channelID
}
// ChanOpenAck performs the channel open ack handshake step on the tendermint chain for the associated
// solo machine client.
func (solo *Solomachine) ChanOpenAck(chain *TestChain, channelID string) {
proofTry := solo.GenerateChanOpenTryProof(transfertypes.PortID, transfertypes.Version, channelID)
msgChanOpenAck := channeltypes.NewMsgChannelOpenAck(
transfertypes.PortID,
channelID,
channelIDSolomachine,
transfertypes.Version,
proofTry,
clienttypes.ZeroHeight(),
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgChanOpenAck)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// ChanCloseConfirm performs the channel close confirm handshake step on the tendermint chain for the associated
// solo machine client.
func (solo *Solomachine) ChanCloseConfirm(chain *TestChain, portID, channelID string) {
proofInit := solo.GenerateChanClosedProof(portID, transfertypes.Version, channelID)
msgChanCloseConfirm := channeltypes.NewMsgChannelCloseConfirm(
portID,
channelID,
proofInit,
clienttypes.ZeroHeight(),
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgChanCloseConfirm)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// SendTransfer constructs a MsgTransfer and sends the message to the given chain. Any number of optional
// functions can be provided which will modify the MsgTransfer before SendMsgs is called.
func (solo *Solomachine) SendTransfer(chain *TestChain, portID, channelID string, fns ...func(*transfertypes.MsgTransfer)) channeltypes.Packet {
msgTransfer := transfertypes.MsgTransfer{
SourcePort: portID,
SourceChannel: channelID,
Token: sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100)),
Sender: chain.SenderAccount.GetAddress().String(),
Receiver: chain.SenderAccount.GetAddress().String(),
TimeoutHeight: clienttypes.ZeroHeight(),
TimeoutTimestamp: uint64(chain.GetContext().BlockTime().Add(time.Hour).UnixNano()),
}
for _, fn := range fns {
fn(&msgTransfer)
}
res, err := chain.SendMsgs(&msgTransfer)
require.NoError(solo.t, err)
packet, err := ParsePacketFromEvents(res.Events)
require.NoError(solo.t, err)
return packet
}
// RecvPacket creates a commitment proof and broadcasts a new MsgRecvPacket.
func (solo *Solomachine) RecvPacket(chain *TestChain, packet channeltypes.Packet) {
proofCommitment := solo.GenerateCommitmentProof(packet)
msgRecvPacket := channeltypes.NewMsgRecvPacket(
packet,
proofCommitment,
clienttypes.ZeroHeight(),
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgRecvPacket)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// AcknowledgePacket creates an acknowledgement proof and broadcasts a MsgAcknowledgement.
func (solo *Solomachine) AcknowledgePacket(chain *TestChain, packet channeltypes.Packet) {
proofAck := solo.GenerateAcknowledgementProof(packet)
transferAck := channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement()
msgAcknowledgement := channeltypes.NewMsgAcknowledgement(
packet, transferAck,
proofAck,
clienttypes.ZeroHeight(),
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgAcknowledgement)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// TimeoutPacket creates a unreceived packet proof and broadcasts a MsgTimeout.
func (solo *Solomachine) TimeoutPacket(chain *TestChain, packet channeltypes.Packet) {
proofUnreceived := solo.GenerateReceiptAbsenceProof(packet)
msgTimeout := channeltypes.NewMsgTimeout(
packet,
1, // nextSequenceRecv is unused for UNORDERED channels
proofUnreceived,
clienttypes.ZeroHeight(),
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgTimeout)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// TimeoutPacket creates a channel closed and unreceived packet proof and broadcasts a MsgTimeoutOnClose.
func (solo *Solomachine) TimeoutPacketOnClose(chain *TestChain, packet channeltypes.Packet, channelID string) {
proofClosed := solo.GenerateChanClosedProof(transfertypes.PortID, transfertypes.Version, channelID)
proofUnreceived := solo.GenerateReceiptAbsenceProof(packet)
msgTimeout := channeltypes.NewMsgTimeoutOnClose(
packet,
1, // nextSequenceRecv is unused for UNORDERED channels
proofUnreceived,
proofClosed,
clienttypes.ZeroHeight(),
chain.SenderAccount.GetAddress().String(),
)
res, err := chain.SendMsgs(msgTimeout)
require.NoError(solo.t, err)
require.NotNil(solo.t, res)
}
// GenerateSignature uses the stored private keys to generate a signature
// over the sign bytes with each key. If the amount of keys is greater than
// 1 then a multisig data type is returned.
func (solo *Solomachine) GenerateSignature(signBytes []byte) []byte {
sigs := make([]signing.SignatureData, len(solo.PrivateKeys))
for i, key := range solo.PrivateKeys {
sig, err := key.Sign(signBytes)
require.NoError(solo.t, err)
sigs[i] = &signing.SingleSignatureData{
Signature: sig,
}
}
var sigData signing.SignatureData
if len(sigs) == 1 {
// single public key
sigData = sigs[0]
} else {
// generate multi signature data
multiSigData := multisig.NewMultisig(len(sigs))
for i, sig := range sigs {
multisig.AddSignature(multiSigData, sig, i)
}
sigData = multiSigData
}
protoSigData := signing.SignatureDataToProto(sigData)
bz, err := solo.cdc.Marshal(protoSigData)
require.NoError(solo.t, err)
return bz
}
// GenerateProof takes in solo machine sign bytes, generates a signature and marshals it as a proof.
// The solo machine sequence is incremented.
func (solo *Solomachine) GenerateProof(signBytes *solomachine.SignBytes) []byte {
bz, err := solo.cdc.Marshal(signBytes)
require.NoError(solo.t, err)
sig := solo.GenerateSignature(bz)
signatureDoc := &solomachine.TimestampedSignatureData{
SignatureData: sig,
Timestamp: solo.Time,
}
proof, err := solo.cdc.Marshal(signatureDoc)
require.NoError(solo.t, err)
solo.Sequence++
return proof
}
// GenerateClientStateProof generates the proof of the client state required for the connection open try and ack handshake steps.
// The client state should be the self client states of the tendermint chain.
func (solo *Solomachine) GenerateClientStateProof(clientState exported.ClientState) []byte {
data, err := clienttypes.MarshalClientState(solo.cdc, clientState)
require.NoError(solo.t, err)
merklePath := solo.GetClientStatePath(clientIDSolomachine)
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: data,
}
return solo.GenerateProof(signBytes)
}
// GenerateConsensusStateProof generates the proof of the consensus state required for the connection open try and ack handshake steps.
// The consensus state should be the self consensus states of the tendermint chain.
func (solo *Solomachine) GenerateConsensusStateProof(consensusState exported.ConsensusState, consensusHeight exported.Height) []byte {
data, err := clienttypes.MarshalConsensusState(solo.cdc, consensusState)
require.NoError(solo.t, err)
merklePath := solo.GetConsensusStatePath(clientIDSolomachine, consensusHeight)
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: data,
}
return solo.GenerateProof(signBytes)
}
// GenerateConnOpenTryProof generates the proofTry required for the connection open ack handshake step.
// The clientID, connectionID provided represent the clientID and connectionID created on the counterparty chain, that is the tendermint chain.
func (solo *Solomachine) GenerateConnOpenTryProof(counterpartyClientID, counterpartyConnectionID string) []byte {
counterparty := connectiontypes.NewCounterparty(counterpartyClientID, counterpartyConnectionID, prefix)
connection := connectiontypes.NewConnectionEnd(connectiontypes.TRYOPEN, clientIDSolomachine, counterparty, []*connectiontypes.Version{ConnectionVersion}, DefaultDelayPeriod)
data, err := solo.cdc.Marshal(&connection)
require.NoError(solo.t, err)
merklePath := solo.GetConnectionStatePath(connectionIDSolomachine)
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: data,
}
return solo.GenerateProof(signBytes)
}
// GenerateChanOpenTryProof generates the proofTry required for the channel open ack handshake step.
// The channelID provided represents the channelID created on the counterparty chain, that is the tendermint chain.
func (solo *Solomachine) GenerateChanOpenTryProof(portID, version, counterpartyChannelID string) []byte {
counterparty := channeltypes.NewCounterparty(portID, counterpartyChannelID)
channel := channeltypes.NewChannel(channeltypes.TRYOPEN, channeltypes.UNORDERED, counterparty, []string{connectionIDSolomachine}, version)
data, err := solo.cdc.Marshal(&channel)
require.NoError(solo.t, err)
merklePath := solo.GetChannelStatePath(portID, channelIDSolomachine)
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: data,
}
return solo.GenerateProof(signBytes)
}
// GenerateChanClosedProof generates a channel closed proof.
// The channelID provided represents the channelID created on the counterparty chain, that is the tendermint chain.
func (solo *Solomachine) GenerateChanClosedProof(portID, version, counterpartyChannelID string) []byte {
counterparty := channeltypes.NewCounterparty(portID, counterpartyChannelID)
channel := channeltypes.NewChannel(channeltypes.CLOSED, channeltypes.UNORDERED, counterparty, []string{connectionIDSolomachine}, version)
data, err := solo.cdc.Marshal(&channel)
require.NoError(solo.t, err)
merklePath := solo.GetChannelStatePath(portID, channelIDSolomachine)
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: data,
}
return solo.GenerateProof(signBytes)
}
// GenerateCommitmentProof generates a commitment proof for the provided packet.
func (solo *Solomachine) GenerateCommitmentProof(packet channeltypes.Packet) []byte {
commitment := channeltypes.CommitPacket(solo.cdc, packet)
merklePath := solo.GetPacketCommitmentPath(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence())
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: commitment,
}
return solo.GenerateProof(signBytes)
}
// GenerateAcknowledgementProof generates an acknowledgement proof.
func (solo *Solomachine) GenerateAcknowledgementProof(packet channeltypes.Packet) []byte {
transferAck := channeltypes.NewResultAcknowledgement([]byte{byte(1)}).Acknowledgement()
merklePath := solo.GetPacketAcknowledgementPath(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: channeltypes.CommitAcknowledgement(transferAck),
}
return solo.GenerateProof(signBytes)
}
// GenerateReceiptAbsenceProof generates a receipt absence proof for the provided packet.
func (solo *Solomachine) GenerateReceiptAbsenceProof(packet channeltypes.Packet) []byte {
merklePath := solo.GetPacketReceiptPath(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence())
key, err := merklePath.GetKey(1) // index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
require.NoError(solo.t, err)
signBytes := &solomachine.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
Path: key,
Data: nil,
}
return solo.GenerateProof(signBytes)
}
// GetClientStatePath returns the commitment path for the client state.
func (solo *Solomachine) GetClientStatePath(counterpartyClientIdentifier string) commitmenttypes.MerklePath {
path, err := commitmenttypes.ApplyPrefix(prefix, commitmenttypes.NewMerklePath(host.FullClientStatePath(counterpartyClientIdentifier)))
require.NoError(solo.t, err)
return path
}
// GetConsensusStatePath returns the commitment path for the consensus state.
func (solo *Solomachine) GetConsensusStatePath(counterpartyClientIdentifier string, consensusHeight exported.Height) commitmenttypes.MerklePath {
path, err := commitmenttypes.ApplyPrefix(prefix, commitmenttypes.NewMerklePath(host.FullConsensusStatePath(counterpartyClientIdentifier, consensusHeight)))
require.NoError(solo.t, err)
return path
}
// GetConnectionStatePath returns the commitment path for the connection state.
func (solo *Solomachine) GetConnectionStatePath(connID string) commitmenttypes.MerklePath {
connectionPath := commitmenttypes.NewMerklePath(host.ConnectionPath(connID))
path, err := commitmenttypes.ApplyPrefix(prefix, connectionPath)
require.NoError(solo.t, err)
return path
}
// GetChannelStatePath returns the commitment path for that channel state.
func (solo *Solomachine) GetChannelStatePath(portID, channelID string) commitmenttypes.MerklePath {
channelPath := commitmenttypes.NewMerklePath(host.ChannelPath(portID, channelID))
path, err := commitmenttypes.ApplyPrefix(prefix, channelPath)
require.NoError(solo.t, err)
return path
}
// GetPacketCommitmentPath returns the commitment path for a packet commitment.
func (solo *Solomachine) GetPacketCommitmentPath(portID, channelID string, sequence uint64) commitmenttypes.MerklePath {
commitmentPath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, sequence))
path, err := commitmenttypes.ApplyPrefix(prefix, commitmentPath)
require.NoError(solo.t, err)
return path
}
// GetPacketAcknowledgementPath returns the commitment path for a packet acknowledgement.
func (solo *Solomachine) GetPacketAcknowledgementPath(portID, channelID string, sequence uint64) commitmenttypes.MerklePath {
ackPath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, sequence))
path, err := commitmenttypes.ApplyPrefix(prefix, ackPath)
require.NoError(solo.t, err)
return path
}
// GetPacketReceiptPath returns the commitment path for a packet receipt
// and an absent receipts.
func (solo *Solomachine) GetPacketReceiptPath(portID, channelID string, sequence uint64) commitmenttypes.MerklePath {
receiptPath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, sequence))
path, err := commitmenttypes.ApplyPrefix(prefix, receiptPath)
require.NoError(solo.t, err)
return path
}
// GetNextSequenceRecvPath returns the commitment path for the next sequence recv counter.
func (solo *Solomachine) GetNextSequenceRecvPath(portID, channelID string) commitmenttypes.MerklePath {
nextSequenceRecvPath := commitmenttypes.NewMerklePath(host.NextSequenceRecvPath(portID, channelID))
path, err := commitmenttypes.ApplyPrefix(prefix, nextSequenceRecvPath)
require.NoError(solo.t, err)
return path
}