-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.go
More file actions
395 lines (330 loc) · 10.6 KB
/
util.go
File metadata and controls
395 lines (330 loc) · 10.6 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex
import (
"context"
cryptoRand "crypto/rand"
"encoding/binary"
"fmt"
"math/rand/v2"
"slices"
"strings"
"sync"
"time"
"go.uber.org/zap"
)
// RetrieveLastIndexFromStorage retrieves the latest block and finalization from storage.
// Returns an error if it cannot be retrieved but the storage has some block.
// Returns (nil, nil) if the storage is empty.
func RetrieveLastIndexFromStorage(s Storage) (*VerifiedFinalizedBlock, error) {
numBlocks := s.NumBlocks()
if numBlocks == 0 {
return nil, nil
}
lastBlock, finalization, err := s.Retrieve(numBlocks - 1)
if err != nil {
return nil, fmt.Errorf("failed retrieving last block from storage with seq %d: %w", numBlocks-1, err)
}
return &VerifiedFinalizedBlock{
VerifiedBlock: lastBlock,
Finalization: finalization,
}, nil
}
func hasSomeNodeSignedTwice(nodeIDs []NodeID, logger Logger) (NodeID, bool) {
seen := make(map[string]struct{}, len(nodeIDs))
for _, nodeID := range nodeIDs {
if _, alreadySeen := seen[string(nodeID)]; alreadySeen {
logger.Debug("Observed a signature originating at least twice from the same node")
return nodeID, true
}
seen[string(nodeID)] = struct{}{}
}
return NodeID{}, false
}
func VerifyQC(qc QuorumCertificate, logger Logger, messageType string, isQuorum func(signers []NodeID) bool, eligibleSigners map[string]struct{}, messageToVerify verifiableMessage, from NodeID) error {
if qc == nil {
logger.Debug("Received nil QuorumCertificate")
return fmt.Errorf("nil QuorumCertificate")
}
msgTypeLowerCase := strings.ToLower(messageType)
// Ensure no node signed the QuorumCertificate twice
doubleSigner, signedTwice := hasSomeNodeSignedTwice(qc.Signers(), logger)
if signedTwice {
logger.Debug(fmt.Sprintf("%s is signed by the same node more than once", messageType), zap.Stringer("signer", doubleSigner))
return fmt.Errorf("%s is signed by the same node (%s) more than once", msgTypeLowerCase, doubleSigner)
}
// Check enough signers signed the QuorumCertificate
if !isQuorum(qc.Signers()) {
logger.Debug(fmt.Sprintf("%s certificate signed by insufficient nodes", messageType),
zap.Int("count", len(qc.Signers())))
return fmt.Errorf("%s certificate signed by insufficient (%d) nodes", msgTypeLowerCase, len(qc.Signers()))
}
// Check QuorumCertificate was signed by only eligible nodes
for _, signer := range qc.Signers() {
if _, exists := eligibleSigners[string(signer)]; !exists {
logger.Debug(fmt.Sprintf("%s quorum certificate contains an unknown signer", messageType), zap.Stringer("signer", signer))
return fmt.Errorf("%s quorum certificate contains an unknown signer (%s)", msgTypeLowerCase, signer)
}
}
if err := messageToVerify.Verify(); err != nil {
if len(from) > 0 {
logger.Debug(fmt.Sprintf("%s quorum certificate is invalid", messageType), zap.Stringer("NodeID", from), zap.Error(err))
} else {
logger.Debug(fmt.Sprintf("%s quorum certificate is invalid", messageType), zap.Error(err))
}
return err
}
return nil
}
// GetLatestVerifiedQuorumRound returns the latest verified quorum round given
// a round and empty notarization. If both are nil, it returns nil.
func GetLatestVerifiedQuorumRound(round *Round, emptyNotarization *EmptyNotarization) *VerifiedQuorumRound {
var verifiedQuorumRound *VerifiedQuorumRound
var highestRound uint64
var exists bool
if round != nil && (round.finalization != nil || round.notarization != nil) {
highestRound = round.num
verifiedQuorumRound = &VerifiedQuorumRound{
VerifiedBlock: round.block,
Notarization: round.notarization,
Finalization: round.finalization,
}
exists = true
}
if emptyNotarization != nil {
emptyNoteRound := emptyNotarization.Vote.Round
if emptyNoteRound > highestRound || !exists {
verifiedQuorumRound = &VerifiedQuorumRound{
EmptyNotarization: emptyNotarization,
}
}
}
return verifiedQuorumRound
}
// SetRound is a helper function that is used for tests to create a round.
func SetRound(block VerifiedBlock, notarization *Notarization, finalization *Finalization) *Round {
round := &Round{
block: block,
notarization: notarization,
finalization: finalization,
num: block.BlockHeader().Round,
}
return round
}
type OneTimeVerifier struct {
lock sync.Mutex
digests map[Digest]verifiedResult
logger Logger
}
func newOneTimeVerifier(logger Logger) *OneTimeVerifier {
return &OneTimeVerifier{
digests: make(map[Digest]verifiedResult),
logger: logger,
}
}
func (otv *OneTimeVerifier) Wrap(block Block) Block {
return &oneTimeVerifiedBlock{
otv: otv,
Block: block,
}
}
type verifiedResult struct {
seq uint64
vb VerifiedBlock
err error
}
type oneTimeVerifiedBlock struct {
otv *OneTimeVerifier
Block
}
func (block *oneTimeVerifiedBlock) Verify(ctx context.Context) (VerifiedBlock, error) {
block.otv.lock.Lock()
defer block.otv.lock.Unlock()
header := block.Block.BlockHeader()
digest := header.Digest
seq := header.Seq
// cleanup
defer func() {
for digest, vr := range block.otv.digests {
if vr.seq < seq {
delete(block.otv.digests, digest)
}
}
}()
if result, exists := block.otv.digests[digest]; exists {
block.otv.logger.Debug("Attempted to verify an already verified block", zap.Uint64("round", header.Round), zap.Error(result.err))
return result.vb, result.err
}
vb, err := block.Block.Verify(ctx)
block.otv.digests[digest] = verifiedResult{
seq: seq,
vb: vb,
err: err,
}
return vb, err
}
type Segment struct {
Start uint64
End uint64
}
// compressSequences takes a slice of uint64 values representing
// missing sequence numbers and compresses consecutive numbers into segments.
// Each segment represents a continuous block of missing sequence numbers.
func CompressSequences(missingSeqs []uint64) []Segment {
slices.Sort(missingSeqs)
var segments []Segment
if len(missingSeqs) == 0 {
return segments
}
startSeq := missingSeqs[0]
endSeq := missingSeqs[0]
for i, currentSeq := range missingSeqs[1:] {
if currentSeq != missingSeqs[i]+1 {
segments = append(segments, Segment{
Start: startSeq,
End: endSeq,
})
startSeq = currentSeq
}
endSeq = currentSeq
}
segments = append(segments, Segment{
Start: startSeq,
End: endSeq,
})
return segments
}
// DistributeSequenceRequests evenly creates segments amongst [numNodes] over
// the range [start, end].
func DistributeSequenceRequests(start, end uint64, numNodes int) []Segment {
var segments []Segment
if numNodes <= 0 || start > end {
return segments
}
numSeqs := end + 1 - start
seqsPerNode := numSeqs / uint64(numNodes)
remainder := numSeqs % uint64(numNodes)
if seqsPerNode == 0 {
seqsPerNode = 1
}
nodeStart := start
for i := 0; i < numNodes && nodeStart <= end; i++ {
segmentLength := seqsPerNode
if remainder > 0 {
segmentLength++
remainder--
}
nodeEnd := min(nodeStart+segmentLength-1, end)
segments = append(segments, Segment{
Start: nodeStart,
End: nodeEnd,
})
nodeStart = nodeEnd + 1
}
return segments
}
type NotarizationTime struct {
// config
getRound func() uint64
haveUnFinalizedNotarization func() (uint64, bool)
rebroadcastFinalizationVotes func()
checkInterval time.Duration
finalizeVoteRebroadcastTimeout time.Duration
// state
lastSampleTime time.Time
latestRound uint64
lastRebroadcastTime time.Time
oldestNotFinalizedRound uint64
}
func NewNotarizationTime(
finalizeVoteRebroadcastTimeout time.Duration,
haveUnFinalizedNotarization func() (uint64, bool),
rebroadcastFinalizationVotes func(),
getRound func() uint64,
) NotarizationTime {
return NotarizationTime{
finalizeVoteRebroadcastTimeout: finalizeVoteRebroadcastTimeout,
haveUnFinalizedNotarization: haveUnFinalizedNotarization,
rebroadcastFinalizationVotes: rebroadcastFinalizationVotes,
getRound: getRound,
checkInterval: finalizeVoteRebroadcastTimeout / 3,
}
}
func (nt *NotarizationTime) CheckForNotFinalizedNotarizedBlocks(now time.Time) {
// If we have recently checked, don't check again
if !nt.lastSampleTime.IsZero() && nt.lastSampleTime.Add(nt.checkInterval).After(now) {
return
}
nt.lastSampleTime = now
round := nt.getRound()
// As long as we make some progress, we don't check for a round not finalized.
if round > nt.latestRound {
nt.latestRound = round
return
}
// It is only if we didn't advance any round, that we check if we have made some progress in finalizing rounds.
oldestNotFinalizedRound, haveNotFinalizedRound := nt.haveUnFinalizedNotarization()
if !haveNotFinalizedRound {
nt.lastRebroadcastTime = time.Time{}
nt.oldestNotFinalizedRound = 0
return
}
lastRebroadcastTime := nt.lastRebroadcastTime
if lastRebroadcastTime.IsZero() {
nt.lastRebroadcastTime = now
nt.oldestNotFinalizedRound = oldestNotFinalizedRound
return
}
if lastRebroadcastTime.Add(nt.finalizeVoteRebroadcastTimeout).Before(now) &&
nt.oldestNotFinalizedRound == oldestNotFinalizedRound {
nt.rebroadcastFinalizationVotes()
nt.lastRebroadcastTime = now
}
}
type voteSigner interface {
Signer() NodeID
}
func NodeIDsFromVotes[VS voteSigner](votes []VS) []NodeID {
nodeIDs := make([]NodeID, 0, len(votes))
for _, vote := range votes {
nodeIDs = append(nodeIDs, vote.Signer())
}
return nodeIDs
}
func emptyVotesToSignatures(votes []*EmptyVote) []Signature {
sigs := make([]Signature, 0, len(votes))
for _, vote := range votes {
sigs = append(sigs, vote.Signature)
}
return sigs
}
type walRound struct {
round uint64
emptyNotarization *EmptyNotarization
emptyVote *ToBeSignedEmptyVote
notarization *Notarization
finalization *Finalization
block Block
}
func (t *walRound) String() string {
hasEmptyNotarization := t.emptyNotarization != nil
hasEmptyVote := t.emptyVote != nil
hasNotarization := t.notarization != nil
hasFinalization := t.finalization != nil
hasBlock := t.block != nil
return fmt.Sprintf("walRound{round: %d, hasEmptyNotarization: %t, hasEmptyVote: %t, hasNotarization: %t, hasFinalization: %t, hasBlock: %t}", t.round, hasEmptyNotarization, hasEmptyVote, hasNotarization, hasFinalization, hasBlock)
}
func newRandomSource() (*rand.Rand, error) {
var seedBytes [32]byte
if _, err := cryptoRand.Read(seedBytes[:]); err != nil {
return nil, fmt.Errorf("failed to read random bytes: %w", err)
}
chacha := rand.NewChaCha8(seedBytes)
return rand.New(chacha), nil
}
func NewRandomSourceFromSeed(seed int64) *rand.Rand {
var seedBytes [32]byte
binary.LittleEndian.PutUint64(seedBytes[:8], uint64(seed))
chacha := rand.NewChaCha8(seedBytes)
return rand.New(chacha)
}