-
Notifications
You must be signed in to change notification settings - Fork 179
/
state.go
85 lines (72 loc) · 1.71 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
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/cluster"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/badger/operation"
)
type State struct {
db *badger.DB
tracer module.Tracer
clusterID flow.ChainID
headers storage.Headers
payloads storage.ClusterPayloads
}
func NewState(db *badger.DB, tracer module.Tracer, clusterID flow.ChainID, headers storage.Headers, payloads storage.ClusterPayloads) (*State, error) {
state := &State{
db: db,
tracer: tracer,
clusterID: clusterID,
headers: headers,
payloads: payloads,
}
return state, nil
}
func (s *State) Params() cluster.Params {
params := &Params{
state: s,
}
return params
}
func (s *State) Final() cluster.Snapshot {
// get the finalized block ID
var blockID flow.Identifier
err := s.db.View(func(tx *badger.Txn) error {
var boundary uint64
err := operation.RetrieveClusterFinalizedHeight(s.clusterID, &boundary)(tx)
if err != nil {
return fmt.Errorf("could not retrieve finalized boundary: %w", err)
}
err = operation.LookupClusterBlockHeight(s.clusterID, boundary, &blockID)(tx)
if err != nil {
return fmt.Errorf("could not retrieve finalized ID: %w", err)
}
return nil
})
if err != nil {
return &Snapshot{
err: err,
}
}
snapshot := &Snapshot{
state: s,
blockID: blockID,
}
return snapshot
}
func (s *State) AtBlockID(blockID flow.Identifier) cluster.Snapshot {
snapshot := &Snapshot{
state: s,
blockID: blockID,
}
return snapshot
}
func (s *State) Mutate() cluster.Mutator {
mutator := &Mutator{
state: s,
}
return mutator
}