2021-08-08 07:27:47 +00: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_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-11-10 15:36:04 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/commands"
|
2021-08-08 07:27:47 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/gasprice"
|
2023-03-29 07:27:06 +00:00
|
|
|
"github.com/ledgerwatch/erigon/eth/gasprice/gaspricecfg"
|
2021-08-08 07:27:47 +00:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFeeHistory(t *testing.T) {
|
|
|
|
var cases = []struct {
|
|
|
|
pending bool
|
|
|
|
maxHeader, maxBlock int
|
|
|
|
count int
|
|
|
|
last rpc.BlockNumber
|
|
|
|
percent []float64
|
2021-08-13 03:47:43 +00:00
|
|
|
expFirst uint64
|
2021-08-08 07:27:47 +00:00
|
|
|
expCount int
|
|
|
|
expErr error
|
|
|
|
}{
|
|
|
|
{false, 0, 0, 10, 30, nil, 21, 10, nil},
|
|
|
|
{false, 0, 0, 10, 30, []float64{0, 10}, 21, 10, nil},
|
|
|
|
{false, 0, 0, 10, 30, []float64{20, 10}, 0, 0, gasprice.ErrInvalidPercentile},
|
|
|
|
{false, 0, 0, 1000000000, 30, nil, 0, 31, nil},
|
|
|
|
{false, 0, 0, 1000000000, rpc.LatestBlockNumber, nil, 0, 33, nil},
|
|
|
|
{false, 0, 0, 10, 40, nil, 0, 0, gasprice.ErrRequestBeyondHead},
|
|
|
|
//{true, 0, 0, 10, 40, nil, 0, 0, gasprice.ErrRequestBeyondHead},
|
|
|
|
{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},
|
|
|
|
{false, 0, 0, 1, rpc.PendingBlockNumber, nil, 0, 0, nil},
|
|
|
|
{false, 0, 0, 2, rpc.PendingBlockNumber, nil, 32, 1, nil},
|
|
|
|
//{true, 0, 0, 2, rpc.PendingBlockNumber, nil, 32, 2, nil},
|
|
|
|
//{true, 0, 0, 2, rpc.PendingBlockNumber, []float64{0, 10}, 32, 2, nil},
|
|
|
|
}
|
|
|
|
for i, c := range cases {
|
2023-03-29 07:27:06 +00:00
|
|
|
config := gaspricecfg.Config{
|
2021-08-08 07:27:47 +00:00
|
|
|
MaxHeaderHistory: c.maxHeader,
|
|
|
|
MaxBlockHistory: c.maxBlock,
|
|
|
|
}
|
|
|
|
backend := newTestBackend(t) //, big.NewInt(16), c.pending)
|
2022-11-10 15:36:04 +00:00
|
|
|
cache := commands.NewGasPriceCache()
|
|
|
|
oracle := gasprice.NewOracle(backend, config, cache)
|
2021-08-08 07:27:47 +00:00
|
|
|
|
|
|
|
first, reward, baseFee, ratio, err := oracle.FeeHistory(context.Background(), c.count, c.last, c.percent)
|
|
|
|
|
|
|
|
expReward := c.expCount
|
|
|
|
if len(c.percent) == 0 {
|
|
|
|
expReward = 0
|
|
|
|
}
|
|
|
|
expBaseFee := c.expCount
|
|
|
|
if expBaseFee != 0 {
|
|
|
|
expBaseFee++
|
|
|
|
}
|
|
|
|
|
2021-08-13 03:47:43 +00:00
|
|
|
if first.Uint64() != c.expFirst {
|
2021-08-08 07:27:47 +00: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))
|
|
|
|
}
|
|
|
|
if err != c.expErr && !errors.Is(err, c.expErr) {
|
|
|
|
t.Fatalf("Test case %d: error mismatch, want %v, got %v", i, c.expErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|