@@ -19,8 +19,11 @@ package catalyst
19
19
import (
20
20
"bytes"
21
21
"context"
22
+ crand "crypto/rand"
22
23
"fmt"
23
24
"math/big"
25
+ "math/rand"
26
+ "reflect"
24
27
"sync"
25
28
"testing"
26
29
"time"
@@ -41,7 +44,6 @@ import (
41
44
"github.com/ethereum/go-ethereum/node"
42
45
"github.com/ethereum/go-ethereum/p2p"
43
46
"github.com/ethereum/go-ethereum/params"
44
- "github.com/ethereum/go-ethereum/rlp"
45
47
"github.com/ethereum/go-ethereum/rpc"
46
48
"github.com/ethereum/go-ethereum/trie"
47
49
)
@@ -473,18 +475,21 @@ func TestFullAPI(t *testing.T) {
473
475
ethservice .TxPool ().AddLocal (tx )
474
476
}
475
477
476
- setupBlocks (t , ethservice , 10 , parent , callback )
478
+ setupBlocks (t , ethservice , 10 , parent , callback , nil )
477
479
}
478
480
479
- func setupBlocks (t * testing.T , ethservice * eth.Ethereum , n int , parent * types.Header , callback func (parent * types.Header )) []* types.Header {
481
+ func setupBlocks (t * testing.T , ethservice * eth.Ethereum , n int , parent * types.Header , callback func (parent * types.Header ), withdrawals [][] * types. Withdrawal ) []* types.Header {
480
482
api := NewConsensusAPI (ethservice )
481
483
var blocks []* types.Header
482
484
for i := 0 ; i < n ; i ++ {
483
485
callback (parent )
486
+ var w []* types.Withdrawal
487
+ if withdrawals != nil {
488
+ w = withdrawals [i ]
489
+ }
484
490
485
- payload := getNewPayload (t , api , parent )
486
-
487
- execResp , err := api .NewPayloadV1 (* payload )
491
+ payload := getNewPayload (t , api , parent , w )
492
+ execResp , err := api .NewPayloadV2 (* payload )
488
493
if err != nil {
489
494
t .Fatalf ("can't execute payload: %v" , err )
490
495
}
@@ -676,10 +681,10 @@ func TestEmptyBlocks(t *testing.T) {
676
681
api := NewConsensusAPI (ethservice )
677
682
678
683
// Setup 10 blocks on the canonical chain
679
- setupBlocks (t , ethservice , 10 , commonAncestor , func (parent * types.Header ) {})
684
+ setupBlocks (t , ethservice , 10 , commonAncestor , func (parent * types.Header ) {}, nil )
680
685
681
686
// (1) check LatestValidHash by sending a normal payload (P1'')
682
- payload := getNewPayload (t , api , commonAncestor )
687
+ payload := getNewPayload (t , api , commonAncestor , nil )
683
688
684
689
status , err := api .NewPayloadV1 (* payload )
685
690
if err != nil {
@@ -693,7 +698,7 @@ func TestEmptyBlocks(t *testing.T) {
693
698
}
694
699
695
700
// (2) Now send P1' which is invalid
696
- payload = getNewPayload (t , api , commonAncestor )
701
+ payload = getNewPayload (t , api , commonAncestor , nil )
697
702
payload .GasUsed += 1
698
703
payload = setBlockhash (payload )
699
704
// Now latestValidHash should be the common ancestor
@@ -711,7 +716,7 @@ func TestEmptyBlocks(t *testing.T) {
711
716
}
712
717
713
718
// (3) Now send a payload with unknown parent
714
- payload = getNewPayload (t , api , commonAncestor )
719
+ payload = getNewPayload (t , api , commonAncestor , nil )
715
720
payload .ParentHash = common.Hash {1 }
716
721
payload = setBlockhash (payload )
717
722
// Now latestValidHash should be the common ancestor
@@ -727,11 +732,12 @@ func TestEmptyBlocks(t *testing.T) {
727
732
}
728
733
}
729
734
730
- func getNewPayload (t * testing.T , api * ConsensusAPI , parent * types.Header ) * engine.ExecutableData {
735
+ func getNewPayload (t * testing.T , api * ConsensusAPI , parent * types.Header , withdrawals [] * types. Withdrawal ) * engine.ExecutableData {
731
736
params := engine.PayloadAttributes {
732
737
Timestamp : parent .Time + 1 ,
733
738
Random : crypto .Keccak256Hash ([]byte {byte (1 )}),
734
739
SuggestedFeeRecipient : parent .Coinbase ,
740
+ Withdrawals : withdrawals ,
735
741
}
736
742
737
743
payload , err := assembleBlock (api , parent .Hash (), & params )
@@ -799,7 +805,7 @@ func TestTrickRemoteBlockCache(t *testing.T) {
799
805
commonAncestor := ethserviceA .BlockChain ().CurrentBlock ()
800
806
801
807
// Setup 10 blocks on the canonical chain
802
- setupBlocks (t , ethserviceA , 10 , commonAncestor , func (parent * types.Header ) {})
808
+ setupBlocks (t , ethserviceA , 10 , commonAncestor , func (parent * types.Header ) {}, nil )
803
809
commonAncestor = ethserviceA .BlockChain ().CurrentBlock ()
804
810
805
811
var invalidChain []* engine.ExecutableData
@@ -808,7 +814,7 @@ func TestTrickRemoteBlockCache(t *testing.T) {
808
814
//invalidChain = append(invalidChain, payload1)
809
815
810
816
// create an invalid payload2 (P2)
811
- payload2 := getNewPayload (t , apiA , commonAncestor )
817
+ payload2 := getNewPayload (t , apiA , commonAncestor , nil )
812
818
//payload2.ParentHash = payload1.BlockHash
813
819
payload2 .GasUsed += 1
814
820
payload2 = setBlockhash (payload2 )
@@ -817,7 +823,7 @@ func TestTrickRemoteBlockCache(t *testing.T) {
817
823
head := payload2
818
824
// create some valid payloads on top
819
825
for i := 0 ; i < 10 ; i ++ {
820
- payload := getNewPayload (t , apiA , commonAncestor )
826
+ payload := getNewPayload (t , apiA , commonAncestor , nil )
821
827
payload .ParentHash = head .BlockHash
822
828
payload = setBlockhash (payload )
823
829
invalidChain = append (invalidChain , payload )
@@ -855,10 +861,10 @@ func TestInvalidBloom(t *testing.T) {
855
861
api := NewConsensusAPI (ethservice )
856
862
857
863
// Setup 10 blocks on the canonical chain
858
- setupBlocks (t , ethservice , 10 , commonAncestor , func (parent * types.Header ) {})
864
+ setupBlocks (t , ethservice , 10 , commonAncestor , func (parent * types.Header ) {}, nil )
859
865
860
866
// (1) check LatestValidHash by sending a normal payload (P1'')
861
- payload := getNewPayload (t , api , commonAncestor )
867
+ payload := getNewPayload (t , api , commonAncestor , nil )
862
868
payload .LogsBloom = append (payload .LogsBloom , byte (1 ))
863
869
status , err := api .NewPayloadV1 (* payload )
864
870
if err != nil {
@@ -1233,8 +1239,10 @@ func TestNilWithdrawals(t *testing.T) {
1233
1239
}
1234
1240
1235
1241
func setupBodies (t * testing.T ) (* node.Node , * eth.Ethereum , []* types.Block ) {
1236
- genesis , preMergeBlocks := generateMergeChain (10 , false )
1237
- n , ethservice := startEthService (t , genesis , preMergeBlocks )
1242
+ genesis , blocks := generateMergeChain (10 , true )
1243
+ n , ethservice := startEthService (t , genesis , blocks )
1244
+ // enable shanghai on the last block
1245
+ ethservice .BlockChain ().Config ().ShanghaiTime = & blocks [len (blocks )- 1 ].Header ().Time
1238
1246
1239
1247
var (
1240
1248
parent = ethservice .BlockChain ().CurrentBlock ()
@@ -1249,12 +1257,38 @@ func setupBodies(t *testing.T) (*node.Node, *eth.Ethereum, []*types.Block) {
1249
1257
ethservice .TxPool ().AddLocal (tx )
1250
1258
}
1251
1259
1252
- postMergeHeaders := setupBlocks (t , ethservice , 10 , parent , callback )
1253
- postMergeBlocks := make ([]* types.Block , len (postMergeHeaders ))
1254
- for i , header := range postMergeHeaders {
1255
- postMergeBlocks [i ] = ethservice .BlockChain ().GetBlock (header .Hash (), header .Number .Uint64 ())
1260
+ withdrawals := make ([][]* types.Withdrawal , 10 )
1261
+ withdrawals [0 ] = nil // should be filtered out by miner
1262
+ withdrawals [1 ] = make ([]* types.Withdrawal , 0 )
1263
+ for i := 2 ; i < len (withdrawals ); i ++ {
1264
+ addr := make ([]byte , 20 )
1265
+ crand .Read (addr )
1266
+ withdrawals [i ] = []* types.Withdrawal {
1267
+ {Index : rand .Uint64 (), Validator : rand .Uint64 (), Amount : rand .Uint64 (), Address : common .BytesToAddress (addr )},
1268
+ }
1269
+ }
1270
+
1271
+ postShanghaiHeaders := setupBlocks (t , ethservice , 10 , parent , callback , withdrawals )
1272
+ postShanghaiBlocks := make ([]* types.Block , len (postShanghaiHeaders ))
1273
+ for i , header := range postShanghaiHeaders {
1274
+ postShanghaiBlocks [i ] = ethservice .BlockChain ().GetBlock (header .Hash (), header .Number .Uint64 ())
1256
1275
}
1257
- return n , ethservice , append (preMergeBlocks , postMergeBlocks ... )
1276
+ return n , ethservice , append (blocks , postShanghaiBlocks ... )
1277
+ }
1278
+
1279
+ func allHashes (blocks []* types.Block ) []common.Hash {
1280
+ var hashes []common.Hash
1281
+ for _ , b := range blocks {
1282
+ hashes = append (hashes , b .Hash ())
1283
+ }
1284
+ return hashes
1285
+ }
1286
+ func allBodies (blocks []* types.Block ) []* types.Body {
1287
+ var bodies []* types.Body
1288
+ for _ , b := range blocks {
1289
+ bodies = append (bodies , b .Body ())
1290
+ }
1291
+ return bodies
1258
1292
}
1259
1293
1260
1294
func TestGetBlockBodiesByHash (t * testing.T ) {
@@ -1296,6 +1330,11 @@ func TestGetBlockBodiesByHash(t *testing.T) {
1296
1330
results : []* types.Body {blocks [0 ].Body (), nil , blocks [0 ].Body (), blocks [0 ].Body ()},
1297
1331
hashes : []common.Hash {blocks [0 ].Hash (), {1 , 2 }, blocks [0 ].Hash (), blocks [0 ].Hash ()},
1298
1332
},
1333
+ // all blocks
1334
+ {
1335
+ results : allBodies (blocks ),
1336
+ hashes : allHashes (blocks ),
1337
+ },
1299
1338
}
1300
1339
1301
1340
for k , test := range tests {
@@ -1364,6 +1403,12 @@ func TestGetBlockBodiesByRange(t *testing.T) {
1364
1403
start : 22 ,
1365
1404
count : 2 ,
1366
1405
},
1406
+ // allBlocks
1407
+ {
1408
+ results : allBodies (blocks ),
1409
+ start : 1 ,
1410
+ count : hexutil .Uint64 (len (blocks )),
1411
+ },
1367
1412
}
1368
1413
1369
1414
for k , test := range tests {
@@ -1434,15 +1479,14 @@ func equalBody(a *types.Body, b *engine.ExecutionPayloadBodyV1) bool {
1434
1479
} else if a == nil || b == nil {
1435
1480
return false
1436
1481
}
1437
- var want []hexutil.Bytes
1438
- for _ , tx := range a .Transactions {
1439
- data , _ := tx .MarshalBinary ()
1440
- want = append (want , hexutil .Bytes (data ))
1441
- }
1442
- aBytes , errA := rlp .EncodeToBytes (want )
1443
- bBytes , errB := rlp .EncodeToBytes (b .TransactionData )
1444
- if errA != errB {
1482
+ if len (a .Transactions ) != len (b .TransactionData ) {
1445
1483
return false
1446
1484
}
1447
- return bytes .Equal (aBytes , bBytes )
1485
+ for i , tx := range a .Transactions {
1486
+ data , _ := tx .MarshalBinary ()
1487
+ if ! bytes .Equal (data , b .TransactionData [i ]) {
1488
+ return false
1489
+ }
1490
+ }
1491
+ return reflect .DeepEqual (a .Withdrawals , b .Withdrawals )
1448
1492
}
0 commit comments