github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/finalizedreader/finalizedreader_test.go (about)

     1  package finalizedreader
     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 TestFinalizedReader(t *testing.T) {
    19  	unittest.RunWithBadgerDB(t, func(db *badger.DB) {
    20  		// prepare the storage.Headers instance
    21  		metrics := metrics.NewNoopCollector()
    22  		headers := badgerstorage.NewHeaders(metrics, db)
    23  		block := unittest.BlockFixture()
    24  
    25  		// store header
    26  		err := headers.Store(block.Header)
    27  		require.NoError(t, err)
    28  
    29  		// index the header
    30  		err = db.Update(operation.IndexBlockHeight(block.Header.Height, block.ID()))
    31  		require.NoError(t, err)
    32  
    33  		// verify is able to reader the finalized block ID
    34  		reader := NewFinalizedReader(headers, block.Header.Height)
    35  		finalized, err := reader.FinalizedBlockIDAtHeight(block.Header.Height)
    36  		require.NoError(t, err)
    37  		require.Equal(t, block.ID(), finalized)
    38  
    39  		// verify is able to return storage.NotFound when the height is not finalized
    40  		_, err = reader.FinalizedBlockIDAtHeight(block.Header.Height + 1)
    41  		require.Error(t, err)
    42  		require.True(t, errors.Is(err, storage.ErrNotFound), err)
    43  
    44  		// finalize one more block
    45  		block2 := unittest.BlockWithParentFixture(block.Header)
    46  		require.NoError(t, headers.Store(block2.Header))
    47  		err = db.Update(operation.IndexBlockHeight(block2.Header.Height, block2.ID()))
    48  		require.NoError(t, err)
    49  		reader.BlockFinalized(block2.Header)
    50  
    51  		// should be able to retrieve the block
    52  		finalized, err = reader.FinalizedBlockIDAtHeight(block2.Header.Height)
    53  		require.NoError(t, err)
    54  		require.Equal(t, block2.ID(), finalized)
    55  
    56  		// should noop and no panic
    57  		reader.BlockProcessable(block.Header, block2.Header.QuorumCertificate())
    58  	})
    59  }