github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/transaction_results_test.go (about) 1 package badger_test 2 3 import ( 4 "fmt" 5 mathRand "math/rand" 6 "testing" 7 8 "github.com/dgraph-io/badger/v2" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 "golang.org/x/exp/rand" 12 13 "github.com/onflow/flow-go/model/flow" 14 "github.com/onflow/flow-go/module/metrics" 15 "github.com/onflow/flow-go/storage" 16 "github.com/onflow/flow-go/utils/unittest" 17 18 bstorage "github.com/onflow/flow-go/storage/badger" 19 ) 20 21 func TestBatchStoringTransactionResults(t *testing.T) { 22 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 23 metrics := metrics.NewNoopCollector() 24 store := bstorage.NewTransactionResults(metrics, db, 1000) 25 26 blockID := unittest.IdentifierFixture() 27 txResults := make([]flow.TransactionResult, 0) 28 for i := 0; i < 10; i++ { 29 txID := unittest.IdentifierFixture() 30 expected := flow.TransactionResult{ 31 TransactionID: txID, 32 ErrorMessage: fmt.Sprintf("a runtime error %d", i), 33 } 34 txResults = append(txResults, expected) 35 } 36 writeBatch := bstorage.NewBatch(db) 37 err := store.BatchStore(blockID, txResults, writeBatch) 38 require.NoError(t, err) 39 40 err = writeBatch.Flush() 41 require.NoError(t, err) 42 43 for _, txResult := range txResults { 44 actual, err := store.ByBlockIDTransactionID(blockID, txResult.TransactionID) 45 require.NoError(t, err) 46 assert.Equal(t, txResult, *actual) 47 } 48 49 // test loading from database 50 newStore := bstorage.NewTransactionResults(metrics, db, 1000) 51 for _, txResult := range txResults { 52 actual, err := newStore.ByBlockIDTransactionID(blockID, txResult.TransactionID) 53 require.NoError(t, err) 54 assert.Equal(t, txResult, *actual) 55 } 56 57 // check retrieving by index from both cache and db 58 for i := len(txResults) - 1; i >= 0; i-- { 59 actual, err := store.ByBlockIDTransactionIndex(blockID, uint32(i)) 60 require.NoError(t, err) 61 assert.Equal(t, txResults[i], *actual) 62 63 actual, err = newStore.ByBlockIDTransactionIndex(blockID, uint32(i)) 64 require.NoError(t, err) 65 assert.Equal(t, txResults[i], *actual) 66 } 67 }) 68 } 69 70 func TestReadingNotStoreTransaction(t *testing.T) { 71 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 72 metrics := metrics.NewNoopCollector() 73 store := bstorage.NewTransactionResults(metrics, db, 1000) 74 75 blockID := unittest.IdentifierFixture() 76 txID := unittest.IdentifierFixture() 77 txIndex := rand.Uint32() 78 79 _, err := store.ByBlockIDTransactionID(blockID, txID) 80 assert.ErrorIs(t, err, storage.ErrNotFound) 81 82 _, err = store.ByBlockIDTransactionIndex(blockID, txIndex) 83 assert.ErrorIs(t, err, storage.ErrNotFound) 84 }) 85 } 86 87 func TestKeyConversion(t *testing.T) { 88 blockID := unittest.IdentifierFixture() 89 txID := unittest.IdentifierFixture() 90 key := bstorage.KeyFromBlockIDTransactionID(blockID, txID) 91 bID, tID, err := bstorage.KeyToBlockIDTransactionID(key) 92 require.NoError(t, err) 93 require.Equal(t, blockID, bID) 94 require.Equal(t, txID, tID) 95 } 96 97 func TestIndexKeyConversion(t *testing.T) { 98 blockID := unittest.IdentifierFixture() 99 txIndex := mathRand.Uint32() 100 key := bstorage.KeyFromBlockIDIndex(blockID, txIndex) 101 bID, tID, err := bstorage.KeyToBlockIDIndex(key) 102 require.NoError(t, err) 103 require.Equal(t, blockID, bID) 104 require.Equal(t, txIndex, tID) 105 }