diff --git a/scripts/mocks.mockgen.txt b/scripts/mocks.mockgen.txt index ba2be886b0b6..139252666ab0 100644 --- a/scripts/mocks.mockgen.txt +++ b/scripts/mocks.mockgen.txt @@ -5,7 +5,7 @@ github.com/ava-labs/avalanchego/database=Batch=database/mock_batch.go github.com/ava-labs/avalanchego/database=Iterator=database/mock_iterator.go github.com/ava-labs/avalanchego/message=OutboundMessage=message/mock_message.go github.com/ava-labs/avalanchego/message=OutboundMsgBuilder=message/mock_outbound_message_builder.go -github.com/ava-labs/avalanchego/snow/consensus/snowman=Block=snow/consensus/snowman/mock_block.go +github.com/ava-labs/avalanchego/snow/consensus/snowman=Block=snow/consensus/snowman/snowmantest/mock_block.go github.com/ava-labs/avalanchego/snow/engine/avalanche/vertex=LinearizableVM=snow/engine/avalanche/vertex/mock_vm.go github.com/ava-labs/avalanchego/snow/engine/snowman/block=BuildBlockWithContextChainVM=snow/engine/snowman/block/mock_build_block_with_context_vm.go github.com/ava-labs/avalanchego/snow/engine/snowman/block=ChainVM=snow/engine/snowman/block/mock_chain_vm.go diff --git a/snow/choices/test_decidable.go b/snow/choices/test_decidable.go index 39e8ed67b7c1..e750c2465f91 100644 --- a/snow/choices/test_decidable.go +++ b/snow/choices/test_decidable.go @@ -48,3 +48,7 @@ func (d *TestDecidable) Reject(context.Context) error { func (d *TestDecidable) Status() Status { return d.StatusV } + +func (d *TestDecidable) SetStatus(status Status) { + d.StatusV = status +} diff --git a/snow/consensus/snowman/consensus_test.go b/snow/consensus/snowman/consensus_test.go index 01ae6199cea5..d52e76759512 100644 --- a/snow/consensus/snowman/consensus_test.go +++ b/snow/consensus/snowman/consensus_test.go @@ -11,7 +11,6 @@ import ( "runtime" "strings" "testing" - "time" "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/require" @@ -20,6 +19,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowball" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/utils/bag" ) @@ -27,14 +27,6 @@ import ( type testFunc func(*testing.T, Factory) var ( - GenesisID = ids.Empty.Prefix(0) - GenesisHeight = uint64(0) - GenesisTimestamp = time.Unix(1, 0) - Genesis = &TestBlock{TestDecidable: choices.TestDecidable{ - IDV: GenesisID, - StatusV: choices.Accepted, - }} - testFuncs = []testFunc{ InitializeTest, NumProcessingTest, @@ -65,7 +57,6 @@ var ( ErrorOnTransitiveRejectionTest, RandomizedConsistencyTest, ErrorOnAddDecidedBlockTest, - ErrorOnAddDuplicateBlockIDTest, RecordPollWithDefaultParameters, RecordPollRegressionCalculateInDegreeIndegreeCalculation, } @@ -105,9 +96,15 @@ func InitializeTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - require.Equal(GenesisID, sm.Preference()) + require.Equal(snowmantest.GenesisID, sm.Preference()) require.Zero(sm.NumProcessing()) } @@ -129,27 +126,24 @@ func NumProcessingTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) require.Zero(sm.NumProcessing()) // Adding to the previous preference will update the preference require.NoError(sm.Add(context.Background(), block)) - require.Equal(1, sm.NumProcessing()) votes := bag.Of(block.ID()) require.NoError(sm.RecordPoll(context.Background(), votes)) - require.Zero(sm.NumProcessing()) } @@ -171,16 +165,15 @@ func AddToTailTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) // Adding to the previous preference will update the preference require.NoError(sm.Add(context.Background(), block)) @@ -210,24 +203,16 @@ func AddToNonTailTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - firstBlock := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - secondBlock := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + firstBlock := snowmantest.BuildChild(snowmantest.Genesis) + secondBlock := snowmantest.BuildChild(snowmantest.Genesis) // Adding to the previous preference will update the preference require.NoError(sm.Add(context.Background(), firstBlock)) @@ -258,26 +243,27 @@ func AddToUnknownTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - parent := &TestBlock{TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Unknown, - }} - - block := &TestBlock{ + block := &snowmantest.Block{ TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), + IDV: ids.GenerateTestID(), StatusV: choices.Processing, }, - ParentV: parent.IDV, - HeightV: parent.HeightV + 1, + ParentV: ids.GenerateTestID(), + HeightV: snowmantest.GenesisHeight + 2, } // Adding a block with an unknown parent means the parent must have already // been rejected. Therefore the block should be immediately rejected require.NoError(sm.Add(context.Background(), block)) - require.Equal(GenesisID, sm.Preference()) + require.Equal(snowmantest.GenesisID, sm.Preference()) require.Equal(choices.Rejected, block.Status()) } @@ -298,16 +284,22 @@ func StatusOrProcessingPreviouslyAcceptedTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - require.Equal(choices.Accepted, Genesis.Status()) - require.False(sm.Processing(Genesis.ID())) - require.True(sm.Decided(Genesis)) - require.True(sm.IsPreferred(Genesis)) + require.Equal(choices.Accepted, snowmantest.Genesis.Status()) + require.False(sm.Processing(snowmantest.Genesis.ID())) + require.True(sm.Decided(snowmantest.Genesis)) + require.True(sm.IsPreferred(snowmantest.Genesis)) - pref, ok := sm.PreferenceAtHeight(Genesis.Height()) + pref, ok := sm.PreferenceAtHeight(snowmantest.Genesis.Height()) require.True(ok) - require.Equal(Genesis.ID(), pref) + require.Equal(snowmantest.Genesis.ID(), pref) } func StatusOrProcessingPreviouslyRejectedTest(t *testing.T, factory Factory) { @@ -327,16 +319,16 @@ func StatusOrProcessingPreviouslyRejectedTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Rejected, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) + require.NoError(block.Reject(context.Background())) require.Equal(choices.Rejected, block.Status()) require.False(sm.Processing(block.ID())) @@ -364,16 +356,15 @@ func StatusOrProcessingUnissuedTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) require.Equal(choices.Processing, block.Status()) require.False(sm.Processing(block.ID())) @@ -401,16 +392,15 @@ func StatusOrProcessingIssuedTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) require.NoError(sm.Add(context.Background(), block)) require.Equal(choices.Processing, block.Status()) @@ -440,16 +430,15 @@ func RecordPollAcceptSingleBlockTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) require.NoError(sm.Add(context.Background(), block)) @@ -482,24 +471,16 @@ func RecordPollAcceptAndRejectTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - firstBlock := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - secondBlock := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + firstBlock := snowmantest.BuildChild(snowmantest.Genesis) + secondBlock := snowmantest.BuildChild(snowmantest.Genesis) require.NoError(sm.Add(context.Background(), firstBlock)) require.NoError(sm.Add(context.Background(), secondBlock)) @@ -538,24 +519,16 @@ func RecordPollSplitVoteNoChangeTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - firstBlock := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - secondBlock := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + firstBlock := snowmantest.BuildChild(snowmantest.Genesis) + secondBlock := snowmantest.BuildChild(snowmantest.Genesis) require.NoError(sm.Add(context.Background(), firstBlock)) require.NoError(sm.Add(context.Background(), secondBlock)) @@ -598,12 +571,18 @@ func RecordPollWhenFinalizedTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - votes := bag.Of(GenesisID) + votes := bag.Of(snowmantest.GenesisID) require.NoError(sm.RecordPoll(context.Background(), votes)) require.Zero(sm.NumProcessing()) - require.Equal(GenesisID, sm.Preference()) + require.Equal(snowmantest.GenesisID, sm.Preference()) } func RecordPollRejectTransitivelyTest(t *testing.T, factory Factory) { @@ -623,32 +602,17 @@ func RecordPollRejectTransitivelyTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block2 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3), - StatusV: choices.Processing, - }, - ParentV: block1.IDV, - HeightV: block1.HeightV + 1, - } + block0 := snowmantest.BuildChild(snowmantest.Genesis) + block1 := snowmantest.BuildChild(snowmantest.Genesis) + block2 := snowmantest.BuildChild(block1) require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -693,40 +657,18 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block2 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3), - StatusV: choices.Processing, - }, - ParentV: block1.IDV, - HeightV: block1.HeightV + 1, - } - block3 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(4), - StatusV: choices.Processing, - }, - ParentV: block1.IDV, - HeightV: block1.HeightV + 1, - } + block0 := snowmantest.BuildChild(snowmantest.Genesis) + block1 := snowmantest.BuildChild(snowmantest.Genesis) + block2 := snowmantest.BuildChild(block1) + block3 := snowmantest.BuildChild(block1) require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -785,17 +727,16 @@ func RecordPollInvalidVoteTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - unknownBlockID := ids.Empty.Prefix(2) + block := snowmantest.BuildChild(snowmantest.Genesis) + unknownBlockID := ids.GenerateTestID() require.NoError(sm.Add(context.Background(), block)) @@ -826,48 +767,19 @@ func RecordPollTransitiveVotingTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: block0.IDV, - HeightV: block0.HeightV + 1, - } - block2 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3), - StatusV: choices.Processing, - }, - ParentV: block1.IDV, - HeightV: block1.HeightV + 1, - } - block3 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(4), - StatusV: choices.Processing, - }, - ParentV: block0.IDV, - HeightV: block0.HeightV + 1, - } - block4 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(5), - StatusV: choices.Processing, - }, - ParentV: block3.IDV, - HeightV: block3.HeightV + 1, - } + block0 := snowmantest.BuildChild(snowmantest.Genesis) + block1 := snowmantest.BuildChild(block0) + block2 := snowmantest.BuildChild(block1) + block3 := snowmantest.BuildChild(block0) + block4 := snowmantest.BuildChild(block3) require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -936,40 +848,39 @@ func RecordPollDivergedVotingWithNoConflictingBitTest(t *testing.T, factory Fact MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ + block0 := &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: ids.ID{0x06}, // 0110 StatusV: choices.Processing, }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, + ParentV: snowmantest.GenesisID, + HeightV: snowmantest.GenesisHeight + 1, } - block1 := &TestBlock{ + block1 := &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: ids.ID{0x08}, // 0001 StatusV: choices.Processing, }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, + ParentV: snowmantest.GenesisID, + HeightV: snowmantest.GenesisHeight + 1, } - block2 := &TestBlock{ + block2 := &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: ids.ID{0x01}, // 1000 StatusV: choices.Processing, }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block3 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: block2.IDV, - HeightV: block2.HeightV + 1, + ParentV: snowmantest.GenesisID, + HeightV: snowmantest.GenesisHeight + 1, } + block3 := snowmantest.BuildChild(block2) require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -1040,40 +951,18 @@ func RecordPollChangePreferredChainTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - a1Block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - b1Block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - a2Block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: a1Block.IDV, - HeightV: a1Block.HeightV + 1, - } - b2Block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: b1Block.IDV, - HeightV: b1Block.HeightV + 1, - } + a1Block := snowmantest.BuildChild(snowmantest.Genesis) + b1Block := snowmantest.BuildChild(snowmantest.Genesis) + a2Block := snowmantest.BuildChild(a1Block) + b2Block := snowmantest.BuildChild(b1Block) require.NoError(sm.Add(context.Background(), a1Block)) require.NoError(sm.Add(context.Background(), a2Block)) @@ -1147,44 +1036,22 @@ func LastAcceptedTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.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{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: block0.IDV, - HeightV: block0.HeightV + 1, - } + block0 := snowmantest.BuildChild(snowmantest.Genesis) + block1 := snowmantest.BuildChild(block0) + block2 := snowmantest.BuildChild(block1) + block1Conflict := snowmantest.BuildChild(block0) lastAcceptedID, lastAcceptedHeight := sm.LastAccepted() - require.Equal(GenesisID, lastAcceptedID) - require.Equal(GenesisHeight, lastAcceptedHeight) + require.Equal(snowmantest.GenesisID, lastAcceptedID) + require.Equal(snowmantest.GenesisHeight, lastAcceptedHeight) require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -1192,14 +1059,14 @@ func LastAcceptedTest(t *testing.T, factory Factory) { require.NoError(sm.Add(context.Background(), block2)) lastAcceptedID, lastAcceptedHeight = sm.LastAccepted() - require.Equal(GenesisID, lastAcceptedID) - require.Equal(GenesisHeight, lastAcceptedHeight) + require.Equal(snowmantest.GenesisID, lastAcceptedID) + require.Equal(snowmantest.GenesisHeight, lastAcceptedHeight) require.NoError(sm.RecordPoll(context.Background(), bag.Of(block0.IDV))) lastAcceptedID, lastAcceptedHeight = sm.LastAccepted() - require.Equal(GenesisID, lastAcceptedID) - require.Equal(GenesisHeight, lastAcceptedHeight) + require.Equal(snowmantest.GenesisID, lastAcceptedID) + require.Equal(snowmantest.GenesisHeight, lastAcceptedHeight) require.NoError(sm.RecordPoll(context.Background(), bag.Of(block1.IDV))) @@ -1244,14 +1111,19 @@ func MetricsProcessingErrorTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - numProcessing := prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "blks_processing", - }) + numProcessing := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "blks_processing", + }) require.NoError(ctx.Registerer.Register(numProcessing)) - err := sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp) + err := sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + ) require.Error(err) //nolint:forbidigo // error is not exported https://github.com/prometheus/client_golang/blob/main/prometheus/registry.go#L315 } @@ -1273,14 +1145,19 @@ func MetricsAcceptedErrorTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - numAccepted := prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "blks_accepted_count", - }) + numAccepted := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "blks_accepted_count", + }) require.NoError(ctx.Registerer.Register(numAccepted)) - err := sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp) + err := sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + ) require.Error(err) //nolint:forbidigo // error is not exported https://github.com/prometheus/client_golang/blob/main/prometheus/registry.go#L315 } @@ -1302,14 +1179,19 @@ func MetricsRejectedErrorTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - numRejected := prometheus.NewGauge( - prometheus.GaugeOpts{ - Name: "blks_rejected_count", - }) + numRejected := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "blks_rejected_count", + }) require.NoError(ctx.Registerer.Register(numRejected)) - err := sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp) + err := sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + ) require.Error(err) //nolint:forbidigo // error is not exported https://github.com/prometheus/client_golang/blob/main/prometheus/registry.go#L315 } @@ -1331,21 +1213,22 @@ func ErrorOnInitialRejectionTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - rejectedBlock := &TestBlock{TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Rejected, - }} - - block := &TestBlock{ + block := &snowmantest.Block{ TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), + IDV: ids.GenerateTestID(), RejectV: errTest, StatusV: choices.Processing, }, - ParentV: rejectedBlock.IDV, - HeightV: rejectedBlock.HeightV + 1, + ParentV: ids.GenerateTestID(), + HeightV: snowmantest.GenesisHeight + 1, } err := sm.Add(context.Background(), block) @@ -1370,17 +1253,16 @@ func ErrorOnAcceptTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - AcceptV: errTest, - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block := snowmantest.BuildChild(snowmantest.Genesis) + block.AcceptV = errTest require.NoError(sm.Add(context.Background(), block)) @@ -1407,25 +1289,17 @@ func ErrorOnRejectSiblingTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - RejectV: errTest, - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + block0 := snowmantest.BuildChild(snowmantest.Genesis) + block1 := snowmantest.BuildChild(snowmantest.Genesis) + block1.RejectV = errTest require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -1453,33 +1327,18 @@ func ErrorOnTransitiveRejectionTest(t *testing.T, factory Factory) { MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2), - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block2 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3), - RejectV: errTest, - StatusV: choices.Processing, - }, - ParentV: block1.IDV, - HeightV: block1.HeightV + 1, - } + block0 := snowmantest.BuildChild(snowmantest.Genesis) + block1 := snowmantest.BuildChild(snowmantest.Genesis) + block2 := snowmantest.BuildChild(block1) + block2.RejectV = errTest require.NoError(sm.Add(context.Background(), block0)) require.NoError(sm.Add(context.Background(), block1)) @@ -1541,57 +1400,18 @@ func ErrorOnAddDecidedBlockTest(t *testing.T, factory Factory) { MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{0x03}, // 0b0011 - StatusV: choices.Accepted, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - err := sm.Add(context.Background(), block0) - require.ErrorIs(err, errDuplicateAdd) -} - -func ErrorOnAddDuplicateBlockIDTest(t *testing.T, factory Factory) { - sm := factory.New() - require := require.New(t) - - snowCtx := snowtest.Context(t, snowtest.CChainID) - ctx := snowtest.ConsensusContext(snowCtx) - params := snowball.Parameters{ - K: 1, - AlphaPreference: 1, - AlphaConfidence: 1, - Beta: 1, - ConcurrentRepolls: 1, - OptimalProcessing: 1, - MaxOutstandingItems: 1, - MaxItemProcessingTime: 1, - } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + block := snowmantest.BuildChild(snowmantest.Genesis) + require.NoError(block.Accept(context.Background())) - block0 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{0x03}, // 0b0011 - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - block1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{0x03}, // 0b0011, same as block0 - StatusV: choices.Processing, - }, - ParentV: block0.IDV, - HeightV: block0.HeightV + 1, - } - - require.NoError(sm.Add(context.Background(), block0)) - err := sm.Add(context.Background(), block1) + err := sm.Add(context.Background(), block) require.ErrorIs(err, errDuplicateAdd) } @@ -1626,25 +1446,18 @@ func RecordPollWithDefaultParameters(t *testing.T, factory Factory) { snowCtx := snowtest.Context(t, snowtest.CChainID) ctx := snowtest.ConsensusContext(snowCtx) params := snowball.DefaultParameters - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) // "blk1" and "blk2" are in conflict - blk1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{1}, - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - blk2 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{2}, - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } + blk1 := snowmantest.BuildChild(snowmantest.Genesis) + blk2 := snowmantest.BuildChild(snowmantest.Genesis) + require.NoError(sm.Add(context.Background(), blk1)) require.NoError(sm.Add(context.Background(), blk2)) @@ -1679,32 +1492,18 @@ func RecordPollRegressionCalculateInDegreeIndegreeCalculation(t *testing.T, fact MaxOutstandingItems: 1, MaxItemProcessingTime: 1, } - require.NoError(sm.Initialize(ctx, params, GenesisID, GenesisHeight, GenesisTimestamp)) + require.NoError(sm.Initialize( + ctx, + params, + snowmantest.GenesisID, + snowmantest.GenesisHeight, + snowmantest.GenesisTimestamp, + )) + + blk1 := snowmantest.BuildChild(snowmantest.Genesis) + blk2 := snowmantest.BuildChild(blk1) + blk3 := snowmantest.BuildChild(blk2) - blk1 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{1}, - StatusV: choices.Processing, - }, - ParentV: Genesis.IDV, - HeightV: Genesis.HeightV + 1, - } - blk2 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{2}, - StatusV: choices.Processing, - }, - ParentV: blk1.IDV, - HeightV: blk1.HeightV + 1, - } - blk3 := &TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{3}, - StatusV: choices.Processing, - }, - ParentV: blk2.IDV, - HeightV: blk2.HeightV + 1, - } require.NoError(sm.Add(context.Background(), blk1)) require.NoError(sm.Add(context.Background(), blk2)) require.NoError(sm.Add(context.Background(), blk3)) diff --git a/snow/consensus/snowman/network_test.go b/snow/consensus/snowman/network_test.go index aead346fb5e4..eec42f017953 100644 --- a/snow/consensus/snowman/network_test.go +++ b/snow/consensus/snowman/network_test.go @@ -10,6 +10,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowball" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/bag" @@ -18,7 +19,7 @@ import ( type Network struct { params snowball.Parameters - colors []*TestBlock + colors []*snowmantest.Block rngSource sampler.Source nodes, running []Consensus } @@ -26,13 +27,13 @@ type Network struct { func NewNetwork(params snowball.Parameters, numColors int, rngSource sampler.Source) *Network { n := &Network{ params: params, - colors: []*TestBlock{{ + colors: []*snowmantest.Block{{ TestDecidable: choices.TestDecidable{ IDV: ids.Empty.Prefix(rngSource.Uint64()), StatusV: choices.Processing, }, - ParentV: Genesis.IDV, - HeightV: 1, + ParentV: snowmantest.GenesisID, + HeightV: snowmantest.GenesisHeight + 1, }}, rngSource: rngSource, } @@ -42,7 +43,7 @@ func NewNetwork(params snowball.Parameters, numColors int, rngSource sampler.Sou s.Initialize(uint64(len(n.colors))) dependencyInd, _ := s.Next() dependency := n.colors[dependencyInd] - n.colors = append(n.colors, &TestBlock{ + n.colors = append(n.colors, &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: ids.Empty.Prefix(rngSource.Uint64()), StatusV: choices.Processing, @@ -58,7 +59,7 @@ func (n *Network) shuffleColors() { s := sampler.NewDeterministicUniform(n.rngSource) s.Initialize(uint64(len(n.colors))) indices, _ := s.Sample(len(n.colors)) - colors := []*TestBlock(nil) + colors := []*snowmantest.Block(nil) for _, index := range indices { colors = append(colors, n.colors[int(index)]) } @@ -69,7 +70,7 @@ func (n *Network) shuffleColors() { func (n *Network) AddNode(t testing.TB, sm Consensus) error { snowCtx := snowtest.Context(t, snowtest.CChainID) ctx := snowtest.ConsensusContext(snowCtx) - if err := sm.Initialize(ctx, n.params, Genesis.ID(), Genesis.Height(), Genesis.Timestamp()); err != nil { + if err := sm.Initialize(ctx, n.params, snowmantest.GenesisID, snowmantest.GenesisHeight, snowmantest.GenesisTimestamp); err != nil { return err } @@ -80,7 +81,7 @@ func (n *Network) AddNode(t testing.TB, sm Consensus) error { if !found { myDep = blk.Parent() } - myVtx := &TestBlock{ + myBlock := &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: blk.ID(), StatusV: blk.Status(), @@ -90,10 +91,10 @@ func (n *Network) AddNode(t testing.TB, sm Consensus) error { VerifyV: blk.Verify(context.Background()), BytesV: blk.Bytes(), } - if err := sm.Add(context.Background(), myVtx); err != nil { + if err := sm.Add(context.Background(), myBlock); err != nil { return err } - deps[myVtx.ID()] = myDep + deps[myBlock.ID()] = myDep } n.nodes = append(n.nodes, sm) n.running = append(n.running, sm) diff --git a/snow/consensus/snowman/snowmantest/block.go b/snow/consensus/snowman/snowmantest/block.go new file mode 100644 index 000000000000..d77ee9759136 --- /dev/null +++ b/snow/consensus/snowman/snowmantest/block.go @@ -0,0 +1,102 @@ +// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package snowmantest + +import ( + "cmp" + "context" + "time" + + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/snow/choices" + "github.com/ava-labs/avalanchego/utils" +) + +const ( + GenesisHeight uint64 = 0 + GenesisUnixTimestamp int64 = 1 +) + +var ( + _ utils.Sortable[*Block] = (*Block)(nil) + + GenesisID = ids.GenerateTestID() + GenesisTimestamp = time.Unix(GenesisUnixTimestamp, 0) + GenesisBytes = GenesisID[:] + Genesis = BuildChain(1)[0] +) + +func BuildChild(parent *Block) *Block { + blkID := ids.GenerateTestID() + return &Block{ + TestDecidable: choices.TestDecidable{ + IDV: blkID, + StatusV: choices.Processing, + }, + ParentV: parent.ID(), + HeightV: parent.Height() + 1, + TimestampV: parent.Timestamp(), + BytesV: blkID[:], + } +} + +func BuildChain(length int) []*Block { + if length == 0 { + return nil + } + + genesis := &Block{ + TestDecidable: choices.TestDecidable{ + IDV: GenesisID, + StatusV: choices.Accepted, + }, + HeightV: GenesisHeight, + TimestampV: GenesisTimestamp, + BytesV: GenesisBytes, + } + return append([]*Block{genesis}, BuildDescendants(genesis, length-1)...) +} + +func BuildDescendants(parent *Block, length int) []*Block { + chain := make([]*Block, length) + for i := range chain { + parent = BuildChild(parent) + chain[i] = parent + } + return chain +} + +type Block struct { + choices.TestDecidable + + ParentV ids.ID + HeightV uint64 + TimestampV time.Time + VerifyV error + BytesV []byte +} + +func (b *Block) Parent() ids.ID { + return b.ParentV +} + +func (b *Block) Height() uint64 { + return b.HeightV +} + +func (b *Block) Timestamp() time.Time { + return b.TimestampV +} + +func (b *Block) Verify(context.Context) error { + return b.VerifyV +} + +func (b *Block) Bytes() []byte { + return b.BytesV +} + +func (b *Block) Compare(other *Block) int { + return cmp.Compare(b.HeightV, other.HeightV) +} diff --git a/snow/consensus/snowman/mock_block.go b/snow/consensus/snowman/snowmantest/mock_block.go similarity index 95% rename from snow/consensus/snowman/mock_block.go rename to snow/consensus/snowman/snowmantest/mock_block.go index 45393bfe7bdb..4c1a4ae7beec 100644 --- a/snow/consensus/snowman/mock_block.go +++ b/snow/consensus/snowman/snowmantest/mock_block.go @@ -3,11 +3,11 @@ // // Generated by this command: // -// mockgen -package=snowman -destination=snow/consensus/snowman/mock_block.go github.com/ava-labs/avalanchego/snow/consensus/snowman Block +// mockgen -package=snowmantest -destination=snow/consensus/snowman/snowmantest/mock_block.go github.com/ava-labs/avalanchego/snow/consensus/snowman Block // -// Package snowman is a generated GoMock package. -package snowman +// Package snowmantest is a generated GoMock package. +package snowmantest import ( context "context" diff --git a/snow/consensus/snowman/test_block.go b/snow/consensus/snowman/test_block.go deleted file mode 100644 index a32872adbb96..000000000000 --- a/snow/consensus/snowman/test_block.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package snowman - -import ( - "cmp" - "context" - "time" - - "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/snow/choices" - "github.com/ava-labs/avalanchego/utils" -) - -var ( - _ Block = (*TestBlock)(nil) - _ utils.Sortable[*TestBlock] = (*TestBlock)(nil) -) - -// TestBlock is a useful test block -type TestBlock struct { - choices.TestDecidable - - ParentV ids.ID - HeightV uint64 - TimestampV time.Time - VerifyV error - BytesV []byte -} - -func (b *TestBlock) Parent() ids.ID { - return b.ParentV -} - -func (b *TestBlock) Height() uint64 { - return b.HeightV -} - -func (b *TestBlock) Timestamp() time.Time { - return b.TimestampV -} - -func (b *TestBlock) Verify(context.Context) error { - return b.VerifyV -} - -func (b *TestBlock) Bytes() []byte { - return b.BytesV -} - -func (b *TestBlock) Compare(other *TestBlock) int { - return cmp.Compare(b.HeightV, other.HeightV) -} diff --git a/snow/engine/snowman/bootstrap/bootstrapper_test.go b/snow/engine/snowman/bootstrap/bootstrapper_test.go index fd7fc0f12835..159fc98fb581 100644 --- a/snow/engine/snowman/bootstrap/bootstrapper_test.go +++ b/snow/engine/snowman/bootstrap/bootstrapper_test.go @@ -6,7 +6,6 @@ package bootstrap import ( "bytes" "context" - "encoding/binary" "errors" "testing" "time" @@ -21,6 +20,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/common/tracker" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" @@ -28,7 +28,6 @@ import ( "github.com/ava-labs/avalanchego/snow/engine/snowman/getter" "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/snow/validators" - "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/version" @@ -155,23 +154,13 @@ func TestBootstrapperStartsOnlyIfEnoughStakeIsConnected(t *testing.T) { VM: vm, } - blkID0 := ids.Empty.Prefix(0) - blkBytes0 := []byte{0} - blk0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: blkID0, - StatusV: choices.Accepted, - }, - HeightV: 0, - BytesV: blkBytes0, - } vm.CantLastAccepted = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return blk0.ID(), nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - require.Equal(blk0.ID(), blkID) - return blk0, nil + require.Equal(snowmantest.GenesisID, blkID) + return snowmantest.Genesis, nil } // create bootstrapper @@ -226,7 +215,7 @@ func TestBootstrapperSingleFrontier(t *testing.T) { config, _, _, vm := newConfig(t) - blks := generateBlockchain(1) + blks := snowmantest.BuildChain(1) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -254,7 +243,7 @@ func TestBootstrapperUnknownByzantineResponse(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(2) + blks := snowmantest.BuildChain(2) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -299,7 +288,7 @@ func TestBootstrapperPartialFetch(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(4) + blks := snowmantest.BuildChain(4) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -349,7 +338,7 @@ func TestBootstrapperEmptyResponse(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(2) + blks := snowmantest.BuildChain(2) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -397,7 +386,7 @@ func TestBootstrapperAncestors(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(4) + blks := snowmantest.BuildChain(4) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -442,7 +431,7 @@ func TestBootstrapperFinalized(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(3) + blks := snowmantest.BuildChain(3) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -484,7 +473,7 @@ func TestRestartBootstrapping(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(5) + blks := snowmantest.BuildChain(5) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -545,10 +534,10 @@ func TestBootstrapOldBlockAfterStateSync(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(2) + blks := snowmantest.BuildChain(2) initializeVMWithBlockchain(vm, blks) - blks[0].(*snowman.TestBlock).StatusV = choices.Processing + blks[0].StatusV = choices.Processing require.NoError(blks[1].Accept(context.Background())) bs, err := New( @@ -588,7 +577,7 @@ func TestBootstrapContinueAfterHalt(t *testing.T) { config, _, _, vm := newConfig(t) - blks := generateBlockchain(2) + blks := snowmantest.BuildChain(2) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -656,28 +645,11 @@ func TestBootstrapNoParseOnNew(t *testing.T) { snowGetHandler, err := getter.New(vm, sender, ctx.Log, time.Second, 2000, ctx.Registerer) require.NoError(err) - blk0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - BytesV: utils.RandomBytes(32), - } - - blk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: blk0.ID(), - HeightV: 1, - BytesV: utils.RandomBytes(32), - } + blk1 := snowmantest.BuildChild(snowmantest.Genesis) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - require.Equal(blk0.ID(), blkID) - return blk0, nil + require.Equal(snowmantest.GenesisID, blkID) + return snowmantest.Genesis, nil } intervalDB := memdb.New() @@ -732,7 +704,7 @@ func TestBootstrapperReceiveStaleAncestorsMessage(t *testing.T) { config, peerID, sender, vm := newConfig(t) - blks := generateBlockchain(3) + blks := snowmantest.BuildChain(3) initializeVMWithBlockchain(vm, blks) bs, err := New( @@ -770,36 +742,7 @@ func TestBootstrapperReceiveStaleAncestorsMessage(t *testing.T) { require.Equal(snow.Bootstrapping, config.Ctx.State.Get().State) } -func generateBlockchain(length uint64) []snowman.Block { - if length == 0 { - return nil - } - - blocks := make([]snowman.Block, length) - blocks[0] = &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - ParentV: ids.Empty, - HeightV: 0, - BytesV: binary.AppendUvarint(nil, 0), - } - for height := uint64(1); height < length; height++ { - blocks[height] = &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: blocks[height-1].ID(), - HeightV: height, - BytesV: binary.AppendUvarint(nil, height), - } - } - return blocks -} - -func initializeVMWithBlockchain(vm *block.TestVM, blocks []snowman.Block) { +func initializeVMWithBlockchain(vm *block.TestVM, blocks []*snowmantest.Block) { vm.CantSetState = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { var ( @@ -833,13 +776,13 @@ func initializeVMWithBlockchain(vm *block.TestVM, blocks []snowman.Block) { } } -func requireStatusIs(require *require.Assertions, blocks []snowman.Block, status choices.Status) { +func requireStatusIs(require *require.Assertions, blocks []*snowmantest.Block, status choices.Status) { for i, blk := range blocks { require.Equal(status, blk.Status(), i) } } -func blocksToIDs(blocks []snowman.Block) []ids.ID { +func blocksToIDs(blocks []*snowmantest.Block) []ids.ID { blkIDs := make([]ids.ID, len(blocks)) for i, blk := range blocks { blkIDs[i] = blk.ID() @@ -847,7 +790,7 @@ func blocksToIDs(blocks []snowman.Block) []ids.ID { return blkIDs } -func blocksToBytes(blocks []snowman.Block) [][]byte { +func blocksToBytes(blocks []*snowmantest.Block) [][]byte { numBlocks := len(blocks) blkBytes := make([][]byte, numBlocks) for i, blk := range blocks { diff --git a/snow/engine/snowman/bootstrap/storage_test.go b/snow/engine/snowman/bootstrap/storage_test.go index 6ac2761f8cd7..a4373f3bbfdf 100644 --- a/snow/engine/snowman/bootstrap/storage_test.go +++ b/snow/engine/snowman/bootstrap/storage_test.go @@ -15,6 +15,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/engine/snowman/bootstrap/interval" @@ -25,7 +26,7 @@ import ( var _ block.Parser = testParser(nil) func TestGetMissingBlockIDs(t *testing.T) { - blocks := generateBlockchain(7) + blocks := snowmantest.BuildChain(7) parser := makeParser(blocks) tests := []struct { @@ -103,7 +104,7 @@ func TestGetMissingBlockIDs(t *testing.T) { } func TestProcess(t *testing.T) { - blocks := generateBlockchain(7) + blocks := snowmantest.BuildChain(7) tests := []struct { name string @@ -259,7 +260,7 @@ func TestExecute(t *testing.T) { tree, err := interval.NewTree(db) require.NoError(err) - blocks := generateBlockchain(numBlocks) + blocks := snowmantest.BuildChain(numBlocks) parser := makeParser(blocks) for _, blk := range blocks { _, err := interval.Add(db, tree, 0, blk.Height(), blk.Bytes()) @@ -299,7 +300,7 @@ func (f testParser) ParseBlock(ctx context.Context, bytes []byte) (snowman.Block return f(ctx, bytes) } -func makeParser(blocks []snowman.Block) block.Parser { +func makeParser(blocks []*snowmantest.Block) block.Parser { return testParser(func(_ context.Context, b []byte) (snowman.Block, error) { for _, block := range blocks { if bytes.Equal(b, block.Bytes()) { diff --git a/snow/engine/snowman/getter/getter_test.go b/snow/engine/snowman/getter/getter_test.go index 7d6482a1d3c3..cf58841b9581 100644 --- a/snow/engine/snowman/getter/getter_test.go +++ b/snow/engine/snowman/getter/getter_test.go @@ -14,8 +14,8 @@ import ( "go.uber.org/mock/gomock" "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/utils/logging" @@ -77,30 +77,23 @@ func TestFilterAccepted(t *testing.T) { require := require.New(t) bs, vm, sender := newTest(t) - blkID0 := ids.GenerateTestID() - blkID1 := ids.GenerateTestID() - blkID2 := ids.GenerateTestID() + acceptedBlk := snowmantest.BuildChild(snowmantest.Genesis) + require.NoError(acceptedBlk.Accept(context.Background())) - blk0 := &snowman.TestBlock{TestDecidable: choices.TestDecidable{ - IDV: blkID0, - StatusV: choices.Accepted, - }} - blk1 := &snowman.TestBlock{TestDecidable: choices.TestDecidable{ - IDV: blkID1, - StatusV: choices.Accepted, - }} + unknownBlkID := ids.GenerateTestID() vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case blkID0: - return blk0, nil - case blkID1: - return blk1, nil - case blkID2: + case snowmantest.GenesisID: + return snowmantest.Genesis, nil + case acceptedBlk.ID(): + return acceptedBlk, nil + case unknownBlkID: + return nil, errUnknownBlock + default: + require.FailNow(errUnknownBlock.Error()) return nil, errUnknownBlock } - require.FailNow(errUnknownBlock.Error()) - return nil, errUnknownBlock } var accepted []ids.ID @@ -108,11 +101,11 @@ func TestFilterAccepted(t *testing.T) { accepted = frontier } - blkIDs := set.Of(blkID0, blkID1, blkID2) + blkIDs := set.Of(snowmantest.GenesisID, acceptedBlk.ID(), unknownBlkID) require.NoError(bs.GetAccepted(context.Background(), ids.EmptyNodeID, 0, blkIDs)) require.Len(accepted, 2) - require.Contains(accepted, blkID0) - require.Contains(accepted, blkID1) - require.NotContains(accepted, blkID2) + require.Contains(accepted, snowmantest.GenesisID) + require.Contains(accepted, acceptedBlk.ID()) + require.NotContains(accepted, unknownBlkID) } diff --git a/snow/engine/snowman/transitive_test.go b/snow/engine/snowman/transitive_test.go index a98e0bfd20d9..75040d9a1f53 100644 --- a/snow/engine/snowman/transitive_test.go +++ b/snow/engine/snowman/transitive_test.go @@ -20,13 +20,13 @@ import ( "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowball" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/snowman/ancestor" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/engine/snowman/getter" "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/snow/validators" - "github.com/ava-labs/avalanchego/utils" "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/version" ) @@ -37,36 +37,9 @@ var ( errInvalid = errors.New("invalid") errUnexpectedCall = errors.New("unexpected call") errTest = errors.New("non-nil test") - - GenesisID = ids.GenerateTestID() - GenesisBytes = utils.RandomBytes(32) - Genesis = &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: GenesisID, - StatusV: choices.Accepted, - }, - BytesV: GenesisBytes, - } ) -func BuildChain(root *snowman.TestBlock, length int) []*snowman.TestBlock { - chain := make([]*snowman.TestBlock, length) - for i := range chain { - root = &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: root.ID(), - HeightV: root.Height() + 1, - BytesV: utils.RandomBytes(32), - } - chain[i] = root - } - return chain -} - -func MakeGetBlockF(blks ...[]*snowman.TestBlock) func(context.Context, ids.ID) (snowman.Block, error) { +func MakeGetBlockF(blks ...[]*snowmantest.Block) func(context.Context, ids.ID) (snowman.Block, error) { return func(_ context.Context, blkID ids.ID) (snowman.Block, error) { for _, blkSet := range blks { for _, blk := range blkSet { @@ -79,7 +52,7 @@ func MakeGetBlockF(blks ...[]*snowman.TestBlock) func(context.Context, ids.ID) ( } } -func MakeParseBlockF(blks ...[]*snowman.TestBlock) func(context.Context, []byte) (snowman.Block, error) { +func MakeParseBlockF(blks ...[]*snowmantest.Block) func(context.Context, []byte) (snowman.Block, error) { return func(_ context.Context, blkBytes []byte) (snowman.Block, error) { for _, blkSet := range blks { for _, blk := range blkSet { @@ -92,7 +65,7 @@ func MakeParseBlockF(blks ...[]*snowman.TestBlock) func(context.Context, []byte) } } -func MakeLastAcceptedBlockF(defaultBlk *snowman.TestBlock, blks ...[]*snowman.TestBlock) func(context.Context) (ids.ID, error) { +func MakeLastAcceptedBlockF(defaultBlk *snowmantest.Block, blks ...[]*snowmantest.Block) func(context.Context) (ids.ID, error) { return func(_ context.Context) (ids.ID, error) { highestHeight := defaultBlk.Height() highestID := defaultBlk.ID() @@ -140,13 +113,13 @@ func setup(t *testing.T, config Config) (ids.NodeID, validators.Manager, *common vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -167,9 +140,8 @@ func TestEngineDropsAttemptToIssueBlockAfterFailedRequest(t *testing.T) { peerID, _, sender, vm, engine := setup(t, DefaultConfig(t)) - blks := BuildChain(Genesis, 2) - parent := blks[0] - child := blks[1] + parent := snowmantest.BuildChild(snowmantest.Genesis) + child := snowmantest.BuildChild(parent) var request *common.Request sender.SendGetF = func(_ context.Context, nodeID ids.NodeID, requestID uint32, blkID ids.ID) { @@ -186,8 +158,8 @@ func TestEngineDropsAttemptToIssueBlockAfterFailedRequest(t *testing.T) { } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -215,18 +187,17 @@ func TestEngineQuery(t *testing.T) { peerID, _, sender, vm, engine := setup(t, DefaultConfig(t)) - blks := BuildChain(Genesis, 2) - parent := blks[0] - child := blks[1] + parent := snowmantest.BuildChild(snowmantest.Genesis) + child := snowmantest.BuildChild(parent) var sendChitsCalled bool sender.SendChitsF = func(_ context.Context, _ ids.NodeID, requestID uint32, preferredID ids.ID, preferredIDByHeight ids.ID, accepted ids.ID) { require.False(sendChitsCalled) sendChitsCalled = true require.Equal(uint32(15), requestID) - require.Equal(GenesisID, preferredID) - require.Equal(GenesisID, preferredIDByHeight) - require.Equal(GenesisID, accepted) + require.Equal(snowmantest.GenesisID, preferredID) + require.Equal(snowmantest.GenesisID, preferredIDByHeight) + require.Equal(snowmantest.GenesisID, accepted) } var getBlockCalled bool @@ -234,8 +205,8 @@ func TestEngineQuery(t *testing.T) { getBlockCalled = true switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -251,7 +222,7 @@ func TestEngineQuery(t *testing.T) { require.Equal(peerID, nodeID) require.Contains([]ids.ID{ parent.ID(), - GenesisID, + snowmantest.GenesisID, }, blkID) } @@ -386,11 +357,11 @@ func TestEngineMultipleQuery(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, blkID) - return Genesis, nil + require.Equal(snowmantest.GenesisID, blkID) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -401,9 +372,8 @@ func TestEngineMultipleQuery(t *testing.T) { vm.GetBlockF = nil vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 2) - blk0 := blks[0] - blk1 := blks[1] + blk0 := snowmantest.BuildChild(snowmantest.Genesis) + blk1 := snowmantest.BuildChild(blk0) queried := new(bool) queryRequestID := new(uint32) @@ -419,8 +389,8 @@ func TestEngineMultipleQuery(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -436,8 +406,8 @@ func TestEngineMultipleQuery(t *testing.T) { vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { switch id { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk0.ID(): return blk0, nil case blk1.ID(): @@ -501,15 +471,14 @@ func TestEngineBlockedIssue(t *testing.T) { sender.Default(false) - blks := BuildChain(Genesis, 2) - blk0 := blks[0] - blk1 := blks[1] + blk0 := snowmantest.BuildChild(snowmantest.Genesis) + blk1 := snowmantest.BuildChild(blk0) sender.SendGetF = func(context.Context, ids.NodeID, uint32, ids.ID) {} vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk0.ID(): return blk0, nil default: @@ -544,8 +513,8 @@ func TestEngineRespondsToGetRequest(t *testing.T) { sender.Default(false) vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, id) - return Genesis, nil + require.Equal(snowmantest.GenesisID, id) + return snowmantest.Genesis, nil } var sentPut bool @@ -555,10 +524,10 @@ func TestEngineRespondsToGetRequest(t *testing.T) { require.Equal(vdr, nodeID) require.Equal(uint32(123), requestID) - require.Equal(GenesisBytes, blk) + require.Equal(snowmantest.GenesisBytes, blk) } - require.NoError(te.Get(context.Background(), vdr, 123, GenesisID)) + require.NoError(te.Get(context.Background(), vdr, 123, snowmantest.GenesisID)) require.True(sentPut) } @@ -569,8 +538,7 @@ func TestEnginePushQuery(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { if bytes.Equal(b, blk.Bytes()) { @@ -581,8 +549,8 @@ func TestEnginePushQuery(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk.ID(): return blk, nil default: @@ -596,9 +564,9 @@ func TestEnginePushQuery(t *testing.T) { *chitted = true require.Equal(vdr, inVdr) require.Equal(uint32(20), requestID) - require.Equal(GenesisID, preferredID) - require.Equal(GenesisID, preferredIDByHeight) - require.Equal(GenesisID, acceptedID) + require.Equal(snowmantest.GenesisID, preferredID) + require.Equal(snowmantest.GenesisID, preferredIDByHeight) + require.Equal(snowmantest.GenesisID, acceptedID) } queried := new(bool) @@ -624,13 +592,12 @@ func TestEngineBuildBlock(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -714,11 +681,11 @@ func TestVoteCanceling(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, id) - return Genesis, nil + require.Equal(snowmantest.GenesisID, id) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -728,8 +695,7 @@ func TestVoteCanceling(t *testing.T) { vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) queried := new(bool) queryRequestID := new(uint32) @@ -778,12 +744,12 @@ func TestEngineNoQuery(t *testing.T) { vm := &block.TestVM{} vm.T = t vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - if blkID == GenesisID { - return Genesis, nil + if blkID == snowmantest.GenesisID { + return snowmantest.Genesis, nil } return nil, errUnknownBlock } @@ -795,8 +761,7 @@ func TestEngineNoQuery(t *testing.T) { require.NoError(te.Start(context.Background(), 0)) - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) require.NoError(te.issue( context.Background(), @@ -819,12 +784,12 @@ func TestEngineNoRepollQuery(t *testing.T) { vm := &block.TestVM{} vm.T = t vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - if blkID == GenesisID { - return Genesis, nil + if blkID == snowmantest.GenesisID { + return snowmantest.Genesis, nil } return nil, errUnknownBlock } @@ -876,13 +841,12 @@ func TestEngineAbandonChit(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk.ID(): return nil, errUnknownBlock } @@ -930,13 +894,12 @@ func TestEngineAbandonChitWithUnexpectedPutBlock(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk.ID(): return nil, errUnknownBlock } @@ -974,13 +937,13 @@ func TestEngineAbandonChitWithUnexpectedPutBlock(t *testing.T) { sender.CantSendPullQuery = false vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { - require.Equal(GenesisBytes, b) - return Genesis, nil + require.Equal(snowmantest.GenesisBytes, b) + return snowmantest.Genesis, nil } // Respond with an unexpected block and verify that the request is correctly // cleared. - require.NoError(te.Put(context.Background(), vdr, reqID, GenesisBytes)) + require.NoError(te.Put(context.Background(), vdr, reqID, snowmantest.GenesisBytes)) require.Empty(te.blocked) } @@ -991,15 +954,14 @@ func TestEngineBlockingChitRequest(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 3) - missingBlk := blks[0] - parentBlk := blks[1] - blockingBlk := blks[2] + missingBlk := snowmantest.BuildChild(snowmantest.Genesis) + parentBlk := snowmantest.BuildChild(missingBlk) + blockingBlk := snowmantest.BuildChild(parentBlk) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blockingBlk.ID(): return blockingBlk, nil default: @@ -1050,17 +1012,15 @@ func TestEngineBlockingChitResponse(t *testing.T) { sender.Default(true) - issuedBlks := BuildChain(Genesis, 1) - issuedBlk := issuedBlks[0] + issuedBlk := snowmantest.BuildChild(snowmantest.Genesis) - blockingBlks := BuildChain(Genesis, 2) - missingBlk := blockingBlks[0] - blockingBlk := blockingBlks[1] + missingBlk := snowmantest.BuildChild(snowmantest.Genesis) + blockingBlk := snowmantest.BuildChild(missingBlk) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case issuedBlk.ID(): return issuedBlk, nil case blockingBlk.ID(): @@ -1071,8 +1031,8 @@ func TestEngineBlockingChitResponse(t *testing.T) { } vm.ParseBlockF = func(_ context.Context, blkBytes []byte) (snowman.Block, error) { switch { - case bytes.Equal(GenesisBytes, blkBytes): - return Genesis, nil + case bytes.Equal(snowmantest.GenesisBytes, blkBytes): + return snowmantest.Genesis, nil case bytes.Equal(issuedBlk.Bytes(), blkBytes): return issuedBlk, nil case bytes.Equal(missingBlk.Bytes(), blkBytes): @@ -1154,8 +1114,8 @@ func TestEngineBlockingChitResponse(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case issuedBlk.ID(): return issuedBlk, nil case missingBlk.ID(): @@ -1187,8 +1147,7 @@ func TestEngineRetryFetch(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 1) - missingBlk := blks[0] + missingBlk := snowmantest.BuildChild(snowmantest.Genesis) vm.CantGetBlock = false @@ -1227,10 +1186,8 @@ func TestEngineUndeclaredDependencyDeadlock(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 2) - validBlk := blks[0] - - invalidBlk := blks[1] + validBlk := snowmantest.BuildChild(snowmantest.Genesis) + invalidBlk := snowmantest.BuildChild(validBlk) invalidBlk.VerifyV = errTest invalidBlkID := invalidBlk.ID() @@ -1242,8 +1199,8 @@ func TestEngineUndeclaredDependencyDeadlock(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case validBlk.ID(): return validBlk, nil case invalidBlk.ID(): @@ -1278,11 +1235,11 @@ func TestEngineGossip(t *testing.T) { nodeID, _, sender, vm, te := setup(t, DefaultConfig(t)) vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, blkID) - return Genesis, nil + require.Equal(snowmantest.GenesisID, blkID) + return snowmantest.Genesis, nil } var calledSendPullQuery bool @@ -1306,9 +1263,8 @@ func TestEngineInvalidBlockIgnoredFromUnexpectedPeer(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 2) - missingBlk := blks[0] - pendingBlk := blks[1] + missingBlk := snowmantest.BuildChild(snowmantest.Genesis) + pendingBlk := snowmantest.BuildChild(missingBlk) parsed := new(bool) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { @@ -1321,8 +1277,8 @@ func TestEngineInvalidBlockIgnoredFromUnexpectedPeer(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case pendingBlk.ID(): if !*parsed { return nil, errUnknownBlock @@ -1355,8 +1311,8 @@ func TestEngineInvalidBlockIgnoredFromUnexpectedPeer(t *testing.T) { } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case missingBlk.ID(): if !*parsed { return nil, errUnknownBlock @@ -1380,9 +1336,8 @@ func TestEnginePushQueryRequestIDConflict(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 2) - missingBlk := blks[0] - pendingBlk := blks[1] + missingBlk := snowmantest.BuildChild(snowmantest.Genesis) + pendingBlk := snowmantest.BuildChild(missingBlk) parsed := new(bool) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { @@ -1395,8 +1350,8 @@ func TestEnginePushQueryRequestIDConflict(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case pendingBlk.ID(): if !*parsed { return nil, errUnknownBlock @@ -1432,8 +1387,8 @@ func TestEnginePushQueryRequestIDConflict(t *testing.T) { } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case missingBlk.ID(): if !*parsed { return nil, errUnknownBlock @@ -1476,11 +1431,11 @@ func TestEngineAggressivePolling(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, blkID) - return Genesis, nil + require.Equal(snowmantest.GenesisID, blkID) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -1491,8 +1446,7 @@ func TestEngineAggressivePolling(t *testing.T) { vm.GetBlockF = nil vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 1) - pendingBlk := blks[0] + pendingBlk := snowmantest.BuildChild(snowmantest.Genesis) parsed := new(bool) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { @@ -1505,8 +1459,8 @@ func TestEngineAggressivePolling(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case pendingBlk.ID(): if !*parsed { return nil, errUnknownBlock @@ -1565,11 +1519,11 @@ func TestEngineDoubleChit(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, id) - return Genesis, nil + require.Equal(snowmantest.GenesisID, id) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -1579,8 +1533,7 @@ func TestEngineDoubleChit(t *testing.T) { vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) queried := new(bool) queryRequestID := new(uint32) @@ -1603,8 +1556,8 @@ func TestEngineDoubleChit(t *testing.T) { vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { switch id { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk.ID(): return blk, nil } @@ -1652,11 +1605,11 @@ func TestEngineBuildBlockLimit(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, blkID) - return Genesis, nil + require.Equal(snowmantest.GenesisID, blkID) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -1667,7 +1620,7 @@ func TestEngineBuildBlockLimit(t *testing.T) { vm.GetBlockF = nil vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 2) + blks := snowmantest.BuildDescendants(snowmantest.Genesis, 2) blk0 := blks[0] var ( @@ -1684,8 +1637,8 @@ func TestEngineBuildBlockLimit(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -1709,8 +1662,8 @@ func TestEngineBuildBlockLimit(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk0.ID(): return blk0, nil default: @@ -1728,12 +1681,9 @@ func TestEngineReceiveNewRejectedBlock(t *testing.T) { vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) - acceptedBlks := BuildChain(Genesis, 1) - acceptedBlk := acceptedBlks[0] - - rejectedBlks := BuildChain(Genesis, 2) - rejectedBlk := rejectedBlks[0] - pendingBlk := rejectedBlks[1] + acceptedBlk := snowmantest.BuildChild(snowmantest.Genesis) + rejectedBlk := snowmantest.BuildChild(snowmantest.Genesis) + pendingBlk := snowmantest.BuildChild(rejectedBlk) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { @@ -1751,8 +1701,8 @@ func TestEngineReceiveNewRejectedBlock(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case acceptedBlk.ID(): return acceptedBlk, nil default: @@ -1799,12 +1749,9 @@ func TestEngineRejectionAmplification(t *testing.T) { vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) - acceptedBlks := BuildChain(Genesis, 1) - acceptedBlk := acceptedBlks[0] - - rejectedBlks := BuildChain(Genesis, 2) - rejectedBlk := rejectedBlks[0] - pendingBlk := rejectedBlks[1] + acceptedBlk := snowmantest.BuildChild(snowmantest.Genesis) + rejectedBlk := snowmantest.BuildChild(snowmantest.Genesis) + pendingBlk := snowmantest.BuildChild(rejectedBlk) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { @@ -1822,8 +1769,8 @@ func TestEngineRejectionAmplification(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case acceptedBlk.ID(): return acceptedBlk, nil default: @@ -1846,8 +1793,8 @@ func TestEngineRejectionAmplification(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case acceptedBlk.ID(): return acceptedBlk, nil default: @@ -1888,12 +1835,9 @@ func TestEngineTransitiveRejectionAmplificationDueToRejectedParent(t *testing.T) vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) - acceptedBlks := BuildChain(Genesis, 1) - acceptedBlk := acceptedBlks[0] - - rejectedBlks := BuildChain(Genesis, 2) - rejectedBlk := rejectedBlks[0] - pendingBlk := rejectedBlks[1] + acceptedBlk := snowmantest.BuildChild(snowmantest.Genesis) + rejectedBlk := snowmantest.BuildChild(snowmantest.Genesis) + pendingBlk := snowmantest.BuildChild(rejectedBlk) pendingBlk.RejectV = errUnexpectedCall vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { @@ -1912,8 +1856,8 @@ func TestEngineTransitiveRejectionAmplificationDueToRejectedParent(t *testing.T) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case acceptedBlk.ID(): return acceptedBlk, nil case rejectedBlk.ID(): @@ -1954,14 +1898,11 @@ func TestEngineTransitiveRejectionAmplificationDueToInvalidParent(t *testing.T) vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) - acceptedBlks := BuildChain(Genesis, 1) - acceptedBlk := acceptedBlks[0] - - rejectedBlks := BuildChain(Genesis, 2) - rejectedBlk := rejectedBlks[0] + acceptedBlk := snowmantest.BuildChild(snowmantest.Genesis) + rejectedBlk := snowmantest.BuildChild(snowmantest.Genesis) rejectedBlk.VerifyV = errUnexpectedCall - pendingBlk := rejectedBlks[1] - pendingBlk.RejectV = errUnexpectedCall + pendingBlk := snowmantest.BuildChild(rejectedBlk) + pendingBlk.VerifyV = errUnexpectedCall vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { @@ -1979,8 +1920,8 @@ func TestEngineTransitiveRejectionAmplificationDueToInvalidParent(t *testing.T) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -2000,8 +1941,8 @@ func TestEngineTransitiveRejectionAmplificationDueToInvalidParent(t *testing.T) vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case rejectedBlk.ID(): return rejectedBlk, nil case acceptedBlk.ID(): @@ -2024,11 +1965,8 @@ func TestEngineNonPreferredAmplification(t *testing.T) { vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) - preferredBlks := BuildChain(Genesis, 1) - preferredBlk := preferredBlks[0] - - nonPreferredBlks := BuildChain(Genesis, 1) - nonPreferredBlk := nonPreferredBlks[0] + preferredBlk := snowmantest.BuildChild(snowmantest.Genesis) + nonPreferredBlk := snowmantest.BuildChild(snowmantest.Genesis) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { @@ -2044,8 +1982,8 @@ func TestEngineNonPreferredAmplification(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -2082,11 +2020,9 @@ func TestEngineBubbleVotesThroughInvalidBlock(t *testing.T) { vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) expectedVdrSet := set.Of(vdr) - blks := BuildChain(Genesis, 2) - blk1 := blks[0] - + blk1 := snowmantest.BuildChild(snowmantest.Genesis) // blk2 cannot pass verification until [blk1] has been marked as accepted. - blk2 := blks[1] + blk2 := snowmantest.BuildChild(blk1) blk2.VerifyV = errInvalid // The VM should be able to parse [blk1] and [blk2] @@ -2107,8 +2043,8 @@ func TestEngineBubbleVotesThroughInvalidBlock(t *testing.T) { // in the following tests vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -2156,8 +2092,8 @@ func TestEngineBubbleVotesThroughInvalidBlock(t *testing.T) { // now [blk1] is verified, vm can return it vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk1.ID(): return blk1, nil default: @@ -2192,8 +2128,8 @@ func TestEngineBubbleVotesThroughInvalidBlock(t *testing.T) { blk2.VerifyV = nil vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk1.ID(): return blk1, nil case blk2.ID(): @@ -2241,14 +2177,11 @@ func TestEngineBubbleVotesThroughInvalidChain(t *testing.T) { vdr, _, sender, vm, te := setup(t, DefaultConfig(t)) expectedVdrSet := set.Of(vdr) - blks := BuildChain(Genesis, 3) - blk1 := blks[0] - + blk1 := snowmantest.BuildChild(snowmantest.Genesis) // blk2 cannot pass verification until [blk1] has been marked as accepted. - blk2 := blks[1] + blk2 := snowmantest.BuildChild(blk1) blk2.VerifyV = errInvalid - - blk3 := blks[2] + blk3 := snowmantest.BuildChild(blk2) // The VM should be able to parse [blk1], [blk2], and [blk3] vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { @@ -2268,8 +2201,8 @@ func TestEngineBubbleVotesThroughInvalidChain(t *testing.T) { // The VM should be able to retrieve [Genesis] and [blk1] from storage vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk1.ID(): return blk1, nil default: @@ -2355,26 +2288,18 @@ func TestEngineBuildBlockWithCachedNonVerifiedParent(t *testing.T) { sender.Default(true) - blks := BuildChain(Genesis, 3) - grandParentBlk := blks[0] + grandParentBlk := snowmantest.BuildChild(snowmantest.Genesis) - parentBlkA := blks[1] + parentBlkA := snowmantest.BuildChild(grandParentBlk) parentBlkA.VerifyV = errInvalid // Note that [parentBlkB] has the same [ID()] as [parentBlkA]; // it's a different instantiation of the same block. - parentBlkB := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: parentBlkA.IDV, - StatusV: parentBlkA.StatusV, - }, - ParentV: parentBlkA.ParentV, - HeightV: parentBlkA.HeightV, - BytesV: parentBlkA.BytesV, - } + parentBlkB := *parentBlkA + parentBlkB.VerifyV = nil // Child of [parentBlkA]/[parentBlkB] - childBlk := blks[2] + childBlk := snowmantest.BuildChild(parentBlkA) vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { require.Equal(grandParentBlk.BytesV, b) @@ -2383,8 +2308,8 @@ func TestEngineBuildBlockWithCachedNonVerifiedParent(t *testing.T) { vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case grandParentBlk.IDV: return grandParentBlk, nil default: @@ -2414,17 +2339,17 @@ func TestEngineBuildBlockWithCachedNonVerifiedParent(t *testing.T) { vm.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { require.Equal(parentBlkB.BytesV, b) - return parentBlkB, nil + return &parentBlkB, nil } vm.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case grandParentBlk.IDV: return grandParentBlk, nil case parentBlkB.IDV: - return parentBlkB, nil + return &parentBlkB, nil default: return nil, errUnknownBlock } @@ -2503,11 +2428,11 @@ func TestEngineApplyAcceptedFrontierInQueryFailed(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, id) - return Genesis, nil + require.Equal(snowmantest.GenesisID, id) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -2516,8 +2441,7 @@ func TestEngineApplyAcceptedFrontierInQueryFailed(t *testing.T) { vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) queryRequestID := new(uint32) sender.SendPushQueryF = func(_ context.Context, inVdrs set.Set[ids.NodeID], requestID uint32, blkBytes []byte, requestedHeight uint64) { @@ -2537,8 +2461,8 @@ func TestEngineApplyAcceptedFrontierInQueryFailed(t *testing.T) { vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { switch id { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk.ID(): return blk, nil } @@ -2598,11 +2522,11 @@ func TestEngineRepollsMisconfiguredSubnet(t *testing.T) { vm.CantSetPreference = false vm.LastAcceptedF = func(context.Context) (ids.ID, error) { - return GenesisID, nil + return snowmantest.GenesisID, nil } vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { - require.Equal(GenesisID, id) - return Genesis, nil + require.Equal(snowmantest.GenesisID, id) + return snowmantest.Genesis, nil } te, err := New(engCfg) @@ -2611,8 +2535,7 @@ func TestEngineRepollsMisconfiguredSubnet(t *testing.T) { vm.LastAcceptedF = nil - blks := BuildChain(Genesis, 1) - blk := blks[0] + blk := snowmantest.BuildChild(snowmantest.Genesis) // Issue the block. This shouldn't call the sender, because creating the // poll should fail. @@ -2650,8 +2573,8 @@ func TestEngineRepollsMisconfiguredSubnet(t *testing.T) { vm.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { switch id { - case GenesisID: - return Genesis, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case blk.ID(): return blk, nil } @@ -2731,8 +2654,8 @@ func TestEngineVoteStallRegression(t *testing.T) { sender.Default(true) config.Sender = sender - acceptedChain := BuildChain(Genesis, 3) - rejectedChain := BuildChain(Genesis, 2) + acceptedChain := snowmantest.BuildDescendants(snowmantest.Genesis, 3) + rejectedChain := snowmantest.BuildDescendants(snowmantest.Genesis, 2) vm := &block.TestVM{ TestVM: common.TestVM{ @@ -2755,19 +2678,19 @@ func TestEngineVoteStallRegression(t *testing.T) { }, }, ParseBlockF: MakeParseBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, acceptedChain, rejectedChain, ), GetBlockF: MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, acceptedChain, ), SetPreferenceF: func(context.Context, ids.ID) error { return nil }, LastAcceptedF: MakeLastAcceptedBlockF( - Genesis, + snowmantest.Genesis, acceptedChain, ), } @@ -2899,7 +2822,7 @@ func TestEngineVoteStallRegression(t *testing.T) { // accepted, block 4 will be dropped. // Then poll 1 should terminate because block 4 was dropped. vm.GetBlockF = MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, acceptedChain, rejectedChain, ) @@ -2939,8 +2862,8 @@ func TestGetProcessingAncestor(t *testing.T) { ctx = snowtest.ConsensusContext( snowtest.Context(t, snowtest.PChainID), ) - issuedChain = BuildChain(Genesis, 1) - unissuedOnIssuedChain = BuildChain(issuedChain[0], 1) + issuedBlock = snowmantest.BuildChild(snowmantest.Genesis) + unissuedBlock = snowmantest.BuildChild(issuedBlock) ) metrics, err := newMetrics("", prometheus.NewRegistry()) @@ -2950,17 +2873,15 @@ func TestGetProcessingAncestor(t *testing.T) { require.NoError(t, c.Initialize( ctx, snowball.DefaultParameters, - GenesisID, + snowmantest.GenesisID, 0, time.Now(), )) - for _, blk := range issuedChain { - require.NoError(t, c.Add(context.Background(), blk)) - } + require.NoError(t, c.Add(context.Background(), issuedBlock)) nonVerifiedAncestors := ancestor.NewTree() - nonVerifiedAncestors.Add(unissuedOnIssuedChain[0].ID(), unissuedOnIssuedChain[0].Parent()) + nonVerifiedAncestors.Add(unissuedBlock.ID(), unissuedBlock.Parent()) tests := []struct { name string @@ -2979,7 +2900,7 @@ func TestGetProcessingAncestor(t *testing.T) { T: t, }, GetBlockF: MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, ), }, Consensus: c, @@ -2989,7 +2910,7 @@ func TestGetProcessingAncestor(t *testing.T) { pending: map[ids.ID]snowman.Block{}, nonVerifiedCache: &cache.Empty[ids.ID, snowman.Block]{}, }, - initialVote: GenesisID, + initialVote: snowmantest.GenesisID, expectedAncestor: ids.Empty, expectedFound: false, }, @@ -3003,7 +2924,7 @@ func TestGetProcessingAncestor(t *testing.T) { T: t, }, GetBlockF: MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, ), }, Consensus: c, @@ -3013,8 +2934,8 @@ func TestGetProcessingAncestor(t *testing.T) { pending: map[ids.ID]snowman.Block{}, nonVerifiedCache: &cache.Empty[ids.ID, snowman.Block]{}, }, - initialVote: issuedChain[0].ID(), - expectedAncestor: issuedChain[0].ID(), + initialVote: issuedBlock.ID(), + expectedAncestor: issuedBlock.ID(), expectedFound: true, }, { @@ -3027,7 +2948,7 @@ func TestGetProcessingAncestor(t *testing.T) { T: t, }, GetBlockF: MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, ), }, Consensus: c, @@ -3051,7 +2972,7 @@ func TestGetProcessingAncestor(t *testing.T) { T: t, }, GetBlockF: MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, ), }, Consensus: c, @@ -3061,8 +2982,8 @@ func TestGetProcessingAncestor(t *testing.T) { pending: map[ids.ID]snowman.Block{}, nonVerifiedCache: &cache.Empty[ids.ID, snowman.Block]{}, }, - initialVote: unissuedOnIssuedChain[0].ID(), - expectedAncestor: issuedChain[0].ID(), + initialVote: unissuedBlock.ID(), + expectedAncestor: issuedBlock.ID(), expectedFound: true, }, { @@ -3075,7 +2996,7 @@ func TestGetProcessingAncestor(t *testing.T) { T: t, }, GetBlockF: MakeGetBlockF( - []*snowman.TestBlock{Genesis}, + []*snowmantest.Block{snowmantest.Genesis}, ), }, Consensus: c, @@ -3083,12 +3004,12 @@ func TestGetProcessingAncestor(t *testing.T) { metrics: metrics, nonVerifieds: ancestor.NewTree(), pending: map[ids.ID]snowman.Block{ - unissuedOnIssuedChain[0].ID(): unissuedOnIssuedChain[0], + unissuedBlock.ID(): unissuedBlock, }, nonVerifiedCache: &cache.Empty[ids.ID, snowman.Block]{}, }, - initialVote: unissuedOnIssuedChain[0].ID(), - expectedAncestor: issuedChain[0].ID(), + initialVote: unissuedBlock.ID(), + expectedAncestor: issuedBlock.ID(), expectedFound: true, }, } diff --git a/vms/components/chain/state_test.go b/vms/components/chain/state_test.go index 8bdda5960f1a..d1a71beedf63 100644 --- a/vms/components/chain/state_test.go +++ b/vms/components/chain/state_test.go @@ -16,13 +16,12 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/utils/hashing" "github.com/ava-labs/avalanchego/utils/metric" ) var ( - _ Block = (*TestBlock)(nil) - errCantBuildBlock = errors.New("can't build new block") errVerify = errors.New("verify failed") errAccept = errors.New("accept failed") @@ -30,36 +29,25 @@ var ( errUnexpectedBlockBytes = errors.New("unexpected block bytes") ) -type TestBlock struct { - *snowman.TestBlock -} - -// SetStatus sets the status of the Block. -func (b *TestBlock) SetStatus(status choices.Status) { - b.TestBlock.TestDecidable.StatusV = status -} - // NewTestBlock returns a new test block with height, bytes, and ID derived from [i] // and using [parentID] as the parent block ID -func NewTestBlock(i uint64, parentID ids.ID) *TestBlock { +func NewTestBlock(i uint64, parentID ids.ID) *snowmantest.Block { b := []byte{byte(i)} id := hashing.ComputeHash256Array(b) - return &TestBlock{ - TestBlock: &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: id, - StatusV: choices.Unknown, - }, - HeightV: i, - ParentV: parentID, - BytesV: b, + return &snowmantest.Block{ + TestDecidable: choices.TestDecidable{ + IDV: id, + StatusV: choices.Unknown, }, + HeightV: i, + ParentV: parentID, + BytesV: b, } } // NewTestBlocks generates [numBlocks] consecutive blocks -func NewTestBlocks(numBlocks uint64) []*TestBlock { - blks := make([]*TestBlock, 0, numBlocks) +func NewTestBlocks(numBlocks uint64) []*snowmantest.Block { + blks := make([]*snowmantest.Block, 0, numBlocks) parentID := ids.Empty for i := uint64(0); i < numBlocks; i++ { blks = append(blks, NewTestBlock(i, parentID)) @@ -70,18 +58,17 @@ func NewTestBlocks(numBlocks uint64) []*TestBlock { return blks } -func createInternalBlockFuncs(t *testing.T, blks []*TestBlock) ( +func createInternalBlockFuncs(blks []*snowmantest.Block) ( func(ctx context.Context, blkID ids.ID) (snowman.Block, error), func(ctx context.Context, b []byte) (snowman.Block, error), func(ctx context.Context, height uint64) (ids.ID, error), ) { - blkMap := make(map[ids.ID]*TestBlock) - blkByteMap := make(map[byte]*TestBlock) + blkMap := make(map[ids.ID]*snowmantest.Block) + blkBytesMap := make(map[string]*snowmantest.Block) for _, blk := range blks { blkMap[blk.ID()] = blk blkBytes := blk.Bytes() - require.Len(t, blkBytes, 1) - blkByteMap[blkBytes[0]] = blk + blkBytesMap[string(blkBytes)] = blk } getBlock := func(_ context.Context, id ids.ID) (snowman.Block, error) { @@ -94,11 +81,7 @@ func createInternalBlockFuncs(t *testing.T, blks []*TestBlock) ( } parseBlk := func(_ context.Context, b []byte) (snowman.Block, error) { - if len(b) != 1 { - return nil, fmt.Errorf("expected block bytes to be length 1, but found %d", len(b)) - } - - blk, ok := blkByteMap[b[0]] + blk, ok := blkBytesMap[string(b)] if !ok { return nil, fmt.Errorf("%w: %x", errUnexpectedBlockBytes, b) } @@ -196,22 +179,10 @@ func TestState(t *testing.T) { blk2 := testBlks[2] // Need to create a block with a different bytes and hash here // to generate a conflict with blk2 - blk3Bytes := []byte{byte(3)} - blk3ID := hashing.ComputeHash256Array(blk3Bytes) - blk3 := &TestBlock{ - TestBlock: &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: blk3ID, - StatusV: choices.Processing, - }, - HeightV: uint64(2), - BytesV: blk3Bytes, - ParentV: blk1.IDV, - }, - } + blk3 := snowmantest.BuildChild(blk1) testBlks = append(testBlks, blk3) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -290,7 +261,7 @@ func TestBuildBlock(t *testing.T) { genesisBlock.SetStatus(choices.Accepted) blk1 := testBlks[1] - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) buildBlock := func(context.Context) (snowman.Block, error) { // Once the block is built, mark it as processing blk1.SetStatus(choices.Processing) @@ -335,7 +306,7 @@ func TestStateDecideBlock(t *testing.T) { badVerifyBlk.VerifyV = errVerify badRejectBlk := testBlks[3] badRejectBlk.RejectV = errReject - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -388,7 +359,7 @@ func TestStateParent(t *testing.T) { blk1 := testBlks[1] blk2 := testBlks[2] - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -429,7 +400,7 @@ func TestGetBlockInternal(t *testing.T) { genesisBlock := testBlks[0] genesisBlock.SetStatus(choices.Accepted) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -443,13 +414,13 @@ func TestGetBlockInternal(t *testing.T) { }) genesisBlockInternal := chainState.LastAcceptedBlockInternal() - require.IsType(&TestBlock{}, genesisBlockInternal) + require.IsType(&snowmantest.Block{}, genesisBlockInternal) require.Equal(genesisBlock.ID(), genesisBlockInternal.ID()) blk, err := chainState.GetBlockInternal(context.Background(), genesisBlock.ID()) require.NoError(err) - require.IsType(&TestBlock{}, blk) + require.IsType(&snowmantest.Block{}, blk) require.Equal(genesisBlock.ID(), blk.ID()) } @@ -461,7 +432,7 @@ func TestGetBlockError(t *testing.T) { genesisBlock.SetStatus(choices.Accepted) blk1 := testBlks[1] - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) wrappedGetBlock := func(ctx context.Context, id ids.ID) (snowman.Block, error) { blk, err := getBlock(ctx, id) if err != nil { @@ -498,7 +469,7 @@ func TestParseBlockError(t *testing.T) { genesisBlock := testBlks[0] genesisBlock.SetStatus(choices.Accepted) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -520,7 +491,7 @@ func TestBuildBlockError(t *testing.T) { genesisBlock := testBlks[0] genesisBlock.SetStatus(choices.Accepted) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -546,7 +517,7 @@ func TestMeteredCache(t *testing.T) { genesisBlock := testBlks[0] genesisBlock.SetStatus(choices.Accepted) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) config := &Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -574,7 +545,7 @@ func TestStateBytesToIDCache(t *testing.T) { blk1 := testBlks[1] blk2 := testBlks[2] - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) buildBlock := func(context.Context) (snowman.Block, error) { require.FailNow("shouldn't have been called") return nil, nil @@ -627,34 +598,13 @@ func TestSetLastAcceptedBlock(t *testing.T) { genesisBlock.SetStatus(choices.Accepted) postSetBlk1ParentID := hashing.ComputeHash256Array([]byte{byte(199)}) - postSetBlk1Bytes := []byte{byte(200)} - postSetBlk2Bytes := []byte{byte(201)} - postSetBlk1 := &TestBlock{ - TestBlock: &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: hashing.ComputeHash256Array(postSetBlk1Bytes), - StatusV: choices.Accepted, - }, - HeightV: uint64(200), - BytesV: postSetBlk1Bytes, - ParentV: postSetBlk1ParentID, - }, - } - postSetBlk2 := &TestBlock{ - TestBlock: &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: hashing.ComputeHash256Array(postSetBlk2Bytes), - StatusV: choices.Processing, - }, - HeightV: uint64(201), - BytesV: postSetBlk2Bytes, - ParentV: postSetBlk1.IDV, - }, - } + postSetBlk1 := NewTestBlock(200, postSetBlk1ParentID) + postSetBlk2 := NewTestBlock(201, postSetBlk1.ID()) + // note we do not need to parse postSetBlk1 so it is omitted here testBlks = append(testBlks, postSetBlk2) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ LastAcceptedBlock: genesisBlock, GetBlock: getBlock, @@ -695,7 +645,7 @@ func TestSetLastAcceptedBlockWithProcessingBlocksErrors(t *testing.T) { blk1 := testBlks[1] resetBlk := testBlks[4] - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) buildBlock := func(context.Context) (snowman.Block, error) { // Once the block is built, mark it as processing blk1.SetStatus(choices.Processing) @@ -737,7 +687,7 @@ func TestStateParseTransitivelyAcceptedBlock(t *testing.T) { blk2 := testBlks[2] blk2.SetStatus(choices.Accepted) - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, @@ -763,7 +713,7 @@ func TestIsProcessing(t *testing.T) { genesisBlock.SetStatus(choices.Accepted) blk1 := testBlks[1] - getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(t, testBlks) + getBlock, parseBlock, getCanonicalBlockID := createInternalBlockFuncs(testBlks) chainState := NewState(&Config{ DecidedCacheSize: 2, MissingCacheSize: 2, diff --git a/vms/proposervm/batched_vm_test.go b/vms/proposervm/batched_vm_test.go index a95e9d5e164d..a6e9ffb2b1d6 100644 --- a/vms/proposervm/batched_vm_test.go +++ b/vms/proposervm/batched_vm_test.go @@ -16,8 +16,8 @@ import ( "github.com/ava-labs/avalanchego/database/prefixdb" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" - "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/snowtest" @@ -32,7 +32,7 @@ func TestCoreVMNotRemote(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - _, _, proVM, _, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + _, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -61,22 +61,13 @@ func TestGetAncestorsPreForkOnly(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, proRemoteVM, coreGenBlk := initTestRemoteProposerVM(t, activationTime, durangoTime) + coreVM, proRemoteVM := initTestRemoteProposerVM(t, activationTime, durangoTime) defer func() { require.NoError(proRemoteVM.Shutdown(context.Background())) }() // Build some prefork blocks.... - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: coreGenBlk.Timestamp(), - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -86,24 +77,15 @@ func TestGetAncestorsPreForkOnly(t *testing.T) { // prepare build of next block require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk1.ID())) coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreBlk1.ID(): + switch blkID { + case coreBlk1.ID(): return coreBlk1, nil default: return nil, errUnknownBlock } } - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk1.ID(), - HeightV: coreBlk1.Height() + 1, - TimestampV: coreBlk1.Timestamp(), - } + coreBlk2 := snowmantest.BuildChild(coreBlk1) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -113,24 +95,15 @@ func TestGetAncestorsPreForkOnly(t *testing.T) { // prepare build of next block require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk2.ID())) coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreBlk2.ID(): + switch blkID { + case coreBlk2.ID(): return coreBlk2, nil default: return nil, errUnknownBlock } } - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk2.ID(), - HeightV: coreBlk2.Height() + 1, - TimestampV: coreBlk2.Timestamp(), - } + coreBlk3 := snowmantest.BuildChild(coreBlk2) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -211,21 +184,13 @@ func TestGetAncestorsPostForkOnly(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, proRemoteVM, coreGenBlk := initTestRemoteProposerVM(t, activationTime, durangoTime) + coreVM, proRemoteVM := initTestRemoteProposerVM(t, activationTime, durangoTime) defer func() { require.NoError(proRemoteVM.Shutdown(context.Background())) }() // Build some post-Fork blocks.... - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -237,15 +202,7 @@ func TestGetAncestorsPostForkOnly(t *testing.T) { require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk1.ID())) require.NoError(waitForProposerWindow(proRemoteVM, builtBlk1, 0)) - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk1.ID(), - HeightV: coreBlk1.Height() + 1, - } + coreBlk2 := snowmantest.BuildChild(coreBlk1) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -257,15 +214,7 @@ func TestGetAncestorsPostForkOnly(t *testing.T) { require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk2.ID())) require.NoError(waitForProposerWindow(proRemoteVM, builtBlk2, 0)) - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk2.ID(), - HeightV: coreBlk2.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(coreBlk2) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -300,8 +249,8 @@ func TestGetAncestorsPostForkOnly(t *testing.T) { coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk1.Bytes()): return coreBlk1, nil case bytes.Equal(b, coreBlk2.Bytes()): @@ -371,23 +320,15 @@ func TestGetAncestorsAtSnomanPlusPlusFork(t *testing.T) { ) // enable ProBlks in next future - coreVM, proRemoteVM, coreGenBlk := initTestRemoteProposerVM(t, forkTime, durangoTime) + coreVM, proRemoteVM := initTestRemoteProposerVM(t, forkTime, durangoTime) defer func() { require.NoError(proRemoteVM.Shutdown(context.Background())) }() // Build some prefork blocks.... proRemoteVM.Set(preForkTime) - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: preForkTime, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) + coreBlk1.TimestampV = preForkTime coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -406,16 +347,8 @@ func TestGetAncestorsAtSnomanPlusPlusFork(t *testing.T) { } } - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk1.ID(), - HeightV: coreBlk1.Height() + 1, - TimestampV: postForkTime, - } + coreBlk2 := snowmantest.BuildChild(coreBlk1) + coreBlk2.TimestampV = postForkTime coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -436,15 +369,7 @@ func TestGetAncestorsAtSnomanPlusPlusFork(t *testing.T) { // .. and some post-fork proRemoteVM.Set(postForkTime) - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk2.ID(), - HeightV: coreBlk2.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(coreBlk2) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -457,15 +382,7 @@ func TestGetAncestorsAtSnomanPlusPlusFork(t *testing.T) { require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk3.ID())) require.NoError(waitForProposerWindow(proRemoteVM, builtBlk3, builtBlk3.(*postForkBlock).PChainHeight())) - coreBlk4 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{4}, - ParentV: coreBlk3.ID(), - HeightV: coreBlk3.Height() + 1, - } + coreBlk4 := snowmantest.BuildChild(coreBlk3) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk4, nil } @@ -573,21 +490,13 @@ func TestBatchedParseBlockPreForkOnly(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, proRemoteVM, coreGenBlk := initTestRemoteProposerVM(t, activationTime, durangoTime) + coreVM, proRemoteVM := initTestRemoteProposerVM(t, activationTime, durangoTime) defer func() { require.NoError(proRemoteVM.Shutdown(context.Background())) }() // Build some prefork blocks.... - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -597,23 +506,15 @@ func TestBatchedParseBlockPreForkOnly(t *testing.T) { // prepare build of next block require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk1.ID())) coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreBlk1.ID(): + switch blkID { + case coreBlk1.ID(): return coreBlk1, nil default: return nil, errUnknownBlock } } - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk1.ID(), - HeightV: coreBlk1.Height() + 1, - } + coreBlk2 := snowmantest.BuildChild(coreBlk1) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -631,15 +532,7 @@ func TestBatchedParseBlockPreForkOnly(t *testing.T) { } } - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk2.ID(), - HeightV: coreBlk2.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(coreBlk2) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -695,21 +588,13 @@ func TestBatchedParseBlockPostForkOnly(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, proRemoteVM, coreGenBlk := initTestRemoteProposerVM(t, activationTime, durangoTime) + coreVM, proRemoteVM := initTestRemoteProposerVM(t, activationTime, durangoTime) defer func() { require.NoError(proRemoteVM.Shutdown(context.Background())) }() // Build some post-Fork blocks.... - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -721,15 +606,7 @@ func TestBatchedParseBlockPostForkOnly(t *testing.T) { require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk1.ID())) require.NoError(waitForProposerWindow(proRemoteVM, builtBlk1, 0)) - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk1.ID(), - HeightV: coreBlk1.Height() + 1, - } + coreBlk2 := snowmantest.BuildChild(coreBlk1) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -741,15 +618,7 @@ func TestBatchedParseBlockPostForkOnly(t *testing.T) { require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk2.ID())) require.NoError(waitForProposerWindow(proRemoteVM, builtBlk2, builtBlk2.(*postForkBlock).PChainHeight())) - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk2.ID(), - HeightV: coreBlk2.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(coreBlk2) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -812,23 +681,15 @@ func TestBatchedParseBlockAtSnomanPlusPlusFork(t *testing.T) { ) // enable ProBlks in next future - coreVM, proRemoteVM, coreGenBlk := initTestRemoteProposerVM(t, forkTime, durangoTime) + coreVM, proRemoteVM := initTestRemoteProposerVM(t, forkTime, durangoTime) defer func() { require.NoError(proRemoteVM.Shutdown(context.Background())) }() // Build some prefork blocks.... proRemoteVM.Set(preForkTime) - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: preForkTime, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) + coreBlk1.TimestampV = preForkTime coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -847,16 +708,8 @@ func TestBatchedParseBlockAtSnomanPlusPlusFork(t *testing.T) { } } - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk1.ID(), - HeightV: coreBlk1.Height() + 1, - TimestampV: postForkTime, - } + coreBlk2 := snowmantest.BuildChild(coreBlk1) + coreBlk2.TimestampV = postForkTime coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -877,15 +730,7 @@ func TestBatchedParseBlockAtSnomanPlusPlusFork(t *testing.T) { // .. and some post-fork proRemoteVM.Set(postForkTime) - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk2.ID(), - HeightV: coreBlk2.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(coreBlk2) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -898,15 +743,7 @@ func TestBatchedParseBlockAtSnomanPlusPlusFork(t *testing.T) { require.NoError(proRemoteVM.SetPreference(context.Background(), builtBlk3.ID())) require.NoError(waitForProposerWindow(proRemoteVM, builtBlk3, builtBlk3.(*postForkBlock).PChainHeight())) - coreBlk4 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{4}, - ParentV: coreBlk3.ID(), - HeightV: coreBlk3.Height() + 1, - } + coreBlk4 := snowmantest.BuildChild(coreBlk3) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk4, nil } @@ -977,20 +814,9 @@ func initTestRemoteProposerVM( ) ( TestRemoteProposerVM, *VM, - *snowman.TestBlock, ) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: []byte{0}, - } - initialState := []byte("genesis state") coreVM := TestRemoteProposerVM{ TestVM: &block.TestVM{}, @@ -1013,20 +839,20 @@ func initTestRemoteProposerVM( return nil } coreVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreGenBlk.ID(): - return coreGenBlk, nil + switch blkID { + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -1049,7 +875,7 @@ func initTestRemoteProposerVM( T: t, } valState.GetMinimumHeightF = func(context.Context) (uint64, error) { - return coreGenBlk.Height(), nil + return snowmantest.GenesisHeight, nil } valState.GetCurrentHeightF = func(context.Context) (uint64, error) { return defaultPChainHeight, nil @@ -1101,6 +927,6 @@ func initTestRemoteProposerVM( coreVM.InitializeF = nil require.NoError(proVM.SetState(context.Background(), snow.NormalOp)) - require.NoError(proVM.SetPreference(context.Background(), coreGenBlk.IDV)) - return coreVM, proVM, coreGenBlk + require.NoError(proVM.SetPreference(context.Background(), snowmantest.GenesisID)) + return coreVM, proVM } diff --git a/vms/proposervm/block_test.go b/vms/proposervm/block_test.go index 3743cf8fa626..1d16222f0124 100644 --- a/vms/proposervm/block_test.go +++ b/vms/proposervm/block_test.go @@ -17,8 +17,8 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" - "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/staking" @@ -45,11 +45,11 @@ func TestPostForkCommonComponents_buildChild(t *testing.T) { blkID = ids.GenerateTestID() ) - innerBlk := snowman.NewMockBlock(ctrl) + innerBlk := snowmantest.NewMockBlock(ctrl) innerBlk.EXPECT().ID().Return(blkID).AnyTimes() innerBlk.EXPECT().Height().Return(parentHeight + 1).AnyTimes() - builtBlk := snowman.NewMockBlock(ctrl) + builtBlk := snowmantest.NewMockBlock(ctrl) builtBlk.EXPECT().Bytes().Return([]byte{1, 2, 3}).AnyTimes() builtBlk.EXPECT().ID().Return(ids.GenerateTestID()).AnyTimes() builtBlk.EXPECT().Height().Return(pChainHeight).AnyTimes() @@ -109,7 +109,7 @@ func TestPreDurangoValidatorNodeBlockBuiltDelaysTests(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(ctx)) }() @@ -118,24 +118,16 @@ func TestPreDurangoValidatorNodeBlockBuiltDelaysTests(t *testing.T) { parentTime := time.Now().Truncate(time.Second) proVM.Set(parentTime) - coreParentBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreParentBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreParentBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreParentBlk.ID(): + switch blkID { + case coreParentBlk.ID(): return coreParentBlk, nil - case blkID == coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -144,8 +136,8 @@ func TestPreDurangoValidatorNodeBlockBuiltDelaysTests(t *testing.T) { switch { case bytes.Equal(b, coreParentBlk.Bytes()): return coreParentBlk, nil - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -176,15 +168,7 @@ func TestPreDurangoValidatorNodeBlockBuiltDelaysTests(t *testing.T) { }, nil } - coreChildBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreParentBlk.ID(), - HeightV: coreParentBlk.Height() + 1, - } + coreChildBlk := snowmantest.BuildChild(coreParentBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreChildBlk, nil } @@ -246,7 +230,7 @@ func TestPreDurangoNonValidatorNodeBlockBuiltDelaysTests(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(ctx)) }() @@ -255,24 +239,16 @@ func TestPreDurangoNonValidatorNodeBlockBuiltDelaysTests(t *testing.T) { parentTime := time.Now().Truncate(time.Second) proVM.Set(parentTime) - coreParentBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreParentBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreParentBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreParentBlk.ID(): + switch blkID { + case coreParentBlk.ID(): return coreParentBlk, nil - case blkID == coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -281,8 +257,8 @@ func TestPreDurangoNonValidatorNodeBlockBuiltDelaysTests(t *testing.T) { switch { case bytes.Equal(b, coreParentBlk.Bytes()): return coreParentBlk, nil - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -315,15 +291,7 @@ func TestPreDurangoNonValidatorNodeBlockBuiltDelaysTests(t *testing.T) { }, nil } - coreChildBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreParentBlk.ID(), - HeightV: coreParentBlk.Height() + 1, - } + coreChildBlk := snowmantest.BuildChild(coreParentBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreChildBlk, nil } @@ -387,7 +355,7 @@ func TestPostDurangoBuildChildResetScheduler(t *testing.T) { parentHeight uint64 = 1234 ) - innerBlk := snowman.NewMockBlock(ctrl) + innerBlk := snowmantest.NewMockBlock(ctrl) innerBlk.EXPECT().Height().Return(parentHeight + 1).AnyTimes() vdrState := validators.NewMockState(ctrl) diff --git a/vms/proposervm/post_fork_block_test.go b/vms/proposervm/post_fork_block_test.go index a16d4a7d6219..416fc087b101 100644 --- a/vms/proposervm/post_fork_block_test.go +++ b/vms/proposervm/post_fork_block_test.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/utils/timer/mockable" "github.com/ava-labs/avalanchego/vms/proposervm/block" @@ -31,7 +32,7 @@ func TestOracle_PostForkBlock_ImplementsInterface(t *testing.T) { // setup proBlk := postForkBlock{ postForkCommonComponents: postForkCommonComponents{ - innerBlk: &snowman.TestBlock{}, + innerBlk: snowmantest.BuildChild(snowmantest.Genesis), }, } @@ -44,31 +45,17 @@ func TestOracle_PostForkBlock_ImplementsInterface(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - _, _, proVM, _, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + _, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() + innerTestBlock := snowmantest.BuildChild(snowmantest.Genesis) innerOracleBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - }, - BytesV: []byte{1}, - }, + Block: *innerTestBlock, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - }, - BytesV: []byte{2}, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - }, - BytesV: []byte{3}, - }, + snowmantest.BuildChild(innerTestBlock), + snowmantest.BuildChild(innerTestBlock), }, } @@ -104,7 +91,7 @@ func TestBlockVerify_PostForkBlock_PreDurango_ParentChecks(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime // pre Durango ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -115,22 +102,14 @@ func TestBlockVerify_PostForkBlock_PreDurango_ParentChecks(t *testing.T) { } // create parent block ... - parentCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + parentCoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return parentCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case parentCoreBlk.ID(): return parentCoreBlk, nil default: @@ -139,8 +118,8 @@ func TestBlockVerify_PostForkBlock_PreDurango_ParentChecks(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, parentCoreBlk.Bytes()): return parentCoreBlk, nil default: @@ -155,11 +134,7 @@ func TestBlockVerify_PostForkBlock_PreDurango_ParentChecks(t *testing.T) { require.NoError(proVM.SetPreference(context.Background(), parentBlk.ID())) // .. create child block ... - childCoreBlk := &snowman.TestBlock{ - ParentV: parentCoreBlk.ID(), - BytesV: []byte{2}, - HeightV: parentCoreBlk.Height() + 1, - } + childCoreBlk := snowmantest.BuildChild(parentCoreBlk) childBlk := postForkBlock{ postForkCommonComponents: postForkCommonComponents{ vm: proVM, @@ -208,7 +183,7 @@ func TestBlockVerify_PostForkBlock_PostDurango_ParentChecks(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime // post Durango ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -218,22 +193,14 @@ func TestBlockVerify_PostForkBlock_PostDurango_ParentChecks(t *testing.T) { return pChainHeight, nil } - parentCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + parentCoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return parentCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case parentCoreBlk.ID(): return parentCoreBlk, nil default: @@ -242,8 +209,8 @@ func TestBlockVerify_PostForkBlock_PostDurango_ParentChecks(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, parentCoreBlk.Bytes()): return parentCoreBlk, nil default: @@ -257,11 +224,7 @@ func TestBlockVerify_PostForkBlock_PostDurango_ParentChecks(t *testing.T) { require.NoError(parentBlk.Verify(context.Background())) require.NoError(proVM.SetPreference(context.Background(), parentBlk.ID())) - childCoreBlk := &snowman.TestBlock{ - ParentV: parentCoreBlk.ID(), - BytesV: []byte{2}, - HeightV: parentCoreBlk.Height() + 1, - } + childCoreBlk := snowmantest.BuildChild(parentCoreBlk) childBlk := postForkBlock{ postForkCommonComponents: postForkCommonComponents{ vm: proVM, @@ -317,7 +280,7 @@ func TestBlockVerify_PostForkBlock_TimestampChecks(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -347,22 +310,14 @@ func TestBlockVerify_PostForkBlock_TimestampChecks(t *testing.T) { } // create parent block ... - parentCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + parentCoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return parentCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case parentCoreBlk.ID(): return parentCoreBlk, nil default: @@ -371,8 +326,8 @@ func TestBlockVerify_PostForkBlock_TimestampChecks(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, parentCoreBlk.Bytes()): return parentCoreBlk, nil default: @@ -391,15 +346,7 @@ func TestBlockVerify_PostForkBlock_TimestampChecks(t *testing.T) { parentPChainHeight = parentBlk.(*postForkBlock).PChainHeight() ) - childCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - ParentV: parentCoreBlk.ID(), - HeightV: parentCoreBlk.Height() + 1, - BytesV: []byte{2}, - } + childCoreBlk := snowmantest.BuildChild(parentCoreBlk) childBlk := postForkBlock{ postForkCommonComponents: postForkCommonComponents{ vm: proVM, @@ -538,7 +485,7 @@ func TestBlockVerify_PostForkBlock_PChainHeightChecks(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -552,22 +499,14 @@ func TestBlockVerify_PostForkBlock_PChainHeightChecks(t *testing.T) { } // create parent block ... - parentCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + parentCoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return parentCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case parentCoreBlk.ID(): return parentCoreBlk, nil default: @@ -576,8 +515,8 @@ func TestBlockVerify_PostForkBlock_PChainHeightChecks(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, parentCoreBlk.Bytes()): return parentCoreBlk, nil default: @@ -596,15 +535,7 @@ func TestBlockVerify_PostForkBlock_PChainHeightChecks(t *testing.T) { parentBlkPChainHeight := parentBlk.(*postForkBlock).PChainHeight() require.NoError(waitForProposerWindow(proVM, parentBlk, parentBlkPChainHeight)) - childCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - ParentV: parentCoreBlk.ID(), - HeightV: parentBlk.Height() + 1, - BytesV: []byte{2}, - } + childCoreBlk := snowmantest.BuildChild(parentCoreBlk) childBlk := postForkBlock{ postForkCommonComponents: postForkCommonComponents{ vm: proVM, @@ -709,7 +640,7 @@ func TestBlockVerify_PostForkBlockBuiltOnOption_PChainHeightChecks(t *testing.T) activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -723,36 +654,14 @@ func TestBlockVerify_PostForkBlockBuiltOnOption_PChainHeightChecks(t *testing.T) } // create post fork oracle block ... + innerTestBlock := snowmantest.BuildChild(snowmantest.Genesis) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, + Block: *innerTestBlock, } + preferredOracleBlkChild := snowmantest.BuildChild(innerTestBlock) oracleCoreBlk.opts = [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - }, + preferredOracleBlkChild, + snowmantest.BuildChild(innerTestBlock), } coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { @@ -760,8 +669,8 @@ func TestBlockVerify_PostForkBlockBuiltOnOption_PChainHeightChecks(t *testing.T) } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -774,8 +683,8 @@ func TestBlockVerify_PostForkBlockBuiltOnOption_PChainHeightChecks(t *testing.T) } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, oracleCoreBlk.Bytes()): return oracleCoreBlk, nil case bytes.Equal(b, oracleCoreBlk.opts[0].Bytes()): @@ -810,15 +719,7 @@ func TestBlockVerify_PostForkBlockBuiltOnOption_PChainHeightChecks(t *testing.T) parentBlkPChainHeight := postForkOracleBlk.PChainHeight() // option takes proposal blocks' Pchain height - childCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - ParentV: oracleCoreBlk.opts[0].ID(), - BytesV: []byte{2}, - HeightV: oracleCoreBlk.opts[0].Height() + 1, - } + childCoreBlk := snowmantest.BuildChild(preferredOracleBlkChild) childBlk := postForkBlock{ postForkCommonComponents: postForkCommonComponents{ vm: proVM, @@ -909,7 +810,7 @@ func TestBlockVerify_PostForkBlock_CoreBlockVerifyIsCalledOnce(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -919,22 +820,14 @@ func TestBlockVerify_PostForkBlock_CoreBlockVerifyIsCalledOnce(t *testing.T) { return pChainHeight, nil } - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -943,8 +836,8 @@ func TestBlockVerify_PostForkBlock_CoreBlockVerifyIsCalledOnce(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil default: @@ -976,7 +869,7 @@ func TestBlockAccept_PostForkBlock_SetsLastAcceptedBlock(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -986,22 +879,14 @@ func TestBlockAccept_PostForkBlock_SetsLastAcceptedBlock(t *testing.T) { return pChainHeight, nil } - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -1010,8 +895,8 @@ func TestBlockAccept_PostForkBlock_SetsLastAcceptedBlock(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil default: @@ -1029,7 +914,7 @@ func TestBlockAccept_PostForkBlock_SetsLastAcceptedBlock(t *testing.T) { if coreBlk.Status() == choices.Accepted { return coreBlk.ID(), nil } - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } acceptedID, err := proVM.LastAccepted(context.Background()) require.NoError(err) @@ -1043,7 +928,7 @@ func TestBlockAccept_PostForkBlock_TwoProBlocksWithSameCoreBlock_OneIsAccepted(t activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -1054,20 +939,12 @@ func TestBlockAccept_PostForkBlock_TwoProBlocksWithSameCoreBlock_OneIsAccepted(t } // generate two blocks with the same core block and store them - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } - minimumHeight = coreGenBlk.Height() + minimumHeight = snowmantest.GenesisHeight proBlk1, err := proVM.BuildBlock(context.Background()) require.NoError(err) @@ -1094,20 +971,12 @@ func TestBlockReject_PostForkBlock_InnerBlockIsNotRejected(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -1130,44 +999,19 @@ func TestBlockVerify_PostForkBlock_ShouldBePostForkOption(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create post fork oracle block ... + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, - } - coreOpt0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - } - coreOpt1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, + Block: *coreTestBlk, + opts: [2]snowman.Block{ + snowmantest.BuildChild(coreTestBlk), + snowmantest.BuildChild(coreTestBlk), }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - } - oracleCoreBlk.opts = [2]snowman.Block{ - coreOpt0, - coreOpt1, } coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { @@ -1175,8 +1019,8 @@ func TestBlockVerify_PostForkBlock_ShouldBePostForkOption(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -1189,8 +1033,8 @@ func TestBlockVerify_PostForkBlock_ShouldBePostForkOption(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, oracleCoreBlk.Bytes()): return oracleCoreBlk, nil case bytes.Equal(b, oracleCoreBlk.opts[0].Bytes()): @@ -1248,25 +1092,16 @@ func TestBlockVerify_PostForkBlock_PChainTooLow(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 5) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 5) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } - + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -1275,8 +1110,8 @@ func TestBlockVerify_PostForkBlock_PChainTooLow(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil default: @@ -1285,8 +1120,8 @@ func TestBlockVerify_PostForkBlock_PChainTooLow(t *testing.T) { } statelessChild, err := block.BuildUnsigned( - coreGenBlk.ID(), - coreGenBlk.Timestamp(), + snowmantest.GenesisID, + snowmantest.GenesisTimestamp, 4, coreBlk.Bytes(), ) diff --git a/vms/proposervm/post_fork_option_test.go b/vms/proposervm/post_fork_option_test.go index dd16f8cdb518..39c6434dddf6 100644 --- a/vms/proposervm/post_fork_option_test.go +++ b/vms/proposervm/post_fork_option_test.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/vms/proposervm/block" ) @@ -23,7 +24,7 @@ import ( var _ snowman.OracleBlock = (*TestOptionsBlock)(nil) type TestOptionsBlock struct { - snowman.TestBlock + snowmantest.Block opts [2]snowman.Block optsErr error } @@ -40,41 +41,19 @@ func TestBlockVerify_PostForkOption_ParentChecks(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create post fork oracle block ... + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) + preferredBlk := snowmantest.BuildChild(coreTestBlk) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, - } - oracleCoreBlk.opts = [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, + Block: *coreTestBlk, + opts: [2]snowman.Block{ + preferredBlk, + snowmantest.BuildChild(coreTestBlk), }, } @@ -83,8 +62,8 @@ func TestBlockVerify_PostForkOption_ParentChecks(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -97,8 +76,8 @@ func TestBlockVerify_PostForkOption_ParentChecks(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, oracleCoreBlk.Bytes()): return oracleCoreBlk, nil case bytes.Equal(b, oracleCoreBlk.opts[0].Bytes()): @@ -130,15 +109,7 @@ func TestBlockVerify_PostForkOption_ParentChecks(t *testing.T) { // show we can build on options require.NoError(proVM.SetPreference(context.Background(), opts[0].ID())) - childCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(4444), - StatusV: choices.Processing, - }, - ParentV: oracleCoreBlk.opts[0].ID(), - BytesV: []byte{4}, - HeightV: oracleCoreBlk.opts[0].Height() + 1, - } + childCoreBlk := snowmantest.BuildChild(preferredBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return childCoreBlk, nil } @@ -159,44 +130,21 @@ func TestBlockVerify_PostForkOption_CoreBlockVerifyIsCalledOnce(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create post fork oracle block ... + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) + coreOpt0 := snowmantest.BuildChild(coreTestBlk) + coreOpt1 := snowmantest.BuildChild(coreTestBlk) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, - } - coreOpt0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - } - coreOpt1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, + Block: *coreTestBlk, + opts: [2]snowman.Block{ + coreOpt0, + coreOpt1, }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - } - oracleCoreBlk.opts = [2]snowman.Block{ - coreOpt0, - coreOpt1, } coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { @@ -204,8 +152,8 @@ func TestBlockVerify_PostForkOption_CoreBlockVerifyIsCalledOnce(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -218,8 +166,8 @@ func TestBlockVerify_PostForkOption_CoreBlockVerifyIsCalledOnce(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, oracleCoreBlk.Bytes()): return oracleCoreBlk, nil case bytes.Equal(b, oracleCoreBlk.opts[0].Bytes()): @@ -264,41 +212,18 @@ func TestBlockAccept_PostForkOption_SetsLastAcceptedBlock(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create post fork oracle block ... + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, - } - oracleCoreBlk.opts = [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, + Block: *coreTestBlk, + opts: [2]snowman.Block{ + snowmantest.BuildChild(coreTestBlk), + snowmantest.BuildChild(coreTestBlk), }, } @@ -307,8 +232,8 @@ func TestBlockAccept_PostForkOption_SetsLastAcceptedBlock(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -321,8 +246,8 @@ func TestBlockAccept_PostForkOption_SetsLastAcceptedBlock(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, oracleCoreBlk.Bytes()): return oracleCoreBlk, nil case bytes.Equal(b, oracleCoreBlk.opts[0].Bytes()): @@ -344,7 +269,7 @@ func TestBlockAccept_PostForkOption_SetsLastAcceptedBlock(t *testing.T) { if oracleCoreBlk.Status() == choices.Accepted { return oracleCoreBlk.ID(), nil } - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } acceptedID, err := proVM.LastAccepted(context.Background()) require.NoError(err) @@ -377,41 +302,18 @@ func TestBlockReject_InnerBlockIsNotRejected(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create post fork oracle block ... + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, - } - oracleCoreBlk.opts = [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - HeightV: oracleCoreBlk.Height() + 1, + Block: *coreTestBlk, + opts: [2]snowman.Block{ + snowmantest.BuildChild(coreTestBlk), + snowmantest.BuildChild(coreTestBlk), }, } @@ -420,8 +322,8 @@ func TestBlockReject_InnerBlockIsNotRejected(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -434,8 +336,8 @@ func TestBlockReject_InnerBlockIsNotRejected(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, oracleCoreBlk.Bytes()): return oracleCoreBlk, nil case bytes.Equal(b, oracleCoreBlk.opts[0].Bytes()): @@ -482,41 +384,26 @@ func TestBlockVerify_PostForkOption_ParentIsNotOracleWithError(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) coreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, + Block: *coreTestBlk, optsErr: snowman.ErrNotOracle, } - coreChildBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk.ID(), - HeightV: coreBlk.Height() + 1, - } + coreChildBlk := snowmantest.BuildChild(coreTestBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil case coreChildBlk.ID(): @@ -527,8 +414,8 @@ func TestBlockVerify_PostForkOption_ParentIsNotOracleWithError(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil case bytes.Equal(b, coreChildBlk.Bytes()): @@ -570,44 +457,20 @@ func TestOptionTimestampValidity(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, db := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, db := initTestProposerVM(t, activationTime, durangoTime, 0) - coreOracleBlkID := ids.GenerateTestID() + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) coreOracleBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: coreOracleBlkID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - }, + Block: *coreTestBlk, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreOracleBlkID, - HeightV: coreGenBlk.Height() + 2, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreOracleBlkID, - HeightV: coreGenBlk.Height() + 2, - }, + snowmantest.BuildChild(coreTestBlk), + snowmantest.BuildChild(coreTestBlk), }, } oracleBlkTime := proVM.Time().Truncate(time.Second) statelessBlock, err := block.BuildUnsigned( - coreGenBlk.ID(), + snowmantest.GenesisID, oracleBlkTime, 0, coreOracleBlk.Bytes(), @@ -616,8 +479,8 @@ func TestOptionTimestampValidity(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreOracleBlk.ID(): return coreOracleBlk, nil case coreOracleBlk.opts[0].ID(): @@ -630,8 +493,8 @@ func TestOptionTimestampValidity(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreOracleBlk.Bytes()): return coreOracleBlk, nil case bytes.Equal(b, coreOracleBlk.opts[0].Bytes()): @@ -707,8 +570,8 @@ func TestOptionTimestampValidity(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreOracleBlk.ID(): return coreOracleBlk, nil case coreOracleBlk.opts[0].ID(): @@ -721,8 +584,8 @@ func TestOptionTimestampValidity(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreOracleBlk.Bytes()): return coreOracleBlk, nil case bytes.Equal(b, coreOracleBlk.opts[0].Bytes()): diff --git a/vms/proposervm/pre_fork_block_test.go b/vms/proposervm/pre_fork_block_test.go index 3261f5f9ee9a..892209781e8c 100644 --- a/vms/proposervm/pre_fork_block_test.go +++ b/vms/proposervm/pre_fork_block_test.go @@ -17,6 +17,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/utils/logging" @@ -30,7 +31,7 @@ func TestOracle_PreForkBlkImplementsInterface(t *testing.T) { // setup proBlk := preForkBlock{ - Block: &snowman.TestBlock{}, + Block: snowmantest.BuildChild(snowmantest.Genesis), } // test @@ -54,38 +55,19 @@ func TestOracle_PreForkBlkCanBuiltOnPreForkOption(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create pre fork oracle block ... + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) + preferredTestBlk := snowmantest.BuildChild(coreTestBlk) oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - }, - } - oracleCoreBlk.opts = [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), + Block: *coreTestBlk, + opts: [2]snowman.Block{ + preferredTestBlk, + snowmantest.BuildChild(coreTestBlk), }, } @@ -94,8 +76,8 @@ func TestOracle_PreForkBlkCanBuiltOnPreForkOption(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -121,14 +103,7 @@ func TestOracle_PreForkBlkCanBuiltOnPreForkOption(t *testing.T) { require.NoError(proVM.SetPreference(context.Background(), opts[0].ID())) lastCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(4444), - StatusV: choices.Processing, - }, - BytesV: []byte{4}, - ParentV: oracleCoreBlk.opts[0].ID(), - }, + Block: *snowmantest.BuildChild(preferredTestBlk), } coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return lastCoreBlk, nil @@ -143,46 +118,30 @@ func TestOracle_PostForkBlkCanBuiltOnPreForkOption(t *testing.T) { require := require.New(t) var ( - activationTime = genesisTimestamp.Add(10 * time.Second) + activationTime = snowmantest.GenesisTimestamp.Add(10 * time.Second) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create pre fork oracle block pre activation time... - oracleCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: activationTime.Add(-1 * time.Second), - }, - } + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) + coreTestBlk.TimestampV = activationTime.Add(-1 * time.Second) // ... whose options are post activation time - oracleCoreBlk.opts = [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: oracleCoreBlk.ID(), - TimestampV: activationTime.Add(time.Second), - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(3333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: oracleCoreBlk.ID(), - TimestampV: activationTime.Add(time.Second), + preferredBlk := snowmantest.BuildChild(coreTestBlk) + preferredBlk.TimestampV = activationTime.Add(time.Second) + + unpreferredBlk := snowmantest.BuildChild(coreTestBlk) + unpreferredBlk.TimestampV = activationTime.Add(time.Second) + + oracleCoreBlk := &TestOptionsBlock{ + Block: *coreTestBlk, + opts: [2]snowman.Block{ + preferredBlk, + unpreferredBlk, }, } @@ -191,8 +150,8 @@ func TestOracle_PostForkBlkCanBuiltOnPreForkOption(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case oracleCoreBlk.ID(): return oracleCoreBlk, nil case oracleCoreBlk.opts[0].ID(): @@ -218,14 +177,7 @@ func TestOracle_PostForkBlkCanBuiltOnPreForkOption(t *testing.T) { require.NoError(proVM.SetPreference(context.Background(), opts[0].ID())) lastCoreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(4444), - StatusV: choices.Processing, - }, - BytesV: []byte{4}, - ParentV: oracleCoreBlk.opts[0].ID(), - }, + Block: *snowmantest.BuildChild(preferredBlk), } coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return lastCoreBlk, nil @@ -240,33 +192,23 @@ func TestBlockVerify_PreFork_ParentChecks(t *testing.T) { require := require.New(t) var ( - activationTime = genesisTimestamp.Add(10 * time.Second) + activationTime = snowmantest.GenesisTimestamp.Add(10 * time.Second) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - require.True(coreGenBlk.Timestamp().Before(activationTime)) - // create parent block ... - parentCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: coreGenBlk.Timestamp(), - } + parentCoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return parentCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case parentCoreBlk.ID(): return parentCoreBlk, nil default: @@ -275,8 +217,8 @@ func TestBlockVerify_PreFork_ParentChecks(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, parentCoreBlk.Bytes()): return parentCoreBlk, nil default: @@ -288,14 +230,7 @@ func TestBlockVerify_PreFork_ParentChecks(t *testing.T) { require.NoError(err) // .. create child block ... - childCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - TimestampV: parentCoreBlk.Timestamp(), - } + childCoreBlk := snowmantest.BuildChild(parentCoreBlk) childBlk := preForkBlock{ Block: childCoreBlk, vm: proVM, @@ -319,28 +254,19 @@ func TestBlockVerify_BlocksBuiltOnPreForkGenesis(t *testing.T) { require := require.New(t) var ( - activationTime = genesisTimestamp.Add(10 * time.Second) + activationTime = snowmantest.GenesisTimestamp.Add(10 * time.Second) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - require.True(coreGenBlk.Timestamp().Before(activationTime)) preActivationTime := activationTime.Add(-1 * time.Second) proVM.Set(preActivationTime) - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: preActivationTime, - VerifyV: nil, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) + coreBlk.TimestampV = preActivationTime coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -354,7 +280,7 @@ func TestBlockVerify_BlocksBuiltOnPreForkGenesis(t *testing.T) { // postFork block does NOT verify if parent is before fork activation time postForkStatelessChild, err := statelessblock.Build( - coreGenBlk.ID(), + snowmantest.GenesisID, coreBlk.Timestamp(), 0, // pChainHeight proVM.StakingCertLeaf, @@ -385,22 +311,15 @@ func TestBlockVerify_BlocksBuiltOnPreForkGenesis(t *testing.T) { } require.NoError(proVM.SetPreference(context.Background(), preForkChild.ID())) - secondCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - }, - BytesV: []byte{2}, - ParentV: coreBlk.ID(), - TimestampV: postActivationTime, - VerifyV: nil, - } + secondCoreBlk := snowmantest.BuildChild(coreBlk) + secondCoreBlk.TimestampV = postActivationTime coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return secondCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { switch id { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -416,22 +335,14 @@ func TestBlockVerify_BlocksBuiltOnPreForkGenesis(t *testing.T) { require.NoError(lastPreForkBlk.Verify(context.Background())) require.NoError(proVM.SetPreference(context.Background(), lastPreForkBlk.ID())) - thirdCoreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(333), - }, - BytesV: []byte{3}, - ParentV: secondCoreBlk.ID(), - TimestampV: postActivationTime, - VerifyV: nil, - } + thirdCoreBlk := snowmantest.BuildChild(secondCoreBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return thirdCoreBlk, nil } coreVM.GetBlockF = func(_ context.Context, id ids.ID) (snowman.Block, error) { switch id { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil case secondCoreBlk.ID(): @@ -453,26 +364,17 @@ func TestBlockVerify_BlocksBuiltOnPostForkGenesis(t *testing.T) { require := require.New(t) var ( - activationTime = genesisTimestamp.Add(-1 * time.Second) + activationTime = snowmantest.GenesisTimestamp.Add(-1 * time.Second) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) proVM.Set(activationTime) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // build parent block after fork activation time ... - coreBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: coreGenBlk.Timestamp(), - VerifyV: nil, - } + coreBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlock, nil } @@ -501,26 +403,19 @@ func TestBlockAccept_PreFork_SetsLastAcceptedBlock(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -529,8 +424,8 @@ func TestBlockAccept_PreFork_SetsLastAcceptedBlock(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil default: @@ -548,7 +443,7 @@ func TestBlockAccept_PreFork_SetsLastAcceptedBlock(t *testing.T) { if coreBlk.Status() == choices.Accepted { return coreBlk.ID(), nil } - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } acceptedID, err := proVM.LastAccepted(context.Background()) require.NoError(err) @@ -563,20 +458,12 @@ func TestBlockReject_PreForkBlock_InnerBlockIsRejected(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -595,55 +482,31 @@ func TestBlockVerify_ForkBlockIsOracleBlock(t *testing.T) { require := require.New(t) var ( - activationTime = genesisTimestamp.Add(10 * time.Second) + activationTime = snowmantest.GenesisTimestamp.Add(10 * time.Second) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - require.True(coreGenBlk.Timestamp().Before(activationTime)) postActivationTime := activationTime.Add(time.Second) proVM.Set(postActivationTime) - coreBlkID := ids.GenerateTestID() + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) + coreTestBlk.TimestampV = postActivationTime coreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: coreBlkID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: postActivationTime, - }, + Block: *coreTestBlk, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlkID, - TimestampV: postActivationTime, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlkID, - TimestampV: postActivationTime, - }, + snowmantest.BuildChild(coreTestBlk), + snowmantest.BuildChild(coreTestBlk), }, } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil case coreBlk.opts[0].ID(): @@ -656,8 +519,8 @@ func TestBlockVerify_ForkBlockIsOracleBlock(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil case bytes.Equal(b, coreBlk.opts[0].Bytes()): @@ -689,55 +552,31 @@ func TestBlockVerify_ForkBlockIsOracleBlockButChildrenAreSigned(t *testing.T) { require := require.New(t) var ( - activationTime = genesisTimestamp.Add(10 * time.Second) + activationTime = snowmantest.GenesisTimestamp.Add(10 * time.Second) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - require.True(coreGenBlk.Timestamp().Before(activationTime)) postActivationTime := activationTime.Add(time.Second) proVM.Set(postActivationTime) - coreBlkID := ids.GenerateTestID() + coreTestBlk := snowmantest.BuildChild(snowmantest.Genesis) + coreTestBlk.TimestampV = postActivationTime coreBlk := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: coreBlkID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: postActivationTime, - }, + Block: *coreTestBlk, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlkID, - TimestampV: postActivationTime, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlkID, - TimestampV: postActivationTime, - }, + snowmantest.BuildChild(coreTestBlk), + snowmantest.BuildChild(coreTestBlk), }, } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil case coreBlk.opts[0].ID(): @@ -750,8 +589,8 @@ func TestBlockVerify_ForkBlockIsOracleBlockButChildrenAreSigned(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil case bytes.Equal(b, coreBlk.opts[0].Bytes()): @@ -798,10 +637,10 @@ func TestPreForkBlock_BuildBlockWithContext(t *testing.T) { pChainHeight := uint64(1337) blkID := ids.GenerateTestID() - innerBlk := snowman.NewMockBlock(ctrl) + innerBlk := snowmantest.NewMockBlock(ctrl) innerBlk.EXPECT().ID().Return(blkID).AnyTimes() innerBlk.EXPECT().Timestamp().Return(mockable.MaxTime) - builtBlk := snowman.NewMockBlock(ctrl) + builtBlk := snowmantest.NewMockBlock(ctrl) builtBlk.EXPECT().Bytes().Return([]byte{1, 2, 3}).AnyTimes() builtBlk.EXPECT().ID().Return(ids.GenerateTestID()).AnyTimes() builtBlk.EXPECT().Height().Return(pChainHeight).AnyTimes() diff --git a/vms/proposervm/state_syncable_vm_test.go b/vms/proposervm/state_syncable_vm_test.go index d03f3c3d1c58..4f44adc0bf74 100644 --- a/vms/proposervm/state_syncable_vm_test.go +++ b/vms/proposervm/state_syncable_vm_test.go @@ -17,6 +17,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/snowtest" @@ -40,13 +41,6 @@ func helperBuildStateSyncTestObjects(t *testing.T) (*fullVM, *VM) { } // load innerVM expectations - innerGenesisBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.ID{'i', 'n', 'n', 'e', 'r', 'G', 'e', 'n', 'e', 's', 'i', 's', 'I', 'D'}, - }, - HeightV: 0, - BytesV: []byte("genesis state"), - } innerVM.InitializeF = func(context.Context, *snow.Context, database.Database, []byte, []byte, []byte, chan<- common.Message, []*common.Fx, common.AppSender, @@ -54,10 +48,10 @@ func helperBuildStateSyncTestObjects(t *testing.T) (*fullVM, *VM) { return nil } innerVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return innerGenesisBlk.ID(), nil + return snowmantest.GenesisID, nil } innerVM.GetBlockF = func(context.Context, ids.ID) (snowman.Block, error) { - return innerGenesisBlk, nil + return snowmantest.Genesis, nil } // create the VM @@ -81,7 +75,7 @@ func helperBuildStateSyncTestObjects(t *testing.T) (*fullVM, *VM) { context.Background(), ctx, prefixdb.New([]byte{}, memdb.New()), - innerGenesisBlk.Bytes(), + snowmantest.GenesisBytes, nil, nil, nil, @@ -166,7 +160,7 @@ func TestStateSyncGetOngoingSyncStateSummary(t *testing.T) { require.NoError(vm.SetForkHeight(innerSummary.Height() - 1)) // store post fork block associated with summary - innerBlk := &snowman.TestBlock{ + innerBlk := &snowmantest.Block{ BytesV: []byte{1}, ParentV: ids.GenerateTestID(), HeightV: innerSummary.Height(), @@ -250,7 +244,7 @@ func TestStateSyncGetLastStateSummary(t *testing.T) { require.NoError(vm.SetForkHeight(innerSummary.Height() - 1)) // store post fork block associated with summary - innerBlk := &snowman.TestBlock{ + innerBlk := &snowmantest.Block{ BytesV: []byte{1}, ParentV: ids.GenerateTestID(), HeightV: innerSummary.Height(), @@ -337,7 +331,7 @@ func TestStateSyncGetStateSummary(t *testing.T) { require.NoError(vm.SetForkHeight(innerSummary.Height() - 1)) // store post fork block associated with summary - innerBlk := &snowman.TestBlock{ + innerBlk := &snowmantest.Block{ BytesV: []byte{1}, ParentV: ids.GenerateTestID(), HeightV: innerSummary.Height(), @@ -409,7 +403,7 @@ func TestParseStateSummary(t *testing.T) { require.NoError(vm.SetForkHeight(innerSummary.Height() - 1)) // store post fork block associated with summary - innerBlk := &snowman.TestBlock{ + innerBlk := &snowmantest.Block{ BytesV: []byte{1}, ParentV: ids.GenerateTestID(), HeightV: innerSummary.Height(), @@ -467,7 +461,7 @@ func TestStateSummaryAccept(t *testing.T) { require.NoError(vm.SetForkHeight(innerSummary.Height() - 1)) // store post fork block associated with summary - innerBlk := &snowman.TestBlock{ + innerBlk := &snowmantest.Block{ BytesV: []byte{1}, ParentV: ids.GenerateTestID(), HeightV: innerSummary.Height(), @@ -538,7 +532,7 @@ func TestStateSummaryAcceptOlderBlock(t *testing.T) { vm.lastAcceptedHeight = innerSummary.Height() + 1 // store post fork block associated with summary - innerBlk := &snowman.TestBlock{ + innerBlk := &snowmantest.Block{ BytesV: []byte{1}, ParentV: ids.GenerateTestID(), HeightV: innerSummary.Height(), diff --git a/vms/proposervm/tree/tree_test.go b/vms/proposervm/tree/tree_test.go index 1e826e418c21..48095a40f638 100644 --- a/vms/proposervm/tree/tree_test.go +++ b/vms/proposervm/tree/tree_test.go @@ -9,32 +9,16 @@ import ( "github.com/stretchr/testify/require" - "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" - "github.com/ava-labs/avalanchego/snow/consensus/snowman" -) - -var ( - GenesisID = ids.GenerateTestID() - Genesis = &snowman.TestBlock{TestDecidable: choices.TestDecidable{ - IDV: GenesisID, - StatusV: choices.Accepted, - }} + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" ) func TestAcceptSingleBlock(t *testing.T) { require := require.New(t) - block := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.ID(), - } - tr := New() + block := snowmantest.BuildChild(snowmantest.Genesis) _, contains := tr.Get(block) require.False(contains) @@ -53,24 +37,11 @@ func TestAcceptSingleBlock(t *testing.T) { func TestAcceptBlockConflict(t *testing.T) { require := require.New(t) - blockToAccept := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.ID(), - } - - blockToReject := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.ID(), - } - tr := New() + blockToAccept := snowmantest.BuildChild(snowmantest.Genesis) + blockToReject := snowmantest.BuildChild(snowmantest.Genesis) + // add conflicting blocks tr.Add(blockToAccept) _, contains := tr.Get(blockToAccept) @@ -96,32 +67,12 @@ func TestAcceptBlockConflict(t *testing.T) { func TestAcceptChainConflict(t *testing.T) { require := require.New(t) - blockToAccept := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.ID(), - } - - blockToReject := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: Genesis.ID(), - } - - blockToRejectChild := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: blockToReject.ID(), - } - tr := New() + blockToAccept := snowmantest.BuildChild(snowmantest.Genesis) + blockToReject := snowmantest.BuildChild(snowmantest.Genesis) + blockToRejectChild := snowmantest.BuildChild(blockToReject) + // add conflicting blocks. tr.Add(blockToAccept) _, contains := tr.Get(blockToAccept) diff --git a/vms/proposervm/vm_byzantine_test.go b/vms/proposervm/vm_byzantine_test.go index c9ad1b98c79b..d4997781cdd4 100644 --- a/vms/proposervm/vm_byzantine_test.go +++ b/vms/proposervm/vm_byzantine_test.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/validators" "github.com/ava-labs/avalanchego/vms/proposervm/block" ) @@ -36,20 +37,12 @@ func TestInvalidByzantineProposerParent(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, gBlock, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - xBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - } + xBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return xBlock, nil } @@ -62,25 +55,15 @@ func TestInvalidByzantineProposerParent(t *testing.T) { require.NoError(aBlock.Verify(context.Background())) require.NoError(aBlock.Accept(context.Background())) - yBlockBytes := []byte{2} - yBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: yBlockBytes, - ParentV: xBlock.ID(), - HeightV: xBlock.Height() + 1, - } - + yBlock := snowmantest.BuildChild(xBlock) coreVM.ParseBlockF = func(_ context.Context, blockBytes []byte) (snowman.Block, error) { - if !bytes.Equal(blockBytes, yBlockBytes) { + if !bytes.Equal(blockBytes, yBlock.Bytes()) { return nil, errUnknownBlock } return yBlock, nil } - parsedBlock, err := proVM.ParseBlock(context.Background(), yBlockBytes) + parsedBlock, err := proVM.ParseBlock(context.Background(), yBlock.Bytes()) if err != nil { // If there was an error parsing, then this is fine. return @@ -107,39 +90,18 @@ func TestInvalidByzantineProposerOracleParent(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) - proVM.Set(coreGenBlk.Timestamp()) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + proVM.Set(snowmantest.GenesisTimestamp) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - xBlockID := ids.GenerateTestID() + xTestBlock := snowmantest.BuildChild(snowmantest.Genesis) xBlock := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: xBlockID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - }, + Block: *xTestBlock, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: xBlockID, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: xBlockID, - }, + snowmantest.BuildChild(xTestBlock), + snowmantest.BuildChild(xTestBlock), }, } @@ -148,8 +110,8 @@ func TestInvalidByzantineProposerOracleParent(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case xBlock.ID(): return xBlock, nil case xBlock.opts[0].ID(): @@ -162,8 +124,8 @@ func TestInvalidByzantineProposerOracleParent(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, xBlock.Bytes()): return xBlock, nil case bytes.Equal(b, xBlock.opts[0].Bytes()): @@ -216,39 +178,21 @@ func TestInvalidByzantineProposerPreForkParent(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, gBlock, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - xBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - } + xBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return xBlock, nil } - yBlockBytes := []byte{2} - yBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: yBlockBytes, - ParentV: xBlock.ID(), - HeightV: xBlock.Height() + 1, - } - + yBlock := snowmantest.BuildChild(xBlock) coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case gBlock.ID(): - return gBlock, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case xBlock.ID(): return xBlock, nil case yBlock.ID(): @@ -259,8 +203,8 @@ func TestInvalidByzantineProposerPreForkParent(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, blockBytes []byte) (snowman.Block, error) { switch { - case bytes.Equal(blockBytes, gBlock.Bytes()): - return gBlock, nil + case bytes.Equal(blockBytes, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(blockBytes, xBlock.Bytes()): return xBlock, nil case bytes.Equal(blockBytes, yBlock.Bytes()): @@ -306,38 +250,17 @@ func TestBlockVerify_PostForkOption_FaultyParent(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) - proVM.Set(coreGenBlk.Timestamp()) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + proVM.Set(snowmantest.GenesisTimestamp) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() xBlock := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - }, - opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreGenBlk.ID(), // valid block should reference xBlock - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreGenBlk.ID(), // valid block should reference xBlock - }, + Block: *snowmantest.BuildChild(snowmantest.Genesis), + opts: [2]snowman.Block{ // valid blocks should reference xBlock + snowmantest.BuildChild(snowmantest.Genesis), + snowmantest.BuildChild(snowmantest.Genesis), }, } @@ -346,8 +269,8 @@ func TestBlockVerify_PostForkOption_FaultyParent(t *testing.T) { } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case xBlock.ID(): return xBlock, nil case xBlock.opts[0].ID(): @@ -360,8 +283,8 @@ func TestBlockVerify_PostForkOption_FaultyParent(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, xBlock.Bytes()): return xBlock, nil case bytes.Equal(b, xBlock.opts[0].Bytes()): @@ -406,40 +329,19 @@ func TestBlockVerify_InvalidPostForkOption(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) - proVM.Set(coreGenBlk.Timestamp()) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + proVM.Set(snowmantest.GenesisTimestamp) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create an Oracle pre-fork block X - xBlockID := ids.GenerateTestID() + xTestBlock := snowmantest.BuildChild(snowmantest.Genesis) xBlock := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: xBlockID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - }, + Block: *xTestBlock, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: xBlockID, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: xBlockID, - }, + snowmantest.BuildChild(xTestBlock), + snowmantest.BuildChild(xTestBlock), }, } @@ -448,19 +350,10 @@ func TestBlockVerify_InvalidPostForkOption(t *testing.T) { xInnerOption := xInnerOptions[0] // create a non-Oracle pre-fork block Y - yBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } - + yBlock := snowmantest.BuildChild(snowmantest.Genesis) ySlb, err := block.BuildUnsigned( - coreGenBlk.ID(), - coreGenBlk.Timestamp(), + snowmantest.GenesisID, + snowmantest.GenesisTimestamp, uint64(2000), yBlock.Bytes(), ) @@ -525,33 +418,12 @@ func TestBlockVerify_InvalidPostForkOption(t *testing.T) { // create an Oracle pre-fork block Z // create post-fork block B from Y - zBlockID := ids.GenerateTestID() + zTestBlock := snowmantest.BuildChild(snowmantest.Genesis) zBlock := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: zBlockID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - }, + Block: *zTestBlock, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: zBlockID, - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: zBlockID, - }, + snowmantest.BuildChild(zTestBlock), + snowmantest.BuildChild(zTestBlock), }, } @@ -590,7 +462,7 @@ func TestGetBlock_MutatedSignature(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -605,33 +477,15 @@ func TestGetBlock_MutatedSignature(t *testing.T) { }, nil } - proVM.Set(coreGenBlk.Timestamp()) + proVM.Set(snowmantest.GenesisTimestamp) // Create valid core blocks to build our chain on. - coreBlk0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(1111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } - - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk0.ID(), - HeightV: coreBlk0.Height() + 1, - } - + coreBlk0 := snowmantest.BuildChild(snowmantest.Genesis) + coreBlk1 := snowmantest.BuildChild(coreBlk0) coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk0.ID(): return coreBlk0, nil case coreBlk1.ID(): @@ -642,8 +496,8 @@ func TestGetBlock_MutatedSignature(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk0.Bytes()): return coreBlk0, nil case bytes.Equal(b, coreBlk1.Bytes()): diff --git a/vms/proposervm/vm_test.go b/vms/proposervm/vm_test.go index ae47a33889a3..fb717c203f7f 100644 --- a/vms/proposervm/vm_test.go +++ b/vms/proposervm/vm_test.go @@ -22,6 +22,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/common" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/snowtest" @@ -49,9 +50,6 @@ var ( pTestSigner crypto.Signer pTestCert *staking.Certificate - genesisUnixTimestamp int64 = 1000 - genesisTimestamp = time.Unix(genesisUnixTimestamp, 0) - defaultPChainHeight uint64 = 2000 errUnknownBlock = errors.New("unknown block") @@ -82,21 +80,10 @@ func initTestProposerVM( *fullVM, *validators.TestState, *VM, - *snowman.TestBlock, database.Database, ) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: []byte{0}, - } - initialState := []byte("genesis state") coreVM := &fullVM{ TestVM: &block.TestVM{ @@ -116,20 +103,20 @@ func initTestProposerVM( return nil } coreVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreGenBlk.ID(): - return coreGenBlk, nil + switch blkID { + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -152,7 +139,7 @@ func initTestProposerVM( T: t, } valState.GetMinimumHeightF = func(context.Context) (uint64, error) { - return coreGenBlk.HeightV, nil + return snowmantest.GenesisHeight, nil } valState.GetCurrentHeightF = func(context.Context) (uint64, error) { return defaultPChainHeight, nil @@ -206,11 +193,11 @@ func initTestProposerVM( coreVM.InitializeF = nil require.NoError(proVM.SetState(context.Background(), snow.NormalOp)) - require.NoError(proVM.SetPreference(context.Background(), coreGenBlk.IDV)) + require.NoError(proVM.SetPreference(context.Background(), snowmantest.GenesisID)) - proVM.Set(coreGenBlk.Timestamp()) + proVM.Set(snowmantest.GenesisTimestamp) - return coreVM, valState, proVM, coreGenBlk, db + return coreVM, valState, proVM, db } func waitForProposerWindow(vm *VM, chainTip snowman.Block, pchainHeight uint64) error { @@ -250,7 +237,7 @@ func TestBuildBlockTimestampAreRoundedToSeconds(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -258,15 +245,7 @@ func TestBuildBlockTimestampAreRoundedToSeconds(t *testing.T) { skewedTimestamp := time.Now().Truncate(time.Second).Add(time.Millisecond) proVM.Set(skewedTimestamp) - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -286,20 +265,12 @@ func TestBuildBlockIsIdempotent(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -324,20 +295,12 @@ func TestFirstProposerBlockIsBuiltOnTopOfGenesis(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -361,36 +324,20 @@ func TestProposerBlocksAreBuiltOnPreferredProBlock(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // add two proBlks... - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } proBlk1, err := proVM.BuildBlock(context.Background()) require.NoError(err) - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk2 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -400,7 +347,7 @@ func TestProposerBlocksAreBuiltOnPreferredProBlock(t *testing.T) { require.NoError(proBlk2.Verify(context.Background())) // ...and set one as preferred - var prefcoreBlk *snowman.TestBlock + var prefcoreBlk *snowmantest.Block coreVM.SetPreferenceF = func(_ context.Context, prefID ids.ID) error { switch prefID { case coreBlk1.ID(): @@ -429,15 +376,7 @@ func TestProposerBlocksAreBuiltOnPreferredProBlock(t *testing.T) { require.NoError(proVM.SetPreference(context.Background(), proBlk2.ID())) // build block... - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: prefcoreBlk.ID(), - HeightV: prefcoreBlk.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(prefcoreBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -457,35 +396,19 @@ func TestCoreBlocksMustBeBuiltOnPreferredCoreBlock(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(111), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk1 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } proBlk1, err := proVM.BuildBlock(context.Background()) require.NoError(err) - coreBlk2 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(222), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk2 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk2, nil } @@ -496,7 +419,7 @@ func TestCoreBlocksMustBeBuiltOnPreferredCoreBlock(t *testing.T) { require.NoError(proBlk2.Verify(context.Background())) // ...and set one as preferred - var wronglyPreferredcoreBlk *snowman.TestBlock + var wronglyPreferredcoreBlk *snowmantest.Block coreVM.SetPreferenceF = func(_ context.Context, prefID ids.ID) error { switch prefID { case coreBlk1.ID(): @@ -525,15 +448,7 @@ func TestCoreBlocksMustBeBuiltOnPreferredCoreBlock(t *testing.T) { require.NoError(proVM.SetPreference(context.Background(), proBlk2.ID())) // build block... - coreBlk3 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: wronglyPreferredcoreBlk.ID(), - HeightV: wronglyPreferredcoreBlk.Height() + 1, - } + coreBlk3 := snowmantest.BuildChild(wronglyPreferredcoreBlk) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk3, nil } @@ -554,17 +469,16 @@ func TestCoreBlockFailureCauseProposerBlockParseFailure(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, _, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - innerBlk := &snowman.TestBlock{ - BytesV: []byte{1}, - } coreVM.ParseBlockF = func(context.Context, []byte) (snowman.Block, error) { return nil, errMarshallingFailed } + + innerBlk := snowmantest.BuildChild(snowmantest.Genesis) slb, err := statelessblock.Build( proVM.preferred, proVM.Time(), @@ -596,17 +510,13 @@ func TestTwoProBlocksWrappingSameCoreBlockCanBeParsed(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, gencoreBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create two Proposer blocks at the same height - innerBlk := &snowman.TestBlock{ - BytesV: []byte{1}, - ParentV: gencoreBlk.ID(), - HeightV: gencoreBlk.Height() + 1, - } + innerBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { require.Equal(innerBlk.Bytes(), b) return innerBlk, nil @@ -672,17 +582,13 @@ func TestTwoProBlocksWithSameParentCanBothVerify(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // one block is built from this proVM - localcoreBlk := &snowman.TestBlock{ - BytesV: []byte{111}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + localcoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return localcoreBlk, nil } @@ -692,15 +598,11 @@ func TestTwoProBlocksWithSameParentCanBothVerify(t *testing.T) { require.NoError(builtBlk.Verify(context.Background())) // another block with same parent comes from network and is parsed - netcoreBlk := &snowman.TestBlock{ - BytesV: []byte{222}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + netcoreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, localcoreBlk.Bytes()): return localcoreBlk, nil case bytes.Equal(b, netcoreBlk.Bytes()): @@ -742,7 +644,7 @@ func TestPreFork_Initialize(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - _, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + _, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -755,7 +657,7 @@ func TestPreFork_Initialize(t *testing.T) { require.NoError(err) require.IsType(&preForkBlock{}, rtvdBlk) - require.Equal(coreGenBlk.Bytes(), rtvdBlk.Bytes()) + require.Equal(snowmantest.GenesisBytes, rtvdBlk.Bytes()) } func TestPreFork_BuildBlock(t *testing.T) { @@ -765,20 +667,12 @@ func TestPreFork_BuildBlock(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk, nil } @@ -806,18 +700,12 @@ func TestPreFork_ParseBlock(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, _, proVM, _, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(2021), - }, - BytesV: []byte{1}, - } - + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { require.Equal(coreBlk.Bytes(), b) return coreBlk, nil @@ -845,21 +733,12 @@ func TestPreFork_SetPreference(t *testing.T) { activationTime = mockable.MaxTime durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(333), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: coreGenBlk.Timestamp(), - } + coreBlk0 := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk0, nil } @@ -868,8 +747,8 @@ func TestPreFork_SetPreference(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk0.ID(): return coreBlk0, nil default: @@ -878,8 +757,8 @@ func TestPreFork_SetPreference(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk0.Bytes()): return coreBlk0, nil default: @@ -888,16 +767,7 @@ func TestPreFork_SetPreference(t *testing.T) { } require.NoError(proVM.SetPreference(context.Background(), builtBlk.ID())) - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.Empty.Prefix(444), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: coreBlk0.ID(), - HeightV: coreBlk0.Height() + 1, - TimestampV: coreBlk0.Timestamp(), - } + coreBlk1 := snowmantest.BuildChild(coreBlk0) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return coreBlk1, nil } @@ -909,34 +779,24 @@ func TestPreFork_SetPreference(t *testing.T) { func TestExpiredBuildBlock(t *testing.T) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: []byte{0}, - } - coreVM := &block.TestVM{} coreVM.T = t coreVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -959,7 +819,7 @@ func TestExpiredBuildBlock(t *testing.T) { T: t, } valState.GetMinimumHeightF = func(context.Context) (uint64, error) { - return coreGenBlk.Height(), nil + return snowmantest.GenesisHeight, nil } valState.GetCurrentHeightF = func(context.Context) (uint64, error) { return defaultPChainHeight, nil @@ -1016,7 +876,7 @@ func TestExpiredBuildBlock(t *testing.T) { coreVM.InitializeF = nil require.NoError(proVM.SetState(context.Background(), snow.NormalOp)) - require.NoError(proVM.SetPreference(context.Background(), coreGenBlk.IDV)) + require.NoError(proVM.SetPreference(context.Background(), snowmantest.GenesisID)) // Notify the proposer VM of a new block on the inner block side toScheduler <- common.PendingTxs @@ -1025,17 +885,9 @@ func TestExpiredBuildBlock(t *testing.T) { // Before calling BuildBlock, verify a remote block and set it as the // preferred block. - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) statelessBlock, err := statelessblock.BuildUnsigned( - coreGenBlk.ID(), + snowmantest.GenesisID, proVM.Time(), 0, coreBlk.Bytes(), @@ -1044,8 +896,8 @@ func TestExpiredBuildBlock(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -1054,8 +906,8 @@ func TestExpiredBuildBlock(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil default: @@ -1118,21 +970,12 @@ func TestInnerBlockDeduplication(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: coreGenBlk.Timestamp(), - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) coreBlk0 := &wrappedBlock{ Block: coreBlk, } @@ -1140,14 +983,14 @@ func TestInnerBlockDeduplication(t *testing.T) { Block: coreBlk, } statelessBlock0, err := statelessblock.BuildUnsigned( - coreGenBlk.ID(), + snowmantest.GenesisID, coreBlk.Timestamp(), 0, coreBlk.Bytes(), ) require.NoError(err) statelessBlock1, err := statelessblock.BuildUnsigned( - coreGenBlk.ID(), + snowmantest.GenesisID, coreBlk.Timestamp(), 1, coreBlk.Bytes(), @@ -1156,8 +999,8 @@ func TestInnerBlockDeduplication(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk0.ID(): return coreBlk0, nil default: @@ -1166,8 +1009,8 @@ func TestInnerBlockDeduplication(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk0.Bytes()): return coreBlk0, nil default: @@ -1184,8 +1027,8 @@ func TestInnerBlockDeduplication(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk1.ID(): return coreBlk1, nil default: @@ -1194,8 +1037,8 @@ func TestInnerBlockDeduplication(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk1.Bytes()): return coreBlk1, nil default: @@ -1216,16 +1059,6 @@ func TestInnerBlockDeduplication(t *testing.T) { func TestInnerVMRollback(t *testing.T) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: []byte{0}, - } - valState := &validators.TestState{ T: t, } @@ -1246,20 +1079,20 @@ func TestInnerVMRollback(t *testing.T) { coreVM.T = t coreVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -1311,20 +1144,11 @@ func TestInnerVMRollback(t *testing.T) { )) require.NoError(proVM.SetState(context.Background(), snow.NormalOp)) - require.NoError(proVM.SetPreference(context.Background(), coreGenBlk.IDV)) + require.NoError(proVM.SetPreference(context.Background(), snowmantest.GenesisID)) - coreBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: coreGenBlk.Timestamp(), - } + coreBlk := snowmantest.BuildChild(snowmantest.Genesis) statelessBlock, err := statelessblock.BuildUnsigned( - coreGenBlk.ID(), + snowmantest.GenesisID, coreBlk.Timestamp(), 0, coreBlk.Bytes(), @@ -1333,8 +1157,8 @@ func TestInnerVMRollback(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk.ID(): return coreBlk, nil default: @@ -1343,8 +1167,8 @@ func TestInnerVMRollback(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk.Bytes()): return coreBlk, nil default: @@ -1403,7 +1227,7 @@ func TestInnerVMRollback(t *testing.T) { lastAcceptedID, err := proVM.LastAccepted(context.Background()) require.NoError(err) - require.Equal(coreGenBlk.IDV, lastAcceptedID) + require.Equal(snowmantest.GenesisID, lastAcceptedID) parsedBlock, err = proVM.ParseBlock(context.Background(), statelessBlock.Bytes()) require.NoError(err) @@ -1418,7 +1242,7 @@ func TestBuildBlockDuringWindow(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, valState, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, valState, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() @@ -1432,26 +1256,10 @@ func TestBuildBlockDuringWindow(t *testing.T) { }, nil } - coreBlk0 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - } - coreBlk1 := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreBlk0.ID(), - HeightV: coreBlk0.Height() + 1, - } + coreBlk0 := snowmantest.BuildChild(snowmantest.Genesis) + coreBlk1 := snowmantest.BuildChild(coreBlk0) statelessBlock0, err := statelessblock.BuildUnsigned( - coreGenBlk.ID(), + snowmantest.GenesisID, proVM.Time(), 0, coreBlk0.Bytes(), @@ -1460,8 +1268,8 @@ func TestBuildBlockDuringWindow(t *testing.T) { coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { switch blkID { - case coreGenBlk.ID(): - return coreGenBlk, nil + case snowmantest.GenesisID: + return snowmantest.Genesis, nil case coreBlk0.ID(): return coreBlk0, nil case coreBlk1.ID(): @@ -1472,8 +1280,8 @@ func TestBuildBlockDuringWindow(t *testing.T) { } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil case bytes.Equal(b, coreBlk0.Bytes()): return coreBlk0, nil case bytes.Equal(b, coreBlk1.Bytes()): @@ -1523,21 +1331,13 @@ func TestTwoForks_OneIsAccepted(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, _, proVM, gBlock, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create pre-fork block X and post-fork block A - xBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - } + xBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return xBlock, nil @@ -1548,18 +1348,10 @@ func TestTwoForks_OneIsAccepted(t *testing.T) { require.NoError(aBlock.Verify(context.Background())) // use a different way to construct pre-fork block Y and post-fork block B - yBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - } + yBlock := snowmantest.BuildChild(snowmantest.Genesis) ySlb, err := statelessblock.BuildUnsigned( - gBlock.ID(), + snowmantest.GenesisID, proVM.Time(), defaultPChainHeight, yBlock.Bytes(), @@ -1578,15 +1370,7 @@ func TestTwoForks_OneIsAccepted(t *testing.T) { require.NoError(bBlock.Verify(context.Background())) // append Z/C to Y/B - zBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: yBlock.ID(), - HeightV: yBlock.Height() + 1, - } + zBlock := snowmantest.BuildChild(yBlock) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return zBlock, nil @@ -1620,32 +1404,13 @@ func TestTooFarAdvanced(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, _, proVM, gBlock, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - xBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - TimestampV: gBlock.Timestamp(), - } - - yBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: xBlock.ID(), - HeightV: xBlock.Height() + 1, - TimestampV: xBlock.Timestamp(), - } + xBlock := snowmantest.BuildChild(snowmantest.Genesis) + yBlock := snowmantest.BuildChild(xBlock) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return xBlock, nil @@ -1714,41 +1479,17 @@ func TestTwoOptions_OneIsAccepted(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = mockable.MaxTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - xBlockID := ids.GenerateTestID() + xTestBlock := snowmantest.BuildChild(snowmantest.Genesis) xBlock := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: xBlockID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: coreGenBlk.Timestamp(), - }, + Block: *xTestBlock, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: xBlockID, - TimestampV: coreGenBlk.Timestamp(), - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: xBlockID, - TimestampV: coreGenBlk.Timestamp(), - }, + snowmantest.BuildChild(xTestBlock), + snowmantest.BuildChild(xTestBlock), }, } @@ -1792,21 +1533,12 @@ func TestLaggedPChainHeight(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, coreGenBlk, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() - innerBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: coreGenBlk.Timestamp(), - } - + innerBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return innerBlock, nil } @@ -1817,7 +1549,7 @@ func TestLaggedPChainHeight(t *testing.T) { block := blockIntf.(*postForkBlock) pChainHeight := block.PChainHeight() - require.Equal(pChainHeight, coreGenBlk.Height()) + require.Equal(snowmantest.GenesisHeight, pChainHeight) } // Ensure that rejecting a block does not modify the accepted block ID for the @@ -1825,17 +1557,7 @@ func TestLaggedPChainHeight(t *testing.T) { func TestRejectedHeightNotIndexed(t *testing.T) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: []byte{0}, - } - - coreHeights := []ids.ID{coreGenBlk.ID()} + coreHeights := []ids.ID{snowmantest.GenesisID} initialState := []byte("genesis state") coreVM := &block.TestVM{ @@ -1857,20 +1579,20 @@ func TestRejectedHeightNotIndexed(t *testing.T) { return nil } coreVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreGenBlk.ID(): - return coreGenBlk, nil + switch blkID { + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -1893,7 +1615,7 @@ func TestRejectedHeightNotIndexed(t *testing.T) { T: t, } valState.GetMinimumHeightF = func(context.Context) (uint64, error) { - return coreGenBlk.HeightV, nil + return snowmantest.GenesisHeight, nil } valState.GetCurrentHeightF = func(context.Context) (uint64, error) { return defaultPChainHeight, nil @@ -1949,19 +1671,10 @@ func TestRejectedHeightNotIndexed(t *testing.T) { require.NoError(proVM.SetState(context.Background(), snow.NormalOp)) - require.NoError(proVM.SetPreference(context.Background(), coreGenBlk.IDV)) + require.NoError(proVM.SetPreference(context.Background(), snowmantest.GenesisID)) // create inner block X and outer block A - xBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: coreGenBlk.Timestamp(), - } + xBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return xBlock, nil @@ -1973,20 +1686,11 @@ func TestRejectedHeightNotIndexed(t *testing.T) { require.NoError(aBlock.Verify(context.Background())) // use a different way to construct inner block Y and outer block B - yBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: coreGenBlk.ID(), - HeightV: coreGenBlk.Height() + 1, - TimestampV: coreGenBlk.Timestamp(), - } + yBlock := snowmantest.BuildChild(snowmantest.Genesis) ySlb, err := statelessblock.BuildUnsigned( - coreGenBlk.ID(), - coreGenBlk.Timestamp(), + snowmantest.GenesisID, + snowmantest.GenesisTimestamp, defaultPChainHeight, yBlock.Bytes(), ) @@ -2024,17 +1728,7 @@ func TestRejectedHeightNotIndexed(t *testing.T) { func TestRejectedOptionHeightNotIndexed(t *testing.T) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: []byte{0}, - } - - coreHeights := []ids.ID{coreGenBlk.ID()} + coreHeights := []ids.ID{snowmantest.GenesisID} initialState := []byte("genesis state") coreVM := &block.TestVM{ @@ -2056,20 +1750,20 @@ func TestRejectedOptionHeightNotIndexed(t *testing.T) { return nil } coreVM.LastAcceptedF = func(context.Context) (ids.ID, error) { - return coreGenBlk.ID(), nil + return snowmantest.GenesisID, nil } coreVM.GetBlockF = func(_ context.Context, blkID ids.ID) (snowman.Block, error) { - switch { - case blkID == coreGenBlk.ID(): - return coreGenBlk, nil + switch blkID { + case snowmantest.GenesisID: + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } } coreVM.ParseBlockF = func(_ context.Context, b []byte) (snowman.Block, error) { switch { - case bytes.Equal(b, coreGenBlk.Bytes()): - return coreGenBlk, nil + case bytes.Equal(b, snowmantest.GenesisBytes): + return snowmantest.Genesis, nil default: return nil, errUnknownBlock } @@ -2092,7 +1786,7 @@ func TestRejectedOptionHeightNotIndexed(t *testing.T) { T: t, } valState.GetMinimumHeightF = func(context.Context) (uint64, error) { - return coreGenBlk.HeightV, nil + return snowmantest.GenesisHeight, nil } valState.GetCurrentHeightF = func(context.Context) (uint64, error) { return defaultPChainHeight, nil @@ -2148,38 +1842,14 @@ func TestRejectedOptionHeightNotIndexed(t *testing.T) { require.NoError(proVM.SetState(context.Background(), snow.NormalOp)) - require.NoError(proVM.SetPreference(context.Background(), coreGenBlk.IDV)) + require.NoError(proVM.SetPreference(context.Background(), snowmantest.GenesisID)) - xBlockID := ids.GenerateTestID() + xTestBlock := snowmantest.BuildChild(snowmantest.Genesis) xBlock := &TestOptionsBlock{ - TestBlock: snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: xBlockID, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: coreGenBlk.ID(), - TimestampV: coreGenBlk.Timestamp(), - }, + Block: *xTestBlock, opts: [2]snowman.Block{ - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{2}, - ParentV: xBlockID, - TimestampV: coreGenBlk.Timestamp(), - }, - &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{3}, - ParentV: xBlockID, - TimestampV: coreGenBlk.Timestamp(), - }, + snowmantest.BuildChild(xTestBlock), + snowmantest.BuildChild(xTestBlock), }, } @@ -2260,7 +1930,7 @@ func TestVMInnerBlkCache(t *testing.T) { innerVM.EXPECT().Shutdown(gomock.Any()).Return(nil) { - innerBlk := snowman.NewMockBlock(ctrl) + innerBlk := snowmantest.NewMockBlock(ctrl) innerBlkID := ids.GenerateTestID() innerVM.EXPECT().LastAccepted(gomock.Any()).Return(innerBlkID, nil) innerVM.EXPECT().GetBlock(gomock.Any(), innerBlkID).Return(innerBlk, nil) @@ -2304,7 +1974,7 @@ func TestVMInnerBlkCache(t *testing.T) { // Not in the VM's state so need to parse it. state.EXPECT().GetBlock(blkNearTip.ID()).Return(blkNearTip, choices.Accepted, nil).Times(2) // We will ask the inner VM to parse. - mockInnerBlkNearTip := snowman.NewMockBlock(ctrl) + mockInnerBlkNearTip := snowmantest.NewMockBlock(ctrl) mockInnerBlkNearTip.EXPECT().Height().Return(uint64(1)).Times(2) mockInnerBlkNearTip.EXPECT().Bytes().Return(blkNearTipInnerBytes).Times(1) @@ -2340,22 +2010,13 @@ func TestVMInnerBlkCacheDeduplicationRegression(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, gBlock, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create pre-fork block X and post-fork block A - xBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - TimestampV: gBlock.Timestamp(), - } + xBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return xBlock, nil @@ -2365,25 +2026,16 @@ func TestVMInnerBlkCacheDeduplicationRegression(t *testing.T) { coreVM.BuildBlockF = nil bStatelessBlock, err := statelessblock.BuildUnsigned( - gBlock.ID(), - gBlock.Timestamp(), + snowmantest.GenesisID, + snowmantest.GenesisTimestamp, defaultPChainHeight, xBlock.Bytes(), ) require.NoError(err) - xBlockCopy := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: xBlock.IDV, - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - TimestampV: gBlock.Timestamp(), - } + xBlockCopy := *xBlock coreVM.ParseBlockF = func(context.Context, []byte) (snowman.Block, error) { - return xBlockCopy, nil + return &xBlockCopy, nil } bBlockBytes := bStatelessBlock.Bytes() @@ -2419,22 +2071,13 @@ func TestVMInnerBlkMarkedAcceptedRegression(t *testing.T) { activationTime = time.Unix(0, 0) durangoTime = activationTime ) - coreVM, _, proVM, gBlock, _ := initTestProposerVM(t, activationTime, durangoTime, 0) + coreVM, _, proVM, _ := initTestProposerVM(t, activationTime, durangoTime, 0) defer func() { require.NoError(proVM.Shutdown(context.Background())) }() // create an inner block and wrap it in an postForkBlock. - innerBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - BytesV: []byte{1}, - ParentV: gBlock.ID(), - HeightV: gBlock.Height() + 1, - TimestampV: gBlock.Timestamp(), - } + innerBlock := snowmantest.BuildChild(snowmantest.Genesis) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return innerBlock, nil @@ -2457,7 +2100,7 @@ func TestVMInnerBlkMarkedAcceptedRegression(t *testing.T) { } type blockWithVerifyContext struct { - *snowman.MockBlock + *snowmantest.MockBlock *block.MockWithVerifyContext } @@ -2500,7 +2143,7 @@ func TestVM_VerifyBlockWithContext(t *testing.T) { innerVM.EXPECT().Shutdown(gomock.Any()).Return(nil) { - innerBlk := snowman.NewMockBlock(ctrl) + innerBlk := snowmantest.NewMockBlock(ctrl) innerBlkID := ids.GenerateTestID() innerVM.EXPECT().LastAccepted(gomock.Any()).Return(innerBlkID, nil) innerVM.EXPECT().GetBlock(gomock.Any(), innerBlkID).Return(innerBlk, nil) @@ -2527,7 +2170,7 @@ func TestVM_VerifyBlockWithContext(t *testing.T) { { pChainHeight := uint64(0) innerBlk := blockWithVerifyContext{ - MockBlock: snowman.NewMockBlock(ctrl), + MockBlock: snowmantest.NewMockBlock(ctrl), MockWithVerifyContext: block.NewMockWithVerifyContext(ctrl), } innerBlk.MockWithVerifyContext.EXPECT().ShouldVerifyWithContext(gomock.Any()).Return(true, nil).Times(2) @@ -2575,7 +2218,7 @@ func TestVM_VerifyBlockWithContext(t *testing.T) { // Ensure we call Verify on a block that returns // false for ShouldVerifyWithContext innerBlk := blockWithVerifyContext{ - MockBlock: snowman.NewMockBlock(ctrl), + MockBlock: snowmantest.NewMockBlock(ctrl), MockWithVerifyContext: block.NewMockWithVerifyContext(ctrl), } innerBlk.MockWithVerifyContext.EXPECT().ShouldVerifyWithContext(gomock.Any()).Return(false, nil) @@ -2598,7 +2241,7 @@ func TestVM_VerifyBlockWithContext(t *testing.T) { { // Ensure we call Verify on a block that doesn't have a valid context innerBlk := blockWithVerifyContext{ - MockBlock: snowman.NewMockBlock(ctrl), + MockBlock: snowmantest.NewMockBlock(ctrl), MockWithVerifyContext: block.NewMockWithVerifyContext(ctrl), } innerBlk.MockBlock.EXPECT().Verify(gomock.Any()).Return(nil) @@ -2615,16 +2258,7 @@ func TestVM_VerifyBlockWithContext(t *testing.T) { func TestHistoricalBlockDeletion(t *testing.T) { require := require.New(t) - coreGenBlk := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Accepted, - }, - HeightV: 0, - TimestampV: genesisTimestamp, - BytesV: utils.RandomBytes(1024), - } - acceptedBlocks := []snowman.Block{coreGenBlk} + acceptedBlocks := []*snowmantest.Block{snowmantest.Genesis} currentHeight := uint64(0) initialState := []byte("genesis state") @@ -2667,7 +2301,7 @@ func TestHistoricalBlockDeletion(t *testing.T) { ctx.ValidatorState = &validators.TestState{ T: t, GetMinimumHeightF: func(context.Context) (uint64, error) { - return coreGenBlk.HeightV, nil + return snowmantest.GenesisHeight, nil }, GetCurrentHeightF: func(context.Context) (uint64, error) { return defaultPChainHeight, nil @@ -2713,16 +2347,7 @@ func TestHistoricalBlockDeletion(t *testing.T) { issueBlock := func() { lastAcceptedBlock := acceptedBlocks[currentHeight] - innerBlock := &snowman.TestBlock{ - TestDecidable: choices.TestDecidable{ - IDV: ids.GenerateTestID(), - StatusV: choices.Processing, - }, - ParentV: lastAcceptedBlock.ID(), - HeightV: lastAcceptedBlock.Height() + 1, - TimestampV: lastAcceptedBlock.Timestamp(), - BytesV: utils.RandomBytes(1024), - } + innerBlock := snowmantest.BuildChild(lastAcceptedBlock) coreVM.BuildBlockF = func(context.Context) (snowman.Block, error) { return innerBlock, nil diff --git a/vms/rpcchainvm/batched_vm_test.go b/vms/rpcchainvm/batched_vm_test.go index 8039c9fb7ba3..b24c0699e597 100644 --- a/vms/rpcchainvm/batched_vm_test.go +++ b/vms/rpcchainvm/batched_vm_test.go @@ -14,7 +14,7 @@ import ( "github.com/ava-labs/avalanchego/database/memdb" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/choices" - "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/vms/components/chain" @@ -43,8 +43,8 @@ func batchedParseBlockCachingTestPlugin(t *testing.T, loadExpectations bool) blo vm := block.NewMockChainVM(ctrl) if loadExpectations { - blk1 := snowman.NewMockBlock(ctrl) - blk2 := snowman.NewMockBlock(ctrl) + blk1 := snowmantest.NewMockBlock(ctrl) + blk2 := snowmantest.NewMockBlock(ctrl) gomock.InOrder( // Initialize vm.EXPECT().Initialize( diff --git a/vms/rpcchainvm/state_syncable_vm_test.go b/vms/rpcchainvm/state_syncable_vm_test.go index 3b71aaa8b4c7..504f628fc90a 100644 --- a/vms/rpcchainvm/state_syncable_vm_test.go +++ b/vms/rpcchainvm/state_syncable_vm_test.go @@ -17,7 +17,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/snow/choices" - "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/snowtest" "github.com/ava-labs/avalanchego/utils/logging" @@ -41,7 +41,7 @@ var ( } // last accepted blocks data before and after summary is accepted - preSummaryBlk = &snowman.TestBlock{ + preSummaryBlk = &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: ids.ID{'f', 'i', 'r', 's', 't', 'B', 'l', 'K'}, StatusV: choices.Accepted, @@ -50,7 +50,7 @@ var ( ParentV: ids.ID{'p', 'a', 'r', 'e', 'n', 't', 'B', 'l', 'k'}, } - summaryBlk = &snowman.TestBlock{ + summaryBlk = &snowmantest.Block{ TestDecidable: choices.TestDecidable{ IDV: ids.ID{'s', 'u', 'm', 'm', 'a', 'r', 'y', 'B', 'l', 'K'}, StatusV: choices.Accepted, diff --git a/vms/rpcchainvm/with_context_vm_test.go b/vms/rpcchainvm/with_context_vm_test.go index f9216f5a5b2e..f87a6cba5f0b 100644 --- a/vms/rpcchainvm/with_context_vm_test.go +++ b/vms/rpcchainvm/with_context_vm_test.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/avalanchego/database/memdb" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/consensus/snowman" + "github.com/ava-labs/avalanchego/snow/consensus/snowman/snowmantest" "github.com/ava-labs/avalanchego/snow/engine/snowman/block" "github.com/ava-labs/avalanchego/snow/snowtest" ) @@ -40,7 +41,7 @@ type ContextEnabledVMMock struct { } type ContextEnabledBlockMock struct { - *snowman.MockBlock + *snowmantest.MockBlock *block.MockWithVerifyContext } @@ -56,7 +57,7 @@ func contextEnabledTestPlugin(t *testing.T, loadExpectations bool) block.ChainVM if loadExpectations { ctxBlock := ContextEnabledBlockMock{ - MockBlock: snowman.NewMockBlock(ctrl), + MockBlock: snowmantest.NewMockBlock(ctrl), MockWithVerifyContext: block.NewMockWithVerifyContext(ctrl), } gomock.InOrder(