-
Notifications
You must be signed in to change notification settings - Fork 276
/
valset.go
91 lines (78 loc) · 2.56 KB
/
valset.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
package types
import (
"fmt"
"math/big"
"time"
"cosmossdk.io/errors"
wrapper "github.com/celestiaorg/blobstream-contracts/v3/wrappers/Blobstream.sol"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var _ AttestationRequestI = &Valset{}
// NewValset returns a new valset.
func NewValset(nonce, height uint64, members InternalBridgeValidators, blockTime time.Time) (*Valset, error) {
if err := members.ValidateBasic(); err != nil {
return nil, errors.Wrap(err, "invalid members")
}
members.Sort()
mem := make([]BridgeValidator, 0)
for _, val := range members {
mem = append(mem, val.ToExternal())
}
vs := Valset{Nonce: nonce, Members: mem, Height: height, Time: blockTime}
return &vs, nil
}
// SignBytes produces the bytes that celestia validators are required to sign
// over when the validator set changes.
func (v *Valset) SignBytes() (ethcmn.Hash, error) {
vsHash, err := v.Hash()
if err != nil {
return ethcmn.Hash{}, err
}
// 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, err := InternalBlobstreamABI.Pack(
"domainSeparateValidatorSetHash",
VsDomainSeparator,
big.NewInt(int64(v.Nonce)),
big.NewInt(int64(v.TwoThirdsThreshold())),
vsHash,
)
// this should never happen outside of test since any case that could crash
// on encoding should be filtered above.
if err != nil {
panic(fmt.Sprintf("Error packing checkpoint! %s/n", err))
}
hash := crypto.Keccak256Hash(bytes[4:])
return hash, nil
}
// Hash mimics the 'computeValsetHash' function used by the Blobstream contracts by
// using a Valset to compute the hash of the abi encoded validator set.
func (v *Valset) Hash() (ethcmn.Hash, error) {
ethVals := make([]wrapper.Validator, len(v.Members))
for i, val := range v.Members {
ethVals[i] = wrapper.Validator{
Addr: ethcmn.HexToAddress(val.EvmAddress),
Power: big.NewInt(int64(val.Power)),
}
}
encodedVals, err := InternalBlobstreamABI.Pack("computeValidatorSetHash", ethVals)
if err != nil {
return ethcmn.Hash{}, err
}
return crypto.Keccak256Hash(encodedVals[4:]), nil
}
func (v *Valset) TwoThirdsThreshold() uint64 {
totalPower := uint64(0)
for _, member := range v.Members {
totalPower += member.Power
}
// todo: fix to be more precise
oneThird := (totalPower / 3) + 1 // +1 to round up
return 2 * oneThird
}
func (v *Valset) BlockTime() time.Time {
return v.Time
}