github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/execution/ingestion/uploader/model_test.go (about)

     1  package uploader
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/engine/execution"
    10  	"github.com/onflow/flow-go/engine/execution/testutil"
    11  	"github.com/onflow/flow-go/ledger"
    12  	"github.com/onflow/flow-go/ledger/common/pathfinder"
    13  	"github.com/onflow/flow-go/ledger/complete"
    14  	"github.com/onflow/flow-go/model/flow"
    15  	"github.com/onflow/flow-go/utils/unittest"
    16  )
    17  
    18  func Test_ComputationResultToBlockDataConversion(t *testing.T) {
    19  
    20  	cr, expectedTrieUpdates := generateComputationResult(t)
    21  
    22  	blockData := ComputationResultToBlockData(cr)
    23  
    24  	assert.Equal(t, cr.ExecutableBlock.Block, blockData.Block)
    25  	assert.Equal(t, cr.ExecutableBlock.Collections(), blockData.Collections)
    26  
    27  	allTxResults := cr.AllTransactionResults()
    28  	require.Equal(t, len(allTxResults), len(blockData.TxResults))
    29  	for i, result := range allTxResults {
    30  		assert.Equal(t, result, *blockData.TxResults[i])
    31  	}
    32  
    33  	// Since returned events are not preserving orders,
    34  	// use map with event.ID() as key to confirm all events
    35  	// are included.
    36  	allEvents := cr.AllEvents()
    37  	require.Equal(t, len(allEvents), len(blockData.Events))
    38  
    39  	eventsInBlockData := make(map[flow.Identifier]flow.Event)
    40  	for _, e := range blockData.Events {
    41  		eventsInBlockData[e.ID()] = *e
    42  	}
    43  
    44  	for _, expectedEvent := range allEvents {
    45  		event, ok := eventsInBlockData[expectedEvent.ID()]
    46  		require.True(t, ok)
    47  		require.Equal(t, expectedEvent, event)
    48  	}
    49  
    50  	assert.Equal(t, len(expectedTrieUpdates), len(blockData.TrieUpdates))
    51  
    52  	assert.Equal(t, cr.CurrentEndState(), blockData.FinalStateCommitment)
    53  }
    54  
    55  func generateComputationResult(
    56  	t *testing.T,
    57  ) (
    58  	*execution.ComputationResult,
    59  	[]*ledger.TrieUpdate,
    60  ) {
    61  
    62  	update1, err := ledger.NewUpdate(
    63  		ledger.State(unittest.StateCommitmentFixture()),
    64  		[]ledger.Key{
    65  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(3, []byte{33})}),
    66  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(1, []byte{11})}),
    67  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(2, []byte{1, 1}), ledger.NewKeyPart(3, []byte{2, 5})}),
    68  		},
    69  		[]ledger.Value{
    70  			[]byte{21, 37},
    71  			nil,
    72  			[]byte{3, 3, 3, 3, 3},
    73  		},
    74  	)
    75  	require.NoError(t, err)
    76  
    77  	trieUpdate1, err := pathfinder.UpdateToTrieUpdate(update1, complete.DefaultPathFinderVersion)
    78  	require.NoError(t, err)
    79  
    80  	update2, err := ledger.NewUpdate(
    81  		ledger.State(unittest.StateCommitmentFixture()),
    82  		[]ledger.Key{},
    83  		[]ledger.Value{},
    84  	)
    85  	require.NoError(t, err)
    86  
    87  	trieUpdate2, err := pathfinder.UpdateToTrieUpdate(update2, complete.DefaultPathFinderVersion)
    88  	require.NoError(t, err)
    89  
    90  	update3, err := ledger.NewUpdate(
    91  		ledger.State(unittest.StateCommitmentFixture()),
    92  		[]ledger.Key{
    93  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}),
    94  		},
    95  		[]ledger.Value{
    96  			[]byte{21, 37},
    97  		},
    98  	)
    99  	require.NoError(t, err)
   100  
   101  	trieUpdate3, err := pathfinder.UpdateToTrieUpdate(update3, complete.DefaultPathFinderVersion)
   102  	require.NoError(t, err)
   103  
   104  	update4, err := ledger.NewUpdate(
   105  		ledger.State(unittest.StateCommitmentFixture()),
   106  		[]ledger.Key{
   107  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}),
   108  		},
   109  		[]ledger.Value{
   110  			[]byte{21, 37},
   111  		},
   112  	)
   113  	require.NoError(t, err)
   114  
   115  	trieUpdate4, err := pathfinder.UpdateToTrieUpdate(update4, complete.DefaultPathFinderVersion)
   116  	require.NoError(t, err)
   117  	return testutil.ComputationResultFixture(t), []*ledger.TrieUpdate{
   118  		trieUpdate1,
   119  		trieUpdate2,
   120  		trieUpdate3,
   121  		trieUpdate4,
   122  	}
   123  }