github.com/koko1123/flow-go-1@v0.29.6/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/koko1123/flow-go-1/engine/execution"
    10  	"github.com/koko1123/flow-go-1/ledger"
    11  	"github.com/koko1123/flow-go-1/ledger/common/pathfinder"
    12  	"github.com/koko1123/flow-go-1/ledger/complete"
    13  	"github.com/koko1123/flow-go-1/model/flow"
    14  	"github.com/koko1123/flow-go-1/utils/unittest"
    15  )
    16  
    17  func Test_ComputationResultToBlockDataConversion(t *testing.T) {
    18  
    19  	cr := generateComputationResult(t)
    20  
    21  	blockData := ComputationResultToBlockData(cr)
    22  
    23  	assert.Equal(t, cr.ExecutableBlock.Block, blockData.Block)
    24  	assert.Equal(t, cr.ExecutableBlock.Collections(), blockData.Collections)
    25  	require.Equal(t, len(cr.TransactionResults), len(blockData.TxResults))
    26  	for i, result := range cr.TransactionResults {
    27  		assert.Equal(t, result, *blockData.TxResults[i])
    28  	}
    29  
    30  	eventsCombined := make([]flow.Event, 0)
    31  	for _, eventsList := range cr.Events {
    32  		eventsCombined = append(eventsCombined, eventsList...)
    33  	}
    34  	require.Equal(t, len(eventsCombined), len(blockData.Events))
    35  
    36  	for i, event := range eventsCombined {
    37  		assert.Equal(t, event, *blockData.Events[i])
    38  	}
    39  
    40  	assert.Equal(t, cr.TrieUpdates, blockData.TrieUpdates)
    41  
    42  	assert.Equal(t, cr.StateCommitments[len(cr.StateCommitments)-1], blockData.FinalStateCommitment)
    43  }
    44  
    45  func generateComputationResult(t *testing.T) *execution.ComputationResult {
    46  
    47  	update1, err := ledger.NewUpdate(
    48  		ledger.State(unittest.StateCommitmentFixture()),
    49  		[]ledger.Key{
    50  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(3, []byte{33})}),
    51  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(1, []byte{11})}),
    52  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(2, []byte{1, 1}), ledger.NewKeyPart(3, []byte{2, 5})}),
    53  		},
    54  		[]ledger.Value{
    55  			[]byte{21, 37},
    56  			nil,
    57  			[]byte{3, 3, 3, 3, 3},
    58  		},
    59  	)
    60  	require.NoError(t, err)
    61  
    62  	trieUpdate1, err := pathfinder.UpdateToTrieUpdate(update1, complete.DefaultPathFinderVersion)
    63  	require.NoError(t, err)
    64  
    65  	update2, err := ledger.NewUpdate(
    66  		ledger.State(unittest.StateCommitmentFixture()),
    67  		[]ledger.Key{},
    68  		[]ledger.Value{},
    69  	)
    70  	require.NoError(t, err)
    71  
    72  	trieUpdate2, err := pathfinder.UpdateToTrieUpdate(update2, complete.DefaultPathFinderVersion)
    73  	require.NoError(t, err)
    74  
    75  	update3, err := ledger.NewUpdate(
    76  		ledger.State(unittest.StateCommitmentFixture()),
    77  		[]ledger.Key{
    78  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}),
    79  		},
    80  		[]ledger.Value{
    81  			[]byte{21, 37},
    82  		},
    83  	)
    84  	require.NoError(t, err)
    85  
    86  	trieUpdate3, err := pathfinder.UpdateToTrieUpdate(update3, complete.DefaultPathFinderVersion)
    87  	require.NoError(t, err)
    88  
    89  	update4, err := ledger.NewUpdate(
    90  		ledger.State(unittest.StateCommitmentFixture()),
    91  		[]ledger.Key{
    92  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}),
    93  		},
    94  		[]ledger.Value{
    95  			[]byte{21, 37},
    96  		},
    97  	)
    98  	require.NoError(t, err)
    99  
   100  	trieUpdate4, err := pathfinder.UpdateToTrieUpdate(update4, complete.DefaultPathFinderVersion)
   101  	require.NoError(t, err)
   102  
   103  	return &execution.ComputationResult{
   104  		ExecutableBlock: unittest.ExecutableBlockFixture([][]flow.Identifier{
   105  			{unittest.IdentifierFixture()},
   106  			{unittest.IdentifierFixture()},
   107  			{unittest.IdentifierFixture()},
   108  		}),
   109  		StateSnapshots: nil,
   110  		StateCommitments: []flow.StateCommitment{
   111  			unittest.StateCommitmentFixture(),
   112  			unittest.StateCommitmentFixture(),
   113  			unittest.StateCommitmentFixture(),
   114  			unittest.StateCommitmentFixture(),
   115  		},
   116  		Proofs: nil,
   117  		Events: []flow.EventsList{
   118  			{
   119  				unittest.EventFixture("what", 0, 0, unittest.IdentifierFixture(), 2),
   120  				unittest.EventFixture("ever", 0, 1, unittest.IdentifierFixture(), 22),
   121  			},
   122  			{},
   123  			{
   124  				unittest.EventFixture("what", 2, 0, unittest.IdentifierFixture(), 2),
   125  				unittest.EventFixture("ever", 2, 1, unittest.IdentifierFixture(), 22),
   126  				unittest.EventFixture("ever", 2, 2, unittest.IdentifierFixture(), 2),
   127  				unittest.EventFixture("ever", 2, 3, unittest.IdentifierFixture(), 22),
   128  			},
   129  			{}, // system chunk events
   130  		},
   131  		EventsHashes:  nil,
   132  		ServiceEvents: nil,
   133  		TransactionResults: []flow.TransactionResult{
   134  			{
   135  				TransactionID:   unittest.IdentifierFixture(),
   136  				ErrorMessage:    "",
   137  				ComputationUsed: 23,
   138  			},
   139  			{
   140  				TransactionID:   unittest.IdentifierFixture(),
   141  				ErrorMessage:    "fail",
   142  				ComputationUsed: 1,
   143  			},
   144  		},
   145  		TransactionResultIndex: []int{1, 1, 2, 2},
   146  		TrieUpdates: []*ledger.TrieUpdate{
   147  			trieUpdate1,
   148  			trieUpdate2,
   149  			trieUpdate3,
   150  			trieUpdate4,
   151  		},
   152  	}
   153  }