2021-06-28 09:16:32 -05:00
|
|
|
// Copyright 2021 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package gasprice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-08 01:57:51 -05:00
|
|
|
"errors"
|
2021-06-28 09:16:32 -05:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFeeHistory(t *testing.T) {
|
|
|
|
var cases = []struct {
|
|
|
|
pending bool
|
2023-03-23 15:12:37 -05:00
|
|
|
maxHeader, maxBlock uint64
|
|
|
|
count uint64
|
2021-06-28 09:16:32 -05:00
|
|
|
last rpc.BlockNumber
|
|
|
|
percent []float64
|
2021-07-26 22:27:28 -05:00
|
|
|
expFirst uint64
|
2021-06-28 09:16:32 -05:00
|
|
|
expCount int
|
|
|
|
expErr error
|
|
|
|
}{
|
2021-08-23 16:50:24 -05:00
|
|
|
{false, 1000, 1000, 10, 30, nil, 21, 10, nil},
|
|
|
|
{false, 1000, 1000, 10, 30, []float64{0, 10}, 21, 10, nil},
|
|
|
|
{false, 1000, 1000, 10, 30, []float64{20, 10}, 0, 0, errInvalidPercentile},
|
|
|
|
{false, 1000, 1000, 1000000000, 30, nil, 0, 31, nil},
|
|
|
|
{false, 1000, 1000, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil},
|
|
|
|
{false, 1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
|
|
|
|
{true, 1000, 1000, 10, 40, nil, 0, 0, errRequestBeyondHead},
|
2021-06-28 09:16:32 -05:00
|
|
|
{false, 20, 2, 100, rpc.LatestBlockNumber, nil, 13, 20, nil},
|
|
|
|
{false, 20, 2, 100, rpc.LatestBlockNumber, []float64{0, 10}, 31, 2, nil},
|
|
|
|
{false, 20, 2, 100, 32, []float64{0, 10}, 31, 2, nil},
|
2021-08-23 16:50:24 -05:00
|
|
|
{false, 1000, 1000, 1, rpc.PendingBlockNumber, nil, 0, 0, nil},
|
|
|
|
{false, 1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
|
|
|
|
{true, 1000, 1000, 2, rpc.PendingBlockNumber, nil, 32, 2, nil},
|
|
|
|
{true, 1000, 1000, 2, rpc.PendingBlockNumber, []float64{0, 10}, 32, 2, nil},
|
2022-08-01 12:13:30 -05:00
|
|
|
{false, 1000, 1000, 2, rpc.FinalizedBlockNumber, []float64{0, 10}, 24, 2, nil},
|
|
|
|
{false, 1000, 1000, 2, rpc.SafeBlockNumber, []float64{0, 10}, 24, 2, nil},
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
for i, c := range cases {
|
|
|
|
config := Config{
|
|
|
|
MaxHeaderHistory: c.maxHeader,
|
|
|
|
MaxBlockHistory: c.maxBlock,
|
|
|
|
}
|
|
|
|
backend := newTestBackend(t, big.NewInt(16), c.pending)
|
|
|
|
oracle := NewOracle(backend, config)
|
|
|
|
|
|
|
|
first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
|
2022-10-06 06:39:20 -05:00
|
|
|
backend.teardown()
|
2021-06-28 09:16:32 -05:00
|
|
|
expReward := c.expCount
|
|
|
|
if len(c.percent) == 0 {
|
|
|
|
expReward = 0
|
|
|
|
}
|
|
|
|
expBaseFee := c.expCount
|
|
|
|
if expBaseFee != 0 {
|
|
|
|
expBaseFee++
|
|
|
|
}
|
|
|
|
|
2021-07-26 22:27:28 -05:00
|
|
|
if first.Uint64() != c.expFirst {
|
2021-06-28 09:16:32 -05:00
|
|
|
t.Fatalf("Test case %d: first block mismatch, want %d, got %d", i, c.expFirst, first)
|
|
|
|
}
|
|
|
|
if len(reward) != expReward {
|
|
|
|
t.Fatalf("Test case %d: reward array length mismatch, want %d, got %d", i, expReward, len(reward))
|
|
|
|
}
|
|
|
|
if len(baseFee) != expBaseFee {
|
|
|
|
t.Fatalf("Test case %d: baseFee array length mismatch, want %d, got %d", i, expBaseFee, len(baseFee))
|
|
|
|
}
|
|
|
|
if len(ratio) != c.expCount {
|
|
|
|
t.Fatalf("Test case %d: gasUsedRatio array length mismatch, want %d, got %d", i, c.expCount, len(ratio))
|
|
|
|
}
|
2021-07-08 01:57:51 -05:00
|
|
|
if err != c.expErr && !errors.Is(err, c.expErr) {
|
2021-06-28 09:16:32 -05:00
|
|
|
t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|