github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/storage/badger/cluster_blocks_test.go (about)

     1  package badger
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/dgraph-io/badger/v2"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/module/metrics"
    10  	"github.com/onflow/flow-go/storage/badger/operation"
    11  	"github.com/onflow/flow-go/storage/badger/procedure"
    12  	"github.com/onflow/flow-go/utils/unittest"
    13  )
    14  
    15  func TestClusterBlocksByHeight(t *testing.T) {
    16  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    17  		chain := unittest.ClusterBlockChainFixture(5)
    18  		parent, blocks := chain[0], chain[1:]
    19  
    20  		// add parent as boundary
    21  		err := db.Update(operation.IndexClusterBlockHeight(parent.Header.ChainID, parent.Header.Height, parent.ID()))
    22  		require.NoError(t, err)
    23  
    24  		err = db.Update(operation.InsertClusterFinalizedHeight(parent.Header.ChainID, parent.Header.Height))
    25  		require.NoError(t, err)
    26  
    27  		// store a chain of blocks
    28  		for _, block := range blocks {
    29  			err := db.Update(procedure.InsertClusterBlock(&block))
    30  			require.NoError(t, err)
    31  
    32  			err = db.Update(procedure.FinalizeClusterBlock(block.Header.ID()))
    33  			require.NoError(t, err)
    34  		}
    35  
    36  		clusterBlocks := NewClusterBlocks(
    37  			db,
    38  			blocks[0].Header.ChainID,
    39  			NewHeaders(metrics.NewNoopCollector(), db),
    40  			NewClusterPayloads(metrics.NewNoopCollector(), db),
    41  		)
    42  
    43  		// check if the block can be retrieved by height
    44  		for _, block := range blocks {
    45  			retrievedBlock, err := clusterBlocks.ByHeight(block.Header.Height)
    46  			require.NoError(t, err)
    47  			require.Equal(t, block.ID(), retrievedBlock.ID())
    48  		}
    49  	})
    50  }