forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution_test.go
554 lines (458 loc) · 19 KB
/
execution_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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package execution_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/vmihailenco/msgpack"
"github.com/onflow/flow-go/engine"
execTestutil "github.com/onflow/flow-go/engine/execution/testutil"
"github.com/onflow/flow-go/engine/testutil"
testmock "github.com/onflow/flow-go/engine/testutil/mock"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/network/mocknetwork"
"github.com/onflow/flow-go/network/stub"
"github.com/onflow/flow-go/utils/unittest"
)
func sendBlock(exeNode *testmock.ExecutionNode, from flow.Identifier, proposal *messages.BlockProposal) error {
return exeNode.FollowerEngine.Process(engine.ReceiveBlocks, from, proposal)
}
// Test when the ingestion engine receives a block, it will
// request collections from collection node, and send ER to
// verification node and consensus node.
// create a block that has two collections: col1 and col2;
// col1 has tx1 and tx2, col2 has tx3 and tx4.
// create another child block which will trigger the parent
// block to valid and be passed to the ingestion engine
func TestExecutionFlow(t *testing.T) {
hub := stub.NewNetworkHub()
chainID := flow.Testnet
colID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleCollection),
unittest.WithKeys,
)
conID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleConsensus),
unittest.WithKeys,
)
exeID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleExecution),
unittest.WithKeys,
)
verID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleVerification),
unittest.WithKeys,
)
identities := unittest.CompleteIdentitySet(colID, conID, exeID, verID)
// create execution node
exeNode := testutil.ExecutionNode(t, hub, exeID, identities, 21, chainID)
exeNode.Ready()
defer exeNode.Done()
genesis, err := exeNode.State.AtHeight(0).Head()
require.NoError(t, err)
tx1 := flow.TransactionBody{
Script: []byte("transaction { execute { log(1) } }"),
}
tx2 := flow.TransactionBody{
Script: []byte("transaction { execute { log(2) } }"),
}
tx3 := flow.TransactionBody{
Script: []byte("transaction { execute { log(3) } }"),
}
tx4 := flow.TransactionBody{
Script: []byte("transaction { execute { log(4) } }"),
}
col1 := flow.Collection{Transactions: []*flow.TransactionBody{&tx1, &tx2}}
col2 := flow.Collection{Transactions: []*flow.TransactionBody{&tx3, &tx4}}
collections := map[flow.Identifier]*flow.Collection{
col1.ID(): &col1,
col2.ID(): &col2,
}
block := unittest.BlockWithParentAndProposerFixture(genesis, conID.NodeID)
block.SetPayload(flow.Payload{
Guarantees: []*flow.CollectionGuarantee{
{
CollectionID: col1.ID(),
SignerIDs: []flow.Identifier{colID.NodeID},
ReferenceBlockID: genesis.ID(),
},
{
CollectionID: col2.ID(),
SignerIDs: []flow.Identifier{colID.NodeID},
ReferenceBlockID: genesis.ID(),
},
},
})
child := unittest.BlockWithParentAndProposerFixture(block.Header, conID.NodeID)
collectionNode := testutil.GenericNodeFromParticipants(t, hub, colID, identities, chainID)
defer collectionNode.Done()
verificationNode := testutil.GenericNodeFromParticipants(t, hub, verID, identities, chainID)
defer verificationNode.Done()
consensusNode := testutil.GenericNodeFromParticipants(t, hub, conID, identities, chainID)
defer consensusNode.Done()
// create collection node that can respond collections to execution node
// check collection node received the collection request from execution node
providerEngine := new(mocknetwork.Engine)
provConduit, _ := collectionNode.Net.Register(engine.ProvideCollections, providerEngine)
providerEngine.On("Submit", mock.AnythingOfType("network.Channel"), exeID.NodeID, mock.Anything).
Run(func(args mock.Arguments) {
originID := args.Get(1).(flow.Identifier)
req := args.Get(2).(*messages.EntityRequest)
var entities []flow.Entity
for _, entityID := range req.EntityIDs {
coll, exists := collections[entityID]
require.True(t, exists)
entities = append(entities, coll)
}
var blobs [][]byte
for _, entity := range entities {
blob, _ := msgpack.Marshal(entity)
blobs = append(blobs, blob)
}
res := &messages.EntityResponse{
Nonce: req.Nonce,
EntityIDs: req.EntityIDs,
Blobs: blobs,
}
err := provConduit.Publish(res, originID)
assert.NoError(t, err)
}).
Once().
Return(nil)
var receipt *flow.ExecutionReceipt
// create verification engine that can create approvals and send to consensus nodes
// check the verification engine received the ER from execution node
verificationEngine := new(mocknetwork.Engine)
_, _ = verificationNode.Net.Register(engine.ReceiveReceipts, verificationEngine)
verificationEngine.On("Submit", mock.AnythingOfType("network.Channel"), exeID.NodeID, mock.Anything).
Run(func(args mock.Arguments) {
receipt, _ = args[2].(*flow.ExecutionReceipt)
assert.Equal(t, block.ID(), receipt.ExecutionResult.BlockID)
}).
Return(nil).
Once()
// create consensus engine that accepts the result
// check the consensus engine has received the result from execution node
consensusEngine := new(mocknetwork.Engine)
_, _ = consensusNode.Net.Register(engine.ReceiveReceipts, consensusEngine)
consensusEngine.On("Submit", mock.AnythingOfType("network.Channel"), exeID.NodeID, mock.Anything).
Run(func(args mock.Arguments) {
receipt, _ = args[2].(*flow.ExecutionReceipt)
assert.Equal(t, block.ID(), receipt.ExecutionResult.BlockID)
assert.Equal(t, len(collections), len(receipt.ExecutionResult.Chunks)-1) // don't count system chunk
for i, chunk := range receipt.ExecutionResult.Chunks {
assert.EqualValues(t, i, chunk.CollectionIndex)
}
}).
Return(nil).
Once()
// submit block from consensus node
err = sendBlock(&exeNode, conID.NodeID, unittest.ProposalFromBlock(&block))
require.NoError(t, err)
// submit the child block from consensus node, which trigger the parent block
// to be passed to BlockProcessable
err = sendBlock(&exeNode, conID.NodeID, unittest.ProposalFromBlock(&child))
require.NoError(t, err)
require.Eventually(t, func() bool {
// when sendBlock returned, ingestion engine might not have processed
// the block yet, because the process is async. we have to wait
hub.DeliverAll()
return receipt != nil
}, time.Second*10, time.Millisecond*500)
// check that the block has been executed.
exeNode.AssertHighestExecutedBlock(t, block.Header)
myReceipt, err := exeNode.MyExecutionReceipts.MyReceipt(block.ID())
require.NoError(t, err)
require.NotNil(t, myReceipt)
require.Equal(t, exeNode.Me.NodeID(), myReceipt.ExecutorID)
providerEngine.AssertExpectations(t)
verificationEngine.AssertExpectations(t)
consensusEngine.AssertExpectations(t)
}
func deployContractBlock(t *testing.T, conID *flow.Identity, colID *flow.Identity, chain flow.Chain, seq uint64, parent *flow.Header, ref *flow.Header) (
*flow.TransactionBody, *flow.Collection, flow.Block, *messages.BlockProposal, uint64) {
// make tx
tx := execTestutil.DeployCounterContractTransaction(chain.ServiceAddress(), chain)
err := execTestutil.SignTransactionAsServiceAccount(tx, seq, chain)
require.NoError(t, err)
// make collection
col := &flow.Collection{Transactions: []*flow.TransactionBody{tx}}
// make block
block := unittest.BlockWithParentAndProposerFixture(parent, conID.NodeID)
block.SetPayload(flow.Payload{
Guarantees: []*flow.CollectionGuarantee{
{
CollectionID: col.ID(),
SignerIDs: []flow.Identifier{colID.NodeID},
ReferenceBlockID: ref.ID(),
},
},
})
// make proposal
proposal := unittest.ProposalFromBlock(&block)
return tx, col, block, proposal, seq + 1
}
func makePanicBlock(t *testing.T, conID *flow.Identity, colID *flow.Identity, chain flow.Chain, seq uint64, parent *flow.Header, ref *flow.Header) (
*flow.TransactionBody, *flow.Collection, flow.Block, *messages.BlockProposal, uint64) {
// make tx
tx := execTestutil.CreateCounterPanicTransaction(chain.ServiceAddress(), chain.ServiceAddress())
err := execTestutil.SignTransactionAsServiceAccount(tx, seq, chain)
require.NoError(t, err)
// make collection
col := &flow.Collection{Transactions: []*flow.TransactionBody{tx}}
// make block
block := unittest.BlockWithParentAndProposerFixture(parent, conID.NodeID)
block.SetPayload(flow.Payload{
Guarantees: []*flow.CollectionGuarantee{
{CollectionID: col.ID(), SignerIDs: []flow.Identifier{colID.NodeID}, ReferenceBlockID: ref.ID()},
},
})
proposal := unittest.ProposalFromBlock(&block)
return tx, col, block, proposal, seq + 1
}
func makeSuccessBlock(t *testing.T, conID *flow.Identity, colID *flow.Identity, chain flow.Chain, seq uint64, parent *flow.Header, ref *flow.Header) (
*flow.TransactionBody, *flow.Collection, flow.Block, *messages.BlockProposal, uint64) {
tx := execTestutil.AddToCounterTransaction(chain.ServiceAddress(), chain.ServiceAddress())
err := execTestutil.SignTransactionAsServiceAccount(tx, seq, chain)
require.NoError(t, err)
col := &flow.Collection{Transactions: []*flow.TransactionBody{tx}}
block := unittest.BlockWithParentAndProposerFixture(parent, conID.NodeID)
block.SetPayload(flow.Payload{
Guarantees: []*flow.CollectionGuarantee{
{CollectionID: col.ID(), SignerIDs: []flow.Identifier{colID.NodeID}, ReferenceBlockID: ref.ID()},
},
})
proposal := unittest.ProposalFromBlock(&block)
return tx, col, block, proposal, seq + 1
}
// Test the following behaviors:
// (1) ENs sync statecommitment with each other
// (2) a failed transaction will not change statecommitment
//
// We prepare 3 transactions in 3 blocks:
// tx1 will deploy a contract
// tx2 will always panic
// tx3 will be succeed and change statecommitment
// and then create 2 EN nodes, both have tx1 executed. To test the synchronization,
// we send tx2 and tx3 in 2 blocks to only EN1, and check that tx2 will not change statecommitment for
// verifying behavior (1);
// and check EN2 should have the same statecommitment as EN1 since they sync
// with each other for verifying behavior (2).
// TODO: state sync is disabled, we are only verifying 2) for now.
func TestExecutionStateSyncMultipleExecutionNodes(t *testing.T) {
hub := stub.NewNetworkHub()
chainID := flow.Emulator
colID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleCollection),
unittest.WithKeys,
)
conID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleConsensus),
unittest.WithKeys,
)
exe1ID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleExecution),
unittest.WithKeys,
)
identities := unittest.CompleteIdentitySet(colID, conID, exe1ID)
key := unittest.NetworkingPrivKeyFixture()
identities[3].NetworkPubKey = key.PublicKey()
collectionNode := testutil.GenericNodeFromParticipants(t, hub, colID, identities, chainID)
defer collectionNode.Done()
consensusNode := testutil.GenericNodeFromParticipants(t, hub, conID, identities, chainID)
defer consensusNode.Done()
exe1Node := testutil.ExecutionNode(t, hub, exe1ID, identities, 27, chainID)
exe1Node.Ready()
defer exe1Node.Done()
genesis, err := exe1Node.State.AtHeight(0).Head()
require.NoError(t, err)
seq := uint64(0)
chain := exe1Node.ChainID.Chain()
// transaction that will change state and succeed, used to test that state commitment changes
// genesis <- block1 [tx1] <- block2 [tx2] <- block3 [tx3] <- child
_, col1, block1, proposal1, seq := deployContractBlock(t, conID, colID, chain, seq, genesis, genesis)
// we don't set the proper sequence number of this one
_, col2, block2, proposal2, _ := makePanicBlock(t, conID, colID, chain, uint64(0), block1.Header, genesis)
_, col3, block3, proposal3, seq := makeSuccessBlock(t, conID, colID, chain, seq, block2.Header, genesis)
_, _, _, proposal4, _ := makeSuccessBlock(t, conID, colID, chain, seq, block3.Header, genesis)
// seq++
// setup mocks and assertions
collectionEngine := mockCollectionEngineToReturnCollections(
t,
&collectionNode,
[]*flow.Collection{col1, col2, col3},
)
receiptsReceived := 0
consensusEngine := new(mocknetwork.Engine)
_, _ = consensusNode.Net.Register(engine.ReceiveReceipts, consensusEngine)
consensusEngine.On("Submit", mock.AnythingOfType("network.Channel"), mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
receiptsReceived++
originID := args[1].(flow.Identifier)
receipt := args[2].(*flow.ExecutionReceipt)
finalState, _ := receipt.ExecutionResult.FinalStateCommitment()
consensusNode.Log.Debug().
Hex("origin", originID[:]).
Hex("block", receipt.ExecutionResult.BlockID[:]).
Hex("final_state_commit", finalState[:]).
Msg("execution receipt delivered")
}).Return(nil)
// submit block2 from consensus node to execution node 1
err = sendBlock(&exe1Node, conID.NodeID, proposal1)
require.NoError(t, err)
err = sendBlock(&exe1Node, conID.NodeID, proposal2)
assert.NoError(t, err)
// ensure block 1 has been executed
hub.DeliverAllEventually(t, func() bool {
return receiptsReceived == 1
})
exe1Node.AssertHighestExecutedBlock(t, block1.Header)
scExe1Genesis, err := exe1Node.ExecutionState.StateCommitmentByBlockID(context.Background(), genesis.ID())
assert.NoError(t, err)
scExe1Block1, err := exe1Node.ExecutionState.StateCommitmentByBlockID(context.Background(), block1.ID())
assert.NoError(t, err)
assert.NotEqual(t, scExe1Genesis, scExe1Block1)
// submit block 3 and block 4 from consensus node to execution node 1 (who have block1),
err = sendBlock(&exe1Node, conID.NodeID, proposal3)
assert.NoError(t, err)
err = sendBlock(&exe1Node, conID.NodeID, proposal4)
assert.NoError(t, err)
// ensure block 1, 2 and 3 have been executed
hub.DeliverAllEventually(t, func() bool {
return receiptsReceived == 3
})
// ensure state has been synced across both nodes
exe1Node.AssertHighestExecutedBlock(t, block3.Header)
// exe2Node.AssertHighestExecutedBlock(t, block3.Header)
// verify state commitment of block 2 is the same as block 1, since tx failed on seq number verification
scExe1Block2, err := exe1Node.ExecutionState.StateCommitmentByBlockID(context.Background(), block2.ID())
assert.NoError(t, err)
// TODO this is no longer valid because the system chunk can change the state
//assert.Equal(t, scExe1Block1, scExe1Block2)
_ = scExe1Block2
collectionEngine.AssertExpectations(t)
consensusEngine.AssertExpectations(t)
}
func mockCollectionEngineToReturnCollections(t *testing.T, collectionNode *testmock.GenericNode, cols []*flow.Collection) *mocknetwork.Engine {
collectionEngine := new(mocknetwork.Engine)
colConduit, _ := collectionNode.Net.Register(engine.RequestCollections, collectionEngine)
// make lookup
colMap := make(map[flow.Identifier][]byte)
for _, col := range cols {
blob, _ := msgpack.Marshal(col)
colMap[col.ID()] = blob
}
collectionEngine.On("Submit", mock.AnythingOfType("network.Channel"), mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
originID := args[1].(flow.Identifier)
req := args[2].(*messages.EntityRequest)
blob, ok := colMap[req.EntityIDs[0]]
if !ok {
assert.FailNow(t, "requesting unexpected collection", req.EntityIDs[0])
}
res := &messages.EntityResponse{Blobs: [][]byte{blob}, EntityIDs: req.EntityIDs[:1]}
err := colConduit.Publish(res, originID)
assert.NoError(t, err)
}).
Return(nil).
Times(len(cols))
return collectionEngine
}
// Test the receipt will be sent to multiple verification nodes
func TestBroadcastToMultipleVerificationNodes(t *testing.T) {
hub := stub.NewNetworkHub()
chainID := flow.Emulator
colID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleCollection),
unittest.WithKeys,
)
conID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleConsensus),
unittest.WithKeys,
)
exeID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleExecution),
unittest.WithKeys,
)
ver1ID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleVerification),
unittest.WithKeys,
)
ver2ID := unittest.IdentityFixture(
unittest.WithRole(flow.RoleVerification),
unittest.WithKeys,
)
identities := unittest.CompleteIdentitySet(colID, conID, exeID, ver1ID, ver2ID)
exeNode := testutil.ExecutionNode(t, hub, exeID, identities, 21, chainID)
exeNode.Ready()
defer exeNode.Done()
verification1Node := testutil.GenericNodeFromParticipants(t, hub, ver1ID, identities, chainID)
defer verification1Node.Done()
verification2Node := testutil.GenericNodeFromParticipants(t, hub, ver2ID, identities, chainID)
defer verification2Node.Done()
genesis, err := exeNode.State.AtHeight(0).Head()
require.NoError(t, err)
block := unittest.BlockWithParentAndProposerFixture(genesis, conID.NodeID)
block.Header.View = 42
block.SetPayload(flow.Payload{})
proposal := unittest.ProposalFromBlock(&block)
child := unittest.BlockWithParentAndProposerFixture(block.Header, conID.NodeID)
actualCalls := 0
var receipt *flow.ExecutionReceipt
verificationEngine := new(mocknetwork.Engine)
_, _ = verification1Node.Net.Register(engine.ReceiveReceipts, verificationEngine)
_, _ = verification2Node.Net.Register(engine.ReceiveReceipts, verificationEngine)
verificationEngine.On("Submit", mock.AnythingOfType("network.Channel"), exeID.NodeID, mock.Anything).
Run(func(args mock.Arguments) {
actualCalls++
receipt, _ = args[2].(*flow.ExecutionReceipt)
assert.Equal(t, block.ID(), receipt.ExecutionResult.BlockID)
}).
Return(nil)
err = sendBlock(&exeNode, exeID.NodeID, proposal)
require.NoError(t, err)
err = sendBlock(&exeNode, conID.NodeID, unittest.ProposalFromBlock(&child))
require.NoError(t, err)
hub.DeliverAllEventually(t, func() bool {
return actualCalls == 2
})
verificationEngine.AssertExpectations(t)
}
// Test that when received the same state delta for the second time,
// the delta will be saved again without causing any error.
// func TestReceiveTheSameDeltaMultipleTimes(t *testing.T) {
// hub := stub.NewNetworkHub()
//
// chainID := flow.Mainnet
//
// colID := unittest.IdentityFixture(unittest.WithRole(flow.RoleCollection))
// exeID := unittest.IdentityFixture(unittest.WithRole(flow.RoleExecution))
// ver1ID := unittest.IdentityFixture(unittest.WithRole(flow.RoleVerification))
// ver2ID := unittest.IdentityFixture(unittest.WithRole(flow.RoleVerification))
//
// identities := unittest.CompleteIdentitySet(colID, exeID, ver1ID, ver2ID)
//
// exeNode := testutil.ExecutionNode(t, hub, exeID, identities, 21, chainID)
// defer exeNode.Done()
//
// genesis, err := exeNode.State.AtHeight(0).Head()
// require.NoError(t, err)
//
// delta := unittest.StateDeltaWithParentFixture(genesis)
// delta.ExecutableBlock.StartState = unittest.GenesisStateCommitment
// delta.EndState = unittest.GenesisStateCommitment
//
// fmt.Printf("block id: %v, delta for block (%v)'s parent id: %v\n", genesis.ID(), delta.Block.ID(), delta.ParentID())
// exeNode.IngestionEngine.SubmitLocal(delta)
// time.Sleep(time.Second)
//
// exeNode.IngestionEngine.SubmitLocal(delta)
// // handling the same delta again to verify the DB calls in saveExecutionResults
// // are idempotent, if they weren't, it will hit log.Fatal and crash before
// // sleep is done
// time.Sleep(time.Second)
//
// }