From 1876f6b5b800d5e161830d7320e3a273b6d86a5f Mon Sep 17 00:00:00 2001 From: Gyuho Lee <6799218+gyuho@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:00:48 -0500 Subject: [PATCH] snow/consensus/snowman/poll: remove "unused" no early term poller (#2095) Signed-off-by: Gyuho Lee Co-authored-by: Stephen Buttolph --- snow/consensus/snowman/poll/no_early_term.go | 68 ------------------- .../snowman/poll/no_early_term_test.go | 66 ------------------ snow/consensus/snowman/poll/set_test.go | 48 +++++++------ 3 files changed, 27 insertions(+), 155 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..d25af3fe2de2 100644 --- a/snow/consensus/snowman/poll/set_test.go +++ b/snow/consensus/snowman/poll/set_test.go @@ -6,10 +6,10 @@ package poll import ( "testing" - "github.com/stretchr/testify/require" - "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" @@ -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)