github.com/onflow/flow-go@v0.33.17/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  
    24  	require.NoError(t, err)
    25  
    26  	A := BlockWithParentFixture(final)
    27  	A.SetPayload(flow.Payload{})
    28  	addToState(t, state, A, true)
    29  
    30  	receiptA := ReceiptForBlockFixture(A)
    31  	receiptA.ExecutionResult.ServiceEvents = []flow.ServiceEvent{beacon.ServiceEvent()}
    32  
    33  	B := BlockWithParentFixture(A.Header)
    34  	B.SetPayload(flow.Payload{
    35  		Receipts: []*flow.ExecutionReceiptMeta{receiptA.Meta()},
    36  		Results:  []*flow.ExecutionResult{&receiptA.ExecutionResult},
    37  	})
    38  	addToState(t, state, B, true)
    39  
    40  	sealsForB := []*flow.Seal{
    41  		Seal.Fixture(Seal.WithResult(&receiptA.ExecutionResult)),
    42  	}
    43  
    44  	C := BlockWithParentFixture(B.Header)
    45  	C.SetPayload(flow.Payload{
    46  		Seals: sealsForB,
    47  	})
    48  	addToState(t, state, C, true)
    49  }
    50  
    51  func addToState(t *testing.T, state protocol.FollowerState, block *flow.Block, finalize bool) {
    52  
    53  	err := state.ExtendCertified(context.Background(), block, CertifyBlock(block.Header))
    54  	require.NoError(t, err)
    55  
    56  	if finalize {
    57  		err = state.Finalize(context.Background(), block.ID())
    58  		require.NoError(t, err)
    59  	}
    60  }