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