github.com/onflow/flow-go@v0.33.17/storage/badger/operation/heights.go (about)

     1  // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
     2  
     3  package operation
     4  
     5  import (
     6  	"github.com/dgraph-io/badger/v2"
     7  )
     8  
     9  func InsertRootHeight(height uint64) func(*badger.Txn) error {
    10  	return insert(makePrefix(codeFinalizedRootHeight), height)
    11  }
    12  
    13  func RetrieveRootHeight(height *uint64) func(*badger.Txn) error {
    14  	return retrieve(makePrefix(codeFinalizedRootHeight), height)
    15  }
    16  
    17  func InsertSealedRootHeight(height uint64) func(*badger.Txn) error {
    18  	return insert(makePrefix(codeSealedRootHeight), height)
    19  }
    20  
    21  func RetrieveSealedRootHeight(height *uint64) func(*badger.Txn) error {
    22  	return retrieve(makePrefix(codeSealedRootHeight), height)
    23  }
    24  
    25  func InsertFinalizedHeight(height uint64) func(*badger.Txn) error {
    26  	return insert(makePrefix(codeFinalizedHeight), height)
    27  }
    28  
    29  func UpdateFinalizedHeight(height uint64) func(*badger.Txn) error {
    30  	return update(makePrefix(codeFinalizedHeight), height)
    31  }
    32  
    33  func RetrieveFinalizedHeight(height *uint64) func(*badger.Txn) error {
    34  	return retrieve(makePrefix(codeFinalizedHeight), height)
    35  }
    36  
    37  func InsertSealedHeight(height uint64) func(*badger.Txn) error {
    38  	return insert(makePrefix(codeSealedHeight), height)
    39  }
    40  
    41  func UpdateSealedHeight(height uint64) func(*badger.Txn) error {
    42  	return update(makePrefix(codeSealedHeight), height)
    43  }
    44  
    45  func RetrieveSealedHeight(height *uint64) func(*badger.Txn) error {
    46  	return retrieve(makePrefix(codeSealedHeight), height)
    47  }
    48  
    49  // InsertEpochFirstHeight inserts the height of the first block in the given epoch.
    50  // The first block of an epoch E is the finalized block with view >= E.FirstView.
    51  // Although we don't store the final height of an epoch, it can be inferred from this index.
    52  // Returns storage.ErrAlreadyExists if the height has already been indexed.
    53  func InsertEpochFirstHeight(epoch, height uint64) func(*badger.Txn) error {
    54  	return insert(makePrefix(codeEpochFirstHeight, epoch), height)
    55  }
    56  
    57  // RetrieveEpochFirstHeight retrieves the height of the first block in the given epoch.
    58  // Returns storage.ErrNotFound if the first block of the epoch has not yet been finalized.
    59  func RetrieveEpochFirstHeight(epoch uint64, height *uint64) func(*badger.Txn) error {
    60  	return retrieve(makePrefix(codeEpochFirstHeight, epoch), height)
    61  }
    62  
    63  // RetrieveEpochLastHeight retrieves the height of the last block in the given epoch.
    64  // It's a more readable, but equivalent query to RetrieveEpochFirstHeight when interested in the last height of an epoch.
    65  // Returns storage.ErrNotFound if the first block of the epoch has not yet been finalized.
    66  func RetrieveEpochLastHeight(epoch uint64, height *uint64) func(*badger.Txn) error {
    67  	var nextEpochFirstHeight uint64
    68  	return func(tx *badger.Txn) error {
    69  		if err := retrieve(makePrefix(codeEpochFirstHeight, epoch+1), &nextEpochFirstHeight)(tx); err != nil {
    70  			return err
    71  		}
    72  		*height = nextEpochFirstHeight - 1
    73  		return nil
    74  	}
    75  }
    76  
    77  // InsertLastCompleteBlockHeightIfNotExists inserts the last full block height if it is not already set.
    78  // Calling this function multiple times is a no-op and returns no expected errors.
    79  func InsertLastCompleteBlockHeightIfNotExists(height uint64) func(*badger.Txn) error {
    80  	return SkipDuplicates(InsertLastCompleteBlockHeight(height))
    81  }
    82  
    83  func InsertLastCompleteBlockHeight(height uint64) func(*badger.Txn) error {
    84  	return insert(makePrefix(codeLastCompleteBlockHeight), height)
    85  }
    86  
    87  func UpdateLastCompleteBlockHeight(height uint64) func(*badger.Txn) error {
    88  	return update(makePrefix(codeLastCompleteBlockHeight), height)
    89  }
    90  
    91  func RetrieveLastCompleteBlockHeight(height *uint64) func(*badger.Txn) error {
    92  	return retrieve(makePrefix(codeLastCompleteBlockHeight), height)
    93  }