Skip to content

Commit ac7ba74

Browse files
fix(taiko-client): fix an issue in missing ancients search (#19323)
1 parent 70fcb0c commit ac7ba74

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

packages/taiko-client/driver/driver_test.go

+59-15
Original file line numberDiff line numberDiff line change
@@ -875,17 +875,17 @@ func (s *DriverTestSuite) TestGossipMessagesRandomReorgs() {
875875
s.Equal(l2Head1.Number.Uint64(), headL1Origin.BlockID.Uint64())
876876
}
877877

878-
func (s *DriverTestSuite) TestOnUnsafeL2PayloadWithMissingAncients() {
878+
func (s *DriverTestSuite) TestOnUnsafeL2PayloadWithMissingChildren() {
879879
s.ForkIntoPacaya(s.p, s.d.ChainSyncer().BlobSyncer())
880880
// Propose some valid L2 blocks
881881
s.ProposeAndInsertEmptyBlocks(s.p, s.d.ChainSyncer().BlobSyncer())
882882

883-
l2Head1, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil)
883+
l2Head1, err := s.d.rpc.L2.BlockByNumber(context.Background(), nil)
884884
s.Nil(err)
885885

886886
headL1Origin, err := s.RPCClient.L2.HeadL1Origin(context.Background())
887887
s.Nil(err)
888-
s.Equal(l2Head1.Number.Uint64(), headL1Origin.BlockID.Uint64())
888+
s.Equal(l2Head1.Number().Uint64(), headL1Origin.BlockID.Uint64())
889889

890890
snapshotID := s.SetL1Snapshot()
891891

@@ -895,33 +895,54 @@ func (s *DriverTestSuite) TestOnUnsafeL2PayloadWithMissingAncients() {
895895

896896
l2Head2, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil)
897897
s.Nil(err)
898-
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
898+
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number().Uint64())
899899

900900
blocks := []*types.Block{}
901-
for i := l2Head1.Number.Uint64() + 1; i <= l2Head2.Number.Uint64(); i++ {
901+
for i := l2Head1.Number().Uint64() + 1; i <= l2Head2.Number.Uint64(); i++ {
902902
block, err := s.RPCClient.L2.BlockByNumber(context.Background(), new(big.Int).SetUint64(i))
903903
s.Nil(err)
904904
blocks = append(blocks, block)
905905
}
906-
s.Equal(l2Head2.Number.Uint64()-l2Head1.Number.Uint64(), uint64(len(blocks)))
906+
s.Equal(l2Head2.Number.Uint64()-l2Head1.Number().Uint64(), uint64(len(blocks)))
907907

908908
s.RevertL1Snapshot(snapshotID)
909-
s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2, l2Head1.Number))
909+
s.Nil(rpc.SetHead(context.Background(), s.RPCClient.L2, l2Head1.Number()))
910910
_, err = s.RPCClient.L2Engine.SetHeadL1Origin(context.Background(), headL1Origin.BlockID)
911911
s.Nil(err)
912912

913913
headL1Origin, err = s.RPCClient.L2.HeadL1Origin(context.Background())
914914
s.Nil(err)
915-
s.Equal(l2Head1.Number.Uint64(), headL1Origin.BlockID.Uint64())
915+
s.Equal(l2Head1.Number().Uint64(), headL1Origin.BlockID.Uint64())
916916

917-
l2Head3, err := s.d.rpc.L2.HeaderByNumber(context.Background(), nil)
917+
l2Head3, err := s.d.rpc.L2.BlockByNumber(context.Background(), nil)
918+
s.Nil(err)
919+
s.Equal(l2Head1.Number().Uint64(), l2Head3.Number().Uint64())
920+
921+
baseFee, overflow := uint256.FromBig(l2Head1.BaseFee())
922+
s.False(overflow)
923+
924+
b, err := utils.EncodeAndCompressTxList(l2Head1.Transactions())
918925
s.Nil(err)
919-
s.Equal(l2Head1.Number.Uint64(), l2Head3.Number.Uint64())
926+
s.GreaterOrEqual(len(l2Head1.Transactions()), 1)
927+
928+
s.d.preconfBlockServer.PutPayloadsCache(l2Head1.Number().Uint64(), &eth.ExecutionPayload{
929+
BlockHash: l2Head1.Hash(),
930+
ParentHash: l2Head1.ParentHash(),
931+
FeeRecipient: l2Head1.Coinbase(),
932+
PrevRandao: eth.Bytes32(l2Head1.MixDigest()),
933+
BlockNumber: eth.Uint64Quantity(l2Head1.Number().Uint64()),
934+
GasLimit: eth.Uint64Quantity(l2Head1.GasLimit()),
935+
Timestamp: eth.Uint64Quantity(l2Head1.Time()),
936+
ExtraData: l2Head1.Extra(),
937+
BaseFeePerGas: eth.Uint256Quantity(*baseFee),
938+
Transactions: []eth.Data{b},
939+
Withdrawals: &types.Withdrawals{},
940+
})
920941

