/
validation.go
331 lines (284 loc) · 9.96 KB
/
validation.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
package types
import (
math "math"
"math/big"
"sort"
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/accounts/abi"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/router-protocol/sdk-go/routerchain/util"
)
//////////////////////////////////////
// BRIDGE VALIDATOR(S) //
//////////////////////////////////////
// ToInternal transforms a BridgeValidator into its fully validated internal type
func (b BridgeValidator) ToInternal() (*InternalBridgeValidator, error) {
return NewInternalBridgeValidator(b)
}
// BridgeValidators is the sorted set of validator data for Ethereum bridge MultiSig set
type BridgeValidators []BridgeValidator
func (b BridgeValidators) ToInternal() (*InternalBridgeValidators, error) {
ret := make(InternalBridgeValidators, len(b))
for i := range b {
ibv, err := NewInternalBridgeValidator(b[i])
if err != nil {
return nil, sdkerrors.Wrapf(err, "member %d", i)
}
ret[i] = ibv
}
return &ret, nil
}
// Equal checks that slice contents and order are equal
func (b BridgeValidators) Equal(o BridgeValidators) bool {
if len(b) != len(o) {
return false
}
for i, bv := range b {
ov := o[i]
if bv != ov {
return false
}
}
return true
}
// InternalBridgeValidator is a BridgeValidator but with validated EthereumAddress
type InternalBridgeValidator struct {
Power uint64
EthereumAddress EthAddress
}
func NewInternalBridgeValidator(bridgeValidator BridgeValidator) (*InternalBridgeValidator, error) {
ethAddr, err := NewEthAddress(bridgeValidator.EthereumAddress)
if err != nil {
return nil, sdkerrors.Wrap(err, "invalid bridge validator eth address")
}
i := &InternalBridgeValidator{
Power: bridgeValidator.Power,
EthereumAddress: *ethAddr,
}
if err := i.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(err, "invalid bridge validator")
}
return i, nil
}
func (i InternalBridgeValidator) ValidateBasic() error {
if err := i.EthereumAddress.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "ethereum address")
}
return nil
}
func (i InternalBridgeValidator) ToExternal() BridgeValidator {
return BridgeValidator{
Power: i.Power,
EthereumAddress: i.EthereumAddress.GetAddress().Hex(),
}
}
// InternalBridgeValidators is the sorted set of validator data for Ethereum bridge MultiSig set
type InternalBridgeValidators []*InternalBridgeValidator
func (i InternalBridgeValidators) ToExternal() BridgeValidators {
bridgeValidators := make([]BridgeValidator, len(i))
for b := range bridgeValidators {
bridgeValidators[b] = i[b].ToExternal()
}
return BridgeValidators(bridgeValidators)
}
// Sort sorts the validators by power
func (b InternalBridgeValidators) Sort() {
sort.Slice(b, func(i, j int) bool {
if b[i].Power == b[j].Power {
// Secondary sort on eth address in case powers are equal
return EthAddrLessThan(b[i].EthereumAddress, b[j].EthereumAddress)
}
return b[i].Power > b[j].Power
})
}
// PowerDiff returns the difference in power between two bridge validator sets
// note this is Gravity bridge power *not* Cosmos voting power. Cosmos voting
// power is based on the absolute number of tokens in the staking pool at any given
// time Gravity bridge power is normalized using the equation.
//
// validators cosmos voting power / total cosmos voting power in this block = gravity bridge power / u32_max
//
// As an example if someone has 52% of the Cosmos voting power when a validator set is created their Gravity
// bridge voting power is u32_max * .52
//
// Normalized voting power dramatically reduces how often we have to produce new validator set updates. For example
// if the total on chain voting power increases by 1% due to inflation, we shouldn't have to generate a new validator
// set, after all the validators retained their relative percentages during inflation and normalized Gravity bridge power
// shows no difference.
func (b InternalBridgeValidators) PowerDiff(c InternalBridgeValidators) float64 {
powers := map[string]int64{}
// loop over b and initialize the map with their powers
for _, bv := range b {
powers[bv.EthereumAddress.GetAddress().Hex()] = int64(bv.Power)
}
// subtract c powers from powers in the map, initializing
// uninitialized keys with negative numbers
for _, bv := range c {
if val, ok := powers[bv.EthereumAddress.GetAddress().Hex()]; ok {
powers[bv.EthereumAddress.GetAddress().Hex()] = val - int64(bv.Power)
} else {
powers[bv.EthereumAddress.GetAddress().Hex()] = -int64(bv.Power)
}
}
var delta float64
for _, v := range powers {
// NOTE: we care about the absolute value of the changes
delta += math.Abs(float64(v))
}
return math.Abs(delta / float64(math.MaxUint32))
}
// TotalPower returns the total power in the bridge validator set
func (b InternalBridgeValidators) TotalPower() (out uint64) {
for _, v := range b {
out += v.Power
}
return
}
// HasDuplicates returns true if there are duplicates in the set
func (b InternalBridgeValidators) HasDuplicates() bool {
m := make(map[string]struct{}, len(b))
// creates a hashmap then ensures that the hashmap and the array
// have the same length, this acts as an O(n) duplicates check
for i := range b {
m[b[i].EthereumAddress.GetAddress().Hex()] = struct{}{}
}
return len(m) != len(b)
}
// GetPowers returns only the power values for all members
func (b InternalBridgeValidators) GetPowers() []uint64 {
r := make([]uint64, len(b))
for i := range b {
r[i] = b[i].Power
}
return r
}
// ValidateBasic performs stateless checks
func (b InternalBridgeValidators) ValidateBasic() error {
if len(b) == 0 {
return ErrEmpty
}
for i := range b {
if err := b[i].ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "member %d", i)
}
}
if b.HasDuplicates() {
return sdkerrors.Wrap(ErrDuplicate, "addresses")
}
return nil
}
//////////////////////////////////////
// VALSETS //
//////////////////////////////////////
// NewValset returns a new valset
func NewValset(nonce, height uint64, members InternalBridgeValidators) (*Valset, error) {
if err := members.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(err, "invalid members")
}
members.Sort()
var mem []BridgeValidator
for _, val := range members {
mem = append(mem, val.ToExternal())
}
vs := Valset{Nonce: uint64(nonce), Members: mem, Height: height}
return &vs,
nil
}
// GetCheckpoint returns the checkpoint
func (v Valset) GetCheckpoint(routerIdString string) ([]byte, error) {
// error case here should not occur outside of testing since the above is a constant
contractAbi, abiErr := abi.JSON(strings.NewReader(util.ValsetCheckpointABIJSON))
if abiErr != nil {
return nil, sdkerrors.Wrap(abiErr, "invalid valset checkpoint abi")
}
// the contract argument is not a arbitrary length array but a fixed length 32 byte
// array, therefore we have to utf8 encode the string (the default in this case) and
// then copy the variable length encoded data into a fixed length array. This function
// will panic if routerIDstring is too long to fit in 32 bytes
// routerId, err := util.StrToFixByteArray(routerIdString)
// if err != nil {
// panic(err)
// }
checkpointBytes := []uint8("checkpoint")
var checkpoint [32]uint8
copy(checkpoint[:], checkpointBytes)
memberAddresses := make([]gethcommon.Address, len(v.Members))
convertedPowers := make([]*big.Int, len(v.Members))
for i, m := range v.Members {
memberAddresses[i] = gethcommon.HexToAddress(m.EthereumAddress)
convertedPowers[i] = big.NewInt(int64(m.Power))
}
// the word 'checkpoint' needs to be the same as the 'name' above in the checkpointAbiJson
// but other than that it's a constant that has no impact on the output. This is because
// it gets encoded as a function name which we must then discard.
bytes, packErr := contractAbi.Pack("checkpoint",
checkpoint,
big.NewInt(int64(v.Nonce)),
memberAddresses,
convertedPowers,
)
// this should never happen outside of test since any case that could crash on encoding
// should be filtered above.
if packErr != nil {
return nil, sdkerrors.Wrap(packErr, "Error packing checkpoint!")
}
// we hash the resulting encoded bytes discarding the first 4 bytes these 4 bytes are the constant
// method name 'checkpoint'. If you were to replace the checkpoint constant in this code you would
// then need to adjust how many bytes you truncate off the front to get the output of abi.encode()
hash := crypto.Keccak256Hash(bytes[4:])
return hash.Bytes(), nil
}
// WithoutEmptyMembers returns a new Valset without member that have 0 power or an empty Ethereum address.
func (v *Valset) WithoutEmptyMembers() *Valset {
if v == nil {
return nil
}
r := Valset{
Nonce: v.Nonce,
Members: make([]BridgeValidator, 0, len(v.Members)),
Height: 0,
}
for i := range v.Members {
if _, err := v.Members[i].ToInternal(); err == nil {
r.Members = append(r.Members, v.Members[i])
}
}
return &r
}
// Equal compares all of the valset members, additionally returning an error explaining the problem
func (v Valset) Equal(o Valset) (bool, error) {
if v.Height != o.Height {
return false, sdkerrors.Wrap(ErrInvalid, "valset heights mismatch")
}
if v.Nonce != o.Nonce {
return false, sdkerrors.Wrap(ErrInvalid, "valset nonces mismatch")
}
var bvs BridgeValidators = v.Members
var ovs BridgeValidators = o.Members
if !bvs.Equal(ovs) {
return false, sdkerrors.Wrap(ErrInvalid, "valset members mismatch")
}
return true, nil
}
// Valsets is a collection of valset
type Valsets []Valset
func (v Valsets) Len() int {
return len(v)
}
func (v Valsets) Less(i, j int) bool {
return v[i].Nonce > v[j].Nonce
}
func (v Valsets) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
// This interface is implemented by all the types that are used
// to create transactions on Ethereum that are signed by validators.
// The naming here could be improved.
type EthereumSigned interface {
GetCheckpoint(gravityIDstring string) ([]byte, error)
}
var (
_ EthereumSigned = &Valset{}
)