github.com/theQRL/go-zond@v0.2.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  	"testing"
    23  
    24  	"github.com/theQRL/go-zond/rpc"
    25  )
    26  
    27  func TestFeeHistory(t *testing.T) {
    28  	var cases = []struct {
    29  		pending             bool
    30  		maxHeader, maxBlock uint64
    31  		count               uint64
    32  		last                rpc.BlockNumber
    33  		percent             []float64
    34  		expFirst            uint64
    35  		expCount            int
    36  		expErr              error
    37  	}{
    38  		{false, 1000, 1000, 10, 30, nil, 21, 10, nil},
    39  		{false, 1000, 1000, 10, 30, []float64{0, 10}, 21, 10, nil},
    40  		{false, 1000, 1000, 10, 30, []float64{20, 10}, 0, 0, errInvalidPercentile},
    41  		{false, 1000, 1000, 1000000000, 30, nil, 0, 31, nil},
    42  		{false, 1000, 1000, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil},
    43  		{false, 1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
    44  		{true, 1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
    45  		{false, 20, 2, 100, rpc.LatestBlockNumber, nil, 13, 20, nil},
    46  		{false, 20, 2, 100, rpc.LatestBlockNumber, []float64{0, 10}, 31, 2, nil},
    47  		{false, 20, 2, 100, 32, []float64{0, 10}, 31, 2, nil},
    48  		{false, 1000, 1000, 1, rpc.PendingBlockNumber, nil, 0, 0, nil},
    49  		{false, 1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
    50  		{true, 1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 2, nil},
    51  		{true, 1000, 1000, 2, rpc.PendingBlockNumber, []float64{0, 10}, 32, 2, nil},
    52  		{false, 1000, 1000, 2, rpc.FinalizedBlockNumber, []float64{0, 10}, 24, 2, nil},
    53  		{false, 1000, 1000, 2, rpc.SafeBlockNumber, []float64{0, 10}, 24, 2, nil},
    54  	}
    55  	for i, c := range cases {
    56  		config := Config{
    57  			MaxHeaderHistory: c.maxHeader,
    58  			MaxBlockHistory:  c.maxBlock,
    59  		}
    60  		backend := newTestBackend(t, c.pending)
    61  		oracle := NewOracle(backend, config)
    62  
    63  		first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
    64  		backend.teardown()
    65  		expReward := c.expCount
    66  		if len(c.percent) == 0 {
    67  			expReward = 0
    68  		}
    69  		expBaseFee := c.expCount
    70  		if expBaseFee != 0 {
    71  			expBaseFee++
    72  		}
    73  
    74  		if first.Uint64() != c.expFirst {
    75  			t.Fatalf("Test case %d: first block mismatch, want %d, got %d", i, c.expFirst, first)
    76  		}
    77  		if len(reward) != expReward {
    78  			t.Fatalf("Test case %d: reward array length mismatch, want %d, got %d", i, expReward, len(reward))
    79  		}
    80  		if len(baseFee) != expBaseFee {
    81  			t.Fatalf("Test case %d: baseFee array length mismatch, want %d, got %d", i, expBaseFee, len(baseFee))
    82  		}
    83  		if len(ratio) != c.expCount {
    84  			t.Fatalf("Test case %d: gasUsedRatio array length mismatch, want %d, got %d", i, c.expCount, len(ratio))
    85  		}
    86  		if err != c.expErr && !errors.Is(err, c.expErr) {
    87  			t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err)
    88  		}
    89  	}
    90  }