github.com/klaytn/klaytn@v1.12.1/node/cn/gasprice/feehistory.go (about)

     1  // Modifications Copyright 2022 The Klaytn Authors
     2  // Copyright 2021 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from eth/gasprice/feehistory.go (2021/11/09).
    19  // Modified and improved for the klaytn development.
    20  
    21  package gasprice
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"fmt"
    27  	"math/big"
    28  	"sort"
    29  	"sync/atomic"
    30  
    31  	"github.com/klaytn/klaytn/blockchain/types"
    32  	"github.com/klaytn/klaytn/common"
    33  	"github.com/klaytn/klaytn/consensus/misc"
    34  	"github.com/klaytn/klaytn/log"
    35  	"github.com/klaytn/klaytn/networks/rpc"
    36  	"github.com/klaytn/klaytn/params"
    37  )
    38  
    39  var (
    40  	logger               = log.NewModuleLogger(log.NodeCnGasPrice)
    41  	errInvalidPercentile = errors.New("invalid reward percentile")
    42  	errRequestBeyondHead = errors.New("request beyond head block")
    43  )
    44  
    45  const (
    46  	// maxBlockFetchers is the max number of goroutines to spin up to pull blocks
    47  	// for the fee history calculation.
    48  	maxBlockFetchers = 1
    49  )
    50  
    51  // blockFees represents a single block for processing
    52  type blockFees struct {
    53  	// set by the caller
    54  	blockNumber uint64
    55  	header      *types.Header
    56  	block       *types.Block // only set if reward percentiles are requested
    57  	receipts    types.Receipts
    58  	// filled by processBlock
    59  	results processedFees
    60  	err     error
    61  }
    62  
    63  // processedFees contains the results of a processed block and is also used for caching
    64  type processedFees struct {
    65  	reward               []*big.Int
    66  	baseFee, nextBaseFee *big.Int
    67  	gasUsedRatio         float64
    68  }
    69  
    70  // txGasAndReward is sorted in ascending order based on reward
    71  type (
    72  	txGasAndReward struct {
    73  		gasUsed uint64
    74  		reward  *big.Int
    75  	}
    76  	sortGasAndReward []txGasAndReward
    77  )
    78  
    79  func (s sortGasAndReward) Len() int { return len(s) }
    80  func (s sortGasAndReward) Swap(i, j int) {
    81  	s[i], s[j] = s[j], s[i]
    82  }
    83  
    84  func (s sortGasAndReward) Less(i, j int) bool {
    85  	return s[i].reward.Cmp(s[j].reward) < 0
    86  }
    87  
    88  // processBlock takes a blockFees structure with the blockNumber, the header and optionally
    89  // the block field filled in, retrieves the block from the backend if not present yet and
    90  // fills in the rest of the fields.
    91  func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) {
    92  	chainconfig := oracle.backend.ChainConfig()
    93  	// TODO-Klaytn: If we implement baseFee feature like Ethereum does, we should set it from header, not constant.
    94  	if bf.results.baseFee = bf.header.BaseFee; bf.results.baseFee == nil {
    95  		bf.results.baseFee = new(big.Int).SetUint64(params.ZeroBaseFee)
    96  	}
    97  	// TODO-Klaytn: If we implement baseFee feature like Ethereum does, we should calculate nextBaseFee from parent block header.
    98  	if chainconfig.IsMagmaForkEnabled(big.NewInt(int64(bf.blockNumber + 1))) {
    99  		bf.results.nextBaseFee = misc.NextMagmaBlockBaseFee(bf.header, chainconfig.Governance.KIP71)
   100  	} else {
   101  		bf.results.nextBaseFee = new(big.Int).SetUint64(params.ZeroBaseFee)
   102  	}
   103  
   104  	// There is no GasLimit in Klaytn, so it is enough to use pre-defined constant in api package as now.
   105  	bf.results.gasUsedRatio = float64(bf.header.GasUsed) / float64(params.UpperGasLimit)
   106  	if len(percentiles) == 0 {
   107  		// rewards were not requested, return null
   108  		return
   109  	}
   110  	if bf.block == nil || (bf.receipts == nil && len(bf.block.Transactions()) != 0) {
   111  		logger.Error("Block or receipts are missing while reward percentiles are requested")
   112  		return
   113  	}
   114  
   115  	bf.results.reward = make([]*big.Int, len(percentiles))
   116  	if len(bf.block.Transactions()) == 0 {
   117  		// return an all zero row if there are no transactions to gather data from
   118  		for i := range bf.results.reward {
   119  			bf.results.reward[i] = new(big.Int)
   120  		}
   121  		return
   122  	}
   123  
   124  	sorter := make(sortGasAndReward, len(bf.block.Transactions()))
   125  	for i := range bf.block.Transactions() {
   126  		// TODO-Klaytn: If we change the fixed unit price policy and add baseFee feature, we should re-calculate reward.
   127  		reward := bf.block.Header().BaseFee
   128  		if reward == nil {
   129  			reward = new(big.Int).SetUint64(chainconfig.UnitPrice)
   130  		}
   131  		sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward}
   132  	}
   133  	sort.Sort(sorter)
   134  
   135  	var txIndex int
   136  	sumGasUsed := sorter[0].gasUsed
   137  
   138  	for i, p := range percentiles {
   139  		thresholdGasUsed := uint64(float64(bf.block.GasUsed()) * p / 100)
   140  		for sumGasUsed < thresholdGasUsed && txIndex < len(bf.block.Transactions())-1 {
   141  			txIndex++
   142  			sumGasUsed += sorter[txIndex].gasUsed
   143  		}
   144  		bf.results.reward[i] = sorter[txIndex].reward
   145  	}
   146  }
   147  
   148  // resolveBlockRange resolves the specified block range to absolute block numbers while also
   149  // enforcing backend specific limitations.
   150  // Pending block does not exist in Klaytn, so there is no logic to look up pending blocks.
   151  // This part has a different implementation with Ethereum.
   152  // Note: an error is only returned if retrieving the head header has failed. If there are no
   153  // retrievable blocks in the specified range then zero block count is returned with no error.
   154  func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlock rpc.BlockNumber, blocks int) (uint64, int, error) {
   155  	var headBlock rpc.BlockNumber
   156  	// query either pending block or head header and set headBlock
   157  	if lastBlock == rpc.PendingBlockNumber {
   158  		// pending block not supported by backend, process until latest block
   159  		lastBlock = rpc.LatestBlockNumber
   160  		blocks--
   161  		if blocks == 0 {
   162  			return 0, 0, nil
   163  		}
   164  	}
   165  	// if pending block is not fetched then we retrieve the head header to get the head block number
   166  	if latestHeader, err := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err == nil {
   167  		headBlock = rpc.BlockNumber(latestHeader.Number.Uint64())
   168  	} else {
   169  		return 0, 0, err
   170  	}
   171  	if lastBlock == rpc.LatestBlockNumber {
   172  		lastBlock = headBlock
   173  	} else if lastBlock > headBlock {
   174  		return 0, 0, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, lastBlock, headBlock)
   175  	}
   176  	// ensure not trying to retrieve before genesis
   177  	if rpc.BlockNumber(blocks) > lastBlock+1 {
   178  		blocks = int(lastBlock + 1)
   179  	}
   180  	return uint64(lastBlock), blocks, nil
   181  }
   182  
   183  // FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
   184  // The range can be specified either with absolute block numbers or ending with the latest
   185  // or pending block. Backends may or may not support gathering data from the pending block
   186  // or blocks older than a certain age (specified in maxHistory). The first block of the
   187  // actually processed range is returned to avoid ambiguity when parts of the requested range
   188  // are not available or when the head has changed during processing this request.
   189  // Three arrays are returned based on the processed blocks:
   190  // - reward: the requested percentiles of effective priority fees per gas of transactions in each
   191  //   block, sorted in ascending order and weighted by gas used.
   192  // - baseFee: base fee per gas in the given block
   193  // - gasUsedRatio: gasUsed/gasLimit in the given block
   194  // Note: baseFee includes the next block after the newest of the returned range, because this
   195  // value can be derived from the newest block.
   196  func (oracle *Oracle) FeeHistory(
   197  	ctx context.Context, blocks int,
   198  	unresolvedLastBlock rpc.BlockNumber,
   199  	rewardPercentiles []float64,
   200  ) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
   201  	if blocks < 1 {
   202  		return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks
   203  	}
   204  	maxFeeHistory := oracle.maxHeaderHistory
   205  	if len(rewardPercentiles) != 0 {
   206  		maxFeeHistory = oracle.maxBlockHistory
   207  	}
   208  	if blocks > maxFeeHistory {
   209  		logger.Warn("Sanitizing fee history length", "requested", blocks, "truncated", maxFeeHistory)
   210  		blocks = maxFeeHistory
   211  	}
   212  	for i, p := range rewardPercentiles {
   213  		if p < 0 || p > 100 {
   214  			return common.Big0, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p)
   215  		}
   216  		if i > 0 && p < rewardPercentiles[i-1] {
   217  			return common.Big0, nil, nil, nil, fmt.Errorf("%w: #%d:%f > #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p)
   218  		}
   219  	}
   220  	var err error
   221  	lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks)
   222  	if err != nil || blocks == 0 {
   223  		return common.Big0, nil, nil, nil, err
   224  	}
   225  	oldestBlock := lastBlock + 1 - uint64(blocks)
   226  
   227  	var (
   228  		next    = oldestBlock
   229  		results = make(chan *blockFees, blocks)
   230  	)
   231  	for i := 0; i < maxBlockFetchers && i < blocks; i++ {
   232  		go func() {
   233  			for {
   234  				// Retrieve the next block number to fetch with this goroutine
   235  				blockNumber := atomic.AddUint64(&next, 1) - 1
   236  				if blockNumber > lastBlock {
   237  					return
   238  				}
   239  
   240  				fees := &blockFees{blockNumber: blockNumber}
   241  				if len(rewardPercentiles) != 0 {
   242  					fees.block, fees.err = oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNumber))
   243  					if fees.block != nil && fees.err == nil {
   244  						fees.receipts = oracle.backend.GetBlockReceipts(ctx, fees.block.Hash())
   245  						fees.header = fees.block.Header()
   246  					}
   247  				} else {
   248  					fees.header, fees.err = oracle.backend.HeaderByNumber(ctx, rpc.BlockNumber(blockNumber))
   249  				}
   250  				if fees.header != nil && fees.err == nil {
   251  					oracle.processBlock(fees, rewardPercentiles)
   252  				}
   253  				// send to results even if empty to guarantee that blocks items are sent in total
   254  				results <- fees
   255  			}
   256  		}()
   257  	}
   258  	var (
   259  		reward       = make([][]*big.Int, blocks)
   260  		baseFee      = make([]*big.Int, blocks+1)
   261  		gasUsedRatio = make([]float64, blocks)
   262  		blockCount   = blocks
   263  	)
   264  	for ; blocks > 0; blocks-- {
   265  		fees := <-results
   266  		if fees.err != nil {
   267  			return common.Big0, nil, nil, nil, fees.err
   268  		}
   269  		i := int(fees.blockNumber - oldestBlock)
   270  		reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = fees.results.reward, fees.results.baseFee, fees.results.nextBaseFee, fees.results.gasUsedRatio
   271  	}
   272  	if len(rewardPercentiles) != 0 {
   273  		reward = reward[:blockCount]
   274  	} else {
   275  		reward = nil
   276  	}
   277  	baseFee, gasUsedRatio = baseFee[:blockCount+1], gasUsedRatio[:blockCount]
   278  	return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, nil
   279  }