/
payload_execution.go
548 lines (470 loc) · 18.5 KB
/
payload_execution.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
package suite_engine
import (
"math/big"
api "github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/hive/simulators/ethereum/engine/client"
"github.com/ethereum/hive/simulators/ethereum/engine/client/hive_rpc"
"github.com/ethereum/hive/simulators/ethereum/engine/clmock"
"github.com/ethereum/hive/simulators/ethereum/engine/config"
"github.com/ethereum/hive/simulators/ethereum/engine/helper"
"github.com/ethereum/hive/simulators/ethereum/engine/test"
typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
)
type ReExecutePayloadTest struct {
test.BaseSpec
PayloadCount int
}
func (s ReExecutePayloadTest) WithMainFork(fork config.Fork) test.Spec {
specCopy := s
specCopy.MainFork = fork
return specCopy
}
func (s ReExecutePayloadTest) GetName() string {
name := "Re-Execute Payload"
return name
}
// Consecutive Payload Execution: Secondary client should be able to set the forkchoiceUpdated to payloads received consecutively
func (spec ReExecutePayloadTest) Execute(t *test.Env) {
// Wait until this client catches up with latest PoS
t.CLMock.WaitForTTD()
// How many Payloads we are going to re-execute
var payloadReExecCount = 10
if spec.PayloadCount > 0 {
payloadReExecCount = spec.PayloadCount
}
// Create those blocks
t.CLMock.ProduceBlocks(payloadReExecCount, clmock.BlockProcessCallbacks{
OnPayloadProducerSelected: func() {
// Send at least one transaction per payload
_, err := t.SendNextTransaction(
t.TestContext,
t.CLMock.NextBlockProducer,
&helper.BaseTransactionCreator{
Recipient: nil,
Amount: nil,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
},
)
if err != nil {
t.Fatalf("FAIL (%s): Error trying to send transaction: %v", t.TestName, err)
}
},
OnGetPayload: func() {
// Check that the transaction was included
if len(t.CLMock.LatestPayloadBuilt.Transactions) == 0 {
t.Fatalf("FAIL (%s): Client failed to include the expected transaction in payload built", t.TestName)
}
},
})
// Re-execute the payloads
r := t.TestEngine.TestBlockNumber()
r.ExpectNoError()
lastBlock := r.Number
t.Logf("INFO (%s): Started re-executing payloads at block: %v", t.TestName, lastBlock)
for i := lastBlock - uint64(payloadReExecCount) + 1; i <= lastBlock; i++ {
payload, found := t.CLMock.ExecutedPayloadHistory[i]
if !found {
t.Fatalf("FAIL (%s): (test issue) Payload with index %d does not exist", i)
}
r := t.TestEngine.TestEngineNewPayload(payload)
r.ExpectStatus(test.Valid)
r.ExpectLatestValidHash(&payload.BlockHash)
}
}
type InOrderPayloadExecutionTest struct {
test.BaseSpec
}
func (s InOrderPayloadExecutionTest) WithMainFork(fork config.Fork) test.Spec {
specCopy := s
specCopy.MainFork = fork
return specCopy
}
func (s InOrderPayloadExecutionTest) GetName() string {
name := "In-Order Consecutive Payload Execution"
return name
}
// Consecutive Payload Execution: Secondary client should be able to set the forkchoiceUpdated to payloads received consecutively
func (spec InOrderPayloadExecutionTest) Execute(t *test.Env) {
// Wait until this client catches up with latest PoS
t.CLMock.WaitForTTD()
// Send a single block to allow sending newer transaction types on the payloads
t.CLMock.ProduceSingleBlock(clmock.BlockProcessCallbacks{})
// First prepare payloads on a first client, which will also contain multiple transactions
// We will be also verifying that the transactions are correctly interpreted in the canonical chain,
// prepare a random account to receive funds.
recipient := common.Address{}
t.Rand.Read(recipient[:])
amountPerTx := big.NewInt(1000)
txPerPayload := 20
payloadCount := 10
txsIncluded := 0
t.CLMock.ProduceBlocks(payloadCount, clmock.BlockProcessCallbacks{
// We send the transactions after we got the Payload ID, before the CLMocker gets the prepared Payload
OnPayloadProducerSelected: func() {
_, err := t.SendNextTransactions(
t.TestContext,
t.CLMock.NextBlockProducer,
&helper.BaseTransactionCreator{
Recipient: &recipient,
Amount: amountPerTx,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
},
uint64(txPerPayload),
)
if err != nil {
t.Fatalf("FAIL (%s): Error trying to send transaction: %v", t.TestName, err)
}
},
OnGetPayload: func() {
if len(t.CLMock.LatestPayloadBuilt.Transactions) < (txPerPayload / 2) {
t.Fatalf("FAIL (%s): Client failed to include all the expected transactions in payload built: %d < %d", t.TestName, len(t.CLMock.LatestPayloadBuilt.Transactions), (txPerPayload / 2))
}
txsIncluded += len(t.CLMock.LatestPayloadBuilt.Transactions)
},
})
expectedBalance := amountPerTx.Mul(amountPerTx, big.NewInt(int64(txsIncluded)))
// Check balance on this first client
r := t.TestEngine.TestBalanceAt(recipient, nil)
r.ExpectBalanceEqual(expectedBalance)
// Start a second client to send newPayload consecutively without fcU
secondaryClient, err := hive_rpc.HiveRPCEngineStarter{}.StartClient(t.T, t.TestContext, t.Genesis, t.ClientParams, t.ClientFiles)
if err != nil {
t.Fatalf("FAIL (%s): Unable to start secondary client: %v", t.TestName, err)
}
secondaryTestEngineClient := test.NewTestEngineClient(t, secondaryClient)
// Send the forkchoiceUpdated with the LatestExecutedPayload hash, we should get SYNCING back
fcU := api.ForkchoiceStateV1{
HeadBlockHash: t.CLMock.LatestExecutedPayload.BlockHash,
SafeBlockHash: t.CLMock.LatestExecutedPayload.BlockHash,
FinalizedBlockHash: t.CLMock.LatestExecutedPayload.BlockHash,
}
s := secondaryTestEngineClient.TestEngineForkchoiceUpdated(&fcU, nil, t.CLMock.LatestExecutedPayload.Timestamp)
s.ExpectPayloadStatus(test.Syncing)
s.ExpectLatestValidHash(nil)
s.ExpectNoValidationError()
// Send all the payloads in the increasing order
for k := t.CLMock.FirstPoSBlockNumber.Uint64(); k <= t.CLMock.LatestExecutedPayload.Number; k++ {
payload := t.CLMock.ExecutedPayloadHistory[k]
s := secondaryTestEngineClient.TestEngineNewPayload(payload)
s.ExpectStatus(test.Valid)
s.ExpectLatestValidHash(&payload.BlockHash)
}
s = secondaryTestEngineClient.TestEngineForkchoiceUpdated(&fcU, nil, t.CLMock.LatestExecutedPayload.Timestamp)
s.ExpectPayloadStatus(test.Valid)
s.ExpectLatestValidHash(&fcU.HeadBlockHash)
s.ExpectNoValidationError()
// At this point we should have our funded account balance equal to the expected value.
q := secondaryTestEngineClient.TestBalanceAt(recipient, nil)
q.ExpectBalanceEqual(expectedBalance)
// Add the client to the CLMocker
t.CLMock.AddEngineClient(secondaryClient)
// Produce a single block on top of the canonical chain, all clients must accept this
t.CLMock.ProduceSingleBlock(clmock.BlockProcessCallbacks{})
// Head must point to the latest produced payload
p := secondaryTestEngineClient.TestHeaderByNumber(nil)
p.ExpectHash(t.CLMock.LatestExecutedPayload.BlockHash)
}
type MultiplePayloadsExtendingCanonicalChainTest struct {
test.BaseSpec
// How many parallel payloads to execute
PayloadCount int
// If set to true, the head will be set to the first payload executed by the client
// If set to false, the head will be set to the latest payload executed by the client
SetHeadToFirstPayloadReceived bool
}
func (s MultiplePayloadsExtendingCanonicalChainTest) WithMainFork(fork config.Fork) test.Spec {
specCopy := s
specCopy.MainFork = fork
return specCopy
}
func (s MultiplePayloadsExtendingCanonicalChainTest) GetName() string {
name := "Multiple New Payloads Extending Canonical Chain,"
if s.SetHeadToFirstPayloadReceived {
name += " Set Head to First Payload Received"
} else {
name += " Wait for Canonical Payload"
}
return name
}
// Consecutive Payload Execution: Secondary client should be able to set the forkchoiceUpdated to payloads received consecutively
func (spec MultiplePayloadsExtendingCanonicalChainTest) Execute(t *test.Env) {
// Wait until this client catches up with latest PoS
t.CLMock.WaitForTTD()
// Produce blocks before starting the test
t.CLMock.ProduceBlocks(5, clmock.BlockProcessCallbacks{})
callbacks := clmock.BlockProcessCallbacks{
// We send the transactions after we got the Payload ID, before the CLMocker gets the prepared Payload
OnPayloadProducerSelected: func() {
recipient := common.Address{}
t.Rand.Read(recipient[:])
_, err := t.SendNextTransaction(
t.TestContext,
t.CLMock.NextBlockProducer,
&helper.BaseTransactionCreator{
Recipient: &recipient,
Amount: nil,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
},
)
if err != nil {
t.Fatalf("FAIL (%s): Error trying to send transaction: %v", t.TestName, err)
}
},
}
reExecFunc := func() {
payloadCount := 80
if spec.PayloadCount > 0 {
payloadCount = spec.PayloadCount
}
basePayload := t.CLMock.LatestPayloadBuilt
// Check that the transaction was included
if len(basePayload.Transactions) == 0 {
t.Fatalf("FAIL (%s): Client failed to include the expected transaction in payload built", t.TestName)
}
// Fabricate and send multiple new payloads by changing the PrevRandao field
for i := 0; i < payloadCount; i++ {
newPrevRandao := common.Hash{}
t.Rand.Read(newPrevRandao[:])
customizer := &helper.CustomPayloadData{
PrevRandao: &newPrevRandao,
}
newPayload, err := customizer.CustomizePayload(t.Rand, &basePayload)
if err != nil {
t.Fatalf("FAIL (%s): Unable to customize payload %v: %v", t.TestName, i, err)
}
r := t.TestEngine.TestEngineNewPayload(newPayload)
r.ExpectStatus(test.Valid)
r.ExpectLatestValidHash(&newPayload.BlockHash)
}
}
if spec.SetHeadToFirstPayloadReceived {
// We are going to set the head of the chain to the first payload executed by the client
// Therefore our re-execution function must be executed after the payload was broadcast
callbacks.OnNewPayloadBroadcast = reExecFunc
} else {
// Otherwise, we execute the payloads after we get the canonical one so it's
// executed last
callbacks.OnGetPayload = reExecFunc
}
t.CLMock.ProduceSingleBlock(callbacks)
// At the end the CLMocker continues to try to execute fcU with the original payload, which should not fail
}
type NewPayloadOnSyncingClientTest struct {
test.BaseSpec
}
func (s NewPayloadOnSyncingClientTest) WithMainFork(fork config.Fork) test.Spec {
specCopy := s
specCopy.MainFork = fork
return specCopy
}
func (s NewPayloadOnSyncingClientTest) GetName() string {
name := "Valid NewPayload->ForkchoiceUpdated on Syncing Client"
return name
}
// Send a valid payload on a client that is currently SYNCING
func (spec NewPayloadOnSyncingClientTest) Execute(t *test.Env) {
var (
secondaryClient client.EngineClient
previousPayload typ.ExecutableData
)
{
// To allow sending the primary engine client into SYNCING state, we need a secondary client to guide the payload creation
var err error
secondaryClient, err = hive_rpc.HiveRPCEngineStarter{}.StartClient(t.T, t.TestContext, t.Genesis, t.ClientParams, t.ClientFiles)
if err != nil {
t.Fatalf("FAIL (%s): Unable to spawn a secondary client: %v", t.TestName, err)
}
t.CLMock.AddEngineClient(secondaryClient)
}
// Wait until TTD is reached by all clients
t.CLMock.WaitForTTD()
// Produce blocks before starting the test
t.CLMock.ProduceBlocks(5, clmock.BlockProcessCallbacks{})
// Set a random transaction recipient
recipient := common.Address{}
t.Rand.Read(recipient[:])
// Disconnect the first engine client from the CL Mocker and produce a block
t.CLMock.RemoveEngineClient(t.Engine)
t.CLMock.ProduceSingleBlock(clmock.BlockProcessCallbacks{
OnPayloadProducerSelected: func() {
// Send at least one transaction per payload
_, err := t.SendNextTransaction(
t.TestContext,
t.CLMock.NextBlockProducer,
&helper.BaseTransactionCreator{
Recipient: &recipient,
Amount: nil,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
},
)
if err != nil {
t.Fatalf("FAIL (%s): Error trying to send transaction: %v", t.TestName, err)
}
},
OnGetPayload: func() {
// Check that the transaction was included
if len(t.CLMock.LatestPayloadBuilt.Transactions) == 0 {
t.Fatalf("FAIL (%s): Client failed to include the expected transaction in payload built", t.TestName)
}
},
})
previousPayload = t.CLMock.LatestPayloadBuilt
// Send the fcU to set it to syncing mode
r := t.TestEngine.TestEngineForkchoiceUpdated(&t.CLMock.LatestForkchoice, nil, t.CLMock.LatestHeader.Time)
r.ExpectPayloadStatus(test.Syncing)
t.CLMock.ProduceSingleBlock(clmock.BlockProcessCallbacks{
OnPayloadProducerSelected: func() {
// Send at least one transaction per payload
_, err := t.SendNextTransaction(
t.TestContext,
t.CLMock.NextBlockProducer,
&helper.BaseTransactionCreator{
Recipient: &recipient,
Amount: nil,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
},
)
if err != nil {
t.Fatalf("FAIL (%s): Error trying to send transaction: %v", t.TestName, err)
}
},
// Run test after the new payload has been obtained
OnGetPayload: func() {
// Send the new payload from the second client to the first, it won't be able to validate it
r := t.TestEngine.TestEngineNewPayload(&t.CLMock.LatestPayloadBuilt)
r.ExpectStatusEither(test.Accepted, test.Syncing)
r.ExpectLatestValidHash(nil)
// Send the forkchoiceUpdated with a reference to the valid payload on the SYNCING client.
var (
random = common.Hash{}
suggestedFeeRecipient = common.Address{}
)
payloadAttributesCustomizer := &helper.BasePayloadAttributesCustomizer{
Random: &random,
SuggestedFeeRecipient: &suggestedFeeRecipient,
}
newPayloadAttributes, err := payloadAttributesCustomizer.GetPayloadAttributes(&t.CLMock.LatestPayloadAttributes)
if err != nil {
t.Fatalf("FAIL (%s): Unable to customize payload attributes: %v", t.TestName, err)
}
s := t.TestEngine.TestEngineForkchoiceUpdated(&api.ForkchoiceStateV1{
HeadBlockHash: t.CLMock.LatestPayloadBuilt.BlockHash,
SafeBlockHash: t.CLMock.LatestPayloadBuilt.BlockHash,
FinalizedBlockHash: t.CLMock.LatestPayloadBuilt.BlockHash,
}, newPayloadAttributes, t.CLMock.LatestPayloadBuilt.Timestamp)
s.ExpectPayloadStatus(test.Syncing)
// Send the previous payload to be able to continue
p := t.TestEngine.TestEngineNewPayload(&previousPayload)
p.ExpectStatus(test.Valid)
p.ExpectLatestValidHash(&previousPayload.BlockHash)
// Send the new payload again
p = t.TestEngine.TestEngineNewPayload(&t.CLMock.LatestPayloadBuilt)
p.ExpectStatus(test.Valid)
p.ExpectLatestValidHash(&t.CLMock.LatestPayloadBuilt.BlockHash)
s = t.TestEngine.TestEngineForkchoiceUpdated(&api.ForkchoiceStateV1{
HeadBlockHash: t.CLMock.LatestPayloadBuilt.BlockHash,
SafeBlockHash: t.CLMock.LatestPayloadBuilt.BlockHash,
FinalizedBlockHash: t.CLMock.LatestPayloadBuilt.BlockHash,
}, nil, t.CLMock.LatestPayloadBuilt.Timestamp)
s.ExpectPayloadStatus(test.Valid)
},
})
// Add the secondary client again to the CL Mocker
t.CLMock.AddEngineClient(t.Engine)
t.CLMock.RemoveEngineClient(secondaryClient)
}
type NewPayloadWithMissingFcUTest struct {
test.BaseSpec
}
func (s NewPayloadWithMissingFcUTest) WithMainFork(fork config.Fork) test.Spec {
specCopy := s
specCopy.MainFork = fork
return specCopy
}
func (s NewPayloadWithMissingFcUTest) GetName() string {
name := "NewPayload with Missing ForkchoiceUpdated"
return name
}
// Send a valid `newPayload` in correct order but skip `forkchoiceUpdated` until the last payload
func (spec NewPayloadWithMissingFcUTest) Execute(t *test.Env) {
// Wait until TTD is reached by this client
t.CLMock.WaitForTTD()
// Get last genesis block hash
genesisHash := t.TestEngine.TestHeaderByNumber(Head).Header.Hash()
// Produce blocks on the main client, these payloads will be replayed on the secondary client.
t.CLMock.ProduceBlocks(5, clmock.BlockProcessCallbacks{
OnPayloadProducerSelected: func() {
var recipient common.Address
t.Rand.Read(recipient[:])
// Send at least one transaction per payload
_, err := t.SendNextTransaction(
t.TestContext,
t.CLMock.NextBlockProducer,
&helper.BaseTransactionCreator{
Recipient: &recipient,
Amount: nil,
Payload: nil,
TxType: t.TestTransactionType,
GasLimit: 75000,
ForkConfig: t.ForkConfig,
},
)
if err != nil {
t.Fatalf("FAIL (%s): Error trying to send transaction: %v", t.TestName, err)
}
},
OnGetPayload: func() {
// Check that the transaction was included
if len(t.CLMock.LatestPayloadBuilt.Transactions) == 0 {
t.Fatalf("FAIL (%s): Client failed to include the expected transaction in payload built", t.TestName)
}
},
})
var secondaryEngineTest *test.TestEngineClient
{
secondaryEngine, err := hive_rpc.HiveRPCEngineStarter{}.StartClient(t.T, t.TestContext, t.Genesis, t.ClientParams, t.ClientFiles)
if err != nil {
t.Fatalf("FAIL (%s): Unable to spawn a secondary client: %v", t.TestName, err)
}
secondaryEngineTest = test.NewTestEngineClient(t, secondaryEngine)
t.CLMock.AddEngineClient(secondaryEngine)
}
// Send each payload in the correct order but skip the ForkchoiceUpdated for each
for i := t.CLMock.FirstPoSBlockNumber.Uint64(); i <= t.CLMock.LatestHeadNumber.Uint64(); i++ {
payload := t.CLMock.ExecutedPayloadHistory[i]
p := secondaryEngineTest.TestEngineNewPayload(payload)
p.ExpectStatus(test.Valid)
p.ExpectLatestValidHash(&payload.BlockHash)
}
// Verify that at this point, the client's head still points to the last non-PoS block
r := secondaryEngineTest.TestHeaderByNumber(Head)
r.ExpectHash(genesisHash)
// Verify that the head correctly changes after the last ForkchoiceUpdated
fcU := api.ForkchoiceStateV1{
HeadBlockHash: t.CLMock.ExecutedPayloadHistory[t.CLMock.LatestHeadNumber.Uint64()].BlockHash,
SafeBlockHash: t.CLMock.ExecutedPayloadHistory[t.CLMock.LatestHeadNumber.Uint64()-1].BlockHash,
FinalizedBlockHash: t.CLMock.ExecutedPayloadHistory[t.CLMock.LatestHeadNumber.Uint64()-2].BlockHash,
}
p := secondaryEngineTest.TestEngineForkchoiceUpdated(&fcU, nil, t.CLMock.LatestHeader.Time)
p.ExpectPayloadStatus(test.Valid)
p.ExpectLatestValidHash(&fcU.HeadBlockHash)
// Now the head should've changed to the latest PoS block
s := secondaryEngineTest.TestHeaderByNumber(Head)
s.ExpectHash(fcU.HeadBlockHash)
}