github.com/koko1123/flow-go-1@v0.29.6/consensus/recovery/recover_test.go (about)

     1  package recovery
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/mock"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/koko1123/flow-go-1/consensus/hotstuff/mocks"
    10  	"github.com/koko1123/flow-go-1/consensus/hotstuff/model"
    11  	"github.com/koko1123/flow-go-1/model/flow"
    12  	"github.com/koko1123/flow-go-1/utils/unittest"
    13  )
    14  
    15  func TestRecover(t *testing.T) {
    16  	finalized := unittest.BlockHeaderFixture()
    17  	blocks := unittest.ChainFixtureFrom(100, finalized)
    18  
    19  	pending := make([]*flow.Header, 0)
    20  	for _, b := range blocks {
    21  		pending = append(pending, b.Header)
    22  	}
    23  	recovered := make([]*model.Proposal, 0)
    24  	onProposal := func(block *model.Proposal) error {
    25  		recovered = append(recovered, block)
    26  		return nil
    27  	}
    28  
    29  	// make 3 invalid blocks extend from the last valid block
    30  	invalidblocks := unittest.ChainFixtureFrom(3, pending[len(pending)-1])
    31  	invalid := make(map[flow.Identifier]struct{})
    32  	for _, b := range invalidblocks {
    33  		invalid[b.ID()] = struct{}{}
    34  		pending = append(pending, b.Header)
    35  	}
    36  
    37  	validator := &mocks.Validator{}
    38  	validator.On("ValidateProposal", mock.Anything).Return(func(proposal *model.Proposal) error {
    39  		header := model.ProposalToFlow(proposal)
    40  		_, isInvalid := invalid[header.ID()]
    41  		if isInvalid {
    42  			return &model.InvalidBlockError{
    43  				BlockID: header.ID(),
    44  				View:    header.View,
    45  			}
    46  		}
    47  		return nil
    48  	})
    49  
    50  	err := Recover(unittest.Logger(), finalized, pending, validator, onProposal)
    51  	require.NoError(t, err)
    52  
    53  	// only pending blocks are valid
    54  	require.Len(t, recovered, len(pending))
    55  }