github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/inmem/initial_protocol_state.go (about) 1 package inmem 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/flow-go/model/flow" 7 "github.com/onflow/flow-go/state/protocol" 8 ) 9 10 // InitialProtocolStateAdapter implements protocol.InitialProtocolState by wrapping a RichProtocolStateEntry. 11 // TODO(yuraolex): for sake of avoiding errors in return values in interface methods this adapter pre-caches 12 // some values. This is debatable as clustering for instance is not accessed frequently and could be lazily loaded. 13 // The problem with lazy loading is handling error value from `inmem.ClusteringFromSetupEvent`. There are two ways to avoid it: 14 // 1. Return error from interface method. 15 // 2. Inject irrecoverable.Signaler into the adapter and panic on error since any error in that method has to be a severe implementation bug. 16 type InitialProtocolStateAdapter struct { 17 *flow.RichProtocolStateEntry 18 } 19 20 var _ protocol.InitialProtocolState = (*InitialProtocolStateAdapter)(nil) 21 22 func NewInitialProtocolStateAdapter(entry *flow.RichProtocolStateEntry) *InitialProtocolStateAdapter { 23 return &InitialProtocolStateAdapter{ 24 RichProtocolStateEntry: entry, 25 } 26 } 27 28 func (s *InitialProtocolStateAdapter) Epoch() uint64 { 29 return s.CurrentEpochSetup.Counter 30 } 31 32 func (s *InitialProtocolStateAdapter) Clustering() (flow.ClusterList, error) { 33 clustering, err := ClusteringFromSetupEvent(s.CurrentEpochSetup) 34 if err != nil { 35 return nil, fmt.Errorf("could not extract cluster list from setup event: %w", err) 36 } 37 return clustering, nil 38 } 39 40 func (s *InitialProtocolStateAdapter) EpochSetup() *flow.EpochSetup { 41 return s.CurrentEpochSetup 42 } 43 44 func (s *InitialProtocolStateAdapter) EpochCommit() *flow.EpochCommit { 45 return s.CurrentEpochCommit 46 } 47 48 func (s *InitialProtocolStateAdapter) DKG() (protocol.DKG, error) { 49 dkg, err := EncodableDKGFromEvents(s.CurrentEpochSetup, s.CurrentEpochCommit) 50 if err != nil { 51 return nil, fmt.Errorf("could not construct encodable DKG from events: %w", err) 52 } 53 54 return NewDKG(dkg), nil 55 } 56 57 // Entry Returns low-level protocol state entry that was used to initialize this object. 58 // It shouldn't be used by high-level logic, it is useful for some cases such as bootstrapping. 59 // Prefer using other methods to access protocol state. 60 func (s *InitialProtocolStateAdapter) Entry() *flow.RichProtocolStateEntry { 61 return s.RichProtocolStateEntry.Copy() 62 }