github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/utils/unittest/version_beacon.go (about)

     1  package unittest
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/model/flow"
    10  	"github.com/onflow/flow-go/state/protocol"
    11  )
    12  
    13  // AddVersionBeacon adds blocks sequence with given VersionBeacon so this
    14  // service events takes effect in Flow protocol.
    15  // This means execution result where event was emitted is sealed, and the seal is
    16  // finalized by a valid block.
    17  // This assumes state is bootstrapped with a root block, as it does NOT produce
    18  // results for final block of the state
    19  // Root <- A <- B(result(A(VB))) <- C(seal(B))
    20  func AddVersionBeacon(t *testing.T, beacon *flow.VersionBeacon, state protocol.FollowerState) {
    21  
    22  	final, err := state.Final().Head()
    23  	require.NoError(t, err)
    24  
    25  	protocolState, err := state.Final().ProtocolState()
    26  	require.NoError(t, err)
    27  	protocolStateID := protocolState.ID()
    28  
    29  	A := BlockWithParentFixture(final)
    30  	A.SetPayload(PayloadFixture(WithProtocolStateID(protocolStateID)))
    31  	addToState(t, state, A, true)
    32  
    33  	receiptA := ReceiptForBlockFixture(A)
    34  	receiptA.ExecutionResult.ServiceEvents = []flow.ServiceEvent{beacon.ServiceEvent()}
    35  
    36  	B := BlockWithParentFixture(A.Header)
    37  	B.SetPayload(flow.Payload{
    38  		Receipts:        []*flow.ExecutionReceiptMeta{receiptA.Meta()},
    39  		Results:         []*flow.ExecutionResult{&receiptA.ExecutionResult},
    40  		ProtocolStateID: protocolStateID,
    41  	})
    42  	addToState(t, state, B, true)
    43  
    44  	sealsForB := []*flow.Seal{
    45  		Seal.Fixture(Seal.WithResult(&receiptA.ExecutionResult)),
    46  	}
    47  
    48  	C := BlockWithParentFixture(B.Header)
    49  	C.SetPayload(flow.Payload{
    50  		Seals:           sealsForB,
    51  		ProtocolStateID: protocolStateID,
    52  	})
    53  	addToState(t, state, C, true)
    54  }
    55  
    56  func addToState(t *testing.T, state protocol.FollowerState, block *flow.Block, finalize bool) {
    57  
    58  	err := state.ExtendCertified(context.Background(), block, CertifyBlock(block.Header))
    59  	require.NoError(t, err)
    60  
    61  	if finalize {
    62  		err = state.Finalize(context.Background(), block.ID())
    63  		require.NoError(t, err)
    64  	}
    65  }