Skip to content

Commit

Permalink
tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gadcl committed Oct 13, 2020
1 parent dd6335b commit d5dcb21
Showing 1 changed file with 29 additions and 59 deletions.
88 changes: 29 additions & 59 deletions services/blockstorage/internodesync/state_processing_blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d5dcb21

Please sign in to comment.