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