From 33208a33ee07a3840d6f1d21a82115fb3e815612 Mon Sep 17 00:00:00 2001 From: StephenButtolph Date: Sat, 21 Mar 2020 02:36:37 -0400 Subject: [PATCH] remove more throughcalls --- snow/consensus/snowball/flat.go | 23 +++++-------------- snow/consensus/snowball/nnary_snowball.go | 7 ------ snow/consensus/snowball/nnary_snowflake.go | 4 ++-- snow/consensus/snowball/tree.go | 26 +++++++++------------- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/snow/consensus/snowball/flat.go b/snow/consensus/snowball/flat.go index da4eb1b..21663c4 100644 --- a/snow/consensus/snowball/flat.go +++ b/snow/consensus/snowball/flat.go @@ -15,40 +15,27 @@ func (FlatFactory) New() Consensus { return &Flat{} } // Flat is a naive implementation of a multi-choice snowball instance type Flat struct { + // wraps the n-nary snowball logic + nnarySnowball + // params contains all the configurations of a snowball instance params Parameters - - // snowball wraps the n-nary snowball logic - snowball nnarySnowball } // Initialize implements the Consensus interface func (f *Flat) Initialize(params Parameters, choice ids.ID) { + f.nnarySnowball.Initialize(params.BetaVirtuous, params.BetaRogue, choice) f.params = params - f.snowball.Initialize(params.BetaVirtuous, params.BetaRogue, choice) } // Parameters implements the Consensus interface func (f *Flat) Parameters() Parameters { return f.params } -// Add implements the Consensus interface -func (f *Flat) Add(choice ids.ID) { f.snowball.Add(choice) } - -// Preference implements the Consensus interface -func (f *Flat) Preference() ids.ID { return f.snowball.Preference() } - // RecordPoll implements the Consensus interface func (f *Flat) RecordPoll(votes ids.Bag) { if pollMode, numVotes := votes.Mode(); numVotes >= f.params.Alpha { - f.snowball.RecordSuccessfulPoll(pollMode) + f.nnarySnowball.RecordSuccessfulPoll(pollMode) } else { f.RecordUnsuccessfulPoll() } } - -// RecordUnsuccessfulPoll implements the Consensus interface -func (f *Flat) RecordUnsuccessfulPoll() { f.snowball.RecordUnsuccessfulPoll() } - -// Finalized implements the Consensus interface -func (f *Flat) Finalized() bool { return f.snowball.Finalized() } -func (f *Flat) String() string { return f.snowball.String() } diff --git a/snow/consensus/snowball/nnary_snowball.go b/snow/consensus/snowball/nnary_snowball.go index 9d6aa36..2595622 100644 --- a/snow/consensus/snowball/nnary_snowball.go +++ b/snow/consensus/snowball/nnary_snowball.go @@ -34,9 +34,6 @@ func (sb *nnarySnowball) Initialize(betaVirtuous, betaRogue int, choice ids.ID) sb.numSuccessfulPolls = make(map[[32]byte]int) } -// Add implements the NnarySnowball interface -func (sb *nnarySnowball) Add(choice ids.ID) { sb.nnarySnowflake.Add(choice) } - // Preference implements the NnarySnowball interface func (sb *nnarySnowball) Preference() ids.ID { // It is possible, with low probability, that the snowflake preference is @@ -51,10 +48,6 @@ func (sb *nnarySnowball) Preference() ids.ID { // RecordSuccessfulPoll implements the NnarySnowball interface func (sb *nnarySnowball) RecordSuccessfulPoll(choice ids.ID) { - if sb.Finalized() { - return - } - key := choice.Key() numSuccessfulPolls := sb.numSuccessfulPolls[key] + 1 sb.numSuccessfulPolls[key] = numSuccessfulPolls diff --git a/snow/consensus/snowball/nnary_snowflake.go b/snow/consensus/snowball/nnary_snowflake.go index 4c2685b..8b461f0 100644 --- a/snow/consensus/snowball/nnary_snowflake.go +++ b/snow/consensus/snowball/nnary_snowflake.go @@ -47,7 +47,7 @@ func (sf *nnarySnowflake) Add(choice ids.ID) { sf.rogue = sf.rogue || !choice.Eq // RecordSuccessfulPoll implements the NnarySnowflake interface func (sf *nnarySnowflake) RecordSuccessfulPoll(choice ids.ID) { - if sf.Finalized() { + if sf.finalized { return // This instace is already decided. } @@ -73,6 +73,6 @@ func (sf *nnarySnowflake) Finalized() bool { return sf.finalized } func (sf *nnarySnowflake) String() string { return fmt.Sprintf("SF(Confidence = %d, Finalized = %v, %s)", sf.confidence, - sf.Finalized(), + sf.finalized, &sf.nnarySlush) } diff --git a/snow/consensus/snowball/tree.go b/snow/consensus/snowball/tree.go index d5983d4..92d417f 100644 --- a/snow/consensus/snowball/tree.go +++ b/snow/consensus/snowball/tree.go @@ -18,6 +18,10 @@ func (TreeFactory) New() Consensus { return &Tree{} } // Tree implements the snowball interface by using a modified patricia tree. type Tree struct { + // node is the root that represents the first snowball instance in the tree, + // and contains references to all the other snowball instances in the tree. + node + // params contains all the configurations of a snowball instance params Parameters @@ -31,10 +35,6 @@ type Tree struct { // that any later traversal into this sub-tree should call // RecordUnsuccessfulPoll before performing any other action. shouldReset bool - - // root is the node that represents the first snowball instance in the tree, - // and contains references to all the other snowball instances in the tree. - root node } // Initialize implements the Consensus interface @@ -44,7 +44,7 @@ func (t *Tree) Initialize(params Parameters, choice ids.ID) { snowball := &unarySnowball{} snowball.Initialize(params.BetaVirtuous) - t.root = &unaryNode{ + t.node = &unaryNode{ tree: t, preference: choice, commonPrefix: ids.NumBits, // The initial state has no conflicts @@ -57,20 +57,17 @@ func (t *Tree) Parameters() Parameters { return t.params } // Add implements the Consensus interface func (t *Tree) Add(choice ids.ID) { - prefix := t.root.DecidedPrefix() + prefix := t.node.DecidedPrefix() // Make sure that we haven't already decided against this new id if ids.EqualSubset(0, prefix, t.Preference(), choice) { - t.root = t.root.Add(choice) + t.node = t.node.Add(choice) } } -// Preference implements the Consensus interface -func (t *Tree) Preference() ids.ID { return t.root.Preference() } - // RecordPoll implements the Consensus interface func (t *Tree) RecordPoll(votes ids.Bag) { // Get the assumed decided prefix of the root node. - decidedPrefix := t.root.DecidedPrefix() + decidedPrefix := t.node.DecidedPrefix() // If any of the bits differ from the preference in this prefix, the vote is // for a rejected operation. So, we filter out these invalid votes. @@ -78,7 +75,7 @@ func (t *Tree) RecordPoll(votes ids.Bag) { // Now that the votes have been restricted to valid votes, pass them into // the first snowball instance - t.root = t.root.RecordPoll(filteredVotes, t.shouldReset) + t.node = t.node.RecordPoll(filteredVotes, t.shouldReset) // Because we just passed the reset into the snowball instance, we should no // longer reset. @@ -88,14 +85,11 @@ func (t *Tree) RecordPoll(votes ids.Bag) { // RecordUnsuccessfulPoll implements the Consensus interface func (t *Tree) RecordUnsuccessfulPoll() { t.shouldReset = true } -// Finalized implements the Consensus interface -func (t *Tree) Finalized() bool { return t.root.Finalized() } - func (t *Tree) String() string { builder := strings.Builder{} prefixes := []string{""} - nodes := []node{t.root} + nodes := []node{t.node} for len(prefixes) > 0 { newSize := len(prefixes) - 1