github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/light_transaction_results_test.go (about) 1 package badger_test 2 3 import ( 4 "testing" 5 6 "github.com/dgraph-io/badger/v2" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 "golang.org/x/exp/rand" 10 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/module/metrics" 13 "github.com/onflow/flow-go/storage" 14 bstorage "github.com/onflow/flow-go/storage/badger" 15 "github.com/onflow/flow-go/utils/unittest" 16 ) 17 18 func TestBatchStoringLightTransactionResults(t *testing.T) { 19 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 20 metrics := metrics.NewNoopCollector() 21 store := bstorage.NewLightTransactionResults(metrics, db, 1000) 22 23 blockID := unittest.IdentifierFixture() 24 txResults := getLightTransactionResultsFixture(10) 25 26 t.Run("batch store results", func(t *testing.T) { 27 writeBatch := bstorage.NewBatch(db) 28 err := store.BatchStore(blockID, txResults, writeBatch) 29 require.NoError(t, err) 30 31 err = writeBatch.Flush() 32 require.NoError(t, err) 33 34 // add a results to a new block to validate they are not included in lookups 35 writeBatch = bstorage.NewBatch(db) 36 err = store.BatchStore(unittest.IdentifierFixture(), getLightTransactionResultsFixture(2), writeBatch) 37 require.NoError(t, err) 38 39 err = writeBatch.Flush() 40 require.NoError(t, err) 41 }) 42 43 t.Run("read results with cache", func(t *testing.T) { 44 for _, txResult := range txResults { 45 actual, err := store.ByBlockIDTransactionID(blockID, txResult.TransactionID) 46 require.NoError(t, err) 47 assert.Equal(t, txResult, *actual) 48 } 49 }) 50 51 newStore := bstorage.NewLightTransactionResults(metrics, db, 1000) 52 t.Run("read results without cache", func(t *testing.T) { 53 // test loading from database (without cache) 54 // create a new instance using the same db so it has an empty cache 55 for _, txResult := range txResults { 56 actual, err := newStore.ByBlockIDTransactionID(blockID, txResult.TransactionID) 57 require.NoError(t, err) 58 assert.Equal(t, txResult, *actual) 59 } 60 }) 61 62 t.Run("cached and non-cached results are equal", func(t *testing.T) { 63 // check retrieving by index from both cache and db 64 for i := len(txResults) - 1; i >= 0; i-- { 65 actual, err := store.ByBlockIDTransactionIndex(blockID, uint32(i)) 66 require.NoError(t, err) 67 assert.Equal(t, txResults[i], *actual) 68 69 actual, err = newStore.ByBlockIDTransactionIndex(blockID, uint32(i)) 70 require.NoError(t, err) 71 assert.Equal(t, txResults[i], *actual) 72 } 73 }) 74 75 t.Run("read all results for block", func(t *testing.T) { 76 actuals, err := store.ByBlockID(blockID) 77 require.NoError(t, err) 78 79 assert.Equal(t, len(txResults), len(actuals)) 80 for i := range txResults { 81 assert.Equal(t, txResults[i], actuals[i]) 82 } 83 }) 84 }) 85 } 86 87 func TestReadingNotStoredLightTransactionResults(t *testing.T) { 88 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 89 metrics := metrics.NewNoopCollector() 90 store := bstorage.NewLightTransactionResults(metrics, db, 1000) 91 92 blockID := unittest.IdentifierFixture() 93 txID := unittest.IdentifierFixture() 94 txIndex := rand.Uint32() 95 96 _, err := store.ByBlockIDTransactionID(blockID, txID) 97 assert.ErrorIs(t, err, storage.ErrNotFound) 98 99 _, err = store.ByBlockIDTransactionIndex(blockID, txIndex) 100 assert.ErrorIs(t, err, storage.ErrNotFound) 101 }) 102 } 103 104 func getLightTransactionResultsFixture(n int) []flow.LightTransactionResult { 105 txResults := make([]flow.LightTransactionResult, 0, n) 106 for i := 0; i < n; i++ { 107 expected := flow.LightTransactionResult{ 108 TransactionID: unittest.IdentifierFixture(), 109 Failed: i%2 == 0, 110 ComputationUsed: unittest.Uint64InRange(1, 1000), 111 } 112 txResults = append(txResults, expected) 113 } 114 return txResults 115 }