-
Notifications
You must be signed in to change notification settings - Fork 175
/
epoch.go
195 lines (174 loc) · 5.1 KB
/
epoch.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package factories
import (
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/engine/collection/epochmgr"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/component"
"github.com/onflow/flow-go/module/mempool/epochs"
"github.com/onflow/flow-go/state/cluster"
"github.com/onflow/flow-go/state/cluster/badger"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/storage"
)
type EpochComponentsFactory struct {
me module.Local
pools *epochs.TransactionPools
builder *BuilderFactory
state *ClusterStateFactory
hotstuff *HotStuffFactory
compliance *ComplianceEngineFactory
sync *SyncEngineFactory
syncCore *SyncCoreFactory
messageHub *MessageHubFactory
}
var _ epochmgr.EpochComponentsFactory = (*EpochComponentsFactory)(nil)
func NewEpochComponentsFactory(
me module.Local,
pools *epochs.TransactionPools,
builder *BuilderFactory,
state *ClusterStateFactory,
hotstuff *HotStuffFactory,
compliance *ComplianceEngineFactory,
syncCore *SyncCoreFactory,
sync *SyncEngineFactory,
messageHub *MessageHubFactory,
) *EpochComponentsFactory {
factory := &EpochComponentsFactory{
me: me,
pools: pools,
builder: builder,
state: state,
hotstuff: hotstuff,
compliance: compliance,
syncCore: syncCore,
sync: sync,
messageHub: messageHub,
}
return factory
}
func (factory *EpochComponentsFactory) Create(
epoch protocol.Epoch,
) (
state cluster.State,
compliance component.Component,
sync module.ReadyDoneAware,
hotstuff module.HotStuff,
voteAggregator hotstuff.VoteAggregator,
timeoutAggregator hotstuff.TimeoutAggregator,
messageHub component.Component,
err error,
) {
epochCounter, err := epoch.Counter()
if err != nil {
err = fmt.Errorf("could not get epoch counter: %w", err)
return
}
// if we are not an authorized participant in this epoch, return a sentinel
identities, err := epoch.InitialIdentities()
if err != nil {
err = fmt.Errorf("could not get initial identities for epoch: %w", err)
return
}
_, exists := identities.ByNodeID(factory.me.NodeID())
if !exists {
err = fmt.Errorf("%w (node_id=%x, epoch=%d)", epochmgr.ErrNotAuthorizedForEpoch, factory.me.NodeID(), epochCounter)
return
}
// determine this node's cluster for the epoch
clusters, err := epoch.Clustering()
if err != nil {
err = fmt.Errorf("could not get clusters for epoch: %w", err)
return
}
_, clusterIndex, ok := clusters.ByNodeID(factory.me.NodeID())
if !ok {
err = fmt.Errorf("could not find my cluster")
return
}
cluster, err := epoch.Cluster(clusterIndex)
if err != nil {
err = fmt.Errorf("could not get cluster info: %w", err)
return
}
// create the cluster state
var (
headers storage.Headers
payloads storage.ClusterPayloads
blocks storage.ClusterBlocks
)
stateRoot, err := badger.NewStateRoot(cluster.RootBlock(), cluster.RootQC(), cluster.EpochCounter())
if err != nil {
err = fmt.Errorf("could not create valid state root: %w", err)
return
}
var mutableState *badger.MutableState
mutableState, headers, payloads, blocks, err = factory.state.Create(stateRoot)
state = mutableState
if err != nil {
err = fmt.Errorf("could not create cluster state: %w", err)
return
}
// get the transaction pool for the epoch
pool := factory.pools.ForEpoch(epochCounter)
builder, finalizer, err := factory.builder.Create(state, headers, payloads, pool, epochCounter)
if err != nil {
err = fmt.Errorf("could not create builder/finalizer: %w", err)
return
}
hotstuffModules, metrics, err := factory.hotstuff.CreateModules(epoch, cluster, state, headers, payloads, finalizer)
if err != nil {
err = fmt.Errorf("could not create consensus modules: %w", err)
return
}
voteAggregator = hotstuffModules.VoteAggregator
timeoutAggregator = hotstuffModules.TimeoutAggregator
validator := hotstuffModules.Validator
hotstuff, err = factory.hotstuff.Create(
cluster,
state,
metrics,
builder,
headers,
hotstuffModules,
)
if err != nil {
err = fmt.Errorf("could not create hotstuff: %w", err)
return
}
syncCore, err := factory.syncCore.Create(cluster.ChainID())
if err != nil {
err = fmt.Errorf("could not create sync core: %w", err)
return
}
complianceEng, err := factory.compliance.Create(
metrics,
hotstuffModules.Notifier,
mutableState,
headers,
payloads,
syncCore,
hotstuff,
hotstuffModules.VoteAggregator,
hotstuffModules.TimeoutAggregator,
validator,
)
if err != nil {
err = fmt.Errorf("could not create compliance engine: %w", err)
return
}
compliance = complianceEng
hotstuffModules.Notifier.AddOnBlockFinalizedConsumer(complianceEng.OnFinalizedBlock)
sync, err = factory.sync.Create(cluster.Members(), state, blocks, syncCore, complianceEng)
if err != nil {
err = fmt.Errorf("could not create sync engine: %w", err)
return
}
clusterMessageHub, err := factory.messageHub.Create(state, payloads, hotstuff, complianceEng, hotstuffModules)
if err != nil {
err = fmt.Errorf("could not create message hub: %w", err)
}
hotstuffModules.Notifier.AddConsumer(clusterMessageHub)
messageHub = clusterMessageHub
return
}