|
| 1 | +// Copyright 2025 The Swarm Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package wrapped_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "math/big" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/ethereum/go-ethereum/core/types" |
| 14 | + "github.com/ethersphere/bee/v2/pkg/transaction/backendmock" |
| 15 | + "github.com/ethersphere/bee/v2/pkg/transaction/wrapped" |
| 16 | + "github.com/google/go-cmp/cmp" |
| 17 | +) |
| 18 | + |
| 19 | +func TestSuggestedFeeAndTip(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + var ( |
| 23 | + ctx = context.Background() |
| 24 | + minimumGasTipCap = uint64(10) |
| 25 | + baseFee = big.NewInt(100) |
| 26 | + ) |
| 27 | + |
| 28 | + testCases := []struct { |
| 29 | + name string |
| 30 | + gasPrice *big.Int |
| 31 | + boostPercent int |
| 32 | + mockSuggestGasTip *big.Int |
| 33 | + mockSuggestGasErr error |
| 34 | + mockHeader *types.Header |
| 35 | + mockHeaderErr error |
| 36 | + wantGasFeeCap *big.Int |
| 37 | + wantGasTipCap *big.Int |
| 38 | + wantErr error |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "with gas price", |
| 42 | + gasPrice: big.NewInt(1000), |
| 43 | + wantGasFeeCap: big.NewInt(1000), |
| 44 | + wantGasTipCap: big.NewInt(1000), |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "suggest tip error", |
| 48 | + mockSuggestGasErr: errors.New("suggest tip error"), |
| 49 | + wantErr: errors.New("failed to suggest gas tip cap: suggest tip error"), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "header error", |
| 53 | + mockSuggestGasTip: big.NewInt(20), |
| 54 | + mockHeaderErr: errors.New("header error"), |
| 55 | + wantErr: errors.New("failed to get latest block header: header error"), |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "no base fee", |
| 59 | + mockSuggestGasTip: big.NewInt(20), |
| 60 | + mockHeader: &types.Header{}, |
| 61 | + wantErr: wrapped.ErrEIP1559NotSupported, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "suggested tip > minimum", |
| 65 | + mockSuggestGasTip: big.NewInt(20), |
| 66 | + mockHeader: &types.Header{BaseFee: baseFee}, |
| 67 | + wantGasFeeCap: big.NewInt(220), // 2*100 + 20 |
| 68 | + wantGasTipCap: big.NewInt(20), |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "suggested tip < minimum", |
| 72 | + mockSuggestGasTip: big.NewInt(5), |
| 73 | + mockHeader: &types.Header{BaseFee: baseFee}, |
| 74 | + wantGasFeeCap: big.NewInt(210), // 2*100 + 10 |
| 75 | + wantGasTipCap: big.NewInt(10), |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "with boost", |
| 79 | + boostPercent: 10, |
| 80 | + mockSuggestGasTip: big.NewInt(20), |
| 81 | + mockHeader: &types.Header{BaseFee: baseFee}, |
| 82 | + wantGasFeeCap: big.NewInt(222), // 2*100 + 22 |
| 83 | + wantGasTipCap: big.NewInt(22), // 20 * 1.1 |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, tc := range testCases { |
| 88 | + t.Run(tc.name, func(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + |
| 91 | + backend := wrapped.NewBackend( |
| 92 | + backendmock.New( |
| 93 | + backendmock.WithSuggestGasTipCapFunc(func(ctx context.Context) (*big.Int, error) { |
| 94 | + return tc.mockSuggestGasTip, tc.mockSuggestGasErr |
| 95 | + }), |
| 96 | + backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) { |
| 97 | + return tc.mockHeader, tc.mockHeaderErr |
| 98 | + }), |
| 99 | + ), |
| 100 | + minimumGasTipCap, |
| 101 | + ) |
| 102 | + |
| 103 | + gasFeeCap, gasTipCap, err := backend.SuggestedFeeAndTip(ctx, tc.gasPrice, tc.boostPercent) |
| 104 | + |
| 105 | + if tc.wantErr != nil { |
| 106 | + if err == nil { |
| 107 | + t.Fatal("expected error but got none") |
| 108 | + } |
| 109 | + if err.Error() != tc.wantErr.Error() { |
| 110 | + t.Fatalf("unexpected error. want %v, got %v", tc.wantErr, err) |
| 111 | + } |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("unexpected error: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + if diff := cmp.Diff(tc.wantGasFeeCap.String(), gasFeeCap.String()); diff != "" { |
| 120 | + t.Errorf("gasFeeCap mismatch (-want +got):\n%s", diff) |
| 121 | + } |
| 122 | + |
| 123 | + if diff := cmp.Diff(tc.wantGasTipCap.String(), gasTipCap.String()); diff != "" { |
| 124 | + t.Errorf("gasTipCap mismatch (-want +got):\n%s", diff) |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
0 commit comments