github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/eth/gasprice/gasprice.go (about)

     1  // Copyright 2015 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  	"math/big"
    22  	"slices"
    23  	"sync"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/common/lru"
    27  	"github.com/ethereum/go-ethereum/core"
    28  	"github.com/ethereum/go-ethereum/core/state"
    29  	"github.com/ethereum/go-ethereum/core/types"
    30  	"github.com/ethereum/go-ethereum/event"
    31  	"github.com/ethereum/go-ethereum/log"
    32  	"github.com/ethereum/go-ethereum/params"
    33  	"github.com/ethereum/go-ethereum/rpc"
    34  )
    35  
    36  const sampleNumber = 3 // Number of transactions sampled in a block
    37  
    38  var (
    39  	DefaultMaxPrice    = big.NewInt(500 * params.GWei)
    40  	DefaultIgnorePrice = big.NewInt(2 * params.Wei)
    41  )
    42  
    43  type Config struct {
    44  	Blocks           int
    45  	Percentile       int
    46  	MaxHeaderHistory uint64
    47  	MaxBlockHistory  uint64
    48  	Default          *big.Int `toml:",omitempty"`
    49  	MaxPrice         *big.Int `toml:",omitempty"`
    50  	IgnorePrice      *big.Int `toml:",omitempty"`
    51  }
    52  
    53  // OracleBackend includes all necessary background APIs for oracle.
    54  type OracleBackend interface {
    55  	HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error)
    56  	BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
    57  	GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
    58  	Pending() (*types.Block, types.Receipts, *state.StateDB)
    59  	ChainConfig() *params.ChainConfig
    60  	SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
    61  }
    62  
    63  // Oracle recommends gas prices based on the content of recent
    64  // blocks. Suitable for both light and full clients.
    65  type Oracle struct {
    66  	backend     OracleBackend
    67  	lastHead    common.Hash
    68  	lastPrice   *big.Int
    69  	maxPrice    *big.Int
    70  	ignorePrice *big.Int
    71  	cacheLock   sync.RWMutex
    72  	fetchLock   sync.Mutex
    73  
    74  	checkBlocks, percentile           int
    75  	maxHeaderHistory, maxBlockHistory uint64
    76  
    77  	historyCache *lru.Cache[cacheKey, processedFees]
    78  }
    79  
    80  // NewOracle returns a new gasprice oracle which can recommend suitable
    81  // gasprice for newly created transaction.
    82  func NewOracle(backend OracleBackend, params Config) *Oracle {
    83  	blocks := params.Blocks
    84  	if blocks < 1 {
    85  		blocks = 1
    86  		log.Warn("Sanitizing invalid gasprice oracle sample blocks", "provided", params.Blocks, "updated", blocks)
    87  	}
    88  	percent := params.Percentile
    89  	if percent < 0 {
    90  		percent = 0
    91  		log.Warn("Sanitizing invalid gasprice oracle sample percentile", "provided", params.Percentile, "updated", percent)
    92  	} else if percent > 100 {
    93  		percent = 100
    94  		log.Warn("Sanitizing invalid gasprice oracle sample percentile", "provided", params.Percentile, "updated", percent)
    95  	}
    96  	maxPrice := params.MaxPrice
    97  	if maxPrice == nil || maxPrice.Int64() <= 0 {
    98  		maxPrice = DefaultMaxPrice
    99  		log.Warn("Sanitizing invalid gasprice oracle price cap", "provided", params.MaxPrice, "updated", maxPrice)
   100  	}
   101  	ignorePrice := params.IgnorePrice
   102  	if ignorePrice == nil || ignorePrice.Int64() <= 0 {
   103  		ignorePrice = DefaultIgnorePrice
   104  		log.Warn("Sanitizing invalid gasprice oracle ignore price", "provided", params.IgnorePrice, "updated", ignorePrice)
   105  	} else if ignorePrice.Int64() > 0 {
   106  		log.Info("Gasprice oracle is ignoring threshold set", "threshold", ignorePrice)
   107  	}
   108  	maxHeaderHistory := params.MaxHeaderHistory
   109  	if maxHeaderHistory < 1 {
   110  		maxHeaderHistory = 1
   111  		log.Warn("Sanitizing invalid gasprice oracle max header history", "provided", params.MaxHeaderHistory, "updated", maxHeaderHistory)
   112  	}
   113  	maxBlockHistory := params.MaxBlockHistory
   114  	if maxBlockHistory < 1 {
   115  		maxBlockHistory = 1
   116  		log.Warn("Sanitizing invalid gasprice oracle max block history", "provided", params.MaxBlockHistory, "updated", maxBlockHistory)
   117  	}
   118  
   119  	cache := lru.NewCache[cacheKey, processedFees](2048)
   120  	headEvent := make(chan core.ChainHeadEvent, 1)
   121  	backend.SubscribeChainHeadEvent(headEvent)
   122  	go func() {
   123  		var lastHead common.Hash
   124  		for ev := range headEvent {
   125  			if ev.Block.ParentHash() != lastHead {
   126  				cache.Purge()
   127  			}
   128  			lastHead = ev.Block.Hash()
   129  		}
   130  	}()
   131  
   132  	return &Oracle{
   133  		backend:          backend,
   134  		lastPrice:        params.Default,
   135  		maxPrice:         maxPrice,
   136  		ignorePrice:      ignorePrice,
   137  		checkBlocks:      blocks,
   138  		percentile:       percent,
   139  		maxHeaderHistory: maxHeaderHistory,
   140  		maxBlockHistory:  maxBlockHistory,
   141  		historyCache:     cache,
   142  	}
   143  }
   144  
   145  // SuggestTipCap returns a tip cap so that newly created transaction can have a
   146  // very high chance to be included in the following blocks.
   147  //
   148  // Note, for legacy transactions and the legacy eth_gasPrice RPC call, it will be
   149  // necessary to add the basefee to the returned number to fall back to the legacy
   150  // behavior.
   151  func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
   152  	head, _ := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
   153  	headHash := head.Hash()
   154  
   155  	// If the latest gasprice is still available, return it.
   156  	oracle.cacheLock.RLock()
   157  	lastHead, lastPrice := oracle.lastHead, oracle.lastPrice
   158  	oracle.cacheLock.RUnlock()
   159  	if headHash == lastHead {
   160  		return new(big.Int).Set(lastPrice), nil
   161  	}
   162  	oracle.fetchLock.Lock()
   163  	defer oracle.fetchLock.Unlock()
   164  
   165  	// Try checking the cache again, maybe the last fetch fetched what we need
   166  	oracle.cacheLock.RLock()
   167  	lastHead, lastPrice = oracle.lastHead, oracle.lastPrice
   168  	oracle.cacheLock.RUnlock()
   169  	if headHash == lastHead {
   170  		return new(big.Int).Set(lastPrice), nil
   171  	}
   172  	var (
   173  		sent, exp int
   174  		number    = head.Number.Uint64()
   175  		result    = make(chan results, oracle.checkBlocks)
   176  		quit      = make(chan struct{})
   177  		results   []*big.Int
   178  	)
   179  	for sent < oracle.checkBlocks && number > 0 {
   180  		go oracle.getBlockValues(ctx, number, sampleNumber, oracle.ignorePrice, result, quit)
   181  		sent++
   182  		exp++
   183  		number--
   184  	}
   185  	for exp > 0 {
   186  		res := <-result
   187  		if res.err != nil {
   188  			close(quit)
   189  			return new(big.Int).Set(lastPrice), res.err
   190  		}
   191  		exp--
   192  		// Nothing returned. There are two special cases here:
   193  		// - The block is empty
   194  		// - All the transactions included are sent by the miner itself.
   195  		// In these cases, use the latest calculated price for sampling.
   196  		if len(res.values) == 0 {
   197  			res.values = []*big.Int{lastPrice}
   198  		}
   199  		// Besides, in order to collect enough data for sampling, if nothing
   200  		// meaningful returned, try to query more blocks. But the maximum
   201  		// is 2*checkBlocks.
   202  		if len(res.values) == 1 && len(results)+1+exp < oracle.checkBlocks*2 && number > 0 {
   203  			go oracle.getBlockValues(ctx, number, sampleNumber, oracle.ignorePrice, result, quit)
   204  			sent++
   205  			exp++
   206  			number--
   207  		}
   208  		results = append(results, res.values...)
   209  	}
   210  	price := lastPrice
   211  	if len(results) > 0 {
   212  		slices.SortFunc(results, func(a, b *big.Int) int { return a.Cmp(b) })
   213  		price = results[(len(results)-1)*oracle.percentile/100]
   214  	}
   215  	if price.Cmp(oracle.maxPrice) > 0 {
   216  		price = new(big.Int).Set(oracle.maxPrice)
   217  	}
   218  	oracle.cacheLock.Lock()
   219  	oracle.lastHead = headHash
   220  	oracle.lastPrice = price
   221  	oracle.cacheLock.Unlock()
   222  
   223  	return new(big.Int).Set(price), nil
   224  }
   225  
   226  type results struct {
   227  	values []*big.Int
   228  	err    error
   229  }
   230  
   231  // getBlockValues calculates the lowest transaction gas price in a given block
   232  // and sends it to the result channel. If the block is empty or all transactions
   233  // are sent by the miner itself(it doesn't make any sense to include this kind of
   234  // transaction prices for sampling), nil gasprice is returned.
   235  func (oracle *Oracle) getBlockValues(ctx context.Context, blockNum uint64, limit int, ignoreUnder *big.Int, result chan results, quit chan struct{}) {
   236  	block, err := oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
   237  	if block == nil {
   238  		select {
   239  		case result <- results{nil, err}:
   240  		case <-quit:
   241  		}
   242  		return
   243  	}
   244  	signer := types.MakeSigner(oracle.backend.ChainConfig(), block.Number(), block.Time())
   245  
   246  	// Sort the transaction by effective tip in ascending sort.
   247  	txs := block.Transactions()
   248  	sortedTxs := make([]*types.Transaction, len(txs))
   249  	copy(sortedTxs, txs)
   250  	baseFee := block.BaseFee()
   251  	slices.SortFunc(sortedTxs, func(a, b *types.Transaction) int {
   252  		// It's okay to discard the error because a tx would never be
   253  		// accepted into a block with an invalid effective tip.
   254  		tip1, _ := a.EffectiveGasTip(baseFee)
   255  		tip2, _ := b.EffectiveGasTip(baseFee)
   256  		return tip1.Cmp(tip2)
   257  	})
   258  
   259  	var prices []*big.Int
   260  	for _, tx := range sortedTxs {
   261  		tip, _ := tx.EffectiveGasTip(baseFee)
   262  		if ignoreUnder != nil && tip.Cmp(ignoreUnder) == -1 {
   263  			continue
   264  		}
   265  		sender, err := types.Sender(signer, tx)
   266  		if err == nil && sender != block.Coinbase() {
   267  			prices = append(prices, tip)
   268  			if len(prices) >= limit {
   269  				break
   270  			}
   271  		}
   272  	}
   273  	select {
   274  	case result <- results{prices, nil}:
   275  	case <-quit:
   276  	}
   277  }