github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/dkg/controller_factory.go (about) 1 package dkg 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/crypto" 7 "github.com/rs/zerolog" 8 9 "github.com/onflow/flow-go/model/flow" 10 "github.com/onflow/flow-go/module" 11 "github.com/onflow/flow-go/module/signature" 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 } 25 26 // NewControllerFactory creates a new factory that generates Controllers with 27 // the same underlying Local object, tunnel and dkg smart-contract client. 28 func NewControllerFactory( 29 log zerolog.Logger, 30 me module.Local, 31 dkgContractClients []module.DKGContractClient, 32 tunnel *BrokerTunnel, 33 ) *ControllerFactory { 34 35 return &ControllerFactory{ 36 log: log, 37 me: me, 38 dkgContractClients: dkgContractClients, 39 tunnel: tunnel, 40 } 41 } 42 43 // Create creates a new epoch-specific Controller equipped with a broker which 44 // is capable of communicating with other nodes. 45 func (f *ControllerFactory) Create( 46 dkgInstanceID string, 47 participants flow.IdentitySkeletonList, 48 seed []byte) (module.DKGController, error) { 49 50 myIndex, ok := participants.GetIndex(f.me.NodeID()) 51 if !ok { 52 return nil, fmt.Errorf("failed to create controller factory, node %s is not part of DKG committee", f.me.NodeID().String()) 53 } 54 55 broker := NewBroker( 56 f.log, 57 dkgInstanceID, 58 participants, 59 f.me, 60 int(myIndex), 61 f.dkgContractClients, 62 f.tunnel, 63 ) 64 65 n := len(participants) 66 threshold := signature.RandomBeaconThreshold(n) 67 dkg, err := crypto.NewJointFeldman(n, threshold, int(myIndex), broker) 68 if err != nil { 69 return nil, err 70 } 71 72 controller := NewController( 73 f.log, 74 dkgInstanceID, 75 dkg, 76 seed, 77 broker, 78 ) 79 80 return controller, nil 81 }