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