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

     1  package operation
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/dgraph-io/badger/v2"
     7  
     8  	"github.com/onflow/flow-go/model/flow"
     9  	"github.com/onflow/flow-go/storage"
    10  )
    11  
    12  func InsertEpochSetup(eventID flow.Identifier, event *flow.EpochSetup) func(*badger.Txn) error {
    13  	return insert(makePrefix(codeEpochSetup, eventID), event)
    14  }
    15  
    16  func RetrieveEpochSetup(eventID flow.Identifier, event *flow.EpochSetup) func(*badger.Txn) error {
    17  	return retrieve(makePrefix(codeEpochSetup, eventID), event)
    18  }
    19  
    20  func InsertEpochCommit(eventID flow.Identifier, event *flow.EpochCommit) func(*badger.Txn) error {
    21  	return insert(makePrefix(codeEpochCommit, eventID), event)
    22  }
    23  
    24  func RetrieveEpochCommit(eventID flow.Identifier, event *flow.EpochCommit) func(*badger.Txn) error {
    25  	return retrieve(makePrefix(codeEpochCommit, eventID), event)
    26  }
    27  
    28  // SetEpochEmergencyFallbackTriggered sets a flag in the DB indicating that
    29  // epoch emergency fallback has been triggered, and the block where it was triggered.
    30  //
    31  // EFM can be triggered in two ways:
    32  //  1. Finalizing the first block past the epoch commitment deadline, when the
    33  //     next epoch has not yet been committed (see protocol.Params for more detail)
    34  //  2. Finalizing a fork in which an invalid service event was incorporated.
    35  //
    36  // Calling this function multiple times is a no-op and returns no expected errors.
    37  func SetEpochEmergencyFallbackTriggered(blockID flow.Identifier) func(txn *badger.Txn) error {
    38  	return SkipDuplicates(insert(makePrefix(codeEpochEmergencyFallbackTriggered), blockID))
    39  }
    40  
    41  // RetrieveEpochEmergencyFallbackTriggeredBlockID gets the block ID where epoch
    42  // emergency was triggered.
    43  func RetrieveEpochEmergencyFallbackTriggeredBlockID(blockID *flow.Identifier) func(*badger.Txn) error {
    44  	return retrieve(makePrefix(codeEpochEmergencyFallbackTriggered), blockID)
    45  }
    46  
    47  // CheckEpochEmergencyFallbackTriggered retrieves the value of the flag
    48  // indicating whether epoch emergency fallback has been triggered. If the key
    49  // is not set, this results in triggered being set to false.
    50  func CheckEpochEmergencyFallbackTriggered(triggered *bool) func(*badger.Txn) error {
    51  	return func(tx *badger.Txn) error {
    52  		var blockID flow.Identifier
    53  		err := RetrieveEpochEmergencyFallbackTriggeredBlockID(&blockID)(tx)
    54  		if errors.Is(err, storage.ErrNotFound) {
    55  			// flag unset, EFM not triggered
    56  			*triggered = false
    57  			return nil
    58  		} else if err != nil {
    59  			// storage error, set triggered to zero value
    60  			*triggered = false
    61  			return err
    62  		}
    63  		// flag is set, EFM triggered
    64  		*triggered = true
    65  		return err
    66  	}
    67  }