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