From 1d2f11fb6ead9425ac2237a591cd4eb6d139e88e Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Mon, 25 Sep 2023 13:52:03 +0800 Subject: [PATCH 1/3] snow/consensus/snowman/poll: remove "unused" no early term poller Signed-off-by: Gyuho Lee --- snow/consensus/snowman/poll/no_early_term.go | 68 ------------------- .../snowman/poll/no_early_term_test.go | 66 ------------------ snow/consensus/snowman/poll/set_test.go | 44 ++++++------ 3 files changed, 25 insertions(+), 153 deletions(-) delete mode 100644 snow/consensus/snowman/poll/no_early_term.go delete mode 100644 snow/consensus/snowman/poll/no_early_term_test.go diff --git a/snow/consensus/snowman/poll/no_early_term.go b/snow/consensus/snowman/poll/no_early_term.go deleted file mode 100644 index ed5744d4e66b..000000000000 --- a/snow/consensus/snowman/poll/no_early_term.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package poll - -import ( - "fmt" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/utils/bag" -) - -type noEarlyTermFactory struct{} - -// NewNoEarlyTermFactory returns a factory that returns polls with no early -// termination -func NewNoEarlyTermFactory() Factory { - return noEarlyTermFactory{} -} - -func (noEarlyTermFactory) New(vdrs bag.Bag[ids.NodeID]) Poll { - return &noEarlyTermPoll{polled: vdrs} -} - -// noEarlyTermPoll finishes when all polled validators either respond to the -// query or a timeout occurs -type noEarlyTermPoll struct { - votes bag.Bag[ids.ID] - polled bag.Bag[ids.NodeID] -} - -// Vote registers a response for this poll -func (p *noEarlyTermPoll) Vote(vdr ids.NodeID, vote ids.ID) { - count := p.polled.Count(vdr) - // make sure that a validator can't respond multiple times - p.polled.Remove(vdr) - - // track the votes the validator responded with - p.votes.AddCount(vote, count) -} - -// Drop any future response for this poll -func (p *noEarlyTermPoll) Drop(vdr ids.NodeID) { - p.polled.Remove(vdr) -} - -// Finished returns true when all validators have voted -func (p *noEarlyTermPoll) Finished() bool { - return p.polled.Len() == 0 -} - -// Result returns the result of this poll -func (p *noEarlyTermPoll) Result() bag.Bag[ids.ID] { - return p.votes -} - -func (p *noEarlyTermPoll) PrefixedString(prefix string) string { - return fmt.Sprintf( - "waiting on %s\n%sreceived %s", - p.polled.PrefixedString(prefix), - prefix, - p.votes.PrefixedString(prefix), - ) -} - -func (p *noEarlyTermPoll) String() string { - return p.PrefixedString("") -} diff --git a/snow/consensus/snowman/poll/no_early_term_test.go b/snow/consensus/snowman/poll/no_early_term_test.go deleted file mode 100644 index 0b263825726e..000000000000 --- a/snow/consensus/snowman/poll/no_early_term_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package poll - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/ava-labs/avalanchego/utils/bag" -) - -func TestNoEarlyTermResults(t *testing.T) { - require := require.New(t) - - vdrs := bag.Of(vdr1) // k = 1 - - factory := NewNoEarlyTermFactory() - poll := factory.New(vdrs) - - poll.Vote(vdr1, blkID1) - require.True(poll.Finished()) - - result := poll.Result() - list := result.List() - require.Len(list, 1) - require.Equal(blkID1, list[0]) - require.Equal(1, result.Count(blkID1)) -} - -func TestNoEarlyTermString(t *testing.T) { - vdrs := bag.Of(vdr1, vdr2) // k = 2 - - factory := NewNoEarlyTermFactory() - poll := factory.New(vdrs) - - poll.Vote(vdr1, blkID1) - - expected := `waiting on Bag[ids.NodeID]: (Size = 1) - NodeID-BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp: 1 -received Bag[ids.ID]: (Size = 1) - SYXsAycDPUu4z2ZksJD5fh5nTDcH3vCFHnpcVye5XuJ2jArg: 1` - require.Equal(t, expected, poll.String()) -} - -func TestNoEarlyTermDropsDuplicatedVotes(t *testing.T) { - require := require.New(t) - - vdrs := bag.Of(vdr1, vdr2) // k = 2 - - factory := NewNoEarlyTermFactory() - poll := factory.New(vdrs) - - poll.Vote(vdr1, blkID1) - require.False(poll.Finished()) - - poll.Vote(vdr1, blkID1) - require.False(poll.Finished()) - - poll.Drop(vdr1) - require.False(poll.Finished()) - - poll.Vote(vdr2, blkID1) - require.True(poll.Finished()) -} diff --git a/snow/consensus/snowman/poll/set_test.go b/snow/consensus/snowman/poll/set_test.go index 0c01f5672d9e..007a402b6de8 100644 --- a/snow/consensus/snowman/poll/set_test.go +++ b/snow/consensus/snowman/poll/set_test.go @@ -31,7 +31,7 @@ var ( func TestNewSetErrorOnMetrics(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + factory := NewEarlyTermNoTraversalFactory(1) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() @@ -49,14 +49,15 @@ func TestNewSetErrorOnMetrics(t *testing.T) { func TestCreateAndFinishPollOutOfOrder_NewerFinishesFirst(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + vdrs := []ids.NodeID{vdr1, vdr2, vdr3} // k = 3 + alpha := 3 + + factory := NewEarlyTermNoTraversalFactory(alpha) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() s := NewSet(factory, log, namespace, registerer) - vdrs := []ids.NodeID{vdr1, vdr2, vdr3} // k = 3 - // create two polls for the two blocks vdrBag := bag.Of(vdrs...) require.True(s.Add(1, vdrBag)) @@ -84,14 +85,15 @@ func TestCreateAndFinishPollOutOfOrder_NewerFinishesFirst(t *testing.T) { func TestCreateAndFinishPollOutOfOrder_OlderFinishesFirst(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + vdrs := []ids.NodeID{vdr1, vdr2, vdr3} // k = 3 + alpha := 3 + + factory := NewEarlyTermNoTraversalFactory(alpha) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() s := NewSet(factory, log, namespace, registerer) - vdrs := []ids.NodeID{vdr1, vdr2, vdr3} // k = 3 - // create two polls for the two blocks vdrBag := bag.Of(vdrs...) require.True(s.Add(1, vdrBag)) @@ -119,14 +121,15 @@ func TestCreateAndFinishPollOutOfOrder_OlderFinishesFirst(t *testing.T) { func TestCreateAndFinishPollOutOfOrder_UnfinishedPollsGaps(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + vdrs := []ids.NodeID{vdr1, vdr2, vdr3} // k = 3 + alpha := 3 + + factory := NewEarlyTermNoTraversalFactory(alpha) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() s := NewSet(factory, log, namespace, registerer) - vdrs := []ids.NodeID{vdr1, vdr2, vdr3} // k = 3 - // create three polls for the two blocks vdrBag := bag.Of(vdrs...) require.True(s.Add(1, vdrBag)) @@ -162,14 +165,15 @@ func TestCreateAndFinishPollOutOfOrder_UnfinishedPollsGaps(t *testing.T) { func TestCreateAndFinishSuccessfulPoll(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + vdrs := bag.Of(vdr1, vdr2) // k = 2 + alpha := 2 + + factory := NewEarlyTermNoTraversalFactory(alpha) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() s := NewSet(factory, log, namespace, registerer) - vdrs := bag.Of(vdr1, vdr2) // k = 2 - require.Zero(s.Len()) require.True(s.Add(0, vdrs)) @@ -193,14 +197,15 @@ func TestCreateAndFinishSuccessfulPoll(t *testing.T) { func TestCreateAndFinishFailedPoll(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + vdrs := bag.Of(vdr1, vdr2) // k = 2 + alpha := 1 + + factory := NewEarlyTermNoTraversalFactory(alpha) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() s := NewSet(factory, log, namespace, registerer) - vdrs := bag.Of(vdr1, vdr2) // k = 2 - require.Zero(s.Len()) require.True(s.Add(0, vdrs)) @@ -221,14 +226,15 @@ func TestCreateAndFinishFailedPoll(t *testing.T) { func TestSetString(t *testing.T) { require := require.New(t) - factory := NewNoEarlyTermFactory() + vdrs := bag.Of(vdr1) // k = 1 + alpha := 1 + + factory := NewEarlyTermNoTraversalFactory(alpha) log := logging.NoLog{} namespace := "" registerer := prometheus.NewRegistry() s := NewSet(factory, log, namespace, registerer) - vdrs := bag.Of(vdr1) // k = 1 - expected := `current polls: (Size = 1) RequestID 0: waiting on Bag[ids.NodeID]: (Size = 1) From 0ad68a3b1826cedf0d9c5e177b10a15d1aae15d9 Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Mon, 25 Sep 2023 13:53:43 +0800 Subject: [PATCH 2/3] cleanup imports Signed-off-by: Gyuho Lee --- snow/consensus/snowman/poll/set_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/snow/consensus/snowman/poll/set_test.go b/snow/consensus/snowman/poll/set_test.go index 007a402b6de8..ab27bd3e2605 100644 --- a/snow/consensus/snowman/poll/set_test.go +++ b/snow/consensus/snowman/poll/set_test.go @@ -6,13 +6,12 @@ package poll import ( "testing" - "github.com/stretchr/testify/require" - - "github.com/prometheus/client_golang/prometheus" - "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/bag" "github.com/ava-labs/avalanchego/utils/logging" + + "github.com/prometheus/client_golang/prometheus" + "github.com/stretchr/testify/require" ) var ( From 839f3e7fb4f82259da14ca3b38d503eefbc183f1 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Mon, 25 Sep 2023 13:12:47 -0400 Subject: [PATCH 3/3] reduce diff --- snow/consensus/snowman/poll/set_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/snow/consensus/snowman/poll/set_test.go b/snow/consensus/snowman/poll/set_test.go index ab27bd3e2605..d25af3fe2de2 100644 --- a/snow/consensus/snowman/poll/set_test.go +++ b/snow/consensus/snowman/poll/set_test.go @@ -6,12 +6,13 @@ package poll import ( "testing" + "github.com/prometheus/client_golang/prometheus" + + "github.com/stretchr/testify/require" + "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/bag" "github.com/ava-labs/avalanchego/utils/logging" - - "github.com/prometheus/client_golang/prometheus" - "github.com/stretchr/testify/require" ) var (