-
Notifications
You must be signed in to change notification settings - Fork 181
/
solomachine.go
342 lines (286 loc) · 12 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
package ibctesting
import (
"github.com/okex/exchain/libs/cosmos-sdk/codec"
codectypes "github.com/okex/exchain/libs/cosmos-sdk/codec/types"
"github.com/okex/exchain/libs/cosmos-sdk/types/tx/signing"
ibc_tx "github.com/okex/exchain/libs/cosmos-sdk/x/auth/ibc-tx"
clienttypes "github.com/okex/exchain/libs/ibc-go/modules/core/02-client/types"
commitmenttypes "github.com/okex/exchain/libs/ibc-go/modules/core/23-commitment/types"
host "github.com/okex/exchain/libs/ibc-go/modules/core/24-host"
"github.com/okex/exchain/libs/ibc-go/modules/core/exported"
solomachinetypes "github.com/okex/exchain/libs/ibc-go/modules/light-clients/06-solomachine/types"
"github.com/okex/exchain/libs/tendermint/crypto"
"github.com/okex/exchain/libs/tendermint/crypto/secp256k1"
"github.com/stretchr/testify/require"
"testing"
)
// import (
// "testing"
// "github.com/stretchr/testify/require"
// "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"
// "github.com/cosmos/cosmos-sdk/types/tx/signing"
// clienttypes "github.com/okex/exchain/libs/ibc-go/modules/core/02-client/types"
// commitmenttypes "github.com/okex/exchain/libs/ibc-go/modules/core/23-commitment/types"
// host "github.com/okex/exchain/libs/ibc-go/modules/core/24-host"
// "github.com/okex/exchain/libs/ibc-go/modules/core/exported"
// solomachinetypes "github.com/okex/exchain/libs/ibc-go/modules/light-clients/06-solomachine/types"
// )
// Solomachine is a testing helper used to simulate a counterparty
// solo machine client.
type Solomachine struct {
t *testing.T
cdc *codec.CodecProxy
ClientID string
PrivateKeys []crypto.PrivKey // keys used for signing
PublicKeys []crypto.PubKey // keys used for generating solo machine pub key
PublicKey crypto.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.CodecProxy, clientID, diversifier string, nKeys uint64) *Solomachine {
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) ([]crypto.PrivKey, []crypto.PubKey, crypto.PubKey) {
require.NotEqual(t, uint64(0), n, "generation of zero keys is not allowed")
// privKeys := make([]cryptotypes.PrivKey, n)
privKeys := make([]crypto.PrivKey, n)
pubKeys := make([]crypto.PubKey, n)
for i := uint64(0); i < n; i++ {
privKeys[i] = secp256k1.GenPrivKey()
pubKeys[i] = privKeys[i].PubKey()
}
// var pk cryptotypes.PubKey
var pk crypto.PubKey
if len(privKeys) > 1 {
// generate multi sig pk
//pk = kmultisig.NewLegacyAminoPubKey(int(n), pubKeys)
//todo Ywmet
panic("donot surport multi sig")
} else {
pk = privKeys[0].PubKey()
}
return privKeys, pubKeys, pk
}
// ClientState returns a new solo machine ClientState instance. Default usage does not allow update
// after governance proposal
func (solo *Solomachine) ClientState() *solomachinetypes.ClientState {
return solomachinetypes.NewClientState(solo.Sequence, solo.ConsensusState(), false)
}
// ConsensusState returns a new solo machine ConsensusState instance
func (solo *Solomachine) ConsensusState() *solomachinetypes.ConsensusState {
pbPk := ibc_tx.LagacyKey2PbKey(solo.PublicKey)
publicKey, err := codectypes.NewAnyWithValue(pbPk)
require.NoError(solo.t, err)
return &solomachinetypes.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)
}
// CreateHeader generates a new private/public key pair and creates the
// necessary signature to construct a valid solo machine header.
func (solo *Solomachine) CreateHeader() *solomachinetypes.Header {
// generate new private keys and signature for header
newPrivKeys, newPubKeys, newPubKey := GenerateKeys(solo.t, uint64(len(solo.PrivateKeys)))
newPbPubKey := ibc_tx.LagacyKey2PbKey(newPubKey)
publicKey, err := codectypes.NewAnyWithValue(newPbPubKey)
require.NoError(solo.t, err)
data := &solomachinetypes.HeaderData{
NewPubKey: publicKey,
NewDiversifier: solo.Diversifier,
}
dataBz, err := solo.cdc.GetProtocMarshal().MarshalBinaryBare(data)
require.NoError(solo.t, err)
signBytes := &solomachinetypes.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
DataType: solomachinetypes.HEADER,
Data: dataBz,
}
bz, err := solo.cdc.GetProtocMarshal().MarshalBinaryBare(signBytes)
require.NoError(solo.t, err)
sig := solo.GenerateSignature(bz)
header := &solomachinetypes.Header{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Signature: sig,
NewPublicKey: publicKey,
NewDiversifier: solo.Diversifier,
}
// assumes successful header update
solo.Sequence++
solo.PrivateKeys = newPrivKeys
solo.PublicKeys = newPubKeys
solo.PublicKey = newPubKey
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() *solomachinetypes.Misbehaviour {
path := solo.GetClientStatePath("counterparty")
dataOne, err := solomachinetypes.ClientStateDataBytes(solo.cdc, path, solo.ClientState())
require.NoError(solo.t, err)
path = solo.GetConsensusStatePath("counterparty", clienttypes.NewHeight(0, 1))
dataTwo, err := solomachinetypes.ConsensusStateDataBytes(solo.cdc, path, solo.ConsensusState())
require.NoError(solo.t, err)
signBytes := &solomachinetypes.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
DataType: solomachinetypes.CLIENT,
Data: dataOne,
}
bz, err := solo.cdc.GetProtocMarshal().MarshalBinaryBare(signBytes)
require.NoError(solo.t, err)
sig := solo.GenerateSignature(bz)
signatureOne := solomachinetypes.SignatureAndData{
Signature: sig,
DataType: solomachinetypes.CLIENT,
Data: dataOne,
Timestamp: solo.Time,
}
// misbehaviour signaturess can have different timestamps
solo.Time++
signBytes = &solomachinetypes.SignBytes{
Sequence: solo.Sequence,
Timestamp: solo.Time,
Diversifier: solo.Diversifier,
DataType: solomachinetypes.CONSENSUS,
Data: dataTwo,
}
bz, err = solo.cdc.GetCdc().MarshalBinaryBare(signBytes)
require.NoError(solo.t, err)
sig = solo.GenerateSignature(bz)
signatureTwo := solomachinetypes.SignatureAndData{
Signature: sig,
DataType: solomachinetypes.CONSENSUS,
Data: dataTwo,
Timestamp: solo.Time,
}
return &solomachinetypes.Misbehaviour{
ClientId: solo.ClientID,
Sequence: solo.Sequence,
SignatureOne: &signatureOne,
SignatureTwo: &signatureTwo,
}
}
// 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
//todo Ywmet
panic("donot surport multi sigs")
//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.GetProtocMarshal().MarshalInterface(protoSigData)
require.NoError(solo.t, err)
return bz
}
// 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) commitmenttypes.MerklePath {
commitmentPath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, solo.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) commitmenttypes.MerklePath {
ackPath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, solo.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) commitmenttypes.MerklePath {
receiptPath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, solo.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
}