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-08-23 16:50:24 -05:00
|
|
|
"encoding/binary"
|
2021-06-28 09:16:32 -05:00
|
|
|
"errors"
|
2021-07-08 01:57:51 -05:00
|
|
|
"fmt"
|
2021-08-23 16:50:24 -05:00
|
|
|
"math"
|
2021-06-28 09:16:32 -05:00
|
|
|
"math/big"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2021-07-26 22:27:28 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-08-01 04:58:45 -05:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
|
2021-06-28 09:16:32 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2023-06-19 16:38:57 -05:00
|
|
|
"golang.org/x/exp/slices"
|
2021-06-28 09:16:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-07-08 01:57:51 -05:00
|
|
|
errInvalidPercentile = errors.New("invalid reward percentile")
|
|
|
|
errRequestBeyondHead = errors.New("request beyond head block")
|
2021-06-28 09:16:32 -05:00
|
|
|
)
|
|
|
|
|
2021-07-08 01:57:51 -05:00
|
|
|
const (
|
|
|
|
// maxBlockFetchers is the max number of goroutines to spin up to pull blocks
|
|
|
|
// for the fee history calculation (mostly relevant for LES).
|
|
|
|
maxBlockFetchers = 4
|
|
|
|
)
|
2021-06-28 09:16:32 -05:00
|
|
|
|
|
|
|
// blockFees represents a single block for processing
|
|
|
|
type blockFees struct {
|
|
|
|
// set by the caller
|
2021-07-26 22:27:28 -05:00
|
|
|
blockNumber uint64
|
2021-06-28 09:16:32 -05:00
|
|
|
header *types.Header
|
|
|
|
block *types.Block // only set if reward percentiles are requested
|
|
|
|
receipts types.Receipts
|
|
|
|
// filled by processBlock
|
2021-08-23 16:50:24 -05:00
|
|
|
results processedFees
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2022-11-14 08:41:56 -06:00
|
|
|
type cacheKey struct {
|
|
|
|
number uint64
|
|
|
|
percentiles string
|
|
|
|
}
|
|
|
|
|
|
|
|
// processedFees contains the results of a processed block.
|
2021-08-23 16:50:24 -05:00
|
|
|
type processedFees struct {
|
2021-06-28 09:16:32 -05:00
|
|
|
reward []*big.Int
|
|
|
|
baseFee, nextBaseFee *big.Int
|
|
|
|
gasUsedRatio float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGasAndReward is sorted in ascending order based on reward
|
2023-06-19 16:38:57 -05:00
|
|
|
type txGasAndReward struct {
|
|
|
|
gasUsed uint64
|
|
|
|
reward *big.Int
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// processBlock takes a blockFees structure with the blockNumber, the header and optionally
|
|
|
|
// the block field filled in, retrieves the block from the backend if not present yet and
|
|
|
|
// fills in the rest of the fields.
|
|
|
|
func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
|
|
|
|
chainconfig := oracle.backend.ChainConfig()
|
2021-08-23 16:50:24 -05:00
|
|
|
if bf.results.baseFee = bf.header.BaseFee; bf.results.baseFee == nil {
|
|
|
|
bf.results.baseFee = new(big.Int)
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
if chainconfig.IsLondon(big.NewInt(int64(bf.blockNumber + 1))) {
|
2023-08-01 04:58:45 -05:00
|
|
|
bf.results.nextBaseFee = eip1559.CalcBaseFee(chainconfig, bf.header)
|
2021-06-28 09:16:32 -05:00
|
|
|
} else {
|
2021-08-23 16:50:24 -05:00
|
|
|
bf.results.nextBaseFee = new(big.Int)
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2021-08-23 16:50:24 -05:00
|
|
|
bf.results.gasUsedRatio = float64(bf.header.GasUsed) / float64(bf.header.GasLimit)
|
2021-06-28 09:16:32 -05:00
|
|
|
if len(percentiles) == 0 {
|
|
|
|
// rewards were not requested, return null
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if bf.block == nil || (bf.receipts == nil && len(bf.block.Transactions()) != 0) {
|
|
|
|
log.Error("Block or receipts are missing while reward percentiles are requested")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:50:24 -05:00
|
|
|
bf.results.reward = make([]*big.Int, len(percentiles))
|
2021-06-28 09:16:32 -05:00
|
|
|
if len(bf.block.Transactions()) == 0 {
|
|
|
|
// return an all zero row if there are no transactions to gather data from
|
2021-08-23 16:50:24 -05:00
|
|
|
for i := range bf.results.reward {
|
|
|
|
bf.results.reward[i] = new(big.Int)
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-19 16:38:57 -05:00
|
|
|
sorter := make([]txGasAndReward, len(bf.block.Transactions()))
|
2021-06-28 09:16:32 -05:00
|
|
|
for i, tx := range bf.block.Transactions() {
|
|
|
|
reward, _ := tx.EffectiveGasTip(bf.block.BaseFee())
|
|
|
|
sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward}
|
|
|
|
}
|
2023-08-11 17:04:12 -05:00
|
|
|
slices.SortStableFunc(sorter, func(a, b txGasAndReward) int {
|
|
|
|
return a.reward.Cmp(b.reward)
|
2023-06-19 16:38:57 -05:00
|
|
|
})
|
2021-06-28 09:16:32 -05:00
|
|
|
|
|
|
|
var txIndex int
|
|
|
|
sumGasUsed := sorter[0].gasUsed
|
|
|
|
|
|
|
|
for i, p := range percentiles {
|
|
|
|
thresholdGasUsed := uint64(float64(bf.block.GasUsed()) * p / 100)
|
|
|
|
for sumGasUsed < thresholdGasUsed && txIndex < len(bf.block.Transactions())-1 {
|
|
|
|
txIndex++
|
|
|
|
sumGasUsed += sorter[txIndex].gasUsed
|
|
|
|
}
|
2021-08-23 16:50:24 -05:00
|
|
|
bf.results.reward[i] = sorter[txIndex].reward
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveBlockRange resolves the specified block range to absolute block numbers while also
|
|
|
|
// enforcing backend specific limitations. The pending block and corresponding receipts are
|
|
|
|
// also returned if requested and available.
|
|
|
|
// Note: an error is only returned if retrieving the head header has failed. If there are no
|
|
|
|
// retrievable blocks in the specified range then zero block count is returned with no error.
|
2023-03-23 15:12:37 -05:00
|
|
|
func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNumber, blocks uint64) (*types.Block, []*types.Receipt, uint64, uint64, error) {
|
2021-06-28 09:16:32 -05:00
|
|
|
var (
|
2022-08-01 12:13:30 -05:00
|
|
|
headBlock *types.Header
|
2021-06-28 09:16:32 -05:00
|
|
|
pendingBlock *types.Block
|
|
|
|
pendingReceipts types.Receipts
|
2022-08-01 12:13:30 -05:00
|
|
|
err error
|
2021-06-28 09:16:32 -05:00
|
|
|
)
|
2022-08-01 12:13:30 -05:00
|
|
|
|
|
|
|
// Get the chain's current head.
|
|
|
|
if headBlock, err = oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err != nil {
|
|
|
|
return nil, nil, 0, 0, err
|
|
|
|
}
|
|
|
|
head := rpc.BlockNumber(headBlock.Number.Uint64())
|
|
|
|
|
|
|
|
// Fail if request block is beyond the chain's current head.
|
|
|
|
if head < reqEnd {
|
|
|
|
return nil, nil, 0, 0, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, reqEnd, head)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve block tag.
|
|
|
|
if reqEnd < 0 {
|
|
|
|
var (
|
|
|
|
resolved *types.Header
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch reqEnd {
|
|
|
|
case rpc.PendingBlockNumber:
|
|
|
|
if pendingBlock, pendingReceipts = oracle.backend.PendingBlockAndReceipts(); pendingBlock != nil {
|
|
|
|
resolved = pendingBlock.Header()
|
|
|
|
} else {
|
|
|
|
// Pending block not supported by backend, process only until latest block.
|
|
|
|
resolved = headBlock
|
|
|
|
|
|
|
|
// Update total blocks to return to account for this.
|
|
|
|
blocks--
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2022-08-01 12:13:30 -05:00
|
|
|
case rpc.LatestBlockNumber:
|
|
|
|
// Retrieved above.
|
|
|
|
resolved = headBlock
|
|
|
|
case rpc.SafeBlockNumber:
|
|
|
|
resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.SafeBlockNumber)
|
|
|
|
case rpc.FinalizedBlockNumber:
|
|
|
|
resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.FinalizedBlockNumber)
|
|
|
|
case rpc.EarliestBlockNumber:
|
|
|
|
resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.EarliestBlockNumber)
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2022-08-01 12:13:30 -05:00
|
|
|
if resolved == nil || err != nil {
|
2021-06-28 09:16:32 -05:00
|
|
|
return nil, nil, 0, 0, err
|
|
|
|
}
|
2022-08-01 12:13:30 -05:00
|
|
|
// Absolute number resolved.
|
|
|
|
reqEnd = rpc.BlockNumber(resolved.Number.Uint64())
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2022-08-01 12:13:30 -05:00
|
|
|
|
|
|
|
// If there are no blocks to return, short circuit.
|
|
|
|
if blocks == 0 {
|
|
|
|
return nil, nil, 0, 0, nil
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2022-08-01 12:13:30 -05:00
|
|
|
// Ensure not trying to retrieve before genesis.
|
2023-03-23 15:12:37 -05:00
|
|
|
if uint64(reqEnd+1) < blocks {
|
|
|
|
blocks = uint64(reqEnd + 1)
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2022-08-01 12:13:30 -05:00
|
|
|
return pendingBlock, pendingReceipts, uint64(reqEnd), blocks, nil
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
|
|
|
|
// The range can be specified either with absolute block numbers or ending with the latest
|
|
|
|
// or pending block. Backends may or may not support gathering data from the pending block
|
|
|
|
// or blocks older than a certain age (specified in maxHistory). The first block of the
|
|
|
|
// actually processed range is returned to avoid ambiguity when parts of the requested range
|
|
|
|
// are not available or when the head has changed during processing this request.
|
|
|
|
// Three arrays are returned based on the processed blocks:
|
2022-09-10 06:25:40 -05:00
|
|
|
// - reward: the requested percentiles of effective priority fees per gas of transactions in each
|
|
|
|
// block, sorted in ascending order and weighted by gas used.
|
|
|
|
// - baseFee: base fee per gas in the given block
|
|
|
|
// - gasUsedRatio: gasUsed/gasLimit in the given block
|
|
|
|
//
|
2021-06-28 09:16:32 -05:00
|
|
|
// Note: baseFee includes the next block after the newest of the returned range, because this
|
|
|
|
// value can be derived from the newest block.
|
2023-03-23 15:12:37 -05:00
|
|
|
func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedLastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
|
2021-07-08 01:57:51 -05:00
|
|
|
if blocks < 1 {
|
2021-07-26 22:27:28 -05:00
|
|
|
return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2021-08-23 16:50:24 -05:00
|
|
|
maxFeeHistory := oracle.maxHeaderHistory
|
|
|
|
if len(rewardPercentiles) != 0 {
|
|
|
|
maxFeeHistory = oracle.maxBlockHistory
|
|
|
|
}
|
2021-07-08 01:57:51 -05:00
|
|
|
if blocks > maxFeeHistory {
|
|
|
|
log.Warn("Sanitizing fee history length", "requested", blocks, "truncated", maxFeeHistory)
|
|
|
|
blocks = maxFeeHistory
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
for i, p := range rewardPercentiles {
|
2021-07-08 01:57:51 -05:00
|
|
|
if p < 0 || p > 100 {
|
2021-07-26 22:27:28 -05:00
|
|
|
return common.Big0, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p)
|
2021-07-08 01:57:51 -05:00
|
|
|
}
|
2024-02-08 06:34:38 -06:00
|
|
|
if i > 0 && p <= rewardPercentiles[i-1] {
|
|
|
|
return common.Big0, nil, nil, nil, fmt.Errorf("%w: #%d:%f >= #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p)
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
pendingBlock *types.Block
|
2021-07-08 01:57:51 -05:00
|
|
|
pendingReceipts []*types.Receipt
|
|
|
|
err error
|
2021-06-28 09:16:32 -05:00
|
|
|
)
|
2021-08-23 16:50:24 -05:00
|
|
|
pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks)
|
2021-07-08 01:57:51 -05:00
|
|
|
if err != nil || blocks == 0 {
|
2021-07-26 22:27:28 -05:00
|
|
|
return common.Big0, nil, nil, nil, err
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2023-03-23 15:12:37 -05:00
|
|
|
oldestBlock := lastBlock + 1 - blocks
|
2021-06-28 09:16:32 -05:00
|
|
|
|
2023-04-25 05:06:50 -05:00
|
|
|
var next atomic.Uint64
|
|
|
|
next.Store(oldestBlock)
|
|
|
|
results := make(chan *blockFees, blocks)
|
|
|
|
|
2021-08-23 16:50:24 -05:00
|
|
|
percentileKey := make([]byte, 8*len(rewardPercentiles))
|
|
|
|
for i, p := range rewardPercentiles {
|
|
|
|
binary.LittleEndian.PutUint64(percentileKey[i*8:(i+1)*8], math.Float64bits(p))
|
|
|
|
}
|
2023-03-23 15:12:37 -05:00
|
|
|
for i := 0; i < maxBlockFetchers && i < int(blocks); i++ {
|
2021-06-28 09:16:32 -05:00
|
|
|
go func() {
|
|
|
|
for {
|
2021-07-08 01:57:51 -05:00
|
|
|
// Retrieve the next block number to fetch with this goroutine
|
2023-04-25 05:06:50 -05:00
|
|
|
blockNumber := next.Add(1) - 1
|
2021-07-08 01:57:51 -05:00
|
|
|
if blockNumber > lastBlock {
|
2021-06-28 09:16:32 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-08 01:57:51 -05:00
|
|
|
fees := &blockFees{blockNumber: blockNumber}
|
2021-07-26 22:27:28 -05:00
|
|
|
if pendingBlock != nil && blockNumber >= pendingBlock.NumberU64() {
|
2021-07-08 01:57:51 -05:00
|
|
|
fees.block, fees.receipts = pendingBlock, pendingReceipts
|
2021-08-23 16:50:24 -05:00
|
|
|
fees.header = fees.block.Header()
|
|
|
|
oracle.processBlock(fees, rewardPercentiles)
|
|
|
|
results <- fees
|
2021-06-28 09:16:32 -05:00
|
|
|
} else {
|
2022-11-14 08:41:56 -06:00
|
|
|
cacheKey := cacheKey{number: blockNumber, percentiles: string(percentileKey)}
|
2021-08-23 16:50:24 -05:00
|
|
|
|
|
|
|
if p, ok := oracle.historyCache.Get(cacheKey); ok {
|
2022-11-14 08:41:56 -06:00
|
|
|
fees.results = p
|
2021-08-23 16:50:24 -05:00
|
|
|
results <- fees
|
2021-06-28 09:16:32 -05:00
|
|
|
} else {
|
2021-08-23 16:50:24 -05:00
|
|
|
if len(rewardPercentiles) != 0 {
|
|
|
|
fees.block, fees.err = oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNumber))
|
|
|
|
if fees.block != nil && fees.err == nil {
|
|
|
|
fees.receipts, fees.err = oracle.backend.GetReceipts(ctx, fees.block.Hash())
|
|
|
|
fees.header = fees.block.Header()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fees.header, fees.err = oracle.backend.HeaderByNumber(ctx, rpc.BlockNumber(blockNumber))
|
|
|
|
}
|
|
|
|
if fees.header != nil && fees.err == nil {
|
|
|
|
oracle.processBlock(fees, rewardPercentiles)
|
|
|
|
if fees.err == nil {
|
|
|
|
oracle.historyCache.Add(cacheKey, fees.results)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send to results even if empty to guarantee that blocks items are sent in total
|
|
|
|
results <- fees
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-07-08 01:57:51 -05:00
|
|
|
var (
|
|
|
|
reward = make([][]*big.Int, blocks)
|
|
|
|
baseFee = make([]*big.Int, blocks+1)
|
|
|
|
gasUsedRatio = make([]float64, blocks)
|
|
|
|
firstMissing = blocks
|
|
|
|
)
|
|
|
|
for ; blocks > 0; blocks-- {
|
|
|
|
fees := <-results
|
|
|
|
if fees.err != nil {
|
2021-07-26 22:27:28 -05:00
|
|
|
return common.Big0, nil, nil, nil, fees.err
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2023-03-23 15:12:37 -05:00
|
|
|
i := fees.blockNumber - oldestBlock
|
2021-08-23 16:50:24 -05:00
|
|
|
if fees.results.baseFee != nil {
|
|
|
|
reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = fees.results.reward, fees.results.baseFee, fees.results.nextBaseFee, fees.results.gasUsedRatio
|
2021-06-28 09:16:32 -05:00
|
|
|
} else {
|
|
|
|
// getting no block and no error means we are requesting into the future (might happen because of a reorg)
|
|
|
|
if i < firstMissing {
|
|
|
|
firstMissing = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if firstMissing == 0 {
|
2021-07-26 22:27:28 -05:00
|
|
|
return common.Big0, nil, nil, nil, nil
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|
2021-07-08 01:57:51 -05:00
|
|
|
if len(rewardPercentiles) != 0 {
|
2021-06-28 09:16:32 -05:00
|
|
|
reward = reward[:firstMissing]
|
|
|
|
} else {
|
|
|
|
reward = nil
|
|
|
|
}
|
|
|
|
baseFee, gasUsedRatio = baseFee[:firstMissing+1], gasUsedRatio[:firstMissing]
|
2021-07-26 22:27:28 -05:00
|
|
|
return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, nil
|
2021-06-28 09:16:32 -05:00
|
|
|
}
|