github.com/onflow/flow-go@v0.33.17/engine/collection/epochmgr/epoch_components.go (about)

     1  package epochmgr
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/onflow/flow-go/consensus/hotstuff"
     7  	"github.com/onflow/flow-go/module"
     8  	"github.com/onflow/flow-go/module/component"
     9  	"github.com/onflow/flow-go/module/irrecoverable"
    10  	"github.com/onflow/flow-go/module/util"
    11  	"github.com/onflow/flow-go/state/cluster"
    12  )
    13  
    14  // EpochComponents represents all dependencies for running an epoch.
    15  type EpochComponents struct {
    16  	*component.ComponentManager
    17  	state             cluster.State
    18  	comp              component.Component
    19  	messageHub        component.Component
    20  	sync              module.ReadyDoneAware
    21  	hotstuff          module.HotStuff
    22  	voteAggregator    hotstuff.VoteAggregator
    23  	timeoutAggregator hotstuff.TimeoutAggregator
    24  }
    25  
    26  var _ component.Component = (*EpochComponents)(nil)
    27  
    28  func NewEpochComponents(
    29  	state cluster.State,
    30  	comp component.Component,
    31  	sync module.ReadyDoneAware,
    32  	hotstuff module.HotStuff,
    33  	voteAggregator hotstuff.VoteAggregator,
    34  	timeoutAggregator hotstuff.TimeoutAggregator,
    35  	messageHub component.Component,
    36  ) *EpochComponents {
    37  	components := &EpochComponents{
    38  		state:             state,
    39  		comp:              comp,
    40  		sync:              sync,
    41  		hotstuff:          hotstuff,
    42  		voteAggregator:    voteAggregator,
    43  		timeoutAggregator: timeoutAggregator,
    44  		messageHub:        messageHub,
    45  	}
    46  
    47  	builder := component.NewComponentManagerBuilder()
    48  	// start new worker that will start child components and wait for them to finish
    49  	builder.AddWorker(func(ctx irrecoverable.SignalerContext, ready component.ReadyFunc) {
    50  		// start components
    51  		voteAggregator.Start(ctx)
    52  		timeoutAggregator.Start(ctx)
    53  		hotstuff.Start(ctx)
    54  		comp.Start(ctx)
    55  		messageHub.Start(ctx)
    56  		// wait until all components start
    57  		<-util.AllReady(
    58  			components.hotstuff,
    59  			components.comp,
    60  			components.sync,
    61  			components.voteAggregator,
    62  			components.timeoutAggregator,
    63  			components.messageHub,
    64  		)
    65  
    66  		// signal that startup has finished, and we are ready to go
    67  		ready()
    68  
    69  		// wait for shutdown to be commenced
    70  		<-ctx.Done()
    71  		// wait for compliance engine and event loop to shut down
    72  		<-util.AllDone(
    73  			components.hotstuff,
    74  			components.comp,
    75  			components.sync,
    76  			components.voteAggregator,
    77  			components.timeoutAggregator,
    78  			components.messageHub,
    79  		)
    80  	})
    81  	components.ComponentManager = builder.Build()
    82  
    83  	return components
    84  }
    85  
    86  // RunningEpochComponents contains all consensus-related components for an epoch
    87  // and the cancel function to stop these components. All components must have been
    88  // started when the RunningEpochComponents is constructed.
    89  type RunningEpochComponents struct {
    90  	*EpochComponents
    91  	cancel context.CancelFunc // used to stop the epoch components
    92  }
    93  
    94  // NewRunningEpochComponents returns a new RunningEpochComponents container for the
    95  // given epoch components. The components must have already been started using some
    96  // context, which the provided cancel function cancels (stopping the epoch components).
    97  func NewRunningEpochComponents(components *EpochComponents, cancel context.CancelFunc) *RunningEpochComponents {
    98  	return &RunningEpochComponents{
    99  		EpochComponents: components,
   100  		cancel:          cancel,
   101  	}
   102  }