github.com/amazechain/amc@v0.1.3/internal/api/feehistory.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"github.com/amazechain/amc/common"
    25  	"github.com/amazechain/amc/common/block"
    26  	"github.com/amazechain/amc/internal/consensus/misc"
    27  	"github.com/amazechain/amc/log"
    28  	"github.com/amazechain/amc/modules/rpc/jsonrpc"
    29  	"github.com/holiman/uint256"
    30  	"math"
    31  	"math/big"
    32  	"sort"
    33  	"sync/atomic"
    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      block.IHeader
    52  	block       block.IBlock // only set if reward percentiles are requested
    53  	receipts    block.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 (
    73  	txGasAndReward struct {
    74  		gasUsed uint64
    75  		reward  *big.Int
    76  	}
    77  	sortGasAndReward []txGasAndReward
    78  )
    79  
    80  func (s sortGasAndReward) Len() int { return len(s) }
    81  func (s sortGasAndReward) Swap(i, j int) {
    82  	s[i], s[j] = s[j], s[i]
    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  	if bf.results.baseFee = bf.header.BaseFee64().ToBig(); bf.results.baseFee == nil {
    93  		bf.results.baseFee = new(big.Int)
    94  	}
    95  
    96  	// todo baseFee = 0
    97  	bf.results.nextBaseFee = new(big.Int)
    98  
    99  	if oracle.chainConfig.IsLondon(bf.blockNumber + 1) {
   100  		bf.results.nextBaseFee = misc.CalcBaseFee(oracle.chainConfig, bf.header.(*block.Header))
   101  	} else {
   102  		bf.results.nextBaseFee = new(big.Int)
   103  	}
   104  
   105  	bf.results.gasUsedRatio = float64(bf.block.GasUsed()) / float64(bf.block.GasLimit())
   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  		log.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, tx := range bf.block.Transactions() {
   126  		reward, _ := tx.EffectiveGasTip(bf.header.BaseFee64())
   127  		sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward.ToBig()}
   128  	}
   129  	sort.Stable(sorter)
   130  
   131  	var txIndex int
   132  	sumGasUsed := sorter[0].gasUsed
   133  
   134  	for i, p := range percentiles {
   135  		thresholdGasUsed := uint64(float64(bf.block.GasUsed()) * p / 100)
   136  		for sumGasUsed < thresholdGasUsed && txIndex < len(bf.block.Transactions())-1 {
   137  			txIndex++
   138  			sumGasUsed += sorter[txIndex].gasUsed
   139  		}
   140  		bf.results.reward[i] = sorter[txIndex].reward
   141  	}
   142  }
   143  
   144  // resolveBlockRange resolves the specified block range to absolute block numbers while also
   145  // enforcing backend specific limitations. The pending block and corresponding receipts are
   146  // also returned if requested and available.
   147  // Note: an error is only returned if retrieving the head header has failed. If there are no
   148  // retrievable blocks in the specified range then zero block count is returned with no error.
   149  func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd jsonrpc.BlockNumber, blocks int) (block.IBlock, []*block.Receipt, uint64, int, error) {
   150  	var (
   151  		headBlock       block.IHeader
   152  		pendingBlock    block.IBlock
   153  		pendingReceipts block.Receipts
   154  		//err             error
   155  	)
   156  
   157  	// Get the chain's current head.
   158  	headBlock = oracle.backend.CurrentBlock().Header()
   159  
   160  	head := jsonrpc.BlockNumber(headBlock.Number64().Uint64())
   161  
   162  	// Fail if request block is beyond the chain's current head.
   163  	if head < reqEnd {
   164  		return nil, nil, 0, 0, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, reqEnd, head)
   165  	}
   166  
   167  	// Resolve block tag.
   168  	if reqEnd < 0 {
   169  		var (
   170  			resolved block.IHeader
   171  			err      error
   172  		)
   173  		switch reqEnd {
   174  		case jsonrpc.PendingBlockNumber:
   175  			if pendingBlock, pendingReceipts = oracle.miner.PendingBlockAndReceipts(); pendingBlock != nil {
   176  				resolved = pendingBlock.Header()
   177  			} else {
   178  				// Pending block not supported by backend, process only until latest block.
   179  				resolved = headBlock
   180  
   181  				// Update total blocks to return to account for this.
   182  				blocks--
   183  			}
   184  		case jsonrpc.LatestBlockNumber:
   185  			// Retrieved above.
   186  			resolved = headBlock
   187  		//case jsonrpc.SafeBlockNumber:
   188  		//case jsonrpc.FinalizedBlockNumber:
   189  		case jsonrpc.EarliestBlockNumber:
   190  			resolved = oracle.backend.GetHeaderByNumber(uint256.NewInt(0))
   191  		}
   192  		if resolved == nil || err != nil {
   193  			return nil, nil, 0, 0, err
   194  		}
   195  		// Absolute number resolved.
   196  		reqEnd = jsonrpc.BlockNumber(resolved.Number64().Uint64())
   197  	}
   198  
   199  	// If there are no blocks to return, short circuit.
   200  	if blocks == 0 {
   201  		return nil, nil, 0, 0, nil
   202  	}
   203  	// Ensure not trying to retrieve before genesis.
   204  	if int(reqEnd+1) < blocks {
   205  		blocks = int(reqEnd + 1)
   206  	}
   207  	return pendingBlock, pendingReceipts, uint64(reqEnd), blocks, nil
   208  }
   209  
   210  // FeeHistory returns data relevant for fee estimation based on the specified range of blocks.
   211  // The range can be specified either with absolute block numbers or ending with the latest
   212  // or pending block. Backends may or may not support gathering data from the pending block
   213  // or blocks older than a certain age (specified in maxHistory). The first block of the
   214  // actually processed range is returned to avoid ambiguity when parts of the requested range
   215  // are not available or when the head has changed during processing this request.
   216  // Three arrays are returned based on the processed blocks:
   217  //   - reward: the requested percentiles of effective priority fees per gas of transactions in each
   218  //     block, sorted in ascending order and weighted by gas used.
   219  //   - baseFee: base fee per gas in the given block
   220  //   - gasUsedRatio: gasUsed/gasLimit in the given block
   221  //
   222  // Note: baseFee includes the next block after the newest of the returned range, because this
   223  // value can be derived from the newest block.
   224  func (oracle *Oracle) FeeHistory(ctx context.Context, blocks int, unresolvedLastBlock jsonrpc.BlockNumber, resolvedLastBlock *uint256.Int, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) {
   225  	if blocks < 1 {
   226  		return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks
   227  	}
   228  	maxFeeHistory := oracle.maxHeaderHistory
   229  	if len(rewardPercentiles) != 0 {
   230  		maxFeeHistory = oracle.maxBlockHistory
   231  	}
   232  	if blocks > maxFeeHistory {
   233  		log.Warn("Sanitizing fee history length", "requested", blocks, "truncated", maxFeeHistory)
   234  		blocks = maxFeeHistory
   235  	}
   236  	for i, p := range rewardPercentiles {
   237  		if p < 0 || p > 100 {
   238  			return common.Big0, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p)
   239  		}
   240  		if i > 0 && p < rewardPercentiles[i-1] {
   241  			return common.Big0, nil, nil, nil, fmt.Errorf("%w: #%d:%f > #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p)
   242  		}
   243  	}
   244  	var (
   245  		pendingBlock    block.IBlock
   246  		pendingReceipts []*block.Receipt
   247  		err             error
   248  	)
   249  	pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks)
   250  	if err != nil || blocks == 0 {
   251  		return common.Big0, nil, nil, nil, err
   252  	}
   253  	oldestBlock := lastBlock + 1 - uint64(blocks)
   254  
   255  	var (
   256  		next    = oldestBlock
   257  		results = make(chan *blockFees, blocks)
   258  	)
   259  	percentileKey := make([]byte, 8*len(rewardPercentiles))
   260  	for i, p := range rewardPercentiles {
   261  		binary.LittleEndian.PutUint64(percentileKey[i*8:(i+1)*8], math.Float64bits(p))
   262  	}
   263  	for i := 0; i < maxBlockFetchers && i < blocks; i++ {
   264  		go func() {
   265  			for {
   266  				// Retrieve the next block number to fetch with this goroutine
   267  				blockNumber := atomic.AddUint64(&next, 1) - 1
   268  				if blockNumber > lastBlock {
   269  					return
   270  				}
   271  
   272  				fees := &blockFees{blockNumber: blockNumber}
   273  				if pendingBlock != nil && blockNumber >= pendingBlock.Number64().Uint64() {
   274  					fees.block, fees.receipts = pendingBlock, pendingReceipts
   275  					fees.header = fees.block.Header()
   276  					oracle.processBlock(fees, rewardPercentiles)
   277  					results <- fees
   278  				} else {
   279  					cacheKey := cacheKey{number: blockNumber, percentiles: string(percentileKey)}
   280  
   281  					if p, ok := oracle.historyCache.Get(cacheKey); ok {
   282  						fees.results = p.(processedFees)
   283  						results <- fees
   284  					} else {
   285  						if len(rewardPercentiles) != 0 {
   286  							fees.block, fees.err = oracle.backend.GetBlockByNumber(resolvedLastBlock)
   287  							if fees.block != nil && fees.err == nil {
   288  								fees.receipts, fees.err = oracle.backend.GetReceipts(fees.block.Hash())
   289  								fees.header = fees.block.Header()
   290  							}
   291  						} else {
   292  							fees.err = nil
   293  							fees.header = oracle.backend.GetHeaderByNumber(resolvedLastBlock)
   294  						}
   295  						if fees.header != nil && fees.err == nil {
   296  							oracle.processBlock(fees, rewardPercentiles)
   297  							if fees.err == nil {
   298  								oracle.historyCache.Add(cacheKey, fees.results)
   299  							}
   300  						}
   301  						// send to results even if empty to guarantee that blocks items are sent in total
   302  						results <- fees
   303  					}
   304  				}
   305  			}
   306  		}()
   307  	}
   308  	var (
   309  		reward       = make([][]*big.Int, blocks)
   310  		baseFee      = make([]*big.Int, blocks+1)
   311  		gasUsedRatio = make([]float64, blocks)
   312  		firstMissing = blocks
   313  	)
   314  	for ; blocks > 0; blocks-- {
   315  		fees := <-results
   316  		if fees.err != nil {
   317  			return common.Big0, nil, nil, nil, fees.err
   318  		}
   319  		i := int(fees.blockNumber - oldestBlock)
   320  		if fees.results.baseFee != nil {
   321  			reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = fees.results.reward, fees.results.baseFee, fees.results.nextBaseFee, fees.results.gasUsedRatio
   322  		} else {
   323  			// getting no block and no error means we are requesting into the future (might happen because of a reorg)
   324  			if i < firstMissing {
   325  				firstMissing = i
   326  			}
   327  		}
   328  	}
   329  	if firstMissing == 0 {
   330  		return common.Big0, nil, nil, nil, nil
   331  	}
   332  	if len(rewardPercentiles) != 0 {
   333  		reward = reward[:firstMissing]
   334  	} else {
   335  		reward = nil
   336  	}
   337  	baseFee, gasUsedRatio = baseFee[:firstMissing+1], gasUsedRatio[:firstMissing]
   338  	return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, nil
   339  }