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

Add last accepted height to the snowman interface #2091

Merged
merged 1 commit into from
Sep 24, 2023
Merged
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
4 changes: 2 additions & 2 deletions snow/consensus/snowman/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type Consensus interface {
// chain.
IsPreferred(Block) bool

// Returns the ID of the last accepted decision.
LastAccepted() ids.ID
// Returns the ID and height of the last accepted decision.
LastAccepted() (ids.ID, uint64)

// Returns the ID of the tail of the strongly preferred sequence of
// decisions.
Expand Down
91 changes: 87 additions & 4 deletions snow/consensus/snowman/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var (
RecordPollDivergedVotingTest,
RecordPollDivergedVotingWithNoConflictingBitTest,
RecordPollChangePreferredChainTest,
LastAcceptedTest,
MetricsProcessingErrorTest,
MetricsAcceptedErrorTest,
MetricsRejectedErrorTest,
Expand All @@ -65,8 +66,8 @@ var (
ErrorOnRejectSiblingTest,
ErrorOnTransitiveRejectionTest,
RandomizedConsistencyTest,
ErrorOnAddDecidedBlock,
ErrorOnAddDuplicateBlockID,
ErrorOnAddDecidedBlockTest,
ErrorOnAddDuplicateBlockIDTest,
}

errTest = errors.New("non-nil error")
Expand Down Expand Up @@ -1171,6 +1172,88 @@ func RecordPollChangePreferredChainTest(t *testing.T, factory Factory) {
require.False(sm.IsPreferred(b2Block))
}

func LastAcceptedTest(t *testing.T, factory Factory) {
sm := factory.New()
require := require.New(t)

ctx := snow.DefaultConsensusContextTest()
params := snowball.Parameters{
K: 1,
Alpha: 1,
BetaVirtuous: 1,
BetaRogue: 2,
ConcurrentRepolls: 1,
OptimalProcessing: 1,
MaxOutstandingItems: 1,
MaxItemProcessingTime: 1,
}
require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp))

block0 := &TestBlock{
TestDecidable: choices.TestDecidable{
IDV: ids.GenerateTestID(),
StatusV: choices.Processing,
},
ParentV: Genesis.IDV,
HeightV: Genesis.HeightV + 1,
}
block1 := &TestBlock{
TestDecidable: choices.TestDecidable{
IDV: ids.GenerateTestID(),
StatusV: choices.Processing,
},
ParentV: block0.IDV,
HeightV: block0.HeightV + 1,
}
block2 := &TestBlock{
TestDecidable: choices.TestDecidable{
IDV: ids.GenerateTestID(),
StatusV: choices.Processing,
},
ParentV: block1.IDV,
HeightV: block1.HeightV + 1,
}
block1Conflict := &TestBlock{
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
TestDecidable: choices.TestDecidable{
IDV: ids.GenerateTestID(),
StatusV: choices.Processing,
},
ParentV: block0.IDV,
HeightV: block0.HeightV + 1,
}

lastAcceptedID, lastAcceptedHeight := sm.LastAccepted()
require.Equal(GenesisID, lastAcceptedID)
require.Equal(GenesisHeight, lastAcceptedHeight)

require.NoError(sm.Add(context.Background(), block0))
require.NoError(sm.Add(context.Background(), block1))
require.NoError(sm.Add(context.Background(), block1Conflict))
require.NoError(sm.Add(context.Background(), block2))

lastAcceptedID, lastAcceptedHeight = sm.LastAccepted()
require.Equal(GenesisID, lastAcceptedID)
require.Equal(GenesisHeight, lastAcceptedHeight)

require.NoError(sm.RecordPoll(context.Background(), bag.Of(block1.IDV)))

lastAcceptedID, lastAcceptedHeight = sm.LastAccepted()
require.Equal(block0.IDV, lastAcceptedID)
require.Equal(block0.HeightV, lastAcceptedHeight)

require.NoError(sm.RecordPoll(context.Background(), bag.Of(block1.IDV)))

lastAcceptedID, lastAcceptedHeight = sm.LastAccepted()
require.Equal(block1.IDV, lastAcceptedID)
require.Equal(block1.HeightV, lastAcceptedHeight)

require.NoError(sm.RecordPoll(context.Background(), bag.Of(block2.IDV)))

lastAcceptedID, lastAcceptedHeight = sm.LastAccepted()
require.Equal(block2.IDV, lastAcceptedID)
require.Equal(block2.HeightV, lastAcceptedHeight)
}

func MetricsProcessingErrorTest(t *testing.T, factory Factory) {
require := require.New(t)

Expand Down Expand Up @@ -1461,7 +1544,7 @@ func RandomizedConsistencyTest(t *testing.T, factory Factory) {
require.True(n.Agreement())
}

func ErrorOnAddDecidedBlock(t *testing.T, factory Factory) {
func ErrorOnAddDecidedBlockTest(t *testing.T, factory Factory) {
sm := factory.New()
require := require.New(t)

Expand Down Expand Up @@ -1490,7 +1573,7 @@ func ErrorOnAddDecidedBlock(t *testing.T, factory Factory) {
require.ErrorIs(err, errDuplicateAdd)
}

func ErrorOnAddDuplicateBlockID(t *testing.T, factory Factory) {
func ErrorOnAddDuplicateBlockIDTest(t *testing.T, factory Factory) {
sm := factory.New()
require := require.New(t)

Expand Down
4 changes: 2 additions & 2 deletions snow/consensus/snowman/topological.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ func (ts *Topological) IsPreferred(blk Block) bool {
return ts.preferredIDs.Contains(blk.ID())
}

func (ts *Topological) LastAccepted() ids.ID {
return ts.head
func (ts *Topological) LastAccepted() (ids.ID, uint64) {
return ts.head, ts.height
}

func (ts *Topological) Preference() ids.ID {
Expand Down
2 changes: 1 addition & 1 deletion snow/engine/snowman/transitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (t *Transitive) GetBlock(ctx context.Context, blkID ids.ID) (snowman.Block,
}

func (t *Transitive) sendChits(ctx context.Context, nodeID ids.NodeID, requestID uint32) {
lastAccepted := t.Consensus.LastAccepted()
lastAccepted, _ := t.Consensus.LastAccepted()
// If we aren't fully verifying blocks, only vote for blocks that are widely
// preferred by the validator set.
if t.Ctx.StateSyncing.Get() || t.Config.PartialSync {
Expand Down