-
Notifications
You must be signed in to change notification settings - Fork 179
/
finalized_reader.go
73 lines (60 loc) · 2.07 KB
/
finalized_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package testutil
import (
"fmt"
"go.uber.org/atomic"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/utils/unittest"
)
type MockFinalizedReader struct {
headerByHeight map[uint64]*flow.Header
blockByHeight map[uint64]*flow.Block
lowest uint64
highest uint64
finalizedHeight *atomic.Uint64
finalizedCalled *atomic.Int64
}
func NewMockFinalizedReader(initHeight uint64, count int) (*MockFinalizedReader, map[uint64]*flow.Header, uint64) {
root := unittest.BlockHeaderFixture(unittest.WithHeaderHeight(initHeight))
blocks := unittest.ChainFixtureFrom(count, root)
headerByHeight := make(map[uint64]*flow.Header, len(blocks)+1)
headerByHeight[root.Height] = root
blockByHeight := make(map[uint64]*flow.Block, len(blocks)+1)
for _, b := range blocks {
headerByHeight[b.Header.Height] = b.Header
blockByHeight[b.Header.Height] = b
}
highest := blocks[len(blocks)-1].Header.Height
return &MockFinalizedReader{
headerByHeight: headerByHeight,
blockByHeight: blockByHeight,
lowest: initHeight,
highest: highest,
finalizedHeight: atomic.NewUint64(initHeight),
finalizedCalled: atomic.NewInt64(0),
}, headerByHeight, highest
}
func (r *MockFinalizedReader) FinalizedBlockIDAtHeight(height uint64) (flow.Identifier, error) {
r.finalizedCalled.Add(1)
finalized := r.finalizedHeight.Load()
if height > finalized {
return flow.Identifier{}, storage.ErrNotFound
}
if height < r.lowest {
return flow.ZeroID, fmt.Errorf("height %d is out of range [%d, %d]", height, r.lowest, r.highest)
}
return r.headerByHeight[height].ID(), nil
}
func (r *MockFinalizedReader) MockFinal(height uint64) error {
if height < r.lowest || height > r.highest {
return fmt.Errorf("height %d is out of range [%d, %d]", height, r.lowest, r.highest)
}
r.finalizedHeight.Store(height)
return nil
}
func (r *MockFinalizedReader) BlockAtHeight(height uint64) *flow.Block {
return r.blockByHeight[height]
}
func (r *MockFinalizedReader) FinalizedCalled() int {
return int(r.finalizedCalled.Load())
}