-
Notifications
You must be signed in to change notification settings - Fork 178
/
nodes.go
204 lines (183 loc) · 6.03 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
package mock
import (
"context"
"os"
"testing"
"github.com/dgraph-io/badger/v2"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
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/execution"
"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/execution/state/delta"
"github.com/onflow/flow-go/engine/verification/finder"
"github.com/onflow/flow-go/engine/verification/match"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/ledger"
"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/lifecycle"
"github.com/onflow/flow-go/module/mempool"
"github.com/onflow/flow-go/module/mempool/entity"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/network"
"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"
)
// StateFixture is a test helper struct that encapsulates a flow protocol state
// as well as all of its backend dependencies.
type StateFixture struct {
DB *badger.DB
Storage *storage.All
DBDir string
ProtocolEvents *events.Distributor
State protocol.MutableState
}
// GenericNode implements a generic in-process node for tests.
type GenericNode struct {
Log zerolog.Logger
Metrics *metrics.NoopCollector
Tracer module.Tracer
DB *badger.DB
Headers storage.Headers
Identities storage.Identities
Guarantees storage.Guarantees
Seals storage.Seals
Payloads storage.Payloads
Blocks storage.Blocks
State protocol.MutableState
Index storage.Index
Me module.Local
Net *stub.Network
DBDir string
ChainID flow.ChainID
ProtocolEvents *events.Distributor
}
func (g *GenericNode) Done() {
_ = g.DB.Close()
_ = os.RemoveAll(g.DBDir)
<-g.Tracer.Done()
}
// Closes closes the badger database of the node
func (g *GenericNode) CloseDB() error {
return g.DB.Close()
}
// CollectionNode implements an in-process collection node for tests.
type CollectionNode struct {
GenericNode
Collections storage.Collections
Transactions storage.Transactions
IngestionEngine *collectioningest.Engine
PusherEngine *pusher.Engine
ProviderEngine *provider.Engine
}
// ConsensusNode implements an in-process consensus node for tests.
type ConsensusNode struct {
GenericNode
Guarantees mempool.Guarantees
Approvals mempool.Approvals
Receipts mempool.ExecutionTree
Seals mempool.IncorporatedResultSeals
IngestionEngine *consensusingest.Engine
MatchingEngine *matching.Engine
}
func (cn ConsensusNode) Ready() {
<-cn.IngestionEngine.Ready()
<-cn.MatchingEngine.Ready()
}
func (cn ConsensusNode) Done() {
<-cn.IngestionEngine.Done()
<-cn.MatchingEngine.Done()
}
type ComputerWrap struct {
*computation.Manager
OnComputeBlock func(ctx context.Context, block *entity.ExecutableBlock, view *delta.View)
}
func (c *ComputerWrap) ComputeBlock(
ctx context.Context,
block *entity.ExecutableBlock,
view *delta.View,
) (*execution.ComputationResult, error) {
if c.OnComputeBlock != nil {
c.OnComputeBlock(ctx, block, view)
}
return c.Manager.ComputeBlock(ctx, block, view)
}
// ExecutionNode implements a mocked execution node for tests.
type ExecutionNode struct {
GenericNode
MutableState protocol.MutableState
IngestionEngine *ingestion.Engine
ExecutionEngine *ComputerWrap
RequestEngine *requester.Engine
ReceiptsEngine *executionprovider.Engine
FollowerEngine *followereng.Engine
SyncEngine *synchronization.Engine
BadgerDB *badger.DB
VM *fvm.VirtualMachine
ExecutionState state.ExecutionState
Ledger ledger.Ledger
LevelDbDir string
Collections storage.Collections
Finalizer *consensus.Finalizer
}
func (en ExecutionNode) Ready() {
<-lifecycle.AllReady(
en.Ledger,
en.ReceiptsEngine,
en.IngestionEngine,
en.FollowerEngine,
en.RequestEngine,
en.SyncEngine,
)
}
func (en ExecutionNode) Done() {
lifecycle.AllDone(
en.IngestionEngine,
en.IngestionEngine,
en.ReceiptsEngine,
en.Ledger,
en.FollowerEngine,
en.RequestEngine,
en.SyncEngine,
)
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)
}
// VerificationNode implements an in-process verification node for tests.
type VerificationNode struct {
*GenericNode
CachedReceipts mempool.ReceiptDataPacks
ReadyReceipts mempool.ReceiptDataPacks
PendingReceipts mempool.ReceiptDataPacks
PendingResults mempool.ResultDataPacks
ProcessedResultIDs mempool.Identifiers
DiscardedResultIDs mempool.Identifiers
BlockIDsCache mempool.Identifiers
PendingReceiptIDsByBlock mempool.IdentifierMap
ReceiptIDsByResult mempool.IdentifierMap
ChunkIDsByResult mempool.IdentifierMap
PendingChunks *match.Chunks
VerifierEngine network.Engine
FinderEngine *finder.Engine
MatchEngine network.Engine
}