forked from cometbft/cometbft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriv_validator.go
175 lines (143 loc) · 4.7 KB
/
priv_validator.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
package types
import (
"bytes"
"errors"
"fmt"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/ed25519"
)
// PrivValidator defines the functionality of a local CometBFT validator
// that signs votes and proposals, and never double signs.
type PrivValidator interface {
// GetPubKey returns the public key of the validator.
GetPubKey() (crypto.PubKey, error)
// FIXME: should use the domain types defined in this package, not the proto types
// SignVote signs a canonical representation of the vote. If signExtension is
// true, it also signs the vote extension.
SignVote(chainID string, vote *cmtproto.Vote, signExtension bool) error
// SignProposal signs a canonical representation of the proposal.
SignProposal(chainID string, proposal *cmtproto.Proposal) error
// SignBytes signs an arbitrary array of bytes.
SignBytes(bytes []byte) ([]byte, error)
}
type PrivValidatorsByAddress []PrivValidator
func (pvs PrivValidatorsByAddress) Len() int {
return len(pvs)
}
func (pvs PrivValidatorsByAddress) Less(i, j int) bool {
pvi, err := pvs[i].GetPubKey()
if err != nil {
panic(err)
}
pvj, err := pvs[j].GetPubKey()
if err != nil {
panic(err)
}
return bytes.Compare(pvi.Address(), pvj.Address()) == -1
}
func (pvs PrivValidatorsByAddress) Swap(i, j int) {
pvs[i], pvs[j] = pvs[j], pvs[i]
}
// ----------------------------------------
// MockPV
// MockPV implements PrivValidator without any safety or persistence.
// Only use it for testing.
type MockPV struct {
PrivKey crypto.PrivKey
breakProposalSigning bool
breakVoteSigning bool
}
func NewMockPV() MockPV {
return MockPV{ed25519.GenPrivKey(), false, false}
}
// NewMockPVWithParams allows one to create a MockPV instance, but with finer
// grained control over the operation of the mock validator. This is useful for
// mocking test failures.
func NewMockPVWithParams(privKey crypto.PrivKey, breakProposalSigning, breakVoteSigning bool) MockPV {
return MockPV{privKey, breakProposalSigning, breakVoteSigning}
}
// GetPubKey implements PrivValidator.
func (pv MockPV) GetPubKey() (crypto.PubKey, error) {
return pv.PrivKey.PubKey(), nil
}
// SignVote implements PrivValidator.
func (pv MockPV) SignVote(chainID string, vote *cmtproto.Vote, signExtension bool) error {
useChainID := chainID
if pv.breakVoteSigning {
useChainID = "incorrect-chain-id"
}
signBytes := VoteSignBytes(useChainID, vote)
sig, err := pv.PrivKey.Sign(signBytes)
if err != nil {
return err
}
vote.Signature = sig
if signExtension {
var extSig []byte
// We only sign vote extensions for non-nil precommits
if vote.Type == PrecommitType && !ProtoBlockIDIsNil(&vote.BlockID) {
extSignBytes := VoteExtensionSignBytes(useChainID, vote)
extSig, err = pv.PrivKey.Sign(extSignBytes)
if err != nil {
return err
}
} else if len(vote.Extension) > 0 {
return errors.New("unexpected vote extension - vote extensions are only allowed in non-nil precommits")
}
vote.ExtensionSignature = extSig
}
return nil
}
// SignProposal implements PrivValidator.
func (pv MockPV) SignProposal(chainID string, proposal *cmtproto.Proposal) error {
useChainID := chainID
if pv.breakProposalSigning {
useChainID = "incorrect-chain-id"
}
signBytes := ProposalSignBytes(useChainID, proposal)
sig, err := pv.PrivKey.Sign(signBytes)
if err != nil {
return err
}
proposal.Signature = sig
return nil
}
// SignBytes implements PrivValidator.
func (pv MockPV) SignBytes(bytes []byte) ([]byte, error) {
return pv.PrivKey.Sign(bytes)
}
func (pv MockPV) ExtractIntoValidator(votingPower int64) *Validator {
pubKey, _ := pv.GetPubKey()
return &Validator{
Address: pubKey.Address(),
PubKey: pubKey,
VotingPower: votingPower,
}
}
// String returns a string representation of the MockPV.
func (pv MockPV) String() string {
mpv, _ := pv.GetPubKey() // mockPV will never return an error, ignored here
return fmt.Sprintf("MockPV{%v}", mpv.Address())
}
// XXX: Implement.
func (MockPV) DisableChecks() {
// Currently this does nothing,
// as MockPV has no safety checks at all.
}
type ErroringMockPV struct {
MockPV
}
var ErroringMockPVErr = errors.New("erroringMockPV always returns an error")
// SignVote implements PrivValidator.
func (*ErroringMockPV) SignVote(string, *cmtproto.Vote, bool) error {
return ErroringMockPVErr
}
// SignProposal implements PrivValidator.
func (*ErroringMockPV) SignProposal(string, *cmtproto.Proposal) error {
return ErroringMockPVErr
}
// NewErroringMockPV returns a MockPV that fails on each signing request. Again, for testing only.
func NewErroringMockPV() *ErroringMockPV {
return &ErroringMockPV{MockPV{ed25519.GenPrivKey(), false, false}}
}