github.com/theQRL/go-zond@v0.2.1/zond/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/theQRL/go-zond/common"
    26  	"github.com/theQRL/go-zond/common/lru"
    27  	"github.com/theQRL/go-zond/core"
    28  	"github.com/theQRL/go-zond/core/state"
    29  	"github.com/theQRL/go-zond/core/types"
    30  	"github.com/theQRL/go-zond/event"
    31  	"github.com/theQRL/go-zond/log"
    32  	"github.com/theQRL/go-zond/params"
    33  	"github.com/theQRL/go-zond/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.
    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  func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
   148  	head, _ := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
   149  	headHash := head.Hash()
   150  
   151  	// If the latest gasprice is still available, return it.
   152  	oracle.cacheLock.RLock()
   153  	lastHead, lastPrice := oracle.lastHead, oracle.lastPrice
   154  	oracle.cacheLock.RUnlock()
   155  	if headHash == lastHead {
   156  		return new(big.Int).Set(lastPrice), nil
   157  	}
   158  	oracle.fetchLock.Lock()
   159  	defer oracle.fetchLock.Unlock()
   160  
   161  	// Try checking the cache again, maybe the last fetch fetched what we need
   162  	oracle.cacheLock.RLock()
   163  	lastHead, lastPrice = oracle.lastHead, oracle.lastPrice
   164  	oracle.cacheLock.RUnlock()
   165  	if headHash == lastHead {
   166  		return new(big.Int).Set(lastPrice), nil
   167  	}
   168  	var (
   169  		sent, exp int
   170  		number    = head.Number.Uint64()
   171  		result    = make(chan results, oracle.checkBlocks)
   172  		quit      = make(chan struct{})
   173  		results   []*big.Int
   174  	)
   175  	for sent < oracle.checkBlocks && number > 0 {
   176  		go oracle.getBlockValues(ctx, number, sampleNumber, oracle.ignorePrice, result, quit)
   177  		sent++
   178  		exp++
   179  		number--
   180  	}
   181  	for exp > 0 {
   182  		res := <-result
   183  		if res.err != nil {
   184  			close(quit)
   185  			return new(big.Int).Set(lastPrice), res.err
   186  		}
   187  		exp--
   188  		// Nothing returned. There are two special cases here:
   189  		// - The block is empty
   190  		// - All the transactions included are sent by the miner itself.
   191  		// In these cases, use the latest calculated price for sampling.
   192  		if len(res.values) == 0 {
   193  			res.values = []*big.Int{lastPrice}
   194  		}
   195  		// Besides, in order to collect enough data for sampling, if nothing
   196  		// meaningful returned, try to query more blocks. But the maximum
   197  		// is 2*checkBlocks.
   198  		if len(res.values) == 1 && len(results)+1+exp < oracle.checkBlocks*2 && number > 0 {
   199  			go oracle.getBlockValues(ctx, number, sampleNumber, oracle.ignorePrice, result, quit)
   200  			sent++
   201  			exp++
   202  			number--
   203  		}
   204  		results = append(results, res.values...)
   205  	}
   206  	price := lastPrice
   207  	if len(results) > 0 {
   208  		slices.SortFunc(results, func(a, b *big.Int) int { return a.Cmp(b) })
   209  		price = results[(len(results)-1)*oracle.percentile/100]
   210  	}
   211  	if price.Cmp(oracle.maxPrice) > 0 {
   212  		price = new(big.Int).Set(oracle.maxPrice)
   213  	}
   214  	oracle.cacheLock.Lock()
   215  	oracle.lastHead = headHash
   216  	oracle.lastPrice = price
   217  	oracle.cacheLock.Unlock()
   218  
   219  	return new(big.Int).Set(price), nil
   220  }
   221  
   222  type results struct {
   223  	values []*big.Int
   224  	err    error
   225  }
   226  
   227  // getBlockValues calculates the lowest transaction gas price in a given block
   228  // and sends it to the result channel. If the block is empty or all transactions
   229  // are sent by the miner itself(it doesn't make any sense to include this kind of
   230  // transaction prices for sampling), nil gasprice is returned.
   231  func (oracle *Oracle) getBlockValues(ctx context.Context, blockNum uint64, limit int, ignoreUnder *big.Int, result chan results, quit chan struct{}) {
   232  	block, err := oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNum))
   233  	if block == nil {
   234  		select {
   235  		case result <- results{nil, err}:
   236  		case <-quit:
   237  		}
   238  		return
   239  	}
   240  	signer := types.MakeSigner(oracle.backend.ChainConfig())
   241  
   242  	// Sort the transaction by effective tip in ascending sort.
   243  	txs := block.Transactions()
   244  	sortedTxs := make([]*types.Transaction, len(txs))
   245  	copy(sortedTxs, txs)
   246  	baseFee := block.BaseFee()
   247  	slices.SortFunc(sortedTxs, func(a, b *types.Transaction) int {
   248  		// It's okay to discard the error because a tx would never be
   249  		// accepted into a block with an invalid effective tip.
   250  		tip1, _ := a.EffectiveGasTip(baseFee)
   251  		tip2, _ := b.EffectiveGasTip(baseFee)
   252  		return tip1.Cmp(tip2)
   253  	})
   254  
   255  	var prices []*big.Int
   256  	for _, tx := range sortedTxs {
   257  		tip, _ := tx.EffectiveGasTip(baseFee)
   258  		if ignoreUnder != nil && tip.Cmp(ignoreUnder) == -1 {
   259  			continue
   260  		}
   261  		sender, err := types.Sender(signer, tx)
   262  		if err == nil && sender != block.Coinbase() {
   263  			prices = append(prices, tip)
   264  			if len(prices) >= limit {
   265  				break
   266  			}
   267  		}
   268  	}
   269  	select {
   270  	case result <- results{prices, nil}:
   271  	case <-quit:
   272  	}
   273  }