-
Notifications
You must be signed in to change notification settings - Fork 179
/
state.go
120 lines (106 loc) · 2.78 KB
/
state.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package badger
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
type State struct {
metrics module.ComplianceMetrics
tracer module.Tracer
db *badger.DB
headers storage.Headers
seals storage.Seals
index storage.Index
payloads storage.Payloads
blocks storage.Blocks
epoch struct {
setups storage.EpochSetups
commits storage.EpochCommits
statuses storage.EpochStatuses
}
consumer protocol.Consumer
cfg Config
}
// NewState initializes a new state backed by a badger database, applying the
// optional configuration parameters.
func NewState(
metrics module.ComplianceMetrics,
tracer module.Tracer,
db *badger.DB,
headers storage.Headers,
seals storage.Seals,
index storage.Index,
payloads storage.Payloads,
blocks storage.Blocks,
setups storage.EpochSetups,
commits storage.EpochCommits,
statuses storage.EpochStatuses,
consumer protocol.Consumer,
) (*State, error) {
s := &State{
metrics: metrics,
tracer: tracer,
db: db,
headers: headers,
seals: seals,
index: index,
payloads: payloads,
blocks: blocks,
epoch: struct {
setups storage.EpochSetups
commits storage.EpochCommits
statuses storage.EpochStatuses
}{
setups: setups,
commits: commits,
statuses: statuses,
},
consumer: consumer,
cfg: DefaultConfig(),
}
return s, nil
}
func (s *State) Params() protocol.Params {
return &Params{state: s}
}
func (s *State) Sealed() protocol.Snapshot {
// retrieve the latest sealed height
var sealed uint64
err := s.db.View(operation.RetrieveSealedHeight(&sealed))
if err != nil {
return NewInvalidSnapshot(fmt.Errorf("could not retrieve sealed height: %w", err))
}
return s.AtHeight(sealed)
}
func (s *State) Final() protocol.Snapshot {
// retrieve the latest finalized height
var finalized uint64
err := s.db.View(operation.RetrieveFinalizedHeight(&finalized))
if err != nil {
return NewInvalidSnapshot(fmt.Errorf("could not retrieve finalized height: %w", err))
}
return s.AtHeight(finalized)
}
func (s *State) AtHeight(height uint64) protocol.Snapshot {
// retrieve the block ID for the finalized height
var blockID flow.Identifier
err := s.db.View(operation.LookupBlockHeight(height, &blockID))
if err != nil {
return NewInvalidSnapshot(fmt.Errorf("could not look up block by height: %w", err))
}
return s.AtBlockID(blockID)
}
func (s *State) AtBlockID(blockID flow.Identifier) protocol.Snapshot {
return NewSnapshot(s, blockID)
}
func (s *State) Mutate() protocol.Mutator {
m := &Mutator{
state: s,
}
return m
}