github.com/MetalBlockchain/subnet-evm@v0.4.9/eth/gasprice/feehistory_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2021 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package gasprice
    28  
    29  import (
    30  	"context"
    31  	"errors"
    32  	"math/big"
    33  	"testing"
    34  
    35  	"github.com/MetalBlockchain/subnet-evm/core"
    36  	"github.com/MetalBlockchain/subnet-evm/core/types"
    37  	"github.com/stretchr/testify/require"
    38  
    39  	"github.com/MetalBlockchain/subnet-evm/params"
    40  	"github.com/MetalBlockchain/subnet-evm/rpc"
    41  	"github.com/ethereum/go-ethereum/common"
    42  )
    43  
    44  func TestFeeHistory(t *testing.T) {
    45  	var cases = []struct {
    46  		pending      bool
    47  		maxCallBlock int
    48  		maxBlock     int
    49  		count        int
    50  		last         rpc.BlockNumber
    51  		percent      []float64
    52  		expFirst     uint64
    53  		expCount     int
    54  		expErr       error
    55  	}{
    56  		// Standard go-ethereum tests
    57  		{false, 0, 1000, 10, 30, nil, 21, 10, nil},
    58  		{false, 0, 1000, 10, 30, []float64{0, 10}, 21, 10, nil},
    59  		{false, 0, 1000, 10, 30, []float64{20, 10}, 0, 0, errInvalidPercentile},
    60  		{false, 0, 1000, 1000000000, 30, nil, 0, 31, nil},
    61  		{false, 0, 1000, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil},
    62  		{false, 0, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
    63  		{true, 0, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
    64  		{false, 0, 2, 100, rpc.LatestBlockNumber, []float64{0, 10}, 31, 2, nil},
    65  		{false, 0, 2, 100, 32, []float64{0, 10}, 31, 2, nil},
    66  		{false, 0, 1000, 1, rpc.PendingBlockNumber, nil, 0, 0, nil},
    67  		{false, 0, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
    68  		{true, 0, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
    69  		{true, 0, 1000, 2, rpc.PendingBlockNumber, []float64{0, 10}, 32, 1, nil},
    70  
    71  		// Modified tests
    72  		{false, 0, 2, 100, rpc.LatestBlockNumber, nil, 31, 2, nil},    // apply block lookback limits even if only headers required
    73  		{false, 0, 10, 10, 30, nil, 23, 8, nil},                       // limit lookback based on maxHistory from latest block
    74  		{false, 0, 33, 1000000000, 10, nil, 0, 11, nil},               // handle truncation edge case
    75  		{false, 0, 2, 10, 20, nil, 0, 0, errBeyondHistoricalLimit},    // query behind historical limit
    76  		{false, 10, 30, 100, rpc.LatestBlockNumber, nil, 23, 10, nil}, // ensure [MaxCallBlockHistory] is honored
    77  	}
    78  	for i, c := range cases {
    79  		config := Config{
    80  			MaxCallBlockHistory: c.maxCallBlock,
    81  			MaxBlockHistory:     c.maxBlock,
    82  		}
    83  		tip := big.NewInt(1 * params.GWei)
    84  		backend := newTestBackendFakerEngine(t, params.TestChainConfig, 32, func(i int, b *core.BlockGen) {
    85  			signer := types.LatestSigner(params.TestChainConfig)
    86  
    87  			b.SetCoinbase(common.Address{1})
    88  
    89  			baseFee := b.BaseFee()
    90  			feeCap := new(big.Int).Add(baseFee, tip)
    91  
    92  			var tx *types.Transaction
    93  			txdata := &types.DynamicFeeTx{
    94  				ChainID:   params.TestChainConfig.ChainID,
    95  				Nonce:     b.TxNonce(addr),
    96  				To:        &common.Address{},
    97  				Gas:       params.TxGas,
    98  				GasFeeCap: feeCap,
    99  				GasTipCap: tip,
   100  				Data:      []byte{},
   101  			}
   102  			tx = types.NewTx(txdata)
   103  			tx, err := types.SignTx(tx, signer, key)
   104  			if err != nil {
   105  				t.Fatalf("failed to create tx: %v", err)
   106  			}
   107  			b.AddTx(tx)
   108  		})
   109  		oracle, err := NewOracle(backend, config)
   110  		require.NoError(t, err)
   111  
   112  		first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
   113  
   114  		expReward := c.expCount
   115  		if len(c.percent) == 0 {
   116  			expReward = 0
   117  		}
   118  		expBaseFee := c.expCount
   119  
   120  		if first.Uint64() != c.expFirst {
   121  			t.Fatalf("Test case %d: first block mismatch, want %d, got %d", i, c.expFirst, first)
   122  		}
   123  		if len(reward) != expReward {
   124  			t.Fatalf("Test case %d: reward array length mismatch, want %d, got %d", i, expReward, len(reward))
   125  		}
   126  		if len(baseFee) != expBaseFee {
   127  			t.Fatalf("Test case %d: baseFee array length mismatch, want %d, got %d", i, expBaseFee, len(baseFee))
   128  		}
   129  		if len(ratio) != c.expCount {
   130  			t.Fatalf("Test case %d: gasUsedRatio array length mismatch, want %d, got %d", i, c.expCount, len(ratio))
   131  		}
   132  		if err != c.expErr && !errors.Is(err, c.expErr) {
   133  			t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err)
   134  		}
   135  	}
   136  }