github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/collection/epochmgr/factories/cluster_state.go (about) 1 package factories 2 3 import ( 4 "fmt" 5 6 "github.com/dgraph-io/badger/v2" 7 8 "github.com/onflow/flow-go/module" 9 clusterkv "github.com/onflow/flow-go/state/cluster/badger" 10 bstorage "github.com/onflow/flow-go/storage/badger" 11 ) 12 13 type ClusterStateFactory struct { 14 db *badger.DB 15 metrics module.CacheMetrics 16 tracer module.Tracer 17 } 18 19 func NewClusterStateFactory( 20 db *badger.DB, 21 metrics module.CacheMetrics, 22 tracer module.Tracer, 23 ) (*ClusterStateFactory, error) { 24 factory := &ClusterStateFactory{ 25 db: db, 26 metrics: metrics, 27 tracer: tracer, 28 } 29 return factory, nil 30 } 31 32 func (f *ClusterStateFactory) Create(stateRoot *clusterkv.StateRoot) ( 33 *clusterkv.MutableState, 34 *bstorage.Headers, 35 *bstorage.ClusterPayloads, 36 *bstorage.ClusterBlocks, 37 error, 38 ) { 39 40 headers := bstorage.NewHeaders(f.metrics, f.db) 41 payloads := bstorage.NewClusterPayloads(f.metrics, f.db) 42 blocks := bstorage.NewClusterBlocks(f.db, stateRoot.ClusterID(), headers, payloads) 43 44 isBootStrapped, err := clusterkv.IsBootstrapped(f.db, stateRoot.ClusterID()) 45 if err != nil { 46 return nil, nil, nil, nil, fmt.Errorf("could not check cluster state db: %w", err) 47 } 48 var clusterState *clusterkv.State 49 if isBootStrapped { 50 clusterState, err = clusterkv.OpenState(f.db, f.tracer, headers, payloads, stateRoot.ClusterID(), stateRoot.EpochCounter()) 51 if err != nil { 52 return nil, nil, nil, nil, fmt.Errorf("could not open cluster state: %w", err) 53 } 54 } else { 55 clusterState, err = clusterkv.Bootstrap(f.db, stateRoot) 56 if err != nil { 57 return nil, nil, nil, nil, fmt.Errorf("could not bootstrap cluster state: %w", err) 58 } 59 } 60 61 mutableState, err := clusterkv.NewMutableState(clusterState, f.tracer, headers, payloads) 62 if err != nil { 63 return nil, nil, nil, nil, fmt.Errorf("could create mutable cluster state: %w", err) 64 } 65 return mutableState, headers, payloads, blocks, err 66 }