github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/finalizedreader/finalizedreader.go (about) 1 package finalizedreader 2 3 import ( 4 "fmt" 5 6 "go.uber.org/atomic" 7 8 "github.com/onflow/flow-go/model/flow" 9 "github.com/onflow/flow-go/state/protocol" 10 "github.com/onflow/flow-go/storage" 11 ) 12 13 type FinalizedReader struct { 14 lastHeight *atomic.Uint64 15 headers storage.Headers 16 } 17 18 var _ protocol.Consumer = (*FinalizedReader)(nil) 19 20 func NewFinalizedReader(headers storage.Headers, lastHeight uint64) *FinalizedReader { 21 return &FinalizedReader{ 22 lastHeight: atomic.NewUint64(lastHeight), 23 headers: headers, 24 } 25 } 26 27 // FinalizedBlockIDAtHeight returns the block ID of the finalized block at the given height. 28 // It return storage.NotFound if the given height has not been finalized yet 29 // any other error returned are exceptions 30 func (r *FinalizedReader) FinalizedBlockIDAtHeight(height uint64) (flow.Identifier, error) { 31 if height > r.lastHeight.Load() { 32 return flow.ZeroID, fmt.Errorf("height not finalized (%v): %w", height, storage.ErrNotFound) 33 } 34 35 finalizedID, err := r.headers.BlockIDByHeight(height) 36 if err != nil { 37 return flow.ZeroID, err 38 } 39 40 return finalizedID, nil 41 } 42 43 // BlockFinalized implements the protocol.Consumer interface, which allows FinalizedReader 44 // to consume finalized blocks from the protocol 45 func (r *FinalizedReader) BlockFinalized(h *flow.Header) { 46 r.lastHeight.Store(h.Height) 47 } 48 49 func (r *FinalizedReader) BlockProcessable(h *flow.Header, qc *flow.QuorumCertificate) { 50 // noop 51 } 52 53 func (r *FinalizedReader) EpochTransition(newEpochCounter uint64, first *flow.Header) { 54 // noop 55 } 56 57 func (r *FinalizedReader) EpochSetupPhaseStarted(currentEpochCounter uint64, first *flow.Header) { 58 // noop 59 } 60 61 func (r *FinalizedReader) EpochCommittedPhaseStarted(currentEpochCounter uint64, first *flow.Header) { 62 // noop 63 } 64 65 func (r *FinalizedReader) EpochEmergencyFallbackTriggered() { 66 // noop 67 }