Skip to content

Commit adc1d4b

Browse files
Replace snowball/snowflake interface with single shared snow interface (#2717)
Signed-off-by: aaronbuchwald <aaron.buchwald56@gmail.com> Signed-off-by: Stephen Buttolph <stephen@avalabs.org> Co-authored-by: Stephen Buttolph <stephen@avalabs.org>
1 parent f1f88ac commit adc1d4b

19 files changed

+161
-167
lines changed

snow/consensus/snowball/binary_slush.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package snowball
55

66
import "fmt"
77

8-
var _ BinarySlush = (*binarySlush)(nil)
9-
108
func newBinarySlush(choice int) binarySlush {
119
return binarySlush{
1210
preference: choice,

snow/consensus/snowball/binary_snowball.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package snowball
55

66
import "fmt"
77

8-
var _ BinarySnowball = (*binarySnowball)(nil)
8+
var _ Binary = (*binarySnowball)(nil)
99

1010
func newBinarySnowball(beta, choice int) binarySnowball {
1111
return binarySnowball{

snow/consensus/snowball/binary_snowflake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package snowball
55

66
import "fmt"
77

8-
var _ BinarySnowflake = (*binarySnowflake)(nil)
8+
var _ Binary = (*binarySnowflake)(nil)
99

1010
func newBinarySnowflake(beta, choice int) binarySnowflake {
1111
return binarySnowflake{

snow/consensus/snowball/consensus.go

Lines changed: 30 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ type Consensus interface {
4040
Finalized() bool
4141
}
4242

43-
// NnarySnowball augments NnarySnowflake with a counter that tracks the total
44-
// number of positive responses from a network sample.
45-
type NnarySnowball interface{ NnarySnowflake }
46-
47-
// NnarySnowflake is a snowflake instance deciding between an unbounded number
48-
// of values. After performing a network sample of k nodes, if you have alpha
49-
// votes for one of the choices, you should vote for that choice. Otherwise, you
50-
// should reset.
51-
type NnarySnowflake interface {
43+
// Factory produces Nnary and Unary decision instances
44+
type Factory interface {
45+
NewNnary(params Parameters, choice ids.ID) Nnary
46+
NewUnary(params Parameters) Unary
47+
}
48+
49+
// Nnary is a snow instance deciding between an unbounded number of values.
50+
// The caller samples k nodes and then calls
51+
// 1. RecordSuccessfulPoll if choice collects >= alphaConfidence votes
52+
// 2. RecordPollPreference if choice collects >= alphaPreference votes
53+
// 3. RecordUnsuccessfulPoll otherwise
54+
type Nnary interface {
5255
fmt.Stringer
5356

5457
// Adds a new possible choice
@@ -73,29 +76,12 @@ type NnarySnowflake interface {
7376
Finalized() bool
7477
}
7578

76-
// NnarySlush is a slush instance deciding between an unbounded number of
77-
// values. After performing a network sample of k nodes, if you have alpha
78-
// votes for one of the choices, you should vote for that choice.
79-
type NnarySlush interface {
80-
fmt.Stringer
81-
82-
// Returns the currently preferred choice to be finalized
83-
Preference() ids.ID
84-
85-
// RecordSuccessfulPoll records a successful poll towards finalizing the
86-
// specified choice. Assumes the choice was previously added.
87-
RecordSuccessfulPoll(choice ids.ID)
88-
}
89-
90-
// BinarySnowball augments BinarySnowflake with a counter that tracks the total
91-
// number of positive responses from a network sample.
92-
type BinarySnowball interface{ BinarySnowflake }
93-
94-
// BinarySnowflake is a snowball instance deciding between two values
95-
// After performing a network sample of k nodes, if you have alpha votes for
96-
// one of the choices, you should vote for that choice. Otherwise, you should
97-
// reset.
98-
type BinarySnowflake interface {
79+
// Binary is a snow instance deciding between two values.
80+
// The caller samples k nodes and then calls
81+
// 1. RecordSuccessfulPoll if choice collects >= alphaConfidence votes
82+
// 2. RecordPollPreference if choice collects >= alphaPreference votes
83+
// 3. RecordUnsuccessfulPoll otherwise
84+
type Binary interface {
9985
fmt.Stringer
10086

10187
// Returns the currently preferred choice to be finalized
@@ -116,31 +102,20 @@ type BinarySnowflake interface {
116102
Finalized() bool
117103
}
118104

119-
// BinarySlush is a slush instance deciding between two values. After performing
120-
// a network sample of k nodes, if you have alpha votes for one of the choices,
121-
// you should vote for that choice.
122-
type BinarySlush interface {
123-
fmt.Stringer
124-
125-
// Returns the currently preferred choice to be finalized
126-
Preference() int
127-
128-
// RecordSuccessfulPoll records a successful poll towards finalizing the
129-
// specified choice
130-
RecordSuccessfulPoll(choice int)
131-
}
132-
133-
// UnarySnowball is a snowball instance deciding on one value. After performing
134-
// a network sample of k nodes, if you have alpha votes for the choice, you
135-
// should vote. Otherwise, you should reset.
136-
type UnarySnowball interface {
105+
// Unary is a snow instance deciding on one value.
106+
// The caller samples k nodes and then calls
107+
// 1. RecordSuccessfulPoll if choice collects >= alphaConfidence votes
108+
// 2. RecordPollPreference if choice collects >= alphaPreference votes
109+
// 3. RecordUnsuccessfulPoll otherwise
110+
type Unary interface {
137111
fmt.Stringer
138112

139-
// RecordSuccessfulPoll records a successful poll towards finalizing
113+
// RecordSuccessfulPoll records a successful poll that reaches an alpha
114+
// confidence threshold.
140115
RecordSuccessfulPoll()
141116

142-
// RecordPollPreference records a poll that strengthens the preference but
143-
// did not contribute towards finalizing
117+
// RecordPollPreference records a poll that receives an alpha preference
118+
// threshold, but not an alpha confidence threshold.
144119
RecordPollPreference()
145120

146121
// RecordUnsuccessfulPoll resets the snowflake counter of this instance
@@ -151,31 +126,8 @@ type UnarySnowball interface {
151126

152127
// Returns a new binary snowball instance with the agreement parameters
153128
// transferred. Takes in the new beta value and the original choice
154-
Extend(beta, originalPreference int) BinarySnowball
155-
156-
// Returns a new unary snowball instance with the same state
157-
Clone() UnarySnowball
158-
}
159-
160-
// UnarySnowflake is a snowflake instance deciding on one value. After
161-
// performing a network sample of k nodes, if you have alpha votes for the
162-
// choice, you should vote. Otherwise, you should reset.
163-
type UnarySnowflake interface {
164-
fmt.Stringer
165-
166-
// RecordSuccessfulPoll records a successful poll towards finalizing
167-
RecordSuccessfulPoll()
168-
169-
// RecordUnsuccessfulPoll resets the snowflake counter of this instance
170-
RecordUnsuccessfulPoll()
171-
172-
// Return whether a choice has been finalized
173-
Finalized() bool
174-
175-
// Returns a new binary snowball instance with the agreement parameters
176-
// transferred. Takes in the new beta value and the original choice
177-
Extend(beta, originalPreference int) BinarySnowflake
129+
Extend(beta, originalPreference int) Binary
178130

179131
// Returns a new unary snowflake instance with the same state
180-
Clone() UnarySnowflake
132+
Clone() Unary
181133
}

snow/consensus/snowball/consensus_performance_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func TestDualAlphaOptimization(t *testing.T) {
2929
source = prng.NewMT19937()
3030
)
3131

32-
singleAlphaNetwork := NewNetwork(params, numColors, source)
32+
singleAlphaNetwork := NewNetwork(SnowballFactory, params, numColors, source)
3333

3434
params.AlphaPreference = params.K/2 + 1
35-
dualAlphaNetwork := NewNetwork(params, numColors, source)
35+
dualAlphaNetwork := NewNetwork(SnowballFactory, params, numColors, source)
3636

3737
source.Seed(seed)
3838
for i := 0; i < numNodes; i++ {
@@ -62,8 +62,8 @@ func TestTreeConvergenceOptimization(t *testing.T) {
6262
source = prng.NewMT19937()
6363
)
6464

65-
treeNetwork := NewNetwork(params, numColors, source)
66-
flatNetwork := NewNetwork(params, numColors, source)
65+
treeNetwork := NewNetwork(SnowballFactory, params, numColors, source)
66+
flatNetwork := NewNetwork(SnowballFactory, params, numColors, source)
6767

6868
source.Seed(seed)
6969
for i := 0; i < numNodes; i++ {

snow/consensus/snowball/consensus_reversibility_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestSnowballGovernance(t *testing.T) {
2323
source = prng.NewMT19937()
2424
)
2525

26-
nBitwise := NewNetwork(params, numColors, source)
26+
nBitwise := NewNetwork(SnowballFactory, params, numColors, source)
2727

2828
source.Seed(seed)
2929
for i := 0; i < numRed; i++ {

snow/consensus/snowball/consensus_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
_ Consensus = (*Byzantine)(nil)
1717
)
1818

19-
func NewByzantine(_ Parameters, choice ids.ID) Consensus {
19+
func NewByzantine(_ Factory, _ Parameters, choice ids.ID) Consensus {
2020
return &Byzantine{
2121
preference: choice,
2222
}

snow/consensus/snowball/factory.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package snowball
5+
6+
import "github.com/ava-labs/avalanchego/ids"
7+
8+
var (
9+
SnowballFactory Factory = snowballFactory{}
10+
SnowflakeFactory Factory = snowflakeFactory{}
11+
)
12+
13+
type snowballFactory struct{}
14+
15+
func (snowballFactory) NewNnary(params Parameters, choice ids.ID) Nnary {
16+
sb := newNnarySnowball(params.BetaVirtuous, params.BetaRogue, choice)
17+
return &sb
18+
}
19+
20+
func (snowballFactory) NewUnary(params Parameters) Unary {
21+
sb := newUnarySnowball(params.BetaVirtuous)
22+
return &sb
23+
}
24+
25+
type snowflakeFactory struct{}
26+
27+
func (snowflakeFactory) NewNnary(params Parameters, choice ids.ID) Nnary {
28+
sf := newNnarySnowflake(params.BetaVirtuous, params.BetaRogue, choice)
29+
return &sf
30+
}
31+
32+
func (snowflakeFactory) NewUnary(params Parameters) Unary {
33+
sf := newUnarySnowflake(params.BetaVirtuous)
34+
return &sf
35+
}

snow/consensus/snowball/flat.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import (
1010

1111
var _ Consensus = (*Flat)(nil)
1212

13-
func NewFlat(params Parameters, choice ids.ID) Consensus {
13+
func NewFlat(factory Factory, params Parameters, choice ids.ID) Consensus {
1414
return &Flat{
15-
nnarySnowball: newNnarySnowball(params.BetaVirtuous, params.BetaRogue, choice),
16-
params: params,
15+
Nnary: factory.NewNnary(params, choice),
16+
params: params,
1717
}
1818
}
1919

20-
// Flat is a naive implementation of a multi-choice snowball instance
20+
// Flat is a naive implementation of a multi-choice snow instance
2121
type Flat struct {
22-
// wraps the n-nary snowball logic
23-
nnarySnowball
22+
// wraps the n-nary snow logic
23+
Nnary
2424

25-
// params contains all the configurations of a snowball instance
25+
// params contains all the configurations of a snow instance
2626
params Parameters
2727
}
2828

snow/consensus/snowball/flat_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestFlat(t *testing.T) {
2121
BetaVirtuous: 1,
2222
BetaRogue: 2,
2323
}
24-
f := NewFlat(params, Red)
24+
f := NewFlat(SnowballFactory, params, Red)
2525
f.Add(Green)
2626
f.Add(Blue)
2727

0 commit comments

Comments
 (0)