github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/blocks_test.go (about)

     1  package convert_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/onflow/flow-go/engine/common/rpc/convert"
    11  	"github.com/onflow/flow-go/model/flow"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  // TestConvertBlock tests that converting a block to and from a protobuf message results in the same
    16  // block
    17  func TestConvertBlock(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	block := unittest.FullBlockFixture()
    21  	block.SetPayload(unittest.PayloadFixture(unittest.WithAllTheFixins))
    22  
    23  	signerIDs := unittest.IdentifierListFixture(5)
    24  
    25  	msg, err := convert.BlockToMessage(&block, signerIDs)
    26  	require.NoError(t, err)
    27  
    28  	converted, err := convert.MessageToBlock(msg)
    29  	require.NoError(t, err)
    30  
    31  	assert.Equal(t, block, *converted)
    32  }
    33  
    34  // TestConvertBlockLight tests that converting a block to its light form results in only the correct
    35  // fields being set
    36  func TestConvertBlockLight(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	block := unittest.FullBlockFixture()
    40  	block.SetPayload(unittest.PayloadFixture(unittest.WithAllTheFixins))
    41  
    42  	msg := convert.BlockToMessageLight(&block)
    43  
    44  	// required fields are set
    45  	blockID := block.ID()
    46  	assert.Equal(t, 0, bytes.Compare(blockID[:], msg.Id))
    47  	assert.Equal(t, block.Header.Height, msg.Height)
    48  	assert.Equal(t, 0, bytes.Compare(block.Header.ParentID[:], msg.ParentId))
    49  	assert.Equal(t, block.Header.Timestamp, msg.Timestamp.AsTime())
    50  	assert.Equal(t, 0, bytes.Compare(block.Header.ParentVoterSigData, msg.Signatures[0]))
    51  
    52  	guarantees := []*flow.CollectionGuarantee{}
    53  	for _, g := range msg.CollectionGuarantees {
    54  		guarantee := convert.MessageToCollectionGuarantee(g)
    55  		guarantees = append(guarantees, guarantee)
    56  	}
    57  
    58  	assert.Equal(t, block.Payload.Guarantees, guarantees)
    59  
    60  	// all other fields are not
    61  	assert.Nil(t, msg.BlockHeader)
    62  	assert.Len(t, msg.BlockSeals, 0)
    63  	assert.Len(t, msg.ExecutionReceiptMetaList, 0)
    64  	assert.Len(t, msg.ExecutionResultList, 0)
    65  }