|
| 1 | +package ethapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestGasCap(t *testing.T) { |
| 12 | + |
| 13 | + var ( |
| 14 | + gas10M = hexutil.Uint64(10_000_000) |
| 15 | + gasMaxUint64 = hexutil.Uint64(math.MaxUint64) |
| 16 | + ) |
| 17 | + |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + argGas *hexutil.Uint64 |
| 21 | + globalGasCap uint64 |
| 22 | + expectedGas uint64 |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "gas cap 0 and arg gas nil", |
| 26 | + globalGasCap: 0, |
| 27 | + argGas: nil, |
| 28 | + expectedGas: math.MaxInt64, |
| 29 | + }, { |
| 30 | + name: "gas cap 0 and arg gas 10M", |
| 31 | + globalGasCap: 0, |
| 32 | + argGas: &gas10M, |
| 33 | + expectedGas: 10_000_000, |
| 34 | + }, { |
| 35 | + name: "gas cap 0 and arg gas maxUint64", |
| 36 | + globalGasCap: 0, |
| 37 | + argGas: &gasMaxUint64, |
| 38 | + expectedGas: math.MaxInt64, |
| 39 | + }, { |
| 40 | + name: "gas cap 50M and arg gas nil", |
| 41 | + globalGasCap: 50_000_000, |
| 42 | + argGas: nil, |
| 43 | + expectedGas: 50_000_000, |
| 44 | + }, { |
| 45 | + name: "gas cap 50M and arg gas 10M", |
| 46 | + globalGasCap: 50_000_000, |
| 47 | + argGas: &gas10M, |
| 48 | + expectedGas: 10_000_000, |
| 49 | + }, { |
| 50 | + name: "gas cap 50M and arg gas maxUint64", |
| 51 | + globalGasCap: 50_000_000, |
| 52 | + argGas: &gasMaxUint64, |
| 53 | + expectedGas: 50_000_000, |
| 54 | + }, { |
| 55 | + name: "gas cap maxUint64 and arg gas 10M", |
| 56 | + globalGasCap: math.MaxUint64, |
| 57 | + argGas: &gas10M, |
| 58 | + expectedGas: 10_000_000, |
| 59 | + }, { |
| 60 | + name: "gas cap maxUint64 and arg gas maxUint64", |
| 61 | + globalGasCap: math.MaxUint64, |
| 62 | + argGas: &gasMaxUint64, |
| 63 | + expectedGas: math.MaxInt64, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, test := range tests { |
| 68 | + args := TransactionArgs{Gas: test.argGas} |
| 69 | + |
| 70 | + msg, err := args.ToMessage(test.globalGasCap, nil) |
| 71 | + |
| 72 | + require.Nil(t, err) |
| 73 | + require.Equal(t, test.expectedGas, msg.GasLimit, test.name) |
| 74 | + } |
| 75 | +} |
0 commit comments