-
Notifications
You must be signed in to change notification settings - Fork 11
/
protobuf.go
189 lines (168 loc) · 5.19 KB
/
protobuf.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
package types
import (
"fmt"
"github.com/dashpay/dashd-go/btcjson"
abci "github.com/dashpay/tenderdash/abci/types"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/crypto/bls12381"
"github.com/dashpay/tenderdash/crypto/ed25519"
cryptoenc "github.com/dashpay/tenderdash/crypto/encoding"
"github.com/dashpay/tenderdash/crypto/secp256k1"
crypto2 "github.com/dashpay/tenderdash/proto/tendermint/crypto"
)
//-------------------------------------------------------
// Use strings to distinguish types in ABCI messages
const (
ABCIPubKeyTypeEd25519 = ed25519.KeyType
ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
ABCIPubKeyTypeBLS12381 = bls12381.KeyType
ABCIEvidenceTypeDuplicateVote = "duplicate/vote"
ABCIEvidenceTypePhantom = "phantom"
ABCIEvidenceTypeLunatic = "lunatic"
ABCIEvidenceTypePotentialAmnesia = "potential_amnesia"
ABCIEvidenceTypeMock = "mock/evidence"
)
// TODO: Make non-global by allowing for registration of more pubkey types
//-------------------------------------------------------
// TM2PB is used for converting Tendermint ABCI to protobuf ABCI.
// UNSTABLE
var TM2PB = tm2pb{}
type tm2pb struct{}
func (tm2pb) Validator(val *Validator) abci.Validator {
return abci.Validator{
Power: val.VotingPower,
ProTxHash: val.ProTxHash,
}
}
// ValidatorUpdate panics on unknown pubkey type
func (tm2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate {
valUpdate := abci.ValidatorUpdate{
Power: val.VotingPower,
ProTxHash: val.ProTxHash,
NodeAddress: val.NodeAddress.String(),
}
if val.PubKey != nil {
pk := cryptoenc.MustPubKeyToProto(val.PubKey)
valUpdate.PubKey = &pk
}
return valUpdate
}
// ValidatorUpdates panics on unknown pubkey type
func (tm2pb) ValidatorUpdates(vals *ValidatorSet) abci.ValidatorSetUpdate {
validators := make([]abci.ValidatorUpdate, vals.Size())
for i, val := range vals.Validators {
validators[i] = TM2PB.ValidatorUpdate(val)
}
abciThresholdPublicKey := cryptoenc.MustPubKeyToProto(vals.ThresholdPublicKey)
return abci.ValidatorSetUpdate{
ValidatorUpdates: validators,
ThresholdPublicKey: abciThresholdPublicKey,
QuorumHash: vals.QuorumHash,
}
}
// XXX: panics on nil or unknown pubkey type
func (tm2pb) NewValidatorUpdate(
pubkey crypto.PubKey,
power int64,
proTxHash []byte,
address string,
) abci.ValidatorUpdate {
var pubkeyABCI *crypto2.PublicKey
if pubkey != nil {
pubkeyProto := cryptoenc.MustPubKeyToProto(pubkey)
pubkeyABCI = &pubkeyProto
} else {
pubkeyABCI = nil
}
return abci.ValidatorUpdate{
PubKey: pubkeyABCI,
Power: power,
ProTxHash: proTxHash,
NodeAddress: address,
}
}
//----------------------------------------------------------------------------
// PB2TM is used for converting protobuf ABCI to Tendermint ABCI.
// UNSTABLE
var PB2TM = pb2tm{}
type pb2tm struct{}
func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) {
tmVals := make([]*Validator, len(vals))
for i, v := range vals {
pub := crypto.PubKey(nil)
var err error
if v.PubKey != nil {
pub, err = cryptoenc.PubKeyFromProto(*v.PubKey)
if err != nil {
return nil, err
}
}
tmVals[i] = NewValidator(pub, v.Power, v.ProTxHash, v.NodeAddress)
}
return tmVals, nil
}
func (pb2tm) ValidatorUpdatesFromValidatorSet(valSetUpdate *abci.ValidatorSetUpdate) (
[]*Validator,
crypto.PubKey,
crypto.QuorumHash,
error,
) {
if valSetUpdate == nil {
return nil, nil, nil, nil
}
tmVals := make([]*Validator, len(valSetUpdate.ValidatorUpdates))
for i, v := range valSetUpdate.ValidatorUpdates {
pub := crypto.PubKey(nil)
var err error
if v.PubKey != nil {
pub, err = cryptoenc.PubKeyFromProto(*v.PubKey)
if err != nil {
return nil, nil, nil, err
}
}
tmVals[i] = NewValidator(pub, v.Power, v.ProTxHash, v.NodeAddress)
err = tmVals[i].ValidateBasic()
if err != nil {
return nil, nil, nil, fmt.Errorf("validator updates from validator set error when validating validator: %s", err)
}
}
if valSetUpdate.ThresholdPublicKey.Sum == nil {
return nil, nil, nil, nil
}
pub, err := cryptoenc.PubKeyFromProto(valSetUpdate.ThresholdPublicKey)
if err != nil {
return nil, nil, nil, err
}
if len(valSetUpdate.QuorumHash) != crypto.DefaultHashSize {
return nil, nil, nil, fmt.Errorf("validator set update must have a quorum"+
" hash of 32 bytes (size: %d bytes)", len(valSetUpdate.QuorumHash))
}
return tmVals, pub, valSetUpdate.QuorumHash, nil
}
func (pb2tm) ValidatorSetFromProtoUpdate(
quorumType btcjson.LLMQType,
valSetUpdate *abci.ValidatorSetUpdate,
) (*ValidatorSet, error) {
hasPublicKeys := true
for _, v := range valSetUpdate.ValidatorUpdates {
if v.PubKey == nil {
hasPublicKeys = false
break
}
}
tmVals, pub, quorumHash, err := PB2TM.ValidatorUpdatesFromValidatorSet(valSetUpdate)
if err != nil {
return nil, err
}
return NewValidatorSet(tmVals, pub, quorumType, quorumHash, hasPublicKeys), nil
}
func (pb2tm) ThresholdPublicKeyUpdate(thresholdPublicKey crypto2.PublicKey) (crypto.PubKey, error) {
if thresholdPublicKey.Sum == nil {
return nil, nil
}
pub, err := cryptoenc.PubKeyFromProto(thresholdPublicKey)
if err != nil {
return nil, err
}
return pub, nil
}