-
Notifications
You must be signed in to change notification settings - Fork 651
/
consensus.go
113 lines (86 loc) · 3.28 KB
/
consensus.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowball
import (
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/bag"
)
// Consensus represents a general snow instance that can be used directly to
// process the results of network queries.
type Consensus interface {
fmt.Stringer
// Adds a new choice to vote on
Add(newChoice ids.ID)
// Returns the currently preferred choice to be finalized
Preference() ids.ID
// RecordPoll records the results of a network poll. Assumes all choices
// have been previously added.
//
// If the consensus instance was not previously finalized, this function
// will return true if the poll was successful and false if the poll was
// unsuccessful.
//
// If the consensus instance was previously finalized, the function may
// return true or false.
RecordPoll(votes bag.Bag[ids.ID]) bool
// RecordUnsuccessfulPoll resets the snowflake counters of this consensus
// instance
RecordUnsuccessfulPoll()
// Return whether a choice has been finalized
Finalized() bool
}
// Factory produces Nnary and Unary decision instances
type Factory interface {
NewNnary(params Parameters, choice ids.ID) Nnary
NewUnary(params Parameters) Unary
}
// Nnary is a snow instance deciding between an unbounded number of values.
// The caller samples k nodes and calls RecordPoll with the result.
// RecordUnsuccessfulPoll resets the confidence counters when one or
// more consecutive polls fail to reach alphaPreference votes.
type Nnary interface {
fmt.Stringer
// Adds a new possible choice
Add(newChoice ids.ID)
// Returns the currently preferred choice to be finalized
Preference() ids.ID
// RecordPoll records the results of a network poll
RecordPoll(count int, choice ids.ID)
// RecordUnsuccessfulPoll resets the snowflake counter of this instance
RecordUnsuccessfulPoll()
// Return whether a choice has been finalized
Finalized() bool
}
// Binary is a snow instance deciding between two values.
// The caller samples k nodes and calls RecordPoll with the result.
// RecordUnsuccessfulPoll resets the confidence counters when one or
// more consecutive polls fail to reach alphaPreference votes.
type Binary interface {
fmt.Stringer
// Returns the currently preferred choice to be finalized
Preference() int
// RecordPoll records the results of a network poll
RecordPoll(count, choice int)
// RecordUnsuccessfulPoll resets the snowflake counter of this instance
RecordUnsuccessfulPoll()
// Return whether a choice has been finalized
Finalized() bool
}
// Unary is a snow instance deciding on one value.
// The caller samples k nodes and calls RecordPoll with the result.
// RecordUnsuccessfulPoll resets the confidence counters when one or
// more consecutive polls fail to reach alphaPreference votes.
type Unary interface {
fmt.Stringer
// RecordPoll records the results of a network poll
RecordPoll(count int)
// RecordUnsuccessfulPoll resets the snowflake counter of this instance
RecordUnsuccessfulPoll()
// Return whether a choice has been finalized
Finalized() bool
// Returns a new binary snowball instance with the original choice.
Extend(originalPreference int) Binary
// Returns a new unary snowflake instance with the same state
Clone() Unary
}