-
Notifications
You must be signed in to change notification settings - Fork 11
/
validator_set_update.go
115 lines (105 loc) · 3 KB
/
validator_set_update.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
package types
import (
"github.com/dashpay/tenderdash/crypto"
cryptoenc "github.com/dashpay/tenderdash/crypto/encoding"
"github.com/dashpay/tenderdash/dash/llmq"
)
// QuorumOptionFunc is an option function for quorum config
type QuorumOptionFunc func(conf *quorumConfig)
type quorumConfig struct {
nodeAddrs []string
power int64
quorumHash crypto.QuorumHash
}
// WithNodeAddrs sets node-addresses using option function
func WithNodeAddrs(addrs []string) QuorumOptionFunc {
return func(conf *quorumConfig) {
conf.nodeAddrs = addrs
}
}
// WithPower sets vote-power using option function
func WithPower(power int64) QuorumOptionFunc {
return func(conf *quorumConfig) {
conf.power = power
}
}
// WithRandQuorumHash generates and sets a quorum-hash through option QuorumOptionFunc function
func WithRandQuorumHash() QuorumOptionFunc {
return WithQuorumHash(crypto.RandQuorumHash())
}
// WithQuorumHash sets a quorum-hash through option QuorumOptionFunc function
func WithQuorumHash(quorumHash crypto.QuorumHash) QuorumOptionFunc {
return func(conf *quorumConfig) {
conf.quorumHash = quorumHash
}
}
// LLMQToValidatorSetProto returns a protobuf validator-set-update structure for passed llmq-data
// use option-functions to override default values like power or node-addresses
func LLMQToValidatorSetProto(ld llmq.Data, opts ...QuorumOptionFunc) (*ValidatorSetUpdate, error) {
conf := quorumConfig{
power: 100,
}
for _, opt := range opts {
opt(&conf)
}
tpk, err := cryptoenc.PubKeyToProto(ld.ThresholdPubKey)
if err != nil {
return nil, err
}
vsu := ValidatorSetUpdate{
ValidatorUpdates: ValidatorUpdatesProto(
ld.ProTxHashes,
ld.PubKeyShares,
conf.nodeAddrs,
conf.power,
),
ThresholdPublicKey: tpk,
QuorumHash: conf.quorumHash,
}
return &vsu, nil
}
// UpdateValidatorProto returns a protobuf validator-update struct with passed data
func UpdateValidatorProto(
proTxHash crypto.ProTxHash,
pubKey crypto.PubKey,
power int64,
nodeAddress string,
) ValidatorUpdate {
valUpdate := ValidatorUpdate{
Power: power,
ProTxHash: proTxHash,
NodeAddress: nodeAddress,
}
if len(pubKey.Bytes()) > 0 {
pubKeyProto := cryptoenc.MustPubKeyToProto(pubKey)
valUpdate.PubKey = &pubKeyProto
}
return valUpdate
}
// ValidatorUpdatesProto creates and returns a slice of protobuf update-validator struct
// if number elements in pubKeys are less than in proTxHashes, then for those the elements will be created
// validator-update with power=0 and public-key=nil
func ValidatorUpdatesProto(
proTxHashes []crypto.ProTxHash,
pubKeys []crypto.PubKey,
nodeAddrs []string,
power int64,
) []ValidatorUpdate {
var (
nodeAddr string
pubKey crypto.PubKey
)
vals := make([]ValidatorUpdate, 0, len(proTxHashes))
for i, proTxHash := range proTxHashes {
nodeAddr = ""
pubKey = nil
if i < len(nodeAddrs) {
nodeAddr = nodeAddrs[i]
}
if i < len(pubKeys) {
pubKey = pubKeys[i]
}
vals = append(vals, UpdateValidatorProto(proTxHash, pubKey, power, nodeAddr))
}
return vals
}