github.com/koko1123/flow-go-1@v0.29.6/storage/badger/procedure/children_test.go (about) 1 package procedure_test 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/dgraph-io/badger/v3" 8 "github.com/stretchr/testify/require" 9 10 "github.com/koko1123/flow-go-1/model/flow" 11 "github.com/koko1123/flow-go-1/storage" 12 "github.com/koko1123/flow-go-1/storage/badger/procedure" 13 "github.com/koko1123/flow-go-1/utils/unittest" 14 ) 15 16 // after indexing a block by its parent, it should be able to retrieve the child block by the parentID 17 func TestIndexAndLookupChild(t *testing.T) { 18 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 19 20 parentID := unittest.IdentifierFixture() 21 childID := unittest.IdentifierFixture() 22 23 err := db.Update(procedure.IndexNewBlock(childID, parentID)) 24 require.NoError(t, err) 25 26 // retrieve child 27 var retrievedIDs flow.IdentifierList 28 err = db.View(procedure.LookupBlockChildren(parentID, &retrievedIDs)) 29 require.NoError(t, err) 30 31 // retrieved child should be the stored child 32 require.Equal(t, flow.IdentifierList{childID}, retrievedIDs) 33 }) 34 } 35 36 // if two blocks connect to the same parent, indexing the second block would have 37 // no effect, retrieving the child of the parent block will return the first block that 38 // was indexed. 39 func TestIndexTwiceAndRetrieve(t *testing.T) { 40 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 41 42 parentID := unittest.IdentifierFixture() 43 child1ID := unittest.IdentifierFixture() 44 child2ID := unittest.IdentifierFixture() 45 46 // index the first child 47 err := db.Update(procedure.IndexNewBlock(child1ID, parentID)) 48 require.NoError(t, err) 49 50 // index the second child 51 err = db.Update(procedure.IndexNewBlock(child2ID, parentID)) 52 require.NoError(t, err) 53 54 var retrievedIDs flow.IdentifierList 55 err = db.View(procedure.LookupBlockChildren(parentID, &retrievedIDs)) 56 require.NoError(t, err) 57 58 require.Equal(t, flow.IdentifierList{child1ID, child2ID}, retrievedIDs) 59 }) 60 } 61 62 // if parent is zero, then we don't index it 63 func TestIndexZeroParent(t *testing.T) { 64 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 65 66 childID := unittest.IdentifierFixture() 67 68 err := db.Update(procedure.IndexNewBlock(childID, flow.ZeroID)) 69 require.NoError(t, err) 70 71 // zero id should have no children 72 var retrievedIDs flow.IdentifierList 73 err = db.View(procedure.LookupBlockChildren(flow.ZeroID, &retrievedIDs)) 74 require.True(t, errors.Is(err, storage.ErrNotFound)) 75 }) 76 } 77 78 // lookup block children will only return direct childrens 79 func TestDirectChildren(t *testing.T) { 80 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 81 82 b1 := unittest.IdentifierFixture() 83 b2 := unittest.IdentifierFixture() 84 b3 := unittest.IdentifierFixture() 85 b4 := unittest.IdentifierFixture() 86 87 err := db.Update(procedure.IndexNewBlock(b2, b1)) 88 require.NoError(t, err) 89 90 err = db.Update(procedure.IndexNewBlock(b3, b2)) 91 require.NoError(t, err) 92 93 err = db.Update(procedure.IndexNewBlock(b4, b3)) 94 require.NoError(t, err) 95 96 // check the children of the first block 97 var retrievedIDs flow.IdentifierList 98 99 err = db.View(procedure.LookupBlockChildren(b1, &retrievedIDs)) 100 require.NoError(t, err) 101 require.Equal(t, flow.IdentifierList{b2}, retrievedIDs) 102 103 err = db.View(procedure.LookupBlockChildren(b2, &retrievedIDs)) 104 require.NoError(t, err) 105 require.Equal(t, flow.IdentifierList{b3}, retrievedIDs) 106 107 err = db.View(procedure.LookupBlockChildren(b3, &retrievedIDs)) 108 require.NoError(t, err) 109 require.Equal(t, flow.IdentifierList{b4}, retrievedIDs) 110 111 err = db.View(procedure.LookupBlockChildren(b4, &retrievedIDs)) 112 require.NoError(t, err) 113 require.Nil(t, retrievedIDs) 114 }) 115 }