github.com/onflow/flow-go@v0.33.17/state/protocol/badger/params.go (about)

     1  package badger
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  	"github.com/onflow/flow-go/state/protocol"
     8  	"github.com/onflow/flow-go/storage/badger/operation"
     9  )
    10  
    11  type Params struct {
    12  	state *State
    13  }
    14  
    15  var _ protocol.Params = (*Params)(nil)
    16  
    17  func (p Params) ChainID() (flow.ChainID, error) {
    18  
    19  	// retrieve root header
    20  	root, err := p.FinalizedRoot()
    21  	if err != nil {
    22  		return "", fmt.Errorf("could not get root: %w", err)
    23  	}
    24  
    25  	return root.ChainID, nil
    26  }
    27  
    28  func (p Params) SporkID() (flow.Identifier, error) {
    29  
    30  	var sporkID flow.Identifier
    31  	err := p.state.db.View(operation.RetrieveSporkID(&sporkID))
    32  	if err != nil {
    33  		return flow.ZeroID, fmt.Errorf("could not get spork id: %w", err)
    34  	}
    35  
    36  	return sporkID, nil
    37  }
    38  
    39  func (p Params) SporkRootBlockHeight() (uint64, error) {
    40  	var sporkRootBlockHeight uint64
    41  	err := p.state.db.View(operation.RetrieveSporkRootBlockHeight(&sporkRootBlockHeight))
    42  	if err != nil {
    43  		return 0, fmt.Errorf("could not get spork root block height: %w", err)
    44  	}
    45  
    46  	return sporkRootBlockHeight, nil
    47  }
    48  
    49  func (p Params) ProtocolVersion() (uint, error) {
    50  
    51  	var version uint
    52  	err := p.state.db.View(operation.RetrieveProtocolVersion(&version))
    53  	if err != nil {
    54  		return 0, fmt.Errorf("could not get protocol version: %w", err)
    55  	}
    56  
    57  	return version, nil
    58  }
    59  
    60  func (p Params) EpochCommitSafetyThreshold() (uint64, error) {
    61  
    62  	var threshold uint64
    63  	err := p.state.db.View(operation.RetrieveEpochCommitSafetyThreshold(&threshold))
    64  	if err != nil {
    65  		return 0, fmt.Errorf("could not get epoch commit safety threshold")
    66  	}
    67  	return threshold, nil
    68  }
    69  
    70  func (p Params) EpochFallbackTriggered() (bool, error) {
    71  	var triggered bool
    72  	err := p.state.db.View(operation.CheckEpochEmergencyFallbackTriggered(&triggered))
    73  	if err != nil {
    74  		return false, fmt.Errorf("could not check epoch fallback triggered: %w", err)
    75  	}
    76  	return triggered, nil
    77  }
    78  
    79  func (p Params) FinalizedRoot() (*flow.Header, error) {
    80  
    81  	// look up root block ID
    82  	var rootID flow.Identifier
    83  	err := p.state.db.View(operation.LookupBlockHeight(p.state.finalizedRootHeight, &rootID))
    84  	if err != nil {
    85  		return nil, fmt.Errorf("could not look up root header: %w", err)
    86  	}
    87  
    88  	// retrieve root header
    89  	header, err := p.state.headers.ByBlockID(rootID)
    90  	if err != nil {
    91  		return nil, fmt.Errorf("could not retrieve root header: %w", err)
    92  	}
    93  
    94  	return header, nil
    95  }
    96  
    97  func (p Params) SealedRoot() (*flow.Header, error) {
    98  	// look up root block ID
    99  	var rootID flow.Identifier
   100  	err := p.state.db.View(operation.LookupBlockHeight(p.state.sealedRootHeight, &rootID))
   101  
   102  	if err != nil {
   103  		return nil, fmt.Errorf("could not look up root header: %w", err)
   104  	}
   105  
   106  	// retrieve root header
   107  	header, err := p.state.headers.ByBlockID(rootID)
   108  	if err != nil {
   109  		return nil, fmt.Errorf("could not retrieve root header: %w", err)
   110  	}
   111  
   112  	return header, nil
   113  }
   114  
   115  func (p Params) Seal() (*flow.Seal, error) {
   116  
   117  	// look up root header
   118  	var rootID flow.Identifier
   119  	err := p.state.db.View(operation.LookupBlockHeight(p.state.finalizedRootHeight, &rootID))
   120  	if err != nil {
   121  		return nil, fmt.Errorf("could not look up root header: %w", err)
   122  	}
   123  
   124  	// retrieve the root seal
   125  	seal, err := p.state.seals.HighestInFork(rootID)
   126  	if err != nil {
   127  		return nil, fmt.Errorf("could not retrieve root seal: %w", err)
   128  	}
   129  
   130  	return seal, nil
   131  }