-
Notifications
You must be signed in to change notification settings - Fork 178
/
epoch.go
155 lines (138 loc) · 3.7 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
package factories
import (
"fmt"
"github.com/onflow/flow-go/engine/collection/epochmgr"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/mempool/epochs"
chainsync "github.com/onflow/flow-go/module/synchronization"
"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
proposal *ProposalEngineFactory
sync *SyncEngineFactory
}
func NewEpochComponentsFactory(
me module.Local,
pools *epochs.TransactionPools,
builder *BuilderFactory,
state *ClusterStateFactory,
hotstuff *HotStuffFactory,
proposal *ProposalEngineFactory,
sync *SyncEngineFactory,
) *EpochComponentsFactory {
factory := &EpochComponentsFactory{
me: me,
pools: pools,
builder: builder,
state: state,
hotstuff: hotstuff,
proposal: proposal,
sync: sync,
}
return factory
}
func (factory *EpochComponentsFactory) Create(
epoch protocol.Epoch,
) (
state cluster.State,
proposal module.Engine,
sync module.Engine,
hotstuff module.HotStuff,
err error,
) {
counter, err := epoch.Counter()
if err != nil {
err = fmt.Errorf("could not get epoch counter: %w", err)
return
}
// if we are not a staked 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.ErrUnstakedForEpoch, factory.me.NodeID(), counter)
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())
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(counter)
builder, finalizer, err := factory.builder.Create(headers, payloads, pool)
if err != nil {
err = fmt.Errorf("could not create builder/finalizer: %w", err)
return
}
proposalEng, err := factory.proposal.Create(mutableState, headers, payloads)
if err != nil {
err = fmt.Errorf("could not create proposal engine: %w", err)
return
}
var syncCore *chainsync.Core
syncCore, sync, err = factory.sync.Create(cluster.Members(), state, blocks, proposalEng)
if err != nil {
err = fmt.Errorf("could not create sync engine: %w", err)
return
}
hotstuff, err = factory.hotstuff.Create(
epoch,
cluster,
state,
headers,
payloads,
builder,
finalizer,
proposalEng,
)
if err != nil {
err = fmt.Errorf("could not create hotstuff: %w", err)
return
}
// attach dependencies to the proposal engine
proposal = proposalEng.
WithHotStuff(hotstuff).
WithSync(syncCore)
return
}