github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/persister/persister.go (about)

     1  package persister
     2  
     3  import (
     4  	"github.com/dgraph-io/badger/v2"
     5  
     6  	"github.com/onflow/flow-go/consensus/hotstuff"
     7  	"github.com/onflow/flow-go/model/flow"
     8  	"github.com/onflow/flow-go/storage/badger/operation"
     9  )
    10  
    11  // Persister can persist relevant information for hotstuff.
    12  // Persister depends on protocol.State root snapshot bootstrapping to set initial values for
    13  // SafetyData and LivenessData. These values must be initialized before first use of Persister.
    14  type Persister struct {
    15  	db      *badger.DB
    16  	chainID flow.ChainID
    17  }
    18  
    19  var _ hotstuff.Persister = (*Persister)(nil)
    20  
    21  // New creates a new Persister using the injected data base to persist
    22  // relevant hotstuff data.
    23  func New(db *badger.DB, chainID flow.ChainID) *Persister {
    24  	p := &Persister{
    25  		db:      db,
    26  		chainID: chainID,
    27  	}
    28  	return p
    29  }
    30  
    31  // GetSafetyData will retrieve last persisted safety data.
    32  // During normal operations, no errors are expected.
    33  func (p *Persister) GetSafetyData() (*hotstuff.SafetyData, error) {
    34  	var safetyData hotstuff.SafetyData
    35  	err := p.db.View(operation.RetrieveSafetyData(p.chainID, &safetyData))
    36  	return &safetyData, err
    37  }
    38  
    39  // GetLivenessData will retrieve last persisted liveness data.
    40  // During normal operations, no errors are expected.
    41  func (p *Persister) GetLivenessData() (*hotstuff.LivenessData, error) {
    42  	var livenessData hotstuff.LivenessData
    43  	err := p.db.View(operation.RetrieveLivenessData(p.chainID, &livenessData))
    44  	return &livenessData, err
    45  }
    46  
    47  // PutSafetyData persists the last safety data.
    48  // During normal operations, no errors are expected.
    49  func (p *Persister) PutSafetyData(safetyData *hotstuff.SafetyData) error {
    50  	return operation.RetryOnConflict(p.db.Update, operation.UpdateSafetyData(p.chainID, safetyData))
    51  }
    52  
    53  // PutLivenessData persists the last liveness data.
    54  // During normal operations, no errors are expected.
    55  func (p *Persister) PutLivenessData(livenessData *hotstuff.LivenessData) error {
    56  	return operation.RetryOnConflict(p.db.Update, operation.UpdateLivenessData(p.chainID, livenessData))
    57  }