-
Notifications
You must be signed in to change notification settings - Fork 175
/
sealed_header_reader.go
55 lines (45 loc) · 1.61 KB
/
sealed_header_reader.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
package jobqueue
import (
"fmt"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
// SealedBlockHeaderReader provides an abstraction for consumers to read blocks as job.
type SealedBlockHeaderReader struct {
state protocol.State
headers storage.Headers
}
// NewSealedBlockHeaderReader creates and returns a SealedBlockHeaderReader.
func NewSealedBlockHeaderReader(state protocol.State, headers storage.Headers) *SealedBlockHeaderReader {
return &SealedBlockHeaderReader{
state: state,
headers: headers,
}
}
// AtIndex returns the block header job at the given index.
// The block header job at an index is just the finalized block header at that index (i.e., height).
func (r SealedBlockHeaderReader) AtIndex(index uint64) (module.Job, error) {
sealed, err := r.Head()
if err != nil {
return nil, fmt.Errorf("could not get last sealed block height: %w", err)
}
if index > sealed {
// return not found error to indicate there is no job available at this height
return nil, fmt.Errorf("block at index %d is not sealed: %w", index, storage.ErrNotFound)
}
header, err := r.headers.ByHeight(index)
if err != nil {
return nil, fmt.Errorf("could not get block by height %d: %w", index, err)
}
// the block at height index is sealed
return BlockHeaderToJob(header), nil
}
// Head returns the last sealed height as job index.
func (r SealedBlockHeaderReader) Head() (uint64, error) {
header, err := r.state.Sealed().Head()
if err != nil {
return 0, fmt.Errorf("could not get header of last sealed block: %w", err)
}
return header.Height, nil
}