921942
// Randomly gossip preconfirmation messages with missing ancients
922943
blockNums := rand.Perm(len(blocks))
923944
for i := range blockNums {
924-
blockNums[i] += int(l2Head1.Number.Uint64() + 1)
945+
blockNums[i] += int(l2Head1.Number().Uint64() + 1)
925946
}
926947

927948
getBlock := func(blockNum uint64) *types.Block {
@@ -1001,7 +1022,7 @@ func (s *DriverTestSuite) TestOnUnsafeL2PayloadWithMissingAncients() {
10011022

10021023
// Insert all blocks except the first one
10031024
for _, blockNum := range blockNums {
1004-
if blockNum == int(l2Head1.Number.Uint64()+1) {
1025+
if blockNum <= int(l2Head1.Number().Uint64()+2) {
10051026
continue
10061027
}
10071028

@@ -1013,10 +1034,33 @@ func (s *DriverTestSuite) TestOnUnsafeL2PayloadWithMissingAncients() {
10131034

10141035
l2Head4, err := s.d.rpc.L2.BlockByNumber(context.Background(), nil)
10151036
s.Nil(err)
1016-
s.Equal(l2Head1.Number.Uint64(), l2Head4.Number().Uint64())
1037+
s.Equal(l2Head1.Number().Uint64(), l2Head4.Number().Uint64())
1038+
1039+
// Insert the only two missing ancient blocks
1040+
block := getBlock(l2Head1.Number().Uint64() + 1)
1041+
s.NotNil(block)
1042+
baseFee, overflow = uint256.FromBig(block.BaseFee())
1043+
s.False(overflow)
1044+
1045+
b, err = utils.EncodeAndCompressTxList(block.Transactions())
1046+
s.Nil(err)
1047+
s.GreaterOrEqual(len(block.Transactions()), 1)
1048+
1049+
s.d.preconfBlockServer.PutPayloadsCache(block.Number().Uint64(), &eth.ExecutionPayload{
1050+
BlockHash: block.Hash(),
1051+
ParentHash: block.ParentHash(),
1052+
FeeRecipient: block.Coinbase(),
1053+
PrevRandao: eth.Bytes32(block.MixDigest()),
1054+
BlockNumber: eth.Uint64Quantity(block.Number().Uint64()),
1055+
GasLimit: eth.Uint64Quantity(block.GasLimit()),
1056+
Timestamp: eth.Uint64Quantity(block.Time()),
1057+
ExtraData: block.Extra(),
1058+
BaseFeePerGas: eth.Uint256Quantity(*baseFee),
1059+
Transactions: []eth.Data{b},
1060+
Withdrawals: &types.Withdrawals{},
1061+
})
10171062

1018-
// Insert the only missing ancient block
1019-
block := getBlock(l2Head1.Number.Uint64() + 1)
1063+
block = getBlock(l2Head1.Number().Uint64() + 2)
10201064
s.NotNil(block)
10211065
insertPayloadFromBlock(block, false)
10221066

packages/taiko-client/driver/preconf_blocks/server.go

+14
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ func (s *PreconfBlockAPIServer) ImportMissingAncientsFromCache(
313313
currentPayload.ParentHash.Hex(),
314314
)
315315
}
316+
if parentPayload.BlockHash == headL1Origin.L2BlockHash {
317+
log.Debug(
318+
"Reached canonical chain head, skip searching for ancients in cache",
319+
"parentNumber", parentPayload.BlockNumber,
320+
"parentHash", parentPayload.BlockHash,
321+
)
322+
break
323+
}
324+
316325
payloadsToImport = append([]*eth.ExecutionPayload{parentPayload}, payloadsToImport...)
317326

318327
// Check if the found parent payload is in the canonical chain,
@@ -507,3 +516,8 @@ func (s *PreconfBlockAPIServer) checkLookaheadHandover(feeRecipient common.Addre
507516
return nil
508517
}
509518
}
519+
520+
// PutPayloadsCache puts the given payload into the payload cache queue, should ONLY be used in testing.
521+
func (s *PreconfBlockAPIServer) PutPayloadsCache(id uint64, payload *eth.ExecutionPayload) {
522+
s.payloadsCache.put(id, payload)
523+
}

0 commit comments

Comments
 (0)