github.com/theQRL/go-zond@v0.1.1/zond/gasprice/feehistory.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  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"math"
    25  	"math/big"
    26  	"sync/atomic"
    27  
    28  	"github.com/theQRL/go-zond/common"
    29  	"github.com/theQRL/go-zond/consensus/misc/eip1559"
    30  	"github.com/theQRL/go-zond/core/types"
    31  	"github.com/theQRL/go-zond/log"
    32  	"github.com/theQRL/go-zond/rpc"
    33  	"golang.org/x/exp/slices"
    34  )
    35  
    36  var (
    37  	errInvalidPercentile = errors.New("invalid reward percentile")
    38  	errRequestBeyondHead = errors.New("request beyond head block")
    39  )
    40  
    41  const (
    42  	// maxBlockFetchers is the max number of goroutines to spin up to pull blocks
    43  	// for the fee history calculation (mostly relevant for LES).
    44  	maxBlockFetchers = 4
    45  )
    46  
    47  // blockFees represents a single block for processing
    48  type blockFees struct {
    49  	// set by the caller
    50  	blockNumber uint64
    51  	header      *types.Header
    52  	block       *types.Block // only set if reward percentiles are requested
    53  	receipts    types.Receipts
    54  	// filled by processBlock
    55  	results processedFees
    56  	err     error
    57  }
    58  
    59  type cacheKey struct {
    60  	number      uint64
    61  	percentiles string
    62  }
    63  
    64  // processedFees contains the results of a processed block.
    65  type processedFees struct {
    66  	reward               []*big.Int
    67  	baseFee, nextBaseFee *big.Int
    68  	gasUsedRatio         float64
    69  }
    70  
    71  // txGasAndReward is sorted in ascending order based on reward
    72  type txGasAndReward struct {
    73  	gasUsed uint64
    74  	reward  *big.Int
    75  }
    76  
    77  // processBlock takes a blockFees structure with the blockNumber, the header and optionally
    78  // the block field filled in, retrieves the block from the backend if not present yet and
    79  // fills in the rest of the fields.
    80  func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
    81  	chainconfig := oracle.backend.ChainConfig()
    82  	if bf.results.baseFee = bf.header.BaseFee; bf.results.baseFee == nil {
    83  		bf.results.baseFee = new(big.Int)
    84  	}
    85  	if chainconfig.IsLondon(big.NewInt(int64(bf.blockNumber + 1))) {
    86  		bf.results.nextBaseFee = eip1559.CalcBaseFee(chainconfig, bf.header)
    87  	} else {
    88  		bf.results.nextBaseFee = new(big.Int)
    89  	}
    90  	bf.results.gasUsedRatio = float64(bf.header.GasUsed) / float64(bf.header.GasLimit)
    91  	if len(percentiles) == 0 {
    92  		// rewards were not requested, return null
    93  		return
    94  	}
    95  	if bf.block == nil || (bf.receipts == nil && len(bf.block.Transactions()) != 0) {
    96  		log.Error("Block or receipts are missing while reward percentiles are requested")
    97  		return
    98  	}
    99  
   100  	bf.results.reward = make([]*big.Int, len(percentiles))
   101  	if len(bf.block.Transactions()) == 0 {
   102  		// return an all zero row if there are no transactions to gather data from
   103  		for i := range bf.results.reward {
   104  			bf.results.reward[i] = new(big.Int)
   105  		}
   106  		return
   107  	}
   108  
   109  	sorter := make([]txGasAndReward, len(bf.block.Transactions()))
   110  	for i, tx := range bf.block.Transactions() {
   111  		reward, _ := tx.EffectiveGasTip(bf.block.BaseFee())
   112  		sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward}
   113  	}
   114  	slices.SortStableFunc(sorter, func(a, b txGasAndReward) int {
   115  		return a.reward.Cmp(b.reward)
   116  	})
   117  
   118  	var txIndex int
   119  	sumGasUsed := sorter[0].gasUsed
   120  
   121  	for i, p := range percentiles {
   122  		thresholdGasUsed := uint64(float64(bf.block.GasUsed()) * p / 100)
   123  		for sumGasUsed < thresholdGasUsed && txIndex < len(bf.block.Transactions())-1 {
   124  			txIndex++
   125  			sumGasUsed += sorter[txIndex].gasUsed
   126  		}
   127  		bf.results.reward[i] = sorter[txIndex].reward
   128  	}
   129  }
   130  
   131  // resolveBlockRange resolves the specified block range to absolute block numbers while also
   132  // enforcing backend specific limitations. The pending block and corresponding receipts are
   133  // also returned if requested and available.
   134  // Note: an error is only returned if retrieving the head header has failed. If there are no
   135  // retrievable blocks in the specified range then zero block count is returned with no error.
   136  func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNumber, blocks uint64) (*types.Block, []*types.Receipt, uint64, uint64, error) {
   137  	var (
   138  		headBlock       *types.Header
   139  		pendingBlock    *types.Block
   140  		pendingReceipts types.Receipts
   141  		err             error
   142  	)
   143  
   144  	// Get the chain's current head.
   145  	if headBlock, err = oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err != nil {
   146  		return nil, nil, 0, 0, err
   147  	}
   148  	head := rpc.BlockNumber(headBlock.Number.Uint64())
   149  
   150  	// Fail if request block is beyond the chain's current head.
   151  	if head < reqEnd {
   152  		return nil, nil, 0, 0, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, reqEnd, head)
   153  	}
   154  
   155  	// Resolve block tag.
   156  	if reqEnd < 0 {
   157  		var (
   158  			resolved *types.Header
   159  			err      error
   160  		)
   161  		switch reqEnd {
   162  		case rpc.PendingBlockNumber:
   163  			if pendingBlock, pendingReceipts = oracle.backend.PendingBlockAndReceipts(); pendingBlock != nil {
   164  				resolved = pendingBlock.Header()
   165  			} else {
   166  				// Pending block not supported by backend, process only until latest block.
   167  				resolved = headBlock
   168  
   169  				// Update total blocks to return to account for this.
   170  				blocks--
   171  			}
   172  		case rpc.LatestBlockNumber:
   173  			// Retrieved above.
   174  			resolved = headBlock
   175  		case rpc.SafeBlockNumber:
   176  			resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.SafeBlockNumber)
   177  		case rpc.FinalizedBlockNumber:
   178  			resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.FinalizedBlockNumber)
   179  		case rpc.EarliestBlockNumber:
   180  			resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.EarliestBlockNumber)
   181  		}
   182  		if resolved == nil || err != nil {
   183  			return nil, nil, 0, 0, err
   184  		}
   185  		// Absolute number resolved.
   186  		reqEnd = rpc.BlockNumber(resolved.Number.Uint64())
   187  	}
   188  
   189  	// If there are no blocks to return, short circuit.
   190  	if blocks == 0 {
   191  		return nil, nil, 0, 0, nil
   192  	}
   193  	// Ensure not trying to retrieve before genesis.
   194  	if uint64(reqEnd+1) < blocks {
   195  		blocks = uint64(reqEnd + 1)
   196  	}
   197  	return pendingBlock, pendingReceipts, uint64(reqEnd), blocks, nil
   198  }
   199  
   200  // FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
   201  // The range can be specified either with absolute block numbers or ending with the latest
   202  // or pending block. Backends may or may not support gathering data from the pending block
   203  // or blocks older than a certain age (specified in maxHistory). The first block of the
   204  // actually processed range is returned to avoid ambiguity when parts of the requested range
   205  // are not available or when the head has changed during processing this request.
   206  // Three arrays are returned based on the processed blocks:
   207  //   - reward: the requested percentiles of effective priority fees per gas of transactions in each
   208  //     block, sorted in ascending order and weighted by gas used.
   209  //   - baseFee: base fee per gas in the given block
   210  //   - gasUsedRatio: gasUsed/gasLimit in the given block
   211  //
   212  // Note: baseFee includes the next block after the newest of the returned range, because this
   213  // value can be derived from the newest block.
   214  func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedLastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
   215  	if blocks < 1 {
   216  		return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks
   217  	}
   218  	maxFeeHistory := oracle.maxHeaderHistory
   219  	if len(rewardPercentiles) != 0 {
   220  		maxFeeHistory = oracle.maxBlockHistory
   221  	}
   222  	if blocks > maxFeeHistory {
   223  		log.Warn("Sanitizing fee history length", "requested", blocks, "truncated", maxFeeHistory)
   224  		blocks = maxFeeHistory
   225  	}
   226  	for i, p := range rewardPercentiles {
   227  		if p < 0 || p > 100 {
   228  			return common.Big0, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p)
   229  		}
   230  		if i > 0 && p < rewardPercentiles[i-1] {
   231  			return common.Big0, nil, nil, nil, fmt.Errorf("%w: #%d:%f > #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p)
   232  		}
   233  	}
   234  	var (
   235  		pendingBlock    *types.Block
   236  		pendingReceipts []*types.Receipt
   237  		err             error
   238  	)
   239  	pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks)
   240  	if err != nil || blocks == 0 {
   241  		return common.Big0, nil, nil, nil, err
   242  	}
   243  	oldestBlock := lastBlock + 1 - blocks
   244  
   245  	var next atomic.Uint64
   246  	next.Store(oldestBlock)
   247  	results := make(chan *blockFees, blocks)
   248  
   249  	percentileKey := make([]byte, 8*len(rewardPercentiles))
   250  	for i, p := range rewardPercentiles {
   251  		binary.LittleEndian.PutUint64(percentileKey[i*8:(i+1)*8], math.Float64bits(p))
   252  	}
   253  	for i := 0; i < maxBlockFetchers && i < int(blocks); i++ {
   254  		go func() {
   255  			for {
   256  				// Retrieve the next block number to fetch with this goroutine
   257  				blockNumber := next.Add(1) - 1
   258  				if blockNumber > lastBlock {
   259  					return
   260  				}
   261  
   262  				fees := &blockFees{blockNumber: blockNumber}
   263  				if pendingBlock != nil && blockNumber >= pendingBlock.NumberU64() {
   264  					fees.block, fees.receipts = pendingBlock, pendingReceipts
   265  					fees.header = fees.block.Header()
   266  					oracle.processBlock(fees, rewardPercentiles)
   267  					results <- fees
   268  				} else {
   269  					cacheKey := cacheKey{number: blockNumber, percentiles: string(percentileKey)}
   270  
   271  					if p, ok := oracle.historyCache.Get(cacheKey); ok {
   272  						fees.results = p
   273  						results <- fees
   274  					} else {
   275  						if len(rewardPercentiles) != 0 {
   276  							fees.block, fees.err = oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNumber))
   277  							if fees.block != nil && fees.err == nil {
   278  								fees.receipts, fees.err = oracle.backend.GetReceipts(ctx, fees.block.Hash())
   279  								fees.header = fees.block.Header()
   280  							}
   281  						} else {
   282  							fees.header, fees.err = oracle.backend.HeaderByNumber(ctx, rpc.BlockNumber(blockNumber))
   283  						}
   284  						if fees.header != nil && fees.err == nil {
   285  							oracle.processBlock(fees, rewardPercentiles)
   286  							if fees.err == nil {
   287  								oracle.historyCache.Add(cacheKey, fees.results)
   288  							}
   289  						}
   290  						// send to results even if empty to guarantee that blocks items are sent in total
   291  						results <- fees
   292  					}
   293  				}
   294  			}
   295  		}()
   296  	}
   297  	var (
   298  		reward       = make([][]*big.Int, blocks)
   299  		baseFee      = make([]*big.Int, blocks+1)
   300  		gasUsedRatio = make([]float64, blocks)
   301  		firstMissing = blocks
   302  	)
   303  	for ; blocks > 0; blocks-- {
   304  		fees := <-results
   305  		if fees.err != nil {
   306  			return common.Big0, nil, nil, nil, fees.err
   307  		}
   308  		i := fees.blockNumber - oldestBlock
   309  		if fees.results.baseFee != nil {
   310  			reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = fees.results.reward, fees.results.baseFee, fees.results.nextBaseFee, fees.results.gasUsedRatio
   311  		} else {
   312  			// getting no block and no error means we are requesting into the future (might happen because of a reorg)
   313  			if i < firstMissing {
   314  				firstMissing = i
   315  			}
   316  		}
   317  	}
   318  	if firstMissing == 0 {
   319  		return common.Big0, nil, nil, nil, nil
   320  	}
   321  	if len(rewardPercentiles) != 0 {
   322  		reward = reward[:firstMissing]
   323  	} else {
   324  		reward = nil
   325  	}
   326  	baseFee, gasUsedRatio = baseFee[:firstMissing+1], gasUsedRatio[:firstMissing]
   327  	return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, nil
   328  }