-
Notifications
You must be signed in to change notification settings - Fork 177
/
combined_vote_processor_v2.go
321 lines (282 loc) · 12.5 KB
/
combined_vote_processor_v2.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
package votecollector
import (
"errors"
"fmt"
"github.com/rs/zerolog"
"go.uber.org/atomic"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/consensus/hotstuff/signature"
"github.com/onflow/flow-go/consensus/hotstuff/verification"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/encoding"
"github.com/onflow/flow-go/model/flow"
msig "github.com/onflow/flow-go/module/signature"
)
/* **************** Base-Factory for CombinedVoteProcessors ***************** */
// combinedVoteProcessorFactoryBaseV2 is a `votecollector.baseFactory` for creating
// CombinedVoteProcessors, holding all needed dependencies.
// combinedVoteProcessorFactoryBaseV2 is intended to be used for the main consensus.
// CAUTION:
// this base factory only creates the VerifyingVoteProcessor for the given block.
// It does _not_ check the proposer's vote for its own block, i.e. it does _not_
// implement `hotstuff.VoteProcessorFactory`. This base factory should be wrapped
// by `votecollector.VoteProcessorFactory` which adds the logic to verify
// the proposer's vote (decorator pattern).
type combinedVoteProcessorFactoryBaseV2 struct {
committee hotstuff.Committee
onQCCreated hotstuff.OnQCCreated
packer hotstuff.Packer
}
// Create creates CombinedVoteProcessorV2 for processing votes for the given block.
// Caller must treat all errors as exceptions
func (f *combinedVoteProcessorFactoryBaseV2) Create(log zerolog.Logger, block *model.Block) (hotstuff.VerifyingVoteProcessor, error) {
allParticipants, err := f.committee.Identities(block.BlockID)
if err != nil {
return nil, fmt.Errorf("error retrieving consensus participants at block %v: %w", block.BlockID, err)
}
// message that has to be verified against aggregated signature
msg := verification.MakeVoteMessage(block.View, block.BlockID)
// prepare the staking public keys of participants
stakingKeys := make([]crypto.PublicKey, 0, len(allParticipants))
for _, participant := range allParticipants {
stakingKeys = append(stakingKeys, participant.StakingPubKey)
}
stakingSigAggtor, err := signature.NewWeightedSignatureAggregator(allParticipants, stakingKeys, msg, encoding.ConsensusVoteTag)
if err != nil {
return nil, fmt.Errorf("could not create aggregator for staking signatures at block %v: %w", block.BlockID, err)
}
publicKeyShares := make([]crypto.PublicKey, 0, len(allParticipants))
dkg, err := f.committee.DKG(block.BlockID)
if err != nil {
return nil, fmt.Errorf("could not get DKG info at block %v: %w", block.BlockID, err)
}
for _, participant := range allParticipants {
pk, err := dkg.KeyShare(participant.NodeID)
if err != nil {
return nil, fmt.Errorf("could not get random beacon key share for %x at block %v: %w", participant.NodeID, block.BlockID, err)
}
publicKeyShares = append(publicKeyShares, pk)
}
threshold := msig.RandomBeaconThreshold(int(dkg.Size()))
randomBeaconInspector, err := signature.NewRandomBeaconInspector(dkg.GroupKey(), publicKeyShares, threshold, msg)
if err != nil {
return nil, fmt.Errorf("could not create random beacon inspector at block %v: %w", block.BlockID, err)
}
rbRector := signature.NewRandomBeaconReconstructor(dkg, randomBeaconInspector)
minRequiredWeight := hotstuff.ComputeWeightThresholdForBuildingQC(allParticipants.TotalWeight())
return NewCombinedVoteProcessor(
log,
block,
stakingSigAggtor,
rbRector,
f.onQCCreated,
f.packer,
minRequiredWeight,
), nil
}
/* ****************** CombinedVoteProcessorV2 Implementation ****************** */
// CombinedVoteProcessorV2 implements the hotstuff.VerifyingVoteProcessor interface.
// It processes votes from the main consensus committee, where participants must
// _always_ provide the staking signature as part of their vote and can _optionally_
// also provide a random beacon signature. Through their staking signature, a
// participant always contributes to HotStuff's progress. Participation in the random
// beacon is optional (but encouraged). This allows nodes that failed the DKG to
// still contribute only to consensus (as fallback).
// CombinedVoteProcessorV2 is Concurrency safe.
type CombinedVoteProcessorV2 struct {
log zerolog.Logger
block *model.Block
stakingSigAggtor hotstuff.WeightedSignatureAggregator
rbRector hotstuff.RandomBeaconReconstructor
onQCCreated hotstuff.OnQCCreated
packer hotstuff.Packer
minRequiredWeight uint64
done atomic.Bool
}
var _ hotstuff.VerifyingVoteProcessor = (*CombinedVoteProcessorV2)(nil)
func NewCombinedVoteProcessor(log zerolog.Logger,
block *model.Block,
stakingSigAggtor hotstuff.WeightedSignatureAggregator,
rbRector hotstuff.RandomBeaconReconstructor,
onQCCreated hotstuff.OnQCCreated,
packer hotstuff.Packer,
minRequiredWeight uint64,
) *CombinedVoteProcessorV2 {
return &CombinedVoteProcessorV2{
log: log.With().Hex("block_id", block.BlockID[:]).Logger(),
block: block,
stakingSigAggtor: stakingSigAggtor,
rbRector: rbRector,
onQCCreated: onQCCreated,
packer: packer,
minRequiredWeight: minRequiredWeight,
done: *atomic.NewBool(false),
}
}
// Block returns block that is part of proposal that we are processing votes for.
func (p *CombinedVoteProcessorV2) Block() *model.Block {
return p.block
}
// Status returns status of this vote processor, it's always verifying.
func (p *CombinedVoteProcessorV2) Status() hotstuff.VoteCollectorStatus {
return hotstuff.VoteCollectorStatusVerifying
}
// Process performs processing of single vote in concurrent safe way. This function is implemented to be
// called by multiple goroutines at the same time. Supports processing of both staking and random beacon signatures.
// Design of this function is event driven: as soon as we collect enough signatures to create a QC we will immediately do so
// and submit it via callback for further processing.
// Expected error returns during normal operations:
// * VoteForIncompatibleBlockError - submitted vote for incompatible block
// * VoteForIncompatibleViewError - submitted vote for incompatible view
// * model.InvalidVoteError - submitted vote with invalid signature
// * model.DuplicatedSignerError if the signer has been already added
// All other errors should be treated as exceptions.
//
// Impossibility of vote double-counting: Our signature scheme requires _every_ vote to supply a
// staking signature. Therefore, the `stakingSigAggtor` has the set of _all_ signerIDs that have
// provided a valid vote. Hence, the `stakingSigAggtor` guarantees that only a single vote can
// be successfully added for each `signerID`, i.e. double-counting votes is impossible.
func (p *CombinedVoteProcessorV2) Process(vote *model.Vote) error {
err := EnsureVoteForBlock(vote, p.block)
if err != nil {
return fmt.Errorf("received incompatible vote %v: %w", vote.ID(), err)
}
// Vote Processing state machine
if p.done.Load() {
return nil
}
stakingSig, randomBeaconSig, err := msig.DecodeDoubleSig(vote.SigData)
if err != nil {
if errors.Is(err, msig.ErrInvalidSignatureFormat) {
return model.NewInvalidVoteErrorf(vote, "could not decode signature: %w", err)
}
return fmt.Errorf("unexpected error decoding vote %v: %w", vote.ID(), err)
}
// Verify staking sig.
err = p.stakingSigAggtor.Verify(vote.SignerID, stakingSig)
if err != nil {
if model.IsInvalidSignerError(err) {
return model.NewInvalidVoteErrorf(vote, "vote %x for view %d is not from an authorized consensus participant: %w",
vote.ID(), vote.View, err)
}
if errors.Is(err, model.ErrInvalidSignature) {
return model.NewInvalidVoteErrorf(vote, "vote %x for view %d has an invalid staking signature: %w",
vote.ID(), vote.View, err)
}
return fmt.Errorf("internal error checking signature validity for vote %v: %w", vote.ID(), err)
}
if p.done.Load() {
return nil
}
// Verify random beacon sig
if randomBeaconSig != nil {
err = p.rbRector.Verify(vote.SignerID, randomBeaconSig)
if err != nil {
// InvalidSignerError is possible in case we have consensus participants that are _not_ part of the random beacon committee.
if model.IsInvalidSignerError(err) {
return model.NewInvalidVoteErrorf(vote, "vote %x for view %d is not from an authorized random beacon participant: %w",
vote.ID(), vote.View, err)
}
if errors.Is(err, model.ErrInvalidSignature) {
return model.NewInvalidVoteErrorf(vote, "vote %x for view %d has an invalid random beacon signature: %w",
vote.ID(), vote.View, err)
}
return fmt.Errorf("internal error checking signature validity for vote %v: %w", vote.ID(), err)
}
}
if p.done.Load() {
return nil
}
// Add staking sig to aggregator.
_, err = p.stakingSigAggtor.TrustedAdd(vote.SignerID, stakingSig)
if err != nil {
// we don't expect any errors here during normal operation, as we previously checked
// for duplicated votes from the same signer and verified the signer+signature
return fmt.Errorf("unexpected exception adding signature from vote %v to staking aggregator: %w", vote.ID(), err)
}
// Add random beacon sig to threshold sig reconstructor
if randomBeaconSig != nil {
_, err = p.rbRector.TrustedAdd(vote.SignerID, randomBeaconSig)
if err != nil {
// we don't expect any errors here during normal operation, as we previously checked
// for duplicated votes from the same signer and verified the signer+signature
return fmt.Errorf("unexpected exception adding signature from vote %v to random beacon reconstructor: %w", vote.ID(), err)
}
}
// checking of conditions for building QC are satisfied
if p.stakingSigAggtor.TotalWeight() < p.minRequiredWeight {
return nil
}
if !p.rbRector.EnoughShares() {
return nil
}
// At this point, we have enough signatures to build a QC. Another routine
// might just be at this point. To avoid duplicate work, only one routine can pass:
if !p.done.CAS(false, true) {
return nil
}
// Our algorithm for checking votes and adding them to the aggregators should
// guarantee that we are _always_ able to successfully construct a QC when we
// reach this point. A failure implies that the VoteProcessor's internal state is corrupted.
qc, err := p.buildQC()
if err != nil {
return fmt.Errorf("internal error constructing QC from votes: %w", err)
}
p.log.Info().
Uint64("view", qc.View).
Hex("signers", qc.SignerIndices).
Msg("new qc has been created")
p.onQCCreated(qc)
return nil
}
// buildQC performs aggregation and reconstruction of signatures when we have collected enough
// signatures for building a QC. This function is run only once by a single worker.
// Any error should be treated as exception.
func (p *CombinedVoteProcessorV2) buildQC() (*flow.QuorumCertificate, error) {
stakingSigners, aggregatedStakingSig, err := p.stakingSigAggtor.Aggregate()
if err != nil {
return nil, fmt.Errorf("could not aggregate staking signature: %w", err)
}
reconstructedBeaconSig, err := p.rbRector.Reconstruct()
if err != nil {
return nil, fmt.Errorf("could not reconstruct random beacon group signature: %w", err)
}
blockSigData := buildBlockSignatureDataForV2(stakingSigners, aggregatedStakingSig, reconstructedBeaconSig)
qc, err := buildQCWithPackerAndSigData(p.packer, p.block, blockSigData)
if err != nil {
return nil, err
}
return qc, nil
}
// buildBlockSignatureDataForV2 build a block sig data for V2
// It reuses the hotstuff.BlockSignatureData type to create the sig data without filling the RandomBeaconSigners field and
// the AggregatedRandomBeaconSig field, so that the packer can be reused by both V2 and V3 to pack QC's sig data.
func buildBlockSignatureDataForV2(
stakingSigners []flow.Identifier,
aggregatedStakingSig []byte,
reconstructedBeaconSig crypto.Signature,
) *hotstuff.BlockSignatureData {
return &hotstuff.BlockSignatureData{
StakingSigners: stakingSigners,
AggregatedStakingSig: aggregatedStakingSig,
ReconstructedRandomBeaconSig: reconstructedBeaconSig,
}
}
// buildQCWithPackerAndSigData builds the QC with the given packer and blockSigData
func buildQCWithPackerAndSigData(
packer hotstuff.Packer,
block *model.Block,
blockSigData *hotstuff.BlockSignatureData,
) (*flow.QuorumCertificate, error) {
signerIndices, sigData, err := packer.Pack(block.BlockID, blockSigData)
if err != nil {
return nil, fmt.Errorf("could not pack the block sig data: %w", err)
}
return &flow.QuorumCertificate{
View: block.View,
BlockID: block.BlockID,
SignerIndices: signerIndices,
SigData: sigData,
}, nil
}