|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum" |
| 10 | + "github.com/ethereum/go-ethereum/beacon/engine" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/core/types" |
| 13 | + "github.com/rs/zerolog" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestSetFinal_UnavailableBlock(t *testing.T) { |
| 18 | + prunedErr := &testRPCError{code: prunedHistoryErrorCode, msg: "pruned history unavailable"} |
| 19 | + |
| 20 | + specs := map[string]struct { |
| 21 | + lookupErr error |
| 22 | + head *big.Int |
| 23 | + headErr error |
| 24 | + expectErr bool |
| 25 | + }{ |
| 26 | + "skip_pruned_below_head": { |
| 27 | + lookupErr: prunedErr, |
| 28 | + head: big.NewInt(1_000), |
| 29 | + }, |
| 30 | + "skip_not_found_below_head": { |
| 31 | + lookupErr: ethereum.NotFound, |
| 32 | + head: big.NewInt(1_000), |
| 33 | + }, |
| 34 | + "error_transient_lookup_below_head": { |
| 35 | + lookupErr: errors.New("i/o timeout"), |
| 36 | + head: big.NewInt(1_000), |
| 37 | + expectErr: true, |
| 38 | + }, |
| 39 | + "error_other_rpc_code_below_head": { |
| 40 | + lookupErr: &testRPCError{code: -32000, msg: "internal error"}, |
| 41 | + head: big.NewInt(1_000), |
| 42 | + expectErr: true, |
| 43 | + }, |
| 44 | + "error_pruned_above_head": { |
| 45 | + lookupErr: prunedErr, |
| 46 | + head: big.NewInt(50), |
| 47 | + expectErr: true, |
| 48 | + }, |
| 49 | + "error_pruned_head_unavailable": { |
| 50 | + lookupErr: prunedErr, |
| 51 | + headErr: errors.New("connection refused"), |
| 52 | + expectErr: true, |
| 53 | + }, |
| 54 | + } |
| 55 | + for name, spec := range specs { |
| 56 | + t.Run(name, func(t *testing.T) { |
| 57 | + engineRPC := &mockSetFinalEngineRPCClient{} |
| 58 | + client := &EngineClient{ |
| 59 | + engineClient: engineRPC, |
| 60 | + ethClient: mockSetFinalEthRPCClient{ |
| 61 | + lookupErr: spec.lookupErr, |
| 62 | + head: spec.head, |
| 63 | + headErr: spec.headErr, |
| 64 | + }, |
| 65 | + logger: zerolog.Nop(), |
| 66 | + } |
| 67 | + |
| 68 | + err := client.SetFinal(t.Context(), 100) |
| 69 | + |
| 70 | + if spec.expectErr { |
| 71 | + require.Error(t, err) |
| 72 | + } else { |
| 73 | + require.NoError(t, err) |
| 74 | + } |
| 75 | + require.Zero(t, engineRPC.forkchoiceCalls) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// mockSetFinalEthRPCClient fails every numbered lookup with lookupErr and |
| 81 | +// serves the configured head for latest-block queries. |
| 82 | +type mockSetFinalEthRPCClient struct { |
| 83 | + lookupErr error |
| 84 | + head *big.Int |
| 85 | + headErr error |
| 86 | +} |
| 87 | + |
| 88 | +func (m mockSetFinalEthRPCClient) HeaderByNumber(_ context.Context, number *big.Int) (*types.Header, error) { |
| 89 | + if number != nil { |
| 90 | + return nil, m.lookupErr |
| 91 | + } |
| 92 | + if m.headErr != nil { |
| 93 | + return nil, m.headErr |
| 94 | + } |
| 95 | + return &types.Header{Number: m.head}, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (mockSetFinalEthRPCClient) GetTxs(_ context.Context) ([]string, error) { |
| 99 | + return nil, errors.New("unexpected GetTxs call") |
| 100 | +} |
| 101 | + |
| 102 | +func (mockSetFinalEthRPCClient) GetNextProposer(_ context.Context, _ *big.Int) (common.Hash, error) { |
| 103 | + return common.Hash{}, errors.New("unexpected GetNextProposer call") |
| 104 | +} |
| 105 | + |
| 106 | +type mockSetFinalEngineRPCClient struct { |
| 107 | + forkchoiceCalls int |
| 108 | +} |
| 109 | + |
| 110 | +func (m *mockSetFinalEngineRPCClient) ForkchoiceUpdated(_ context.Context, _ engine.ForkchoiceStateV1, _ map[string]any) (*engine.ForkChoiceResponse, error) { |
| 111 | + m.forkchoiceCalls++ |
| 112 | + return nil, errors.New("unexpected ForkchoiceUpdated call") |
| 113 | +} |
| 114 | + |
| 115 | +func (m *mockSetFinalEngineRPCClient) GetPayload(_ context.Context, _ engine.PayloadID) (*EnginePayloadEnvelope, error) { |
| 116 | + return nil, errors.New("unexpected GetPayload call") |
| 117 | +} |
| 118 | + |
| 119 | +func (m *mockSetFinalEngineRPCClient) NewPayload(_ context.Context, _ *EnginePayloadEnvelope, _ []string, _ string, _ [][]byte) (*engine.PayloadStatusV1, error) { |
| 120 | + return nil, errors.New("unexpected NewPayload call") |
| 121 | +} |
0 commit comments