github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/state/protocol/protocol_state/epochs/factory.go (about)

     1  package epochs
     2  
     3  import (
     4  	"github.com/onflow/flow-go/model/flow"
     5  	"github.com/onflow/flow-go/state/protocol"
     6  	"github.com/onflow/flow-go/state/protocol/protocol_state"
     7  	"github.com/onflow/flow-go/storage"
     8  )
     9  
    10  // EpochStateMachineFactory is a factory for creating EpochStateMachine instances.
    11  // It holds all the necessary data to create a new instance of EpochStateMachine.
    12  type EpochStateMachineFactory struct {
    13  	params               protocol.GlobalParams
    14  	setups               storage.EpochSetups
    15  	commits              storage.EpochCommits
    16  	epochProtocolStateDB storage.ProtocolState
    17  }
    18  
    19  var _ protocol_state.KeyValueStoreStateMachineFactory = (*EpochStateMachineFactory)(nil)
    20  
    21  func NewEpochStateMachineFactory(
    22  	params protocol.GlobalParams,
    23  	setups storage.EpochSetups,
    24  	commits storage.EpochCommits,
    25  	epochProtocolStateDB storage.ProtocolState,
    26  ) *EpochStateMachineFactory {
    27  	return &EpochStateMachineFactory{
    28  		params:               params,
    29  		setups:               setups,
    30  		commits:              commits,
    31  		epochProtocolStateDB: epochProtocolStateDB,
    32  	}
    33  }
    34  
    35  // Create creates a new instance of an underlying type that operates on KV Store and is created for a specific candidate block.
    36  // No errors are expected during normal operations.
    37  func (f *EpochStateMachineFactory) Create(candidateView uint64, parentBlockID flow.Identifier, parentState protocol.KVStoreReader, mutator protocol_state.KVStoreMutator) (protocol_state.KeyValueStoreStateMachine, error) {
    38  	return NewEpochStateMachine(
    39  		candidateView,
    40  		parentBlockID,
    41  		f.params,
    42  		f.setups,
    43  		f.commits,
    44  		f.epochProtocolStateDB,
    45  		parentState,
    46  		mutator,
    47  		func(candidateView uint64, parentState *flow.RichProtocolStateEntry) (StateMachine, error) {
    48  			return NewHappyPathStateMachine(candidateView, parentState)
    49  		},
    50  		func(candidateView uint64, parentState *flow.RichProtocolStateEntry) (StateMachine, error) {
    51  			return NewFallbackStateMachine(candidateView, parentState), nil
    52  		},
    53  	)
    54  }