github.com/klaytn/klaytn@v1.12.1/node/cn/gasprice/feehistory_test.go (about)

     1  // Modifications Copyright 2022 The Klaytn Authors
     2  // Copyright 2021 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from eth/gasprice/feehistory.go (2021/11/09).
    19  // Modified and improved for the klaytn development
    20  
    21  package gasprice
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"testing"
    27  
    28  	"github.com/klaytn/klaytn/networks/rpc"
    29  )
    30  
    31  func TestFeeHistory(t *testing.T) {
    32  	cases := []struct {
    33  		maxHeader, maxBlock int
    34  		count               int
    35  		last                rpc.BlockNumber
    36  		percent             []float64
    37  		expFirst            uint64
    38  		expCount            int
    39  		expErr              error
    40  	}{
    41  		{1000, 1000, 10, 30, nil, 21, 10, nil},
    42  		{1000, 1000, 10, 30, []float64{0, 10}, 21, 10, nil},
    43  		{1000, 1000, 10, 30, []float64{20, 10}, 0, 0, errInvalidPercentile},
    44  		{1000, 1000, 1000000000, 30, nil, 0, 31, nil},
    45  		{1000, 1000, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil},
    46  		{1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
    47  		{1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
    48  		{20, 2, 100, rpc.LatestBlockNumber, nil, 13, 20, nil},
    49  		{20, 2, 100, rpc.LatestBlockNumber, []float64{0, 10}, 31, 2, nil},
    50  		{20, 2, 100, 32, []float64{0, 10}, 31, 2, nil},
    51  		{1000, 1000, 1, rpc.PendingBlockNumber, nil, 0, 0, nil},
    52  		{1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
    53  	}
    54  	for i, c := range cases {
    55  		config := Config{
    56  			MaxHeaderHistory: c.maxHeader,
    57  			MaxBlockHistory:  c.maxBlock,
    58  		}
    59  		backend := newTestBackend(t)
    60  		oracle := NewOracle(backend, config, nil)
    61  
    62  		first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
    63  
    64  		expReward := c.expCount
    65  		if len(c.percent) == 0 {
    66  			expReward = 0
    67  		}
    68  		expBaseFee := c.expCount
    69  		if expBaseFee != 0 {
    70  			expBaseFee++
    71  		}
    72  
    73  		if first.Uint64() != c.expFirst {
    74  			t.Fatalf("Test case %d: first block mismatch, want %d, got %d", i, c.expFirst, first)
    75  		}
    76  		if len(reward) != expReward {
    77  			t.Fatalf("Test case %d: reward array length mismatch, want %d, got %d", i, expReward, len(reward))
    78  		}
    79  		if len(baseFee) != expBaseFee {
    80  			t.Fatalf("Test case %d: baseFee array length mismatch, want %d, got %d", i, expBaseFee, len(baseFee))
    81  		}
    82  		if len(ratio) != c.expCount {
    83  			t.Fatalf("Test case %d: gasUsedRatio array length mismatch, want %d, got %d", i, c.expCount, len(ratio))
    84  		}
    85  		if err != c.expErr && !errors.Is(err, c.expErr) {
    86  			t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err)
    87  		}
    88  	}
    89  }