github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/protocol_state.go (about)

     1  package unittest
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/mock"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/state/protocol"
    12  	mockprotocol "github.com/koko1123/flow-go-1/state/protocol/mock"
    13  )
    14  
    15  // FinalizedProtocolStateWithParticipants returns a protocol state with finalized participants
    16  func FinalizedProtocolStateWithParticipants(participants flow.IdentityList) (
    17  	*flow.Block, *mockprotocol.Snapshot, *mockprotocol.State, *mockprotocol.Snapshot) {
    18  	sealed := BlockFixture()
    19  	block := BlockWithParentFixture(sealed.Header)
    20  	head := block.Header
    21  
    22  	// set up protocol snapshot mock
    23  	snapshot := &mockprotocol.Snapshot{}
    24  	snapshot.On("Identities", mock.Anything).Return(
    25  		func(filter flow.IdentityFilter) flow.IdentityList {
    26  			return participants.Filter(filter)
    27  		},
    28  		nil,
    29  	)
    30  	snapshot.On("Identity", mock.Anything).Return(func(id flow.Identifier) *flow.Identity {
    31  		for _, n := range participants {
    32  			if n.ID() == id {
    33  				return n
    34  			}
    35  		}
    36  		return nil
    37  	}, nil)
    38  	snapshot.On("Head").Return(
    39  		func() *flow.Header {
    40  			return head
    41  		},
    42  		nil,
    43  	)
    44  
    45  	sealedSnapshot := &mockprotocol.Snapshot{}
    46  	sealedSnapshot.On("Head").Return(
    47  		func() *flow.Header {
    48  			return sealed.Header
    49  		},
    50  		nil,
    51  	)
    52  
    53  	// set up protocol state mock
    54  	state := &mockprotocol.State{}
    55  	state.On("Final").Return(
    56  		func() protocol.Snapshot {
    57  			return snapshot
    58  		},
    59  	)
    60  	state.On("Sealed").Return(func() protocol.Snapshot {
    61  		return sealedSnapshot
    62  	},
    63  	)
    64  	state.On("AtBlockID", mock.Anything).Return(
    65  		func(blockID flow.Identifier) protocol.Snapshot {
    66  			return snapshot
    67  		},
    68  	)
    69  	return block, snapshot, state, sealedSnapshot
    70  }
    71  
    72  // SealBlock seals a block by building two blocks on it, the first containing
    73  // a receipt for the block, the second containing a seal for the block.
    74  // Returns the block containing the seal.
    75  func SealBlock(t *testing.T, st protocol.MutableState, block *flow.Block, receipt *flow.ExecutionReceipt, seal *flow.Seal) *flow.Header {
    76  
    77  	block2 := BlockWithParentFixture(block.Header)
    78  	block2.SetPayload(flow.Payload{
    79  		Receipts: []*flow.ExecutionReceiptMeta{receipt.Meta()},
    80  		Results:  []*flow.ExecutionResult{&receipt.ExecutionResult},
    81  	})
    82  	err := st.Extend(context.Background(), block2)
    83  	require.NoError(t, err)
    84  
    85  	block3 := BlockWithParentFixture(block2.Header)
    86  	block3.SetPayload(flow.Payload{
    87  		Seals: []*flow.Seal{seal},
    88  	})
    89  	err = st.Extend(context.Background(), block3)
    90  	require.NoError(t, err)
    91  
    92  	return block3.Header
    93  }