forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
snowball.go
51 lines (41 loc) · 1.56 KB
/
snowball.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowstorm
type snowball struct {
// numSuccessfulPolls is the number of times this choice was the successful
// result of a network poll
numSuccessfulPolls int
// confidence is the number of consecutive times this choice was the
// successful result of a network poll as of [lastVote]
confidence int
// lastVote is the last poll number that this choice was included in a
// successful network poll
lastVote uint64
// rogue identifies if there is a known conflict with this choice
rogue bool
}
func (sb *snowball) getConfidence(currentVote uint64) int {
if sb.lastVote != currentVote {
return 0
}
return sb.confidence
}
func (sb *snowball) recordSuccessfulPoll(currentVote uint64) {
// If this choice wasn't voted for during the last poll, the confidence
// should have been reset during the last poll. So, we reset it now.
if sb.lastVote+1 != currentVote {
sb.confidence = 0
}
// This choice was voted for in this poll. Mark it as such.
sb.lastVote = currentVote
// An affirmative vote increases both the snowball and snowflake counters.
sb.numSuccessfulPolls++
sb.confidence++
}
func (sb *snowball) finalized(betaVirtuous, betaRogue int) bool {
// This choice is finalized if the snowflake counter is at least
// [betaRogue]. If there are no known conflicts with this operation, it can
// be accepted with a snowflake counter of at least [betaVirtuous].
return (!sb.rogue && sb.confidence >= betaVirtuous) ||
sb.confidence >= betaRogue
}