github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/seals_test.go (about) 1 package badger_test 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/dgraph-io/badger/v2" 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/flow-go/module/metrics" 11 "github.com/onflow/flow-go/storage" 12 "github.com/onflow/flow-go/storage/badger/operation" 13 "github.com/onflow/flow-go/utils/unittest" 14 15 badgerstorage "github.com/onflow/flow-go/storage/badger" 16 ) 17 18 func TestRetrieveWithoutStore(t *testing.T) { 19 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 20 metrics := metrics.NewNoopCollector() 21 store := badgerstorage.NewSeals(metrics, db) 22 23 _, err := store.ByID(unittest.IdentifierFixture()) 24 require.True(t, errors.Is(err, storage.ErrNotFound)) 25 26 _, err = store.HighestInFork(unittest.IdentifierFixture()) 27 require.True(t, errors.Is(err, storage.ErrNotFound)) 28 }) 29 } 30 31 // TestSealStoreRetrieve verifies that a seal can be stored and retrieved by its ID 32 func TestSealStoreRetrieve(t *testing.T) { 33 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 34 metrics := metrics.NewNoopCollector() 35 store := badgerstorage.NewSeals(metrics, db) 36 37 expected := unittest.Seal.Fixture() 38 // store seal 39 err := store.Store(expected) 40 require.NoError(t, err) 41 42 // retrieve seal 43 seal, err := store.ByID(expected.ID()) 44 require.NoError(t, err) 45 require.Equal(t, expected, seal) 46 }) 47 } 48 49 // TestSealIndexAndRetrieve verifies that: 50 // - for a block, we can store (aka index) the latest sealed block along this fork. 51 // 52 // Note: indexing the seal for a block is currently implemented only through a direct 53 // Badger operation. The Seals mempool only supports retrieving the latest sealed block. 54 func TestSealIndexAndRetrieve(t *testing.T) { 55 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 56 metrics := metrics.NewNoopCollector() 57 store := badgerstorage.NewSeals(metrics, db) 58 59 expectedSeal := unittest.Seal.Fixture() 60 blockID := unittest.IdentifierFixture() 61 62 // store the seal first 63 err := store.Store(expectedSeal) 64 require.NoError(t, err) 65 66 // index the seal ID for the heighest sealed block in this fork 67 err = operation.RetryOnConflict(db.Update, operation.IndexLatestSealAtBlock(blockID, expectedSeal.ID())) 68 require.NoError(t, err) 69 70 // retrieve latest seal 71 seal, err := store.HighestInFork(blockID) 72 require.NoError(t, err) 73 require.Equal(t, expectedSeal, seal) 74 }) 75 } 76 77 // TestSealedBlockIndexAndRetrieve checks after indexing a seal by a sealed block ID, it can be 78 // retrieved by the sealed block ID 79 func TestSealedBlockIndexAndRetrieve(t *testing.T) { 80 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 81 metrics := metrics.NewNoopCollector() 82 store := badgerstorage.NewSeals(metrics, db) 83 84 expectedSeal := unittest.Seal.Fixture() 85 blockID := unittest.IdentifierFixture() 86 expectedSeal.BlockID = blockID 87 88 // store the seal first 89 err := store.Store(expectedSeal) 90 require.NoError(t, err) 91 92 // index the seal ID for the highest sealed block in this fork 93 err = operation.RetryOnConflict(db.Update, operation.IndexFinalizedSealByBlockID(expectedSeal.BlockID, expectedSeal.ID())) 94 require.NoError(t, err) 95 96 // retrieve latest seal 97 seal, err := store.FinalizedSealForBlock(blockID) 98 require.NoError(t, err) 99 require.Equal(t, expectedSeal, seal) 100 }) 101 }