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

     1  package operation
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/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  func InsertEpochStatus(blockID flow.Identifier, status *flow.EpochStatus) func(*badger.Txn) error {
    29  	return insert(makePrefix(codeBlockEpochStatus, blockID), status)
    30  }
    31  
    32  func RetrieveEpochStatus(blockID flow.Identifier, status *flow.EpochStatus) func(*badger.Txn) error {
    33  	return retrieve(makePrefix(codeBlockEpochStatus, blockID), status)
    34  }
    35  
    36  // SetEpochEmergencyFallbackTriggered sets a flag in the DB indicating that
    37  // epoch emergency fallback has been triggered, and the block where it was triggered.
    38  // EECC can be triggered by 2 blocks:
    39  //
    40  // 1. The first block of a new epoch, when that epoch has not been set up.
    41  // 2. The block where an invalid service event is being applied to the state.
    42  //
    43  // Calling this function multiple times is a no-op and returns no expected errors.
    44  func SetEpochEmergencyFallbackTriggered(blockID flow.Identifier) func(txn *badger.Txn) error {
    45  	return SkipDuplicates(insert(makePrefix(codeEpochEmergencyFallbackTriggered), blockID))
    46  }
    47  
    48  // RetrieveEpochEmergencyFallbackTriggeredBlockID gets the block ID where epoch
    49  // emergency was triggered.
    50  func RetrieveEpochEmergencyFallbackTriggeredBlockID(blockID *flow.Identifier) func(*badger.Txn) error {
    51  	return retrieve(makePrefix(codeEpochEmergencyFallbackTriggered), blockID)
    52  }
    53  
    54  // CheckEpochEmergencyFallbackTriggered retrieves the value of the flag
    55  // indicating whether epoch emergency fallback has been triggered. If the key
    56  // is not set, this results in triggered being set to false.
    57  func CheckEpochEmergencyFallbackTriggered(triggered *bool) func(*badger.Txn) error {
    58  	return func(tx *badger.Txn) error {
    59  		var blockID flow.Identifier
    60  		err := RetrieveEpochEmergencyFallbackTriggeredBlockID(&blockID)(tx)
    61  		if errors.Is(err, storage.ErrNotFound) {
    62  			// flag unset, EECC not triggered
    63  			*triggered = false
    64  			return nil
    65  		} else if err != nil {
    66  			// storage error, set triggered to zero value
    67  			*triggered = false
    68  			return err
    69  		}
    70  		// flag is set, EECC triggered
    71  		*triggered = true
    72  		return err
    73  	}
    74  }