github.com/theQRL/go-zond@v0.1.1/zond/gasprice/feehistory_test.go (about)

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