-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove snowball.Initialize
and snowball.Factory
#2104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,16 +16,18 @@ var ( | |
_ Consensus = (*Byzantine)(nil) | ||
) | ||
|
||
func NewByzantine(_ Parameters, choice ids.ID) Consensus { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of the unused parameter here? Looks like we only had it previous for interface signature |
||
return &Byzantine{ | ||
preference: choice, | ||
} | ||
} | ||
|
||
// Byzantine is a naive implementation of a multi-choice snowball instance | ||
type Byzantine struct { | ||
// Hardcode the preference | ||
preference ids.ID | ||
} | ||
|
||
func (b *Byzantine) Initialize(_ Parameters, choice ids.ID) { | ||
b.preference = choice | ||
} | ||
|
||
func (*Byzantine) Add(ids.ID) {} | ||
|
||
func (b *Byzantine) Preference() ids.ID { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,16 +8,14 @@ import ( | |
"github.com/ava-labs/avalanchego/utils/bag" | ||
) | ||
|
||
var ( | ||
_ Factory = (*FlatFactory)(nil) | ||
_ Consensus = (*Flat)(nil) | ||
) | ||
|
||
// FlatFactory implements Factory by returning a flat struct | ||
type FlatFactory struct{} | ||
var _ Consensus = (*Flat)(nil) | ||
|
||
func (FlatFactory) New() Consensus { | ||
return &Flat{} | ||
func NewFlat(params Parameters, choice ids.ID) Consensus { | ||
f := &Flat{ | ||
params: params, | ||
} | ||
f.nnarySnowball.Initialize(params.BetaVirtuous, params.BetaRogue, choice) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q: why not dropping this Initialize as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is being done in a followup |
||
return f | ||
} | ||
|
||
// Flat is a naive implementation of a multi-choice snowball instance | ||
|
@@ -29,11 +27,6 @@ type Flat struct { | |
params Parameters | ||
} | ||
|
||
func (f *Flat) Initialize(params Parameters, choice ids.ID) { | ||
f.nnarySnowball.Initialize(params.BetaVirtuous, params.BetaRogue, choice) | ||
f.params = params | ||
} | ||
|
||
func (f *Flat) RecordPoll(votes bag.Bag[ids.ID]) bool { | ||
if pollMode, numVotes := votes.Mode(); numVotes >= f.params.Alpha { | ||
f.RecordSuccessfulPoll(pollMode) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ import ( | |
"github.com/ava-labs/avalanchego/utils/sampler" | ||
) | ||
|
||
type newConsensusFunc func(params Parameters, choice ids.ID) Consensus | ||
|
||
type Network struct { | ||
params Parameters | ||
colors []ids.ID | ||
|
@@ -26,34 +28,44 @@ func (n *Network) Initialize(params Parameters, numColors int) { | |
} | ||
} | ||
|
||
func (n *Network) AddNode(sb Consensus) { | ||
func (n *Network) AddNode(newConsensusFunc newConsensusFunc) Consensus { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for the caller to just give us an initialized |
||
s := sampler.NewUniform() | ||
s.Initialize(uint64(len(n.colors))) | ||
indices, _ := s.Sample(len(n.colors)) | ||
sb.Initialize(n.params, n.colors[int(indices[0])]) | ||
|
||
consensus := newConsensusFunc(n.params, n.colors[int(indices[0])]) | ||
for _, index := range indices[1:] { | ||
sb.Add(n.colors[int(index)]) | ||
consensus.Add(n.colors[int(index)]) | ||
} | ||
|
||
n.nodes = append(n.nodes, sb) | ||
if !sb.Finalized() { | ||
n.running = append(n.running, sb) | ||
n.nodes = append(n.nodes, consensus) | ||
if !consensus.Finalized() { | ||
n.running = append(n.running, consensus) | ||
} | ||
|
||
return consensus | ||
} | ||
|
||
// AddNodeSpecificColor adds [sb] to the network which will initially prefer | ||
// [initialPreference] and additionally adds each of the specified [options] to | ||
// consensus. | ||
func (n *Network) AddNodeSpecificColor(sb Consensus, initialPreference int, options []int) { | ||
sb.Initialize(n.params, n.colors[initialPreference]) | ||
// AddNodeSpecificColor adds a new consensus instance to the network which will | ||
// initially prefer [initialPreference] and additionally adds each of the | ||
// specified [options] to consensus. | ||
func (n *Network) AddNodeSpecificColor( | ||
newConsensusFunc newConsensusFunc, | ||
initialPreference int, | ||
options []int, | ||
) Consensus { | ||
consensus := newConsensusFunc(n.params, n.colors[initialPreference]) | ||
|
||
for _, i := range options { | ||
sb.Add(n.colors[i]) | ||
consensus.Add(n.colors[i]) | ||
} | ||
|
||
n.nodes = append(n.nodes, sb) | ||
if !sb.Finalized() { | ||
n.running = append(n.running, sb) | ||
n.nodes = append(n.nodes, consensus) | ||
if !consensus.Finalized() { | ||
n.running = append(n.running, consensus) | ||
} | ||
|
||
return consensus | ||
} | ||
|
||
// Finalized returns true iff every node added to the network has finished | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it's possible for us to pass parameters into
NewTree
andNewFlat
so we can avoid passing in this function directly