forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_test.go
217 lines (180 loc) · 7.19 KB
/
engine_test.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
// (c) 2021 Dapper Labs - ALL RIGHTS RESERVED
package sealing
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/engine"
mockconsensus "github.com/onflow/flow-go/engine/consensus/mock"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/module/metrics"
mockmodule "github.com/onflow/flow-go/module/mock"
"github.com/onflow/flow-go/network/channels"
mockprotocol "github.com/onflow/flow-go/state/protocol/mock"
mockstorage "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/utils/unittest"
)
func TestSealingEngineContext(t *testing.T) {
suite.Run(t, new(SealingEngineSuite))
}
type SealingEngineSuite struct {
suite.Suite
core *mockconsensus.SealingCore
state *mockprotocol.State
index *mockstorage.Index
results *mockstorage.ExecutionResults
myID flow.Identifier
// Sealing Engine
engine *Engine
}
func (s *SealingEngineSuite) SetupTest() {
metrics := metrics.NewNoopCollector()
s.core = &mockconsensus.SealingCore{}
s.state = &mockprotocol.State{}
s.index = &mockstorage.Index{}
s.results = &mockstorage.ExecutionResults{}
s.myID = unittest.IdentifierFixture()
me := &mockmodule.Local{}
// set up local module mock
me.On("NodeID").Return(
func() flow.Identifier {
return s.myID
},
)
rootHeader, err := unittest.RootSnapshotFixture(unittest.IdentityListFixture(5, unittest.WithAllRoles())).Head()
require.NoError(s.T(), err)
s.engine = &Engine{
log: unittest.Logger(),
unit: engine.NewUnit(),
core: s.core,
me: me,
engineMetrics: metrics,
cacheMetrics: metrics,
rootHeader: rootHeader,
index: s.index,
results: s.results,
state: s.state,
}
// setup inbound queues for trusted inputs and message handler for untrusted inputs
err = s.engine.setupTrustedInboundQueues()
require.NoError(s.T(), err)
err = s.engine.setupMessageHandler(unittest.NewSealingConfigs(RequiredApprovalsForSealConstructionTestingValue))
require.NoError(s.T(), err)
<-s.engine.Ready()
}
// TestOnFinalizedBlock tests if finalized block gets processed when send through `Engine`.
// Tests the whole processing pipeline.
func (s *SealingEngineSuite) TestOnFinalizedBlock() {
finalizedBlock := unittest.BlockHeaderFixture()
finalizedBlockID := finalizedBlock.ID()
s.state.On("Final").Return(unittest.StateSnapshotForKnownBlock(finalizedBlock, nil))
s.core.On("ProcessFinalizedBlock", finalizedBlockID).Return(nil).Once()
s.engine.OnFinalizedBlock(model.BlockFromFlow(finalizedBlock))
// matching engine has at least 100ms ticks for processing events
time.Sleep(1 * time.Second)
s.core.AssertExpectations(s.T())
}
// TestOnBlockIncorporated tests if incorporated block gets processed when send through `Engine`.
// Tests the whole processing pipeline.
func (s *SealingEngineSuite) TestOnBlockIncorporated() {
parentBlock := unittest.BlockHeaderFixture()
incorporatedBlock := unittest.BlockHeaderWithParentFixture(parentBlock)
incorporatedBlockID := incorporatedBlock.ID()
// setup payload fixture
payload := unittest.PayloadFixture(unittest.WithAllTheFixins)
index := &flow.Index{}
for _, result := range payload.Results {
index.ResultIDs = append(index.ReceiptIDs, result.ID())
s.results.On("ByID", result.ID()).Return(result, nil).Once()
IR := flow.NewIncorporatedResult(parentBlock.ID(), result)
s.core.On("ProcessIncorporatedResult", IR).Return(nil).Once()
}
s.index.On("ByBlockID", parentBlock.ID()).Return(index, nil)
// setup headers storage
headers := &mockstorage.Headers{}
headers.On("ByBlockID", incorporatedBlockID).Return(incorporatedBlock, nil).Once()
s.engine.headers = headers
s.engine.OnBlockIncorporated(model.BlockFromFlow(incorporatedBlock))
// matching engine has at least 100ms ticks for processing events
time.Sleep(1 * time.Second)
s.core.AssertExpectations(s.T())
}
// TestMultipleProcessingItems tests that the engine queues multiple receipts and approvals
// and eventually feeds them into sealing.Core for processing
func (s *SealingEngineSuite) TestMultipleProcessingItems() {
originID := unittest.IdentifierFixture()
block := unittest.BlockFixture()
receipts := make([]*flow.ExecutionReceipt, 20)
for i := range receipts {
receipt := unittest.ExecutionReceiptFixture(
unittest.WithExecutorID(originID),
unittest.WithResult(unittest.ExecutionResultFixture(unittest.WithBlock(&block))),
)
receipts[i] = receipt
}
numApprovalsPerReceipt := 1
approvals := make([]*flow.ResultApproval, 0, len(receipts)*numApprovalsPerReceipt)
responseApprovals := make([]*messages.ApprovalResponse, 0)
approverID := unittest.IdentifierFixture()
for _, receipt := range receipts {
for j := 0; j < numApprovalsPerReceipt; j++ {
approval := unittest.ResultApprovalFixture(unittest.WithExecutionResultID(receipt.ID()),
unittest.WithApproverID(approverID))
responseApproval := &messages.ApprovalResponse{
Approval: *approval,
}
responseApprovals = append(responseApprovals, responseApproval)
approvals = append(approvals, approval)
s.core.On("ProcessApproval", approval).Return(nil).Twice()
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for _, approval := range approvals {
err := s.engine.Process(channels.ReceiveApprovals, approverID, approval)
s.Require().NoError(err, "should process approval")
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for _, approval := range responseApprovals {
err := s.engine.Process(channels.ReceiveApprovals, approverID, approval)
s.Require().NoError(err, "should process approval")
}
}()
wg.Wait()
// sealing engine has at least 100ms ticks for processing events
time.Sleep(1 * time.Second)
s.core.AssertExpectations(s.T())
}
// try to submit an approval where the message origin is inconsistent with the message creator
func (s *SealingEngineSuite) TestApprovalInvalidOrigin() {
// approval from valid origin (i.e. a verification node) but with random ApproverID
originID := unittest.IdentifierFixture()
approval := unittest.ResultApprovalFixture() // with random ApproverID
err := s.engine.Process(channels.ReceiveApprovals, originID, approval)
s.Require().NoError(err, "approval from unknown verifier should be dropped but not error")
// sealing engine has at least 100ms ticks for processing events
time.Sleep(1 * time.Second)
// In both cases, we expect the approval to be rejected without hitting the mempools
s.core.AssertNumberOfCalls(s.T(), "ProcessApproval", 0)
}
// TestProcessUnsupportedMessageType tests that Process and ProcessLocal correctly handle a case where invalid message type
// was submitted from network layer.
func (s *SealingEngineSuite) TestProcessUnsupportedMessageType() {
invalidEvent := uint64(42)
err := s.engine.Process("ch", unittest.IdentifierFixture(), invalidEvent)
// shouldn't result in error since byzantine inputs are expected
require.NoError(s.T(), err)
// in case of local processing error cannot be consumed since all inputs are trusted
err = s.engine.ProcessLocal(invalidEvent)
require.Error(s.T(), err)
require.True(s.T(), engine.IsIncompatibleInputTypeError(err))
}