-
Notifications
You must be signed in to change notification settings - Fork 175
/
nodes.go
291 lines (259 loc) · 8.9 KB
/
nodes.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package mock
import (
"context"
"os"
"sync"
"testing"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/engine/collection/epochmgr"
collectioningest "github.com/onflow/flow-go/engine/collection/ingest"
"github.com/onflow/flow-go/engine/collection/pusher"
followereng "github.com/onflow/flow-go/engine/common/follower"
"github.com/onflow/flow-go/engine/common/provider"
"github.com/onflow/flow-go/engine/common/requester"
"github.com/onflow/flow-go/engine/common/synchronization"
consensusingest "github.com/onflow/flow-go/engine/consensus/ingestion"
"github.com/onflow/flow-go/engine/consensus/matching"
"github.com/onflow/flow-go/engine/consensus/sealing"
"github.com/onflow/flow-go/engine/execution/computation"
"github.com/onflow/flow-go/engine/execution/ingestion"
executionprovider "github.com/onflow/flow-go/engine/execution/provider"
"github.com/onflow/flow-go/engine/execution/state"
"github.com/onflow/flow-go/engine/verification/assigner"
"github.com/onflow/flow-go/engine/verification/assigner/blockconsumer"
"github.com/onflow/flow-go/engine/verification/fetcher"
"github.com/onflow/flow-go/engine/verification/fetcher/chunkconsumer"
verificationrequester "github.com/onflow/flow-go/engine/verification/requester"
"github.com/onflow/flow-go/engine/verification/verifier"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/complete"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/finalizer/consensus"
"github.com/onflow/flow-go/module/irrecoverable"
"github.com/onflow/flow-go/module/mempool"
epochpool "github.com/onflow/flow-go/module/mempool/epochs"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/module/util"
"github.com/onflow/flow-go/network/stub"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/state/protocol/events"
"github.com/onflow/flow-go/storage"
bstorage "github.com/onflow/flow-go/storage/badger"
"github.com/onflow/flow-go/utils/unittest"
)
// StateFixture is a test helper struct that encapsulates a flow protocol state
// as well as all of its backend dependencies.
type StateFixture struct {
DBDir string
PublicDB *badger.DB
SecretsDB *badger.DB
Storage *storage.All
ProtocolEvents *events.Distributor
State protocol.ParticipantState
}
// GenericNode implements a generic in-process node for tests.
type GenericNode struct {
// context and cancel function used to start/stop components
Ctx irrecoverable.SignalerContext
Cancel context.CancelFunc
Errs <-chan error
Log zerolog.Logger
Metrics *metrics.NoopCollector
Tracer module.Tracer
PublicDB *badger.DB
SecretsDB *badger.DB
Headers storage.Headers
Guarantees storage.Guarantees
Seals storage.Seals
Payloads storage.Payloads
Blocks storage.Blocks
QuorumCertificates storage.QuorumCertificates
State protocol.ParticipantState
Index storage.Index
Me module.Local
Net *stub.Network
DBDir string
ChainID flow.ChainID
ProtocolEvents *events.Distributor
}
func (g *GenericNode) Done() {
_ = g.PublicDB.Close()
_ = os.RemoveAll(g.DBDir)
<-g.Tracer.Done()
}
// RequireGenericNodesDoneBefore invokes the done method of all input generic nodes concurrently, and
// fails the test if any generic node's shutdown takes longer than the specified duration.
func RequireGenericNodesDoneBefore(t testing.TB, duration time.Duration, nodes ...*GenericNode) {
wg := &sync.WaitGroup{}
wg.Add(len(nodes))
for _, node := range nodes {
go func(n *GenericNode) {
n.Done()
wg.Done()
}(node)
}
unittest.RequireReturnsBefore(t, wg.Wait, duration, "failed to shutdown all components on time")
}
// CloseDB closes the badger database of the node
func (g *GenericNode) CloseDB() error {
return g.PublicDB.Close()
}
// CollectionNode implements an in-process collection node for tests.
type CollectionNode struct {
GenericNode
Collections storage.Collections
Transactions storage.Transactions
ClusterPayloads storage.ClusterPayloads
TxPools *epochpool.TransactionPools
Voter module.ClusterRootQCVoter
IngestionEngine *collectioningest.Engine
PusherEngine *pusher.Engine
ProviderEngine *provider.Engine
EpochManagerEngine *epochmgr.Engine
}
func (n CollectionNode) Start(t *testing.T) {
go unittest.FailOnIrrecoverableError(t, n.Ctx.Done(), n.Errs)
n.IngestionEngine.Start(n.Ctx)
n.EpochManagerEngine.Start(n.Ctx)
n.ProviderEngine.Start(n.Ctx)
}
func (n CollectionNode) Ready() <-chan struct{} {
return util.AllReady(
n.PusherEngine,
n.ProviderEngine,
n.IngestionEngine,
n.EpochManagerEngine,
)
}
func (n CollectionNode) Done() <-chan struct{} {
done := make(chan struct{})
go func() {
n.GenericNode.Cancel()
<-util.AllDone(
n.PusherEngine,
n.ProviderEngine,
n.IngestionEngine,
n.EpochManagerEngine,
)
n.GenericNode.Done()
close(done)
}()
return done
}
// ConsensusNode implements an in-process consensus node for tests.
type ConsensusNode struct {
GenericNode
Guarantees mempool.Guarantees
Receipts mempool.ExecutionTree
Seals mempool.IncorporatedResultSeals
IngestionEngine *consensusingest.Engine
SealingEngine *sealing.Engine
MatchingEngine *matching.Engine
}
func (cn ConsensusNode) Ready() {
<-cn.IngestionEngine.Ready()
<-cn.SealingEngine.Ready()
}
func (cn ConsensusNode) Done() {
<-cn.IngestionEngine.Done()
<-cn.SealingEngine.Done()
}
// ExecutionNode implements a mocked execution node for tests.
type ExecutionNode struct {
GenericNode
FollowerState protocol.FollowerState
IngestionEngine *ingestion.Engine
ExecutionEngine *computation.Manager
RequestEngine *requester.Engine
ReceiptsEngine *executionprovider.Engine
FollowerCore module.HotStuffFollower
FollowerEngine *followereng.ComplianceEngine
SyncEngine *synchronization.Engine
Compactor *complete.Compactor
BadgerDB *badger.DB
VM fvm.VM
ExecutionState state.ExecutionState
Ledger ledger.Ledger
LevelDbDir string
Collections storage.Collections
Finalizer *consensus.Finalizer
MyExecutionReceipts storage.MyExecutionReceipts
StorehouseEnabled bool
}
func (en ExecutionNode) Ready(ctx context.Context) {
// TODO: receipt engine has been migrated to the new component interface, hence
// is using Start. Other engines' startup should be refactored once migrated to
// new interface.
irctx, _ := irrecoverable.WithSignaler(ctx)
en.ReceiptsEngine.Start(irctx)
en.FollowerCore.Start(irctx)
en.FollowerEngine.Start(irctx)
en.SyncEngine.Start(irctx)
<-util.AllReady(
en.Ledger,
en.ReceiptsEngine,
en.IngestionEngine,
en.FollowerCore,
en.FollowerEngine,
en.RequestEngine,
en.SyncEngine,
)
}
func (en ExecutionNode) Done(cancelFunc context.CancelFunc) {
// to stop all components running with a component manager.
cancelFunc()
// to stop all (deprecated) ready-done-aware
<-util.AllDone(
en.IngestionEngine,
en.IngestionEngine,
en.ReceiptsEngine,
en.Ledger,
en.FollowerCore,
en.FollowerEngine,
en.RequestEngine,
en.SyncEngine,
en.Compactor,
)
os.RemoveAll(en.LevelDbDir)
en.GenericNode.Done()
}
func (en ExecutionNode) AssertHighestExecutedBlock(t *testing.T, header *flow.Header) {
height, blockID, err := en.ExecutionState.GetHighestExecutedBlockID(context.Background())
require.NoError(t, err)
require.Equal(t, header.ID(), blockID)
require.Equal(t, header.Height, height)
}
func (en ExecutionNode) AssertBlockIsExecuted(t *testing.T, header *flow.Header) {
executed, err := en.ExecutionState.IsBlockExecuted(header.Height, header.ID())
require.NoError(t, err)
require.True(t, executed)
}
func (en ExecutionNode) AssertBlockNotExecuted(t *testing.T, header *flow.Header) {
executed, err := en.ExecutionState.IsBlockExecuted(header.Height, header.ID())
require.NoError(t, err)
require.False(t, executed)
}
// VerificationNode implements an in-process verification node for tests.
type VerificationNode struct {
*GenericNode
ChunkStatuses mempool.ChunkStatuses
ChunkRequests mempool.ChunkRequests
Results storage.ExecutionResults
Receipts storage.ExecutionReceipts
// chunk consumer and processor for fetcher engine
ProcessedChunkIndex storage.ConsumerProgress
ChunksQueue *bstorage.ChunksQueue
ChunkConsumer *chunkconsumer.ChunkConsumer
// block consumer for chunk consumer
ProcessedBlockHeight storage.ConsumerProgress
BlockConsumer *blockconsumer.BlockConsumer
VerifierEngine *verifier.Engine
AssignerEngine *assigner.Engine
FetcherEngine *fetcher.Engine
RequesterEngine *verificationrequester.Engine
}