github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/computation_result_test.go (about)

     1  package operation
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/koko1123/flow-go-1/ledger"
     8  	"github.com/koko1123/flow-go-1/ledger/common/pathfinder"
     9  	"github.com/koko1123/flow-go-1/ledger/complete"
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/storage"
    12  
    13  	"github.com/dgraph-io/badger/v3"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/koko1123/flow-go-1/engine/execution"
    18  	"github.com/koko1123/flow-go-1/utils/unittest"
    19  )
    20  
    21  func TestInsertAndUpdateAndRetrieveComputationResultUpdateStatus(t *testing.T) {
    22  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    23  		expected := generateComputationResult(t)
    24  		expectedId := expected.ExecutableBlock.ID()
    25  
    26  		t.Run("Update existing ComputationResult", func(t *testing.T) {
    27  			// insert as False
    28  			testUploadStatusVal := false
    29  
    30  			err := db.Update(InsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    31  			require.NoError(t, err)
    32  
    33  			var actualUploadStatus bool
    34  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    35  			require.NoError(t, err)
    36  
    37  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    38  
    39  			// update to True
    40  			testUploadStatusVal = true
    41  			err = db.Update(UpdateComputationResultUploadStatus(expectedId, testUploadStatusVal))
    42  			require.NoError(t, err)
    43  
    44  			// check if value is updated
    45  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    46  			require.NoError(t, err)
    47  
    48  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    49  		})
    50  
    51  		t.Run("Update non-existed ComputationResult", func(t *testing.T) {
    52  			testUploadStatusVal := true
    53  			randomFlowID := flow.Identifier{}
    54  			err := db.Update(UpdateComputationResultUploadStatus(randomFlowID, testUploadStatusVal))
    55  			require.Error(t, err)
    56  			require.Equal(t, err, storage.ErrNotFound)
    57  		})
    58  	})
    59  }
    60  
    61  func TestUpsertAndRetrieveComputationResultUpdateStatus(t *testing.T) {
    62  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    63  		expected := generateComputationResult(t)
    64  		expectedId := expected.ExecutableBlock.ID()
    65  
    66  		t.Run("Upsert ComputationResult", func(t *testing.T) {
    67  			// first upsert as false
    68  			testUploadStatusVal := false
    69  
    70  			err := db.Update(UpsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    71  			require.NoError(t, err)
    72  
    73  			var actualUploadStatus bool
    74  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    75  			require.NoError(t, err)
    76  
    77  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    78  
    79  			// upsert to true
    80  			testUploadStatusVal = true
    81  			err = db.Update(UpsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
    82  			require.NoError(t, err)
    83  
    84  			// check if value is updated
    85  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
    86  			require.NoError(t, err)
    87  
    88  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
    89  		})
    90  	})
    91  }
    92  
    93  func TestRemoveComputationResultUploadStatus(t *testing.T) {
    94  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    95  		expected := generateComputationResult(t)
    96  		expectedId := expected.ExecutableBlock.ID()
    97  
    98  		t.Run("Remove ComputationResult", func(t *testing.T) {
    99  			testUploadStatusVal := true
   100  
   101  			err := db.Update(InsertComputationResultUploadStatus(expectedId, testUploadStatusVal))
   102  			require.NoError(t, err)
   103  
   104  			var actualUploadStatus bool
   105  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
   106  			require.NoError(t, err)
   107  
   108  			assert.Equal(t, testUploadStatusVal, actualUploadStatus)
   109  
   110  			err = db.Update(RemoveComputationResultUploadStatus(expectedId))
   111  			require.NoError(t, err)
   112  
   113  			err = db.View(GetComputationResultUploadStatus(expectedId, &actualUploadStatus))
   114  			assert.NotNil(t, err)
   115  		})
   116  	})
   117  }
   118  
   119  func TestListComputationResults(t *testing.T) {
   120  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
   121  		expected := [...]*execution.ComputationResult{
   122  			generateComputationResult(t),
   123  			generateComputationResult(t),
   124  		}
   125  		t.Run("List all ComputationResult with status True", func(t *testing.T) {
   126  			expectedIDs := make(map[string]bool, 0)
   127  			// Store a list of ComputationResult instances first
   128  			for _, cr := range expected {
   129  				expectedId := cr.ExecutableBlock.ID()
   130  				expectedIDs[expectedId.String()] = true
   131  				err := db.Update(InsertComputationResultUploadStatus(expectedId, true))
   132  				require.NoError(t, err)
   133  			}
   134  
   135  			// Get the list of IDs of stored ComputationResult
   136  			crIDs := make([]flow.Identifier, 0)
   137  			err := db.View(GetBlockIDsByStatus(&crIDs, true))
   138  			require.NoError(t, err)
   139  			crIDsStrMap := make(map[string]bool, 0)
   140  			for _, crID := range crIDs {
   141  				crIDsStrMap[crID.String()] = true
   142  			}
   143  
   144  			assert.True(t, reflect.DeepEqual(crIDsStrMap, expectedIDs))
   145  		})
   146  	})
   147  }
   148  
   149  // Generate ComputationResult for testing purposes
   150  func generateComputationResult(t *testing.T) *execution.ComputationResult {
   151  
   152  	update1, err := ledger.NewUpdate(
   153  		ledger.State(unittest.StateCommitmentFixture()),
   154  		[]ledger.Key{
   155  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(3, []byte{33})}),
   156  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(1, []byte{11})}),
   157  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(2, []byte{1, 1}), ledger.NewKeyPart(3, []byte{2, 5})}),
   158  		},
   159  		[]ledger.Value{
   160  			[]byte{21, 37},
   161  			nil,
   162  			[]byte{3, 3, 3, 3, 3},
   163  		},
   164  	)
   165  	require.NoError(t, err)
   166  
   167  	trieUpdate1, err := pathfinder.UpdateToTrieUpdate(update1, complete.DefaultPathFinderVersion)
   168  	require.NoError(t, err)
   169  
   170  	update2, err := ledger.NewUpdate(
   171  		ledger.State(unittest.StateCommitmentFixture()),
   172  		[]ledger.Key{},
   173  		[]ledger.Value{},
   174  	)
   175  	require.NoError(t, err)
   176  
   177  	trieUpdate2, err := pathfinder.UpdateToTrieUpdate(update2, complete.DefaultPathFinderVersion)
   178  	require.NoError(t, err)
   179  
   180  	update3, err := ledger.NewUpdate(
   181  		ledger.State(unittest.StateCommitmentFixture()),
   182  		[]ledger.Key{
   183  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}),
   184  		},
   185  		[]ledger.Value{
   186  			[]byte{21, 37},
   187  		},
   188  	)
   189  	require.NoError(t, err)
   190  
   191  	trieUpdate3, err := pathfinder.UpdateToTrieUpdate(update3, complete.DefaultPathFinderVersion)
   192  	require.NoError(t, err)
   193  
   194  	update4, err := ledger.NewUpdate(
   195  		ledger.State(unittest.StateCommitmentFixture()),
   196  		[]ledger.Key{
   197  			ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}),
   198  		},
   199  		[]ledger.Value{
   200  			[]byte{21, 37},
   201  		},
   202  	)
   203  	require.NoError(t, err)
   204  
   205  	trieUpdate4, err := pathfinder.UpdateToTrieUpdate(update4, complete.DefaultPathFinderVersion)
   206  	require.NoError(t, err)
   207  
   208  	return &execution.ComputationResult{
   209  		ExecutableBlock: unittest.ExecutableBlockFixture([][]flow.Identifier{
   210  			{unittest.IdentifierFixture()},
   211  			{unittest.IdentifierFixture()},
   212  			{unittest.IdentifierFixture()},
   213  		}),
   214  		StateSnapshots: nil,
   215  		StateCommitments: []flow.StateCommitment{
   216  			unittest.StateCommitmentFixture(),
   217  			unittest.StateCommitmentFixture(),
   218  			unittest.StateCommitmentFixture(),
   219  			unittest.StateCommitmentFixture(),
   220  		},
   221  		Proofs: nil,
   222  		Events: []flow.EventsList{
   223  			{
   224  				unittest.EventFixture("what", 0, 0, unittest.IdentifierFixture(), 2),
   225  				unittest.EventFixture("ever", 0, 1, unittest.IdentifierFixture(), 22),
   226  			},
   227  			{},
   228  			{
   229  				unittest.EventFixture("what", 2, 0, unittest.IdentifierFixture(), 2),
   230  				unittest.EventFixture("ever", 2, 1, unittest.IdentifierFixture(), 22),
   231  				unittest.EventFixture("ever", 2, 2, unittest.IdentifierFixture(), 2),
   232  				unittest.EventFixture("ever", 2, 3, unittest.IdentifierFixture(), 22),
   233  			},
   234  			{}, // system chunk events
   235  		},
   236  		EventsHashes:  nil,
   237  		ServiceEvents: nil,
   238  		TransactionResults: []flow.TransactionResult{
   239  			{
   240  				TransactionID:   unittest.IdentifierFixture(),
   241  				ErrorMessage:    "",
   242  				ComputationUsed: 23,
   243  				MemoryUsed:      101,
   244  			},
   245  			{
   246  				TransactionID:   unittest.IdentifierFixture(),
   247  				ErrorMessage:    "fail",
   248  				ComputationUsed: 1,
   249  				MemoryUsed:      22,
   250  			},
   251  		},
   252  		TransactionResultIndex: []int{1, 1, 2, 2},
   253  		TrieUpdates: []*ledger.TrieUpdate{
   254  			trieUpdate1,
   255  			trieUpdate2,
   256  			trieUpdate3,
   257  			trieUpdate4,
   258  		},
   259  	}
   260  }