github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/timeoutcollector/factory.go (about)

     1  package timeoutcollector
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog"
     7  
     8  	"github.com/onflow/flow-go/consensus/hotstuff"
     9  )
    10  
    11  // TimeoutCollectorFactory implements hotstuff.TimeoutCollectorFactory, it is responsible for creating timeout collector
    12  // for given view.
    13  type TimeoutCollectorFactory struct {
    14  	log              zerolog.Logger
    15  	notifier         hotstuff.TimeoutAggregationConsumer
    16  	processorFactory hotstuff.TimeoutProcessorFactory
    17  }
    18  
    19  var _ hotstuff.TimeoutCollectorFactory = (*TimeoutCollectorFactory)(nil)
    20  
    21  // NewTimeoutCollectorFactory creates new instance of TimeoutCollectorFactory.
    22  // No error returns are expected during normal operations.
    23  func NewTimeoutCollectorFactory(log zerolog.Logger,
    24  	notifier hotstuff.TimeoutAggregationConsumer,
    25  	createProcessor hotstuff.TimeoutProcessorFactory,
    26  ) *TimeoutCollectorFactory {
    27  	return &TimeoutCollectorFactory{
    28  		log:              log,
    29  		notifier:         notifier,
    30  		processorFactory: createProcessor,
    31  	}
    32  }
    33  
    34  // Create is a factory method to generate a TimeoutCollector for a given view
    35  // Expected error returns during normal operations:
    36  //   - model.ErrViewForUnknownEpoch if view is not yet pruned but no epoch containing the given view is known
    37  //
    38  // All other errors should be treated as exceptions.
    39  func (f *TimeoutCollectorFactory) Create(view uint64) (hotstuff.TimeoutCollector, error) {
    40  	processor, err := f.processorFactory.Create(view)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("could not create TimeoutProcessor at view %d: %w", view, err)
    43  	}
    44  	return NewTimeoutCollector(f.log, view, f.notifier, processor), nil
    45  }
    46  
    47  // TimeoutProcessorFactory implements hotstuff.TimeoutProcessorFactory, it is responsible for creating timeout processor
    48  // for given view.
    49  type TimeoutProcessorFactory struct {
    50  	log                 zerolog.Logger
    51  	committee           hotstuff.Replicas
    52  	notifier            hotstuff.TimeoutCollectorConsumer
    53  	validator           hotstuff.Validator
    54  	domainSeparationTag string
    55  }
    56  
    57  var _ hotstuff.TimeoutProcessorFactory = (*TimeoutProcessorFactory)(nil)
    58  
    59  // NewTimeoutProcessorFactory creates new instance of TimeoutProcessorFactory.
    60  // No error returns are expected during normal operations.
    61  func NewTimeoutProcessorFactory(
    62  	log zerolog.Logger,
    63  	notifier hotstuff.TimeoutCollectorConsumer,
    64  	committee hotstuff.Replicas,
    65  	validator hotstuff.Validator,
    66  	domainSeparationTag string,
    67  ) *TimeoutProcessorFactory {
    68  	return &TimeoutProcessorFactory{
    69  		log:                 log,
    70  		committee:           committee,
    71  		notifier:            notifier,
    72  		validator:           validator,
    73  		domainSeparationTag: domainSeparationTag,
    74  	}
    75  }
    76  
    77  // Create is a factory method to generate a TimeoutProcessor for a given view
    78  // Expected error returns during normal operations:
    79  //   - model.ErrViewForUnknownEpoch no epoch containing the given view is known
    80  //
    81  // All other errors should be treated as exceptions.
    82  func (f *TimeoutProcessorFactory) Create(view uint64) (hotstuff.TimeoutProcessor, error) {
    83  	allParticipants, err := f.committee.IdentitiesByEpoch(view)
    84  	if err != nil {
    85  		return nil, fmt.Errorf("error retrieving consensus participants: %w", err)
    86  	}
    87  
    88  	sigAggregator, err := NewTimeoutSignatureAggregator(view, allParticipants, f.domainSeparationTag)
    89  	if err != nil {
    90  		return nil, fmt.Errorf("could not create TimeoutSignatureAggregator at view %d: %w", view, err)
    91  	}
    92  
    93  	return NewTimeoutProcessor(f.log, f.committee, f.validator, sigAggregator, f.notifier)
    94  }