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