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

     1  package operation
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/koko1123/flow-go-1/model/flow"
    11  	"github.com/koko1123/flow-go-1/utils/unittest"
    12  	"github.com/onflow/flow-go/crypto"
    13  )
    14  
    15  func TestGuaranteeInsertRetrieve(t *testing.T) {
    16  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    17  		g := unittest.CollectionGuaranteeFixture()
    18  
    19  		err := db.Update(InsertGuarantee(g.CollectionID, g))
    20  		require.Nil(t, err)
    21  
    22  		var retrieved flow.CollectionGuarantee
    23  		err = db.View(RetrieveGuarantee(g.CollectionID, &retrieved))
    24  		require.NoError(t, err)
    25  
    26  		assert.Equal(t, g, &retrieved)
    27  	})
    28  }
    29  
    30  func TestIndexGuaranteedCollectionByBlockHashInsertRetrieve(t *testing.T) {
    31  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    32  		blockID := flow.Identifier{0x10}
    33  		collID1 := flow.Identifier{0x01}
    34  		collID2 := flow.Identifier{0x02}
    35  		guarantees := []*flow.CollectionGuarantee{
    36  			{CollectionID: collID1, Signature: crypto.Signature{0x10}},
    37  			{CollectionID: collID2, Signature: crypto.Signature{0x20}},
    38  		}
    39  		expected := flow.GetIDs(guarantees)
    40  
    41  		err := db.Update(func(tx *badger.Txn) error {
    42  			for _, guarantee := range guarantees {
    43  				if err := InsertGuarantee(guarantee.ID(), guarantee)(tx); err != nil {
    44  					return err
    45  				}
    46  			}
    47  			if err := IndexPayloadGuarantees(blockID, expected)(tx); err != nil {
    48  				return err
    49  			}
    50  			return nil
    51  		})
    52  		require.Nil(t, err)
    53  
    54  		var actual []flow.Identifier
    55  		err = db.View(LookupPayloadGuarantees(blockID, &actual))
    56  		require.Nil(t, err)
    57  
    58  		assert.Equal(t, []flow.Identifier{collID1, collID2}, actual)
    59  	})
    60  }
    61  
    62  func TestIndexGuaranteedCollectionByBlockHashMultipleBlocks(t *testing.T) {
    63  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    64  		blockID1 := flow.Identifier{0x10}
    65  		blockID2 := flow.Identifier{0x20}
    66  		collID1 := flow.Identifier{0x01}
    67  		collID2 := flow.Identifier{0x02}
    68  		collID3 := flow.Identifier{0x03}
    69  		collID4 := flow.Identifier{0x04}
    70  		set1 := []*flow.CollectionGuarantee{
    71  			{CollectionID: collID1, Signature: crypto.Signature{0x1}},
    72  		}
    73  		set2 := []*flow.CollectionGuarantee{
    74  			{CollectionID: collID2, Signature: crypto.Signature{0x2}},
    75  			{CollectionID: collID3, Signature: crypto.Signature{0x3}},
    76  			{CollectionID: collID4, Signature: crypto.Signature{0x1}},
    77  		}
    78  		ids1 := flow.GetIDs(set1)
    79  		ids2 := flow.GetIDs(set2)
    80  
    81  		// insert block 1
    82  		err := db.Update(func(tx *badger.Txn) error {
    83  			for _, guarantee := range set1 {
    84  				if err := InsertGuarantee(guarantee.CollectionID, guarantee)(tx); err != nil {
    85  					return err
    86  				}
    87  			}
    88  			if err := IndexPayloadGuarantees(blockID1, ids1)(tx); err != nil {
    89  				return err
    90  			}
    91  			return nil
    92  		})
    93  		require.Nil(t, err)
    94  
    95  		// insert block 2
    96  		err = db.Update(func(tx *badger.Txn) error {
    97  			for _, guarantee := range set2 {
    98  				if err := InsertGuarantee(guarantee.CollectionID, guarantee)(tx); err != nil {
    99  					return err
   100  				}
   101  			}
   102  			if err := IndexPayloadGuarantees(blockID2, ids2)(tx); err != nil {
   103  				return err
   104  			}
   105  			return nil
   106  		})
   107  		require.Nil(t, err)
   108  
   109  		t.Run("should retrieve collections for block", func(t *testing.T) {
   110  			var actual1 []flow.Identifier
   111  			err = db.View(LookupPayloadGuarantees(blockID1, &actual1))
   112  			assert.NoError(t, err)
   113  			assert.ElementsMatch(t, []flow.Identifier{collID1}, actual1)
   114  
   115  			// get block 2
   116  			var actual2 []flow.Identifier
   117  			err = db.View(LookupPayloadGuarantees(blockID2, &actual2))
   118  			assert.NoError(t, err)
   119  			assert.Equal(t, []flow.Identifier{collID2, collID3, collID4}, actual2)
   120  		})
   121  	})
   122  }