github.com/koko1123/flow-go-1@v0.29.6/module/dkg/controller_factory.go (about)

     1  package dkg
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rs/zerolog"
     7  
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/module"
    10  	"github.com/koko1123/flow-go-1/module/signature"
    11  	"github.com/onflow/flow-go/crypto"
    12  )
    13  
    14  // ControllerFactory is a factory object that creates new Controllers for new
    15  // epochs. Each Controller produced by a factory shares the same underlying
    16  // Local object to sign broadcast messages, the same tunnel tying it to the
    17  // MessagingEngine, and the same client to communicate with the DKG
    18  // smart-contract.
    19  type ControllerFactory struct {
    20  	log                zerolog.Logger
    21  	me                 module.Local
    22  	dkgContractClients []module.DKGContractClient
    23  	tunnel             *BrokerTunnel
    24  	config             ControllerConfig
    25  }
    26  
    27  // NewControllerFactory creates a new factory that generates Controllers with
    28  // the same underlying Local object, tunnel and dkg smart-contract client.
    29  func NewControllerFactory(
    30  	log zerolog.Logger,
    31  	me module.Local,
    32  	dkgContractClients []module.DKGContractClient,
    33  	tunnel *BrokerTunnel,
    34  	config ControllerConfig) *ControllerFactory {
    35  
    36  	return &ControllerFactory{
    37  		log:                log,
    38  		me:                 me,
    39  		dkgContractClients: dkgContractClients,
    40  		tunnel:             tunnel,
    41  		config:             config,
    42  	}
    43  }
    44  
    45  // Create creates a new epoch-specific Controller equipped with a broker which
    46  // is capable of communicating with other nodes.
    47  func (f *ControllerFactory) Create(
    48  	dkgInstanceID string,
    49  	participants flow.IdentityList,
    50  	seed []byte) (module.DKGController, error) {
    51  
    52  	myIndex, ok := participants.GetIndex(f.me.NodeID())
    53  	if !ok {
    54  		return nil, fmt.Errorf("failed to create controller factory, node %s is not part of DKG committee", f.me.NodeID().String())
    55  	}
    56  
    57  	broker := NewBroker(
    58  		f.log,
    59  		dkgInstanceID,
    60  		participants,
    61  		f.me,
    62  		int(myIndex),
    63  		f.dkgContractClients,
    64  		f.tunnel,
    65  	)
    66  
    67  	n := len(participants)
    68  	threshold := signature.RandomBeaconThreshold(n)
    69  	dkg, err := crypto.NewJointFeldman(n, threshold, int(myIndex), broker)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	controller := NewController(
    75  		f.log,
    76  		dkgInstanceID,
    77  		dkg,
    78  		seed,
    79  		broker,
    80  		f.config,
    81  	)
    82  
    83  	return controller, nil
    84  }