-
Notifications
You must be signed in to change notification settings - Fork 178
/
combined_vote_processor_v3.go
322 lines (285 loc) · 13.7 KB
/
combined_vote_processor_v3.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
322
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 ***************** */
// combinedVoteProcessorFactoryBaseV3 is a `votecollector.baseFactory` for creating
// CombinedVoteProcessors, holding all needed dependencies.
// combinedVoteProcessorFactoryBaseV3 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).
// nolint:unused
type combinedVoteProcessorFactoryBaseV3 struct {
committee hotstuff.DynamicCommittee
onQCCreated hotstuff.OnQCCreated
packer hotstuff.Packer
}
// Create creates CombinedVoteProcessorV3 for processing votes for the given block.
// Caller must treat all errors as exceptions
// nolint:unused
func (f *combinedVoteProcessorFactoryBaseV3) Create(log zerolog.Logger, block *model.Block) (hotstuff.VerifyingVoteProcessor, error) {
allParticipants, err := f.committee.IdentitiesByBlock(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, msig.ConsensusVoteTag)
if err != nil {
return nil, fmt.Errorf("could not create aggregator for staking signatures: %w", err)
}
dkg, err := f.committee.DKG(block.View)
if err != nil {
return nil, fmt.Errorf("could not get DKG info at block %v: %w", block.BlockID, err)
}
// prepare the random beacon public keys of participants
beaconKeys := make([]crypto.PublicKey, 0, len(allParticipants))
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: %w", participant.NodeID, err)
}
beaconKeys = append(beaconKeys, pk)
}
rbSigAggtor, err := signature.NewWeightedSignatureAggregator(allParticipants, beaconKeys, msg, msig.RandomBeaconTag)
if err != nil {
return nil, fmt.Errorf("could not create aggregator for thershold signatures: %w", err)
}
threshold := msig.RandomBeaconThreshold(int(dkg.Size()))
randomBeaconInspector, err := signature.NewRandomBeaconInspector(dkg.GroupKey(), beaconKeys, threshold, msg)
if err != nil {
return nil, fmt.Errorf("could not create random beacon inspector: %w", err)
}
rbRector := signature.NewRandomBeaconReconstructor(dkg, randomBeaconInspector)
minRequiredWeight, err := f.committee.QuorumThresholdForView(block.View)
if err != nil {
return nil, fmt.Errorf("could not get weight threshold for view %d: %w", block.View, err)
}
return &CombinedVoteProcessorV3{
log: log.With().Hex("block_id", block.BlockID[:]).Logger(),
block: block,
stakingSigAggtor: stakingSigAggtor,
rbSigAggtor: rbSigAggtor,
rbRector: rbRector,
onQCCreated: f.onQCCreated,
packer: f.packer,
minRequiredWeight: minRequiredWeight,
done: *atomic.NewBool(false),
}, nil
}
/* ****************** CombinedVoteProcessorV3 Implementation ****************** */
// CombinedVoteProcessorV3 implements the hotstuff.VerifyingVoteProcessor interface.
// It processes votes from the main consensus committee, where participants vote in
// favour of a block by proving either their staking key signature or their random
// beacon signature. In the former case, the participant only contributes to HotStuff
// progress; while in the latter case, the voter also contributes to running the
// random beacon. Concurrency safe.
type CombinedVoteProcessorV3 struct {
log zerolog.Logger
block *model.Block
stakingSigAggtor hotstuff.WeightedSignatureAggregator
rbSigAggtor hotstuff.WeightedSignatureAggregator
rbRector hotstuff.RandomBeaconReconstructor
onQCCreated hotstuff.OnQCCreated
packer hotstuff.Packer
minRequiredWeight uint64
done atomic.Bool
}
var _ hotstuff.VerifyingVoteProcessor = (*CombinedVoteProcessorV3)(nil)
// Block returns block that is part of proposal that we are processing votes for.
func (p *CombinedVoteProcessorV3) Block() *model.Block {
return p.block
}
// Status returns status of this vote processor, it's always verifying.
func (p *CombinedVoteProcessorV3) 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 - vote from a signer whose vote was previously already processed
// All other errors should be treated as exceptions.
//
// CAUTION: implementation is NOT (yet) BFT
// Explanation: for correctness, we require that no voter can be counted repeatedly. However,
// CombinedVoteProcessorV3 relies on the `VoteCollector`'s `votesCache` filter out all votes but the first for
// every signerID. However, we have the edge case, where we still feed the proposers vote twice into the
// `VerifyingVoteProcessor` (once as part of a cached vote, once as an individual vote). This can be exploited
// by a byzantine proposer to be erroneously counted twice, which would lead to a safety fault.
//
// TODO (suggestion): I think it would be worth-while to include a second `votesCache` into the `CombinedVoteProcessorV3`.
// Thereby, `CombinedVoteProcessorV3` inherently guarantees correctness of the QCs it produces without relying on
// external conditions (making the code more modular, less interdependent and thereby easier to maintain). The
// runtime overhead is marginal: For `votesCache` to add 500 votes (concurrently with 20 threads) takes about
// 0.25ms. This runtime overhead is neglectable and a good tradeoff for the gain in maintainability and code clarity.
func (p *CombinedVoteProcessorV3) 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
}
sigType, sig, err := msig.DecodeSingleSig(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)
}
switch sigType {
case encoding.SigTypeStaking:
err := p.stakingSigAggtor.Verify(vote.SignerID, sig)
if err != nil {
if model.IsInvalidSignerError(err) {
return model.NewInvalidVoteErrorf(vote, "vote %x for view %d is not signed by 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
}
_, err = p.stakingSigAggtor.TrustedAdd(vote.SignerID, sig)
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("adding the signature to staking aggregator failed for vote %v: %w", vote.ID(), err)
}
case encoding.SigTypeRandomBeacon:
err := p.rbSigAggtor.Verify(vote.SignerID, sig)
if err != nil {
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 signatures to `rbSigAggtor` and `rbRector`: we don't expect any errors during normal operation,
// as we previously checked for duplicated votes from the same signer and verified the signer+signature
_, err = p.rbSigAggtor.TrustedAdd(vote.SignerID, sig)
if err != nil {
return fmt.Errorf("unexpected exception adding signature from vote %v to random beacon aggregator: %w", vote.ID(), err)
}
_, err = p.rbRector.TrustedAdd(vote.SignerID, sig)
if err != nil {
return fmt.Errorf("unexpected exception adding signature from vote %v to random beacon reconstructor: %w", vote.ID(), err)
}
default:
return model.NewInvalidVoteErrorf(vote, "invalid signature type %d: %w", sigType, model.NewInvalidFormatErrorf(""))
}
// checking of conditions for building QC are satisfied
if p.stakingSigAggtor.TotalWeight()+p.rbSigAggtor.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.CompareAndSwap(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 *CombinedVoteProcessorV3) buildQC() (*flow.QuorumCertificate, error) {
// STEP 1: aggregate staking signatures (if there are any)
// * It is possible that all replicas signed with their random beacon keys.
// Per Convention, we represent an empty set of staking signers as
// `stakingSigners` and `aggregatedStakingSig` both being zero-length
// (here, we use `nil`).
// * If it has _not collected any_ signatures, `stakingSigAggtor.Aggregate()`
// errors with a `model.InsufficientSignaturesError`. We shortcut this case,
// and only call `Aggregate`, if the `stakingSigAggtor` has collected signatures
// with non-zero weight (i.e. at least one signature was collected).
var stakingSigners []flow.Identifier // nil (zero value) represents empty set of staking signers
var aggregatedStakingSig []byte // nil (zero value) for empty set of staking signers
if p.stakingSigAggtor.TotalWeight() > 0 {
var err error
stakingSigners, aggregatedStakingSig, err = p.stakingSigAggtor.Aggregate()
if err != nil {
return nil, fmt.Errorf("unexpected error aggregating staking signatures: %w", err)
}
}
// STEP 2: reconstruct random beacon group sig and aggregate random beacon sig shares
// Note: A valid random beacon group sig is required for QC validity. Our logic guarantees
// that we always collect the minimally required number (non-zero) of signature shares.
beaconSigners, aggregatedRandomBeaconSig, err := p.rbSigAggtor.Aggregate()
if err != nil {
return nil, fmt.Errorf("could not aggregate random beacon signatures: %w", err)
}
reconstructedBeaconSig, err := p.rbRector.Reconstruct()
if err != nil {
return nil, fmt.Errorf("could not reconstruct random beacon group signature: %w", err)
}
// STEP 3: generate BlockSignatureData and serialize it
blockSigData := &hotstuff.BlockSignatureData{
StakingSigners: stakingSigners,
RandomBeaconSigners: beaconSigners,
AggregatedStakingSig: aggregatedStakingSig,
AggregatedRandomBeaconSig: aggregatedRandomBeaconSig,
ReconstructedRandomBeaconSig: reconstructedBeaconSig,
}
signerIndices, sigData, err := p.packer.Pack(p.block.View, blockSigData)
if err != nil {
return nil, fmt.Errorf("could not pack the block sig data: %w", err)
}
return &flow.QuorumCertificate{
View: p.block.View,
BlockID: p.block.BlockID,
SignerIndices: signerIndices,
SigData: sigData,
}, nil
}