From d5dcb212f780848631ed8e5c5d59ace1c1056848 Mon Sep 17 00:00:00 2001 From: gadcl Date: Tue, 13 Oct 2020 11:18:54 +0300 Subject: [PATCH] tests cleanup --- .../state_processing_blocks_test.go | 88 ++++++------------- 1 file changed, 29 insertions(+), 59 deletions(-) diff --git a/services/blockstorage/internodesync/state_processing_blocks_test.go b/services/blockstorage/internodesync/state_processing_blocks_test.go index cc307914a..8060dcf44 100644 --- a/services/blockstorage/internodesync/state_processing_blocks_test.go +++ b/services/blockstorage/internodesync/state_processing_blocks_test.go @@ -14,7 +14,7 @@ import ( "github.com/orbs-network/orbs-spec/types/go/primitives" "github.com/orbs-network/orbs-spec/types/go/protocol" "github.com/orbs-network/orbs-spec/types/go/protocol/gossipmessages" - "github.com/orbs-network/orbs-spec/types/go/services" + "github.com/pkg/errors" "github.com/stretchr/testify/require" "testing" "time" @@ -180,6 +180,7 @@ func TestStateProcessingBlocksDescending_ValidateBlockFailureReturnsToCollecting Build().Message expectedFailedBlockHeight := primitives.BlockHeight(1) + h.storage.When("ValidateChainTip", mock.Any, mock.Any).Return(nil, nil).Times(1) h.expectBlockValidationQueriesFromStorageAndFailLastValidation(11, expectedFailedBlockHeight) h.expectBlockCommitsToStorage(10) @@ -192,6 +193,31 @@ func TestStateProcessingBlocksDescending_ValidateBlockFailureReturnsToCollecting }) } +func TestStateProcessingBlocksDescending_ValidateChainTipFailureReturnsToCollectingAvailabilityResponses(t *testing.T) { + with.Context(func(ctx context.Context) { + with.Logging(t, func(harness *with.LoggingHarness) { + h := newBlockSyncHarness(harness.Logger). + withDescendingEnabled(true) + harness.AllowErrorsMatching("failed to validate block received via sync") + + message := builders.BlockSyncResponseInput(). + WithBlocksOrder(gossipmessages.SYNC_BLOCKS_ORDER_DESCENDING). + WithFirstBlockHeight(11). + WithLastBlockHeight(1). + WithLastCommittedBlockHeight(11). + Build().Message + + h.storage.When("GetSyncState").Return(nil).Times(1) + h.storage.When("ValidateChainTip", mock.Any, mock.Any).Return(nil, errors.New(" failed to validate the chain tip")).Times(1) + state := h.factory.CreateProcessingBlocksState(message) + nextState := state.processState(ctx) + + require.IsType(t, &collectingAvailabilityResponsesState{}, nextState, "next state after validation error should be collecting availability responses") + h.verifyMocks(t) + }) + }) +} + func TestStateProcessingBlocksDescending_ValidateBlockChunkRangeFailureReturnsToCollectingAvailabilityResponses(t *testing.T) { with.Context(func(ctx context.Context) { with.Logging(t, func(harness *with.LoggingHarness) { @@ -243,64 +269,6 @@ func TestStateProcessingBlocksDescending_ValidateBlockChunkRangeFailureReturnsTo }) } -func TestStateProcessingBlocksDescending_ValidatePosChainRefTimeFailure(t *testing.T) { - with.Context(func(ctx context.Context) { - with.Logging(t, func(harness *with.LoggingHarness) { - h := newBlockSyncHarness(harness.Logger). - withDescendingEnabled(true) - harness.AllowErrorsMatching("failed to verify the blocks chunk PoS received via sync") - - blockPair := builders.BlockPair(). - WithHeight(primitives.BlockHeight(10)). - Build() - syncState := SyncState{InOrderBlock: blockPair, TopBlock: blockPair, LastSyncedBlock: blockPair} - h.storage.When("GetSyncState").Return(syncState).Times(1) - h.storage.When("GetBlock", mock.Any).Return(nil) - - h.storage.When("ValidateBlockForCommit", mock.Any, mock.Any).Call(func(ctx context.Context, input *services.ValidateBlockForCommitInput) (*services.ValidateBlockForCommitOutput, error) { - if input.BlockPair.TransactionsBlock.Header.BlockHeight() == 11 && input.PrevBlockPair != nil { - t.Fatalf("block chunk is out of PoS honesty assumption (12hr) - should use current committee for validation using prevblock nil") - } - return &services.ValidateBlockForCommitOutput{}, nil - }).Times(11) - h.storage.When("NodeSyncCommitBlock", mock.Any, mock.Any).Return(nil, nil).Times(10) - - var blocks []*protocol.BlockPairContainer - var prevBlock *protocol.BlockPairContainer - currentTime := time.Now() - committeeGracePeriod := 12 * time.Hour - flakinessBuffer := 10 * time.Second - // block chunk does pertain to PoS honesty assumption of 12hr committee (even though in tests it set to 100 seconds) - refTime := primitives.TimestampSeconds(currentTime.Add(-committeeGracePeriod).Add(-flakinessBuffer).Unix()) - for i := 11; i <= 20; i++ { - blockTime := time.Unix(currentTime.Unix()+int64(i), 0) // deterministic block creation - blockPair := builders.BlockPair(). - WithHeight(primitives.BlockHeight(i)). - WithBlockCreated(blockTime). - WithReferenceTime(refTime). - WithPrevBlock(prevBlock). - Build() - prevBlock = blockPair - blocks = append(blocks, blockPair) - } - reverse(blocks) - - message := builders.BlockSyncResponseInput(). - WithBlocksOrder(gossipmessages.SYNC_BLOCKS_ORDER_DESCENDING). - WithBlocks(blocks). - WithFirstBlockHeight(20). - WithLastBlockHeight(11). - WithLastCommittedBlockHeight(20). - Build().Message - - state := h.factory.CreateProcessingBlocksState(message) - nextState := state.processState(ctx) - require.IsType(t, &collectingAvailabilityResponsesState{}, nextState, "next state after validation error should be collecting availability responses") - h.verifyMocks(t) - }) - }) -} - func TestStateProcessingBlocksDescending_ValidatePosChainPrevHashFailure(t *testing.T) { with.Context(func(ctx context.Context) { with.Logging(t, func(harness *with.LoggingHarness) { @@ -399,6 +367,7 @@ func TestStateProcessingBlocksDescending_CommitBlockFailureReturnsToCollectingAv WithLastCommittedBlockHeight(11). Build().Message + h.storage.When("ValidateChainTip", mock.Any, mock.Any).Return(nil, nil).Times(1) h.expectBlockValidationQueriesFromStorage(11) h.expectBlockCommitsToStorageAndFailLastCommit(11, message.SignedChunkRange.FirstBlockHeight()) @@ -460,6 +429,7 @@ func TestStateProcessingBlocksDescending_TerminatesOnContextTermination(t *testi h.storage.When("GetSyncState").Return(syncState).Times(1) h.storage.When("GetBlock", mock.Any).Return(nil) h.storage.When("UpdateConsensusAlgosAboutLastCommittedBlockInLocalPersistence", mock.Any) + h.storage.When("ValidateChainTip", mock.Any, mock.Any).Return(nil, nil).Times(1) cancel() state := h.factory.CreateProcessingBlocksState(message)