Skip to content

Commit

Permalink
Address code review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Yacov Manevich <[email protected]>
  • Loading branch information
yacovm committed Oct 21, 2024
1 parent e3597d8 commit 1c161b2
Show file tree
Hide file tree
Showing 4 changed files with 283 additions and 75 deletions.
139 changes: 69 additions & 70 deletions snow/consensus/snowball/binary_snowball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ func TestBinarySnowball(t *testing.T) {
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sf := newBinarySnowflake(alphaPreference, terminationConditions, red)

require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb := newBinarySnowball(alphaPreference, terminationConditions, red)
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, red)
require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, red)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.True(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.True(sb.Finalized())
}

func TestBinarySnowballRecordPollPreference(t *testing.T) {
Expand All @@ -51,32 +50,32 @@ func TestBinarySnowballRecordPollPreference(t *testing.T) {
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sf := newBinarySnowflake(alphaPreference, terminationConditions, red)
require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb := newBinarySnowball(alphaPreference, terminationConditions, red)
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, red)
require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, red)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaPreference, red)
require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaPreference, red)
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, red)
require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, red)
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, red)
require.Equal(red, sf.Preference())
require.True(sf.Finalized())
sb.RecordPoll(alphaConfidence, red)
require.Equal(red, sb.Preference())
require.True(sb.Finalized())

expected := "SF(Confidence = [2], Finalized = true, SL(Preference = 0))"
require.Equal(expected, sf.String())
expected := "SB(Preference = 0, PreferenceStrength[0] = 4, PreferenceStrength[1] = 1, SF(Confidence = [2], Finalized = true, SL(Preference = 0)))"
require.Equal(expected, sb.String())
}

func TestBinarySnowballRecordUnsuccessfulPoll(t *testing.T) {
Expand All @@ -89,26 +88,26 @@ func TestBinarySnowballRecordUnsuccessfulPoll(t *testing.T) {
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sf := newBinarySnowflake(alphaPreference, terminationConditions, red)
require.Equal(red, sf.Preference())
require.False(sf.Finalized())
sb := newBinarySnowball(alphaPreference, terminationConditions, red)
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordUnsuccessfulPoll()
sb.RecordUnsuccessfulPoll()

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.False(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sf.Preference())
require.True(sf.Finalized())
sb.RecordPoll(alphaConfidence, blue)
require.Equal(blue, sb.Preference())
require.True(sb.Finalized())

expected := "SF(Confidence = [2], Finalized = true, SL(Preference = 1))"
require.Equal(expected, sf.String())
expected := "SB(Preference = 1, PreferenceStrength[0] = 0, PreferenceStrength[1] = 3, SF(Confidence = [2], Finalized = true, SL(Preference = 1)))"
require.Equal(expected, sb.String())
}

func TestBinarySnowballAcceptWeirdColor(t *testing.T) {
Expand All @@ -121,36 +120,36 @@ func TestBinarySnowballAcceptWeirdColor(t *testing.T) {
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sf := newBinarySnowflake(alphaPreference, terminationConditions, red)
sb := newBinarySnowball(alphaPreference, terminationConditions, red)

require.Equal(red, sf.Preference())
require.False(sf.Finalized())
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, red)
sf.RecordUnsuccessfulPoll()
sb.RecordPoll(alphaConfidence, red)
sb.RecordUnsuccessfulPoll()

require.Equal(red, sf.Preference())
require.False(sf.Finalized())
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, red)
sb.RecordPoll(alphaConfidence, red)

sf.RecordUnsuccessfulPoll()
sb.RecordUnsuccessfulPoll()

require.Equal(red, sf.Preference())
require.False(sf.Finalized())
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
sb.RecordPoll(alphaConfidence, blue)

require.Equal(blue, sf.Preference())
require.False(sf.Finalized())
require.Equal(red, sb.Preference())
require.False(sb.Finalized())

sf.RecordPoll(alphaConfidence, blue)
sb.RecordPoll(alphaConfidence, blue)

require.Equal(blue, sf.Preference())
require.True(sf.Finalized())
require.Equal(blue, sb.Preference())
require.True(sb.Finalized())

expected := "SF(Confidence = [2], Finalized = true, SL(Preference = 0))"
require.Equal(expected, sf.String())
expected := "SB(Preference = 1, PreferenceStrength[0] = 2, PreferenceStrength[1] = 2, SF(Confidence = [2], Finalized = true, SL(Preference = 0)))"
require.Equal(expected, sb.String())
}

func TestBinarySnowballLockColor(t *testing.T) {
Expand Down
133 changes: 133 additions & 0 deletions snow/consensus/snowball/nnary_snowball_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package snowball

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestNnarySnowball(t *testing.T) {
require := require.New(t)

alphaPreference, alphaConfidence := 1, 2
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sb := newNnarySnowball(alphaPreference, terminationConditions, Red)
sb.Add(Blue)
sb.Add(Green)

require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Blue)
require.Equal(Blue, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Red)
require.Equal(Blue, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaPreference, Red)
require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Red)
require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaPreference, Blue)
require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Blue)
require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Blue)
require.Equal(Blue, sb.Preference())
require.True(sb.Finalized())
}

func TestVirtuousNnarySnowball(t *testing.T) {
require := require.New(t)

alphaPreference, alphaConfidence := 1, 2
beta := 1
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sb := newNnarySnowball(alphaPreference, terminationConditions, Red)

require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Red)
require.Equal(Red, sb.Preference())
require.True(sb.Finalized())
}

func TestNarySnowballRecordUnsuccessfulPoll(t *testing.T) {
require := require.New(t)

alphaPreference, alphaConfidence := 1, 2
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sb := newNnarySnowball(alphaPreference, terminationConditions, Red)
sb.Add(Blue)

require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Blue)
require.Equal(Blue, sb.Preference())
require.False(sb.Finalized())

sb.RecordUnsuccessfulPoll()

sb.RecordPoll(alphaConfidence, Blue)

require.Equal(Blue, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Blue)

require.Equal(Blue, sb.Preference())
require.True(sb.Finalized())

expected := "SB(Preference = TtF4d2QWbk5vzQGTEPrN48x6vwgAoAmKQ9cbp79inpQmcRKES, PreferenceStrength = 3, SF(Confidence = [2], Finalized = true, SL(Preference = TtF4d2QWbk5vzQGTEPrN48x6vwgAoAmKQ9cbp79inpQmcRKES)))"
require.Equal(expected, sb.String())

for i := 0; i < 4; i++ {
sb.RecordPoll(alphaConfidence, Red)

require.Equal(Blue, sb.Preference())
require.True(sb.Finalized())
}
}

func TestNarySnowballDifferentSnowflakeColor(t *testing.T) {
require := require.New(t)

alphaPreference, alphaConfidence := 1, 2
beta := 2
terminationConditions := newSingleTerminationCondition(alphaConfidence, beta)

sb := newNnarySnowball(alphaPreference, terminationConditions, Red)
sb.Add(Blue)

require.Equal(Red, sb.Preference())
require.False(sb.Finalized())

sb.RecordPoll(alphaConfidence, Blue)

require.Equal(Blue, sb.nnarySnowflake.Preference())

sb.RecordPoll(alphaConfidence, Red)

require.Equal(Blue, sb.Preference())
require.Equal(Red, sb.nnarySnowflake.Preference())
}
Loading

0 comments on commit 1c161b2

Please sign in to comment.