-
Notifications
You must be signed in to change notification settings - Fork 175
/
cluster_state.go
66 lines (57 loc) · 1.79 KB
/
cluster_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
package factories
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/module"
clusterkv "github.com/onflow/flow-go/state/cluster/badger"
bstorage "github.com/onflow/flow-go/storage/badger"
)
type ClusterStateFactory struct {
db *badger.DB
metrics module.CacheMetrics
tracer module.Tracer
}
func NewClusterStateFactory(
db *badger.DB,
metrics module.CacheMetrics,
tracer module.Tracer,
) (*ClusterStateFactory, error) {
factory := &ClusterStateFactory{
db: db,
metrics: metrics,
tracer: tracer,
}
return factory, nil
}
func (f *ClusterStateFactory) Create(stateRoot *clusterkv.StateRoot) (
*clusterkv.MutableState,
*bstorage.Headers,
*bstorage.ClusterPayloads,
*bstorage.ClusterBlocks,
error,
) {
headers := bstorage.NewHeaders(f.metrics, f.db)
payloads := bstorage.NewClusterPayloads(f.metrics, f.db)
blocks := bstorage.NewClusterBlocks(f.db, stateRoot.ClusterID(), headers, payloads)
isBootStrapped, err := clusterkv.IsBootstrapped(f.db, stateRoot.ClusterID())
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not check cluster state db: %w", err)
}
var clusterState *clusterkv.State
if isBootStrapped {
clusterState, err = clusterkv.OpenState(f.db, f.tracer, headers, payloads, stateRoot.ClusterID())
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not open cluster state: %w", err)
}
} else {
clusterState, err = clusterkv.Bootstrap(f.db, stateRoot)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not bootstrap cluster state: %w", err)
}
}
mutableState, err := clusterkv.NewMutableState(clusterState, f.tracer, headers, payloads)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could create mutable cluster state: %w", err)
}
return mutableState, headers, payloads, blocks, err
}