Skip to content
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

Update snowman engine #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions snow/consensus/snowman/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type Consensus interface {
) error

// Returns the number of blocks processing
// An instance is finalized if there are no processing blocks
// (all decisions have been finalized, NumProcessing == 0).
// Note that, it is possible that after returning, a new decision may be added such
// that this instance is no longer finalized.
NumProcessing() int

// Adds a new decision. Assumes the dependency has already been added.
Expand All @@ -55,9 +59,4 @@ type Consensus interface {
// RecordPoll collects the results of a network poll. Assumes all decisions
// have been previously added. Returns if a critical error has occurred.
RecordPoll(context.Context, bag.Bag[ids.ID]) error

// Finalized returns true if all decisions that have been added have been
// finalized. Note, it is possible that after returning finalized, a new
// decision may be added such that this instance is no longer finalized.
Finalized() bool
}
38 changes: 19 additions & 19 deletions snow/consensus/snowman/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func InitializeTest(t *testing.T, factory Factory) {

if pref := sm.Preference(); pref != GenesisID {
t.Fatalf("Wrong preference returned")
} else if !sm.Finalized() {
} else if sm.NumProcessing() > 0 {
t.Fatalf("Wrong should have marked the instance as being finalized")
}
}
Expand Down Expand Up @@ -494,15 +494,15 @@ func RecordPollAcceptSingleBlockTest(t *testing.T, factory Factory) {
t.Fatal(err)
} else if pref := sm.Preference(); pref != block.ID() {
t.Fatalf("Preference returned the wrong block")
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Snowman instance finalized too soon")
} else if status := block.Status(); status != choices.Processing {
t.Fatalf("Block's status changed unexpectedly")
} else if err := sm.RecordPoll(context.Background(), votes); err != nil {
t.Fatal(err)
} else if pref := sm.Preference(); pref != block.ID() {
t.Fatalf("Preference returned the wrong block")
} else if !sm.Finalized() {
} else if sm.NumProcessing() > 0 {
t.Fatalf("Snowman instance didn't finalize")
} else if status := block.Status(); status != choices.Accepted {
t.Fatalf("Block's status should have been set to accepted")
Expand Down Expand Up @@ -557,7 +557,7 @@ func RecordPollAcceptAndRejectTest(t *testing.T, factory Factory) {
t.Fatal(err)
} else if pref := sm.Preference(); pref != firstBlock.ID() {
t.Fatalf("Preference returned the wrong block")
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Snowman instance finalized too soon")
} else if status := firstBlock.Status(); status != choices.Processing {
t.Fatalf("Block's status changed unexpectedly")
Expand All @@ -567,7 +567,7 @@ func RecordPollAcceptAndRejectTest(t *testing.T, factory Factory) {
t.Fatal(err)
} else if pref := sm.Preference(); pref != firstBlock.ID() {
t.Fatalf("Preference returned the wrong block")
} else if !sm.Finalized() {
} else if sm.NumProcessing() > 0 {
t.Fatalf("Snowman instance didn't finalize")
} else if status := firstBlock.Status(); status != choices.Accepted {
t.Fatalf("Block's status should have been set to accepted")
Expand Down Expand Up @@ -623,7 +623,7 @@ func RecordPollSplitVoteNoChangeTest(t *testing.T, factory Factory) {
// The first poll will accept shared bits
require.NoError(sm.RecordPoll(context.Background(), votes))
require.Equal(firstBlock.ID(), sm.Preference())
require.False(sm.Finalized())
require.False(!(sm.NumProcessing() > 0))

metrics := gatherCounterGauge(t, registerer)
require.Zero(metrics["polls_failed"])
Expand All @@ -632,7 +632,7 @@ func RecordPollSplitVoteNoChangeTest(t *testing.T, factory Factory) {
// The second poll will do nothing
require.NoError(sm.RecordPoll(context.Background(), votes))
require.Equal(firstBlock.ID(), sm.Preference())
require.False(sm.Finalized())
require.False(!(sm.NumProcessing() > 0))

metrics = gatherCounterGauge(t, registerer)
require.Equal(float64(1), metrics["polls_failed"])
Expand Down Expand Up @@ -661,7 +661,7 @@ func RecordPollWhenFinalizedTest(t *testing.T, factory Factory) {
votes.Add(GenesisID)
if err := sm.RecordPoll(context.Background(), votes); err != nil {
t.Fatal(err)
} else if !sm.Finalized() {
} else if sm.NumProcessing() > 0 {
t.Fatalf("Consensus should still be finalized")
} else if pref := sm.Preference(); GenesisID != pref {
t.Fatalf("Wrong preference listed")
Expand Down Expand Up @@ -737,7 +737,7 @@ func RecordPollRejectTransitivelyTest(t *testing.T, factory Factory) {
// 0
// Tail = 0

if !sm.Finalized() {
if sm.NumProcessing() > 0 {
t.Fatalf("Finalized too late")
} else if pref := sm.Preference(); block0.ID() != pref {
t.Fatalf("Wrong preference listed")
Expand Down Expand Up @@ -822,7 +822,7 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) {
votesFor2.Add(block2.ID())
if err := sm.RecordPoll(context.Background(), votesFor2); err != nil {
t.Fatal(err)
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Finalized too early")
} else if pref := sm.Preference(); block2.ID() != pref {
t.Fatalf("Wrong preference listed")
Expand All @@ -831,13 +831,13 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) {
emptyVotes := bag.Bag[ids.ID]{}
if err := sm.RecordPoll(context.Background(), emptyVotes); err != nil {
t.Fatal(err)
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Finalized too early")
} else if pref := sm.Preference(); block2.ID() != pref {
t.Fatalf("Wrong preference listed")
} else if err := sm.RecordPoll(context.Background(), votesFor2); err != nil {
t.Fatal(err)
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Finalized too early")
} else if pref := sm.Preference(); block2.ID() != pref {
t.Fatalf("Wrong preference listed")
Expand All @@ -847,13 +847,13 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) {
votesFor3.Add(block3.ID())
if err := sm.RecordPoll(context.Background(), votesFor3); err != nil {
t.Fatal(err)
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Finalized too early")
} else if pref := sm.Preference(); block2.ID() != pref {
t.Fatalf("Wrong preference listed")
} else if err := sm.RecordPoll(context.Background(), votesFor3); err != nil {
t.Fatal(err)
} else if !sm.Finalized() {
} else if sm.NumProcessing() > 0 {
t.Fatalf("Finalized too late")
} else if pref := sm.Preference(); block3.ID() != pref {
t.Fatalf("Wrong preference listed")
Expand Down Expand Up @@ -912,7 +912,7 @@ func RecordPollInvalidVoteTest(t *testing.T, factory Factory) {
t.Fatal(err)
} else if err := sm.RecordPoll(context.Background(), validVotes); err != nil {
t.Fatal(err)
} else if sm.Finalized() {
} else if !(sm.NumProcessing() > 0) {
t.Fatalf("Finalized too early")
} else if pref := sm.Preference(); block.ID() != pref {
t.Fatalf("Wrong preference listed")
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func RecordPollTransitiveVotingTest(t *testing.T, factory Factory) {
switch {
case block2.ID() != pref:
t.Fatalf("Wrong preference listed")
case sm.Finalized():
case !(sm.NumProcessing() > 0):
t.Fatalf("Finalized too early")
case block0.Status() != choices.Accepted:
t.Fatalf("Should have accepted")
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func RecordPollTransitiveVotingTest(t *testing.T, factory Factory) {
switch {
case block2.ID() != pref:
t.Fatalf("Wrong preference listed")
case !sm.Finalized():
case sm.NumProcessing() > 0:
t.Fatalf("Finalized too late")
case block0.Status() != choices.Accepted:
t.Fatalf("Should have accepted")
Expand Down Expand Up @@ -1162,7 +1162,7 @@ func RecordPollDivergedVotingTest(t *testing.T, factory Factory) {
votes3.Add(block3.ID())
require.NoError(sm.RecordPoll(context.Background(), votes3))

require.True(sm.Finalized(), "finalized too late")
require.True(!(sm.NumProcessing() > 0), "finalized too late")
require.Equal(choices.Accepted, block0.Status(), "should be accepted")
require.Equal(choices.Rejected, block1.Status())
require.Equal(choices.Rejected, block2.Status())
Expand Down Expand Up @@ -1266,7 +1266,7 @@ func RecordPollDivergedVotingWithNoConflictingBitTest(t *testing.T, factory Fact
votes3.Add(block3.ID())
require.NoError(sm.RecordPoll(context.Background(), votes3))

require.False(sm.Finalized(), "finalized too early")
require.False(!(sm.NumProcessing() > 0), "finalized too early")
require.Equal(choices.Processing, block0.Status())
require.Equal(choices.Processing, block1.Status())
require.Equal(choices.Processing, block2.Status())
Expand Down
2 changes: 1 addition & 1 deletion snow/consensus/snowman/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (n *Network) Round() error {
}

// If this node has been finalized, remove it from the poller
if running.Finalized() {
if running.NumProcessing() == 0 {
newSize := len(n.running) - 1
n.running[runningInd] = n.running[newSize]
n.running = n.running[:newSize]
Expand Down
4 changes: 0 additions & 4 deletions snow/consensus/snowman/topological.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,6 @@ func (ts *Topological) RecordPoll(ctx context.Context, voteBag bag.Bag[ids.ID])
return nil
}

func (ts *Topological) Finalized() bool {
return len(ts.blocks) == 1
}

// HealthCheck returns information about the consensus health.
func (ts *Topological) HealthCheck(context.Context) (interface{}, error) {
numOutstandingBlks := ts.Latency.NumProcessing()
Expand Down
10 changes: 5 additions & 5 deletions snow/engine/snowman/transitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2355,7 +2355,7 @@ func TestEngineRejectionAmplification(t *testing.T) {
t.Fatal(err)
}

if !te.Consensus.Finalized() {
if te.Consensus.NumProcessing() > 0 {
t.Fatalf("Should have finalized the consensus instance")
}

Expand Down Expand Up @@ -2476,15 +2476,15 @@ func TestEngineTransitiveRejectionAmplificationDueToRejectedParent(t *testing.T)
t.Fatal(err)
}

if !te.Consensus.Finalized() {
if te.Consensus.NumProcessing() > 0 {
t.Fatalf("Should have finalized the consensus instance")
}

if err := te.Put(context.Background(), vdr, 0, pendingBlk.Bytes()); err != nil {
t.Fatal(err)
}

if !te.Consensus.Finalized() {
if te.Consensus.NumProcessing() > 0 {
t.Fatalf("Should have finalized the consensus instance")
}

Expand Down Expand Up @@ -2585,15 +2585,15 @@ func TestEngineTransitiveRejectionAmplificationDueToInvalidParent(t *testing.T)
t.Fatal(err)
}

if !te.Consensus.Finalized() {
if te.Consensus.NumProcessing() > 0 {
t.Fatalf("Should have finalized the consensus instance")
}

if err := te.Put(context.Background(), vdr, 0, pendingBlk.Bytes()); err != nil {
t.Fatal(err)
}

if !te.Consensus.Finalized() {
if te.Consensus.NumProcessing() > 0 {
t.Fatalf("Should have finalized the consensus instance")
}

Expand Down
2 changes: 1 addition & 1 deletion snow/engine/snowman/voter.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (v *voter) Update(ctx context.Context) {
return
}

if v.t.Consensus.Finalized() {
if v.t.Consensus.NumProcessing() == 0 {
v.t.Ctx.Log.Debug("Snowman engine can quiesce")
return
}
Expand Down