/
genesis.go
378 lines (332 loc) · 13.3 KB
/
genesis.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
package types
import (
"bytes"
"fmt"
"time"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/ethereum/go-ethereum/common"
)
// DefaultParamspace defines the default auth module parameter subspace
const (
// todo: implement oracle constants as params
DefaultParamspace = ModuleName
EventVoteRecordPeriod = 24 * time.Hour // TODO: value????
)
var (
// ParamsStoreKeyGravityID stores the gravity id
ParamsStoreKeyGravityID = []byte("GravityID")
// ParamsStoreKeyContractHash stores the contract hash
ParamsStoreKeyContractHash = []byte("ContractHash")
// ParamsStoreKeyBridgeContractAddress stores the contract address
ParamsStoreKeyBridgeContractAddress = []byte("BridgeContractAddress")
// ParamsStoreKeyBridgeContractChainID stores the bridge chain id
ParamsStoreKeyBridgeContractChainID = []byte("BridgeChainID")
// ParamsStoreKeySignedSignerSetTxsWindow stores the signed blocks window
ParamsStoreKeySignedSignerSetTxsWindow = []byte("SignedSignerSetTxWindow")
// ParamsStoreKeySignedBatchesWindow stores the signed blocks window
ParamsStoreKeySignedBatchesWindow = []byte("SignedBatchesWindow")
// ParamsStoreKeyEthereumSignaturesWindow stores the signed blocks window
ParamsStoreKeyEthereumSignaturesWindow = []byte("EthereumSignaturesWindow")
// ParamsStoreKeyTargetEthTxTimeout stores the target ethereum transaction timeout
ParamsStoreKeyTargetEthTxTimeout = []byte("TargetEthTxTimeout")
// ParamsStoreKeyAverageBlockTime stores the signed blocks window
ParamsStoreKeyAverageBlockTime = []byte("AverageBlockTime")
// ParamsStoreKeyAverageEthereumBlockTime stores the signed blocks window
ParamsStoreKeyAverageEthereumBlockTime = []byte("AverageEthereumBlockTime")
// ParamsStoreSlashFractionSignerSetTx stores the slash fraction valset
ParamsStoreSlashFractionSignerSetTx = []byte("SlashFractionSignerSetTx")
// ParamsStoreSlashFractionBatch stores the slash fraction Batch
ParamsStoreSlashFractionBatch = []byte("SlashFractionBatch")
// ParamsStoreSlashFractionEthereumSignature stores the slash fraction ethereum siganture
ParamsStoreSlashFractionEthereumSignature = []byte("SlashFractionEthereumSignature")
// ParamsStoreSlashFractionConflictingEthereumSignature stores the slash fraction ConflictingEthereumSignature
ParamsStoreSlashFractionConflictingEthereumSignature = []byte("SlashFractionConflictingEthereumSignature")
// ParamStoreUnbondSlashingSignerSetTxsWindow stores unbond slashing valset window
ParamStoreUnbondSlashingSignerSetTxsWindow = []byte("UnbondSlashingSignerSetTxsWindow")
// Ensure that params implements the proper interface
_ paramtypes.ParamSet = &Params{}
)
func (gs *GenesisState) UnpackInterfaces(unpacker cdctypes.AnyUnpacker) error {
for _, otx := range gs.OutgoingTxs {
var outgoing OutgoingTx
if err := unpacker.UnpackAny(otx, &outgoing); err != nil {
return err
}
}
for _, sig := range gs.Confirmations {
var signature EthereumTxConfirmation
if err := unpacker.UnpackAny(sig, &signature); err != nil {
return err
}
}
for _, evr := range gs.EthereumEventVoteRecords {
if err := evr.UnpackInterfaces(unpacker); err != nil {
return err
}
}
return nil
}
func EventVoteRecordPowerThreshold(totalPower sdk.Int) sdk.Int {
return sdk.NewInt(66).Mul(totalPower).Quo(sdk.NewInt(100))
}
// ValidateBasic validates genesis state by looping through the params and
// calling their validation functions
func (s GenesisState) ValidateBasic() error {
if err := s.Params.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "params")
}
if len(s.DelegateKeys) != 0 {
for _, delegateKey := range s.DelegateKeys {
if err := delegateKey.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "delegates")
}
}
}
return nil
}
// DefaultGenesisState returns empty genesis state
// TODO: set some better defaults here
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
}
}
// DefaultParams returns a copy of the default params
func DefaultParams() *Params {
return &Params{
GravityId: "defaultgravityid",
BridgeEthereumAddress: "0x0000000000000000000000000000000000000000",
SignedSignerSetTxsWindow: 10000,
SignedBatchesWindow: 10000,
EthereumSignaturesWindow: 10000,
TargetEthTxTimeout: 43200000,
AverageBlockTime: 5000,
AverageEthereumBlockTime: 15000,
SlashFractionSignerSetTx: sdk.NewDec(1).Quo(sdk.NewDec(1000)),
SlashFractionBatch: sdk.NewDec(1).Quo(sdk.NewDec(1000)),
SlashFractionEthereumSignature: sdk.NewDec(1).Quo(sdk.NewDec(1000)),
SlashFractionConflictingEthereumSignature: sdk.NewDec(1).Quo(sdk.NewDec(1000)),
UnbondSlashingSignerSetTxsWindow: 10000,
}
}
// ValidateBasic checks that the parameters have valid values.
func (p Params) ValidateBasic() error {
if err := validateGravityID(p.GravityId); err != nil {
return sdkerrors.Wrap(err, "gravity id")
}
if err := validateContractHash(p.ContractSourceHash); err != nil {
return sdkerrors.Wrap(err, "contract hash")
}
if err := validateBridgeContractAddress(p.BridgeEthereumAddress); err != nil {
return sdkerrors.Wrap(err, "bridge contract address")
}
if err := validateBridgeChainID(p.BridgeChainId); err != nil {
return sdkerrors.Wrap(err, "bridge chain id")
}
if err := validateTargetEthTxTimeout(p.TargetEthTxTimeout); err != nil {
return sdkerrors.Wrap(err, "Batch timeout")
}
if err := validateAverageBlockTime(p.AverageBlockTime); err != nil {
return sdkerrors.Wrap(err, "Block time")
}
if err := validateAverageEthereumBlockTime(p.AverageEthereumBlockTime); err != nil {
return sdkerrors.Wrap(err, "Ethereum block time")
}
if err := validateSignedSignerSetTxsWindow(p.SignedSignerSetTxsWindow); err != nil {
return sdkerrors.Wrap(err, "signed blocks window")
}
if err := validateSignedBatchesWindow(p.SignedBatchesWindow); err != nil {
return sdkerrors.Wrap(err, "signed blocks window")
}
if err := validateEthereumSignaturesWindow(p.EthereumSignaturesWindow); err != nil {
return sdkerrors.Wrap(err, "signed blocks window")
}
if err := validateSlashFractionSignerSetTx(p.SlashFractionSignerSetTx); err != nil {
return sdkerrors.Wrap(err, "slash fraction signersettx")
}
if err := validateSlashFractionBatch(p.SlashFractionBatch); err != nil {
return sdkerrors.Wrap(err, "slash fraction batch tx")
}
if err := validateSlashFractionEthereumSignature(p.SlashFractionEthereumSignature); err != nil {
return sdkerrors.Wrap(err, "slash fraction ethereum signature")
}
if err := validateSlashFractionConflictingEthereumSignature(p.SlashFractionConflictingEthereumSignature); err != nil {
return sdkerrors.Wrap(err, "slash fraction conflicting ethereum signature")
}
if err := validateUnbondSlashingSignerSetTxsWindow(p.UnbondSlashingSignerSetTxsWindow); err != nil {
return sdkerrors.Wrap(err, "unbond slashing signersettx window")
}
return nil
}
// ParamKeyTable for auth module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
// pairs of auth module's parameters.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamsStoreKeyGravityID, &p.GravityId, validateGravityID),
paramtypes.NewParamSetPair(ParamsStoreKeyContractHash, &p.ContractSourceHash, validateContractHash),
paramtypes.NewParamSetPair(ParamsStoreKeyBridgeContractAddress, &p.BridgeEthereumAddress, validateBridgeContractAddress),
paramtypes.NewParamSetPair(ParamsStoreKeyBridgeContractChainID, &p.BridgeChainId, validateBridgeChainID),
paramtypes.NewParamSetPair(ParamsStoreKeySignedSignerSetTxsWindow, &p.SignedSignerSetTxsWindow, validateSignedSignerSetTxsWindow),
paramtypes.NewParamSetPair(ParamsStoreKeySignedBatchesWindow, &p.SignedBatchesWindow, validateSignedBatchesWindow),
paramtypes.NewParamSetPair(ParamsStoreKeyEthereumSignaturesWindow, &p.EthereumSignaturesWindow, validateEthereumSignaturesWindow),
paramtypes.NewParamSetPair(ParamsStoreKeyAverageBlockTime, &p.AverageBlockTime, validateAverageBlockTime),
paramtypes.NewParamSetPair(ParamsStoreKeyTargetEthTxTimeout, &p.TargetEthTxTimeout, validateTargetEthTxTimeout),
paramtypes.NewParamSetPair(ParamsStoreKeyAverageEthereumBlockTime, &p.AverageEthereumBlockTime, validateAverageEthereumBlockTime),
paramtypes.NewParamSetPair(ParamsStoreSlashFractionSignerSetTx, &p.SlashFractionSignerSetTx, validateSlashFractionSignerSetTx),
paramtypes.NewParamSetPair(ParamsStoreSlashFractionBatch, &p.SlashFractionBatch, validateSlashFractionBatch),
paramtypes.NewParamSetPair(ParamsStoreSlashFractionEthereumSignature, &p.SlashFractionEthereumSignature, validateSlashFractionEthereumSignature),
paramtypes.NewParamSetPair(ParamsStoreSlashFractionConflictingEthereumSignature, &p.SlashFractionConflictingEthereumSignature, validateSlashFractionConflictingEthereumSignature),
paramtypes.NewParamSetPair(ParamStoreUnbondSlashingSignerSetTxsWindow, &p.UnbondSlashingSignerSetTxsWindow, validateUnbondSlashingSignerSetTxsWindow),
}
}
// Equal returns a boolean determining if two Params types are identical.
func (p Params) Equal(p2 Params) bool {
pb, err := p.Marshal()
if err != nil {
panic(err)
}
p2b, err := p2.Marshal()
if err != nil {
panic(err)
}
return bytes.Equal(pb, p2b)
}
func validateGravityID(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if _, err := strToFixByteArray(v); err != nil {
return err
}
return nil
}
func validateContractHash(i interface{}) error {
// TODO: should we validate that the input here is a properly formatted
// SHA256 (or other) hash?
if _, ok := i.(string); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateBridgeChainID(i interface{}) error {
if _, ok := i.(uint64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateTargetEthTxTimeout(i interface{}) error {
val, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
} else if val < 60000 {
return fmt.Errorf("invalid target batch timeout, less than 60 seconds is too short")
}
return nil
}
func validateAverageBlockTime(i interface{}) error {
val, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
} else if val < 100 {
return fmt.Errorf("invalid average Cosmos block time, too short for latency limitations")
}
return nil
}
func validateAverageEthereumBlockTime(i interface{}) error {
val, ok := i.(uint64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
} else if val < 100 {
return fmt.Errorf("invalid average Ethereum block time, too short for latency limitations")
}
return nil
}
func validateBridgeContractAddress(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if !common.IsHexAddress(v) {
return fmt.Errorf("not an ethereum address: %s", v)
}
return nil
}
func validateSignedSignerSetTxsWindow(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(uint64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateUnbondSlashingSignerSetTxsWindow(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(uint64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateSlashFractionSignerSetTx(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(sdk.Dec); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateSignedBatchesWindow(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(uint64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateEthereumSignaturesWindow(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(uint64); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateSlashFractionBatch(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(sdk.Dec); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateSlashFractionEthereumSignature(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(sdk.Dec); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func validateSlashFractionConflictingEthereumSignature(i interface{}) error {
// TODO: do we want to set some bounds on this value?
if _, ok := i.(sdk.Dec); !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return nil
}
func strToFixByteArray(s string) ([32]byte, error) {
var out [32]byte
if len([]byte(s)) > 32 {
return out, fmt.Errorf("string too long")
}
copy(out[:], s)
return out, nil
}
func byteArrayToFixByteArray(b []byte) (out [32]byte, err error) {
if len(b) > 32 {
return out, fmt.Errorf("array too long")
}
copy(out[:], b)
return out, nil
}