github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/store_llr_state.go (about)

     1  package gossip
     2  
     3  import (
     4  	"github.com/unicornultrafoundation/go-helios/native/idx"
     5  	"github.com/unicornultrafoundation/go-u2u/log"
     6  )
     7  
     8  type LlrState struct {
     9  	LowestEpochToDecide idx.Epoch
    10  	LowestEpochToFill   idx.Epoch
    11  
    12  	LowestBlockToDecide idx.Block
    13  	LowestBlockToFill   idx.Block
    14  }
    15  
    16  func (s *Store) setLlrState(llrs LlrState) {
    17  	s.cache.LlrState.Store(&llrs)
    18  }
    19  
    20  func (s *Store) ModifyLlrState(f func(*LlrState)) {
    21  	s.mutex.WriteLlrState.Lock()
    22  	defer s.mutex.WriteLlrState.Unlock()
    23  	llrs := s.GetLlrState()
    24  	f(&llrs)
    25  	s.setLlrState(llrs)
    26  }
    27  
    28  func (s *Store) GetLlrState() LlrState {
    29  	if v := s.cache.LlrState.Load(); v != nil {
    30  		return *v.(*LlrState)
    31  	}
    32  	v, ok := s.rlp.Get(s.table.LlrState, []byte{}, &LlrState{}).(*LlrState)
    33  	if !ok {
    34  		log.Crit("LLR state reading failed: genesis not applied")
    35  	}
    36  	s.cache.LlrState.Store(v)
    37  	return *v
    38  }
    39  
    40  // FlushLlrState stores the LLR state in DB
    41  func (s *Store) FlushLlrState() {
    42  	s.rlp.Set(s.table.LlrState, []byte{}, s.GetLlrState())
    43  }