|
| 1 | +// Copyright 2021 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/atomic" |
| 25 | + |
| 26 | + "github.com/ethereum/go-ethereum/consensus/misc" |
| 27 | + "github.com/ethereum/go-ethereum/core/types" |
| 28 | + "github.com/ethereum/go-ethereum/log" |
| 29 | + "github.com/ethereum/go-ethereum/rpc" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + errInvalidPercentiles = errors.New("Invalid reward percentiles") |
| 34 | + errRequestBeyondHead = errors.New("Request beyond head block") |
| 35 | +) |
| 36 | + |
| 37 | +const maxBlockCount = 1024 // number of blocks retrievable with a single query |
| 38 | + |
| 39 | +// blockFees represents a single block for processing |
| 40 | +type blockFees struct { |
| 41 | + // set by the caller |
| 42 | + blockNumber rpc.BlockNumber |
| 43 | + header *types.Header |
| 44 | + block *types.Block // only set if reward percentiles are requested |
| 45 | + receipts types.Receipts |
| 46 | + // filled by processBlock |
| 47 | + reward []*big.Int |
| 48 | + baseFee, nextBaseFee *big.Int |
| 49 | + gasUsedRatio float64 |
| 50 | + err error |
| 51 | +} |
| 52 | + |
| 53 | +// txGasAndReward is sorted in ascending order based on reward |
| 54 | +type ( |
| 55 | + txGasAndReward struct { |
| 56 | + gasUsed uint64 |
| 57 | + reward *big.Int |
| 58 | + } |
| 59 | + sortGasAndReward []txGasAndReward |
| 60 | +) |
| 61 | + |
| 62 | +func (s sortGasAndReward) Len() int { return len(s) } |
| 63 | +func (s sortGasAndReward) Swap(i, j int) { |
| 64 | + s[i], s[j] = s[j], s[i] |
| 65 | +} |
| 66 | +func (s sortGasAndReward) Less(i, j int) bool { |
| 67 | + return s[i].reward.Cmp(s[j].reward) < 0 |
| 68 | +} |
| 69 | + |
| 70 | +// processBlock takes a blockFees structure with the blockNumber, the header and optionally |
| 71 | +// the block field filled in, retrieves the block from the backend if not present yet and |
| 72 | +// fills in the rest of the fields. |
| 73 | +func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) { |
| 74 | + chainconfig := oracle.backend.ChainConfig() |
| 75 | + if bf.baseFee = bf.header.BaseFee; bf.baseFee == nil { |
| 76 | + bf.baseFee = new(big.Int) |
| 77 | + } |
| 78 | + if chainconfig.IsLondon(big.NewInt(int64(bf.blockNumber + 1))) { |
| 79 | + bf.nextBaseFee = misc.CalcBaseFee(chainconfig, bf.header) |
| 80 | + } else { |
| 81 | + bf.nextBaseFee = new(big.Int) |
| 82 | + } |
| 83 | + bf.gasUsedRatio = float64(bf.header.GasUsed) / float64(bf.header.GasLimit) |
| 84 | + if len(percentiles) == 0 { |
| 85 | + // rewards were not requested, return null |
| 86 | + return |
| 87 | + } |
| 88 | + if bf.block == nil || (bf.receipts == nil && len(bf.block.Transactions()) != 0) { |
| 89 | + log.Error("Block or receipts are missing while reward percentiles are requested") |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + bf.reward = make([]*big.Int, len(percentiles)) |
| 94 | + if len(bf.block.Transactions()) == 0 { |
| 95 | + // return an all zero row if there are no transactions to gather data from |
| 96 | + for i := range bf.reward { |
| 97 | + bf.reward[i] = new(big.Int) |
| 98 | + } |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + sorter := make(sortGasAndReward, len(bf.block.Transactions())) |
| 103 | + for i, tx := range bf.block.Transactions() { |
| 104 | + reward, _ := tx.EffectiveGasTip(bf.block.BaseFee()) |
| 105 | + sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward} |
| 106 | + } |
| 107 | + sort.Sort(sorter) |
| 108 | + |
| 109 | + var txIndex int |
| 110 | + sumGasUsed := sorter[0].gasUsed |
| 111 | + |
| 112 | + for i, p := range percentiles { |
| 113 | + thresholdGasUsed := uint64(float64(bf.block.GasUsed()) * p / 100) |
| 114 | + for sumGasUsed < thresholdGasUsed && txIndex < len(bf.block.Transactions())-1 { |
| 115 | + txIndex++ |
| 116 | + sumGasUsed += sorter[txIndex].gasUsed |
| 117 | + } |
| 118 | + bf.reward[i] = sorter[txIndex].reward |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// resolveBlockRange resolves the specified block range to absolute block numbers while also |
| 123 | +// enforcing backend specific limitations. The pending block and corresponding receipts are |
| 124 | +// also returned if requested and available. |
| 125 | +// Note: an error is only returned if retrieving the head header has failed. If there are no |
| 126 | +// retrievable blocks in the specified range then zero block count is returned with no error. |
| 127 | +func (oracle *Oracle) resolveBlockRange(ctx context.Context, lastBlockNumber rpc.BlockNumber, blockCount, maxHistory int) (*types.Block, types.Receipts, rpc.BlockNumber, int, error) { |
| 128 | + var ( |
| 129 | + headBlockNumber rpc.BlockNumber |
| 130 | + pendingBlock *types.Block |
| 131 | + pendingReceipts types.Receipts |
| 132 | + ) |
| 133 | + |
| 134 | + // query either pending block or head header and set headBlockNumber |
| 135 | + if lastBlockNumber == rpc.PendingBlockNumber { |
| 136 | + if pendingBlock, pendingReceipts = oracle.backend.PendingBlockAndReceipts(); pendingBlock != nil { |
| 137 | + lastBlockNumber = rpc.BlockNumber(pendingBlock.NumberU64()) |
| 138 | + headBlockNumber = lastBlockNumber - 1 |
| 139 | + } else { |
| 140 | + // pending block not supported by backend, process until latest block |
| 141 | + lastBlockNumber = rpc.LatestBlockNumber |
| 142 | + blockCount-- |
| 143 | + if blockCount == 0 { |
| 144 | + return nil, nil, 0, 0, nil |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + if pendingBlock == nil { |
| 149 | + // if pending block is not fetched then we retrieve the head header to get the head block number |
| 150 | + if latestHeader, err := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err == nil { |
| 151 | + headBlockNumber = rpc.BlockNumber(latestHeader.Number.Uint64()) |
| 152 | + } else { |
| 153 | + return nil, nil, 0, 0, err |
| 154 | + } |
| 155 | + } |
| 156 | + if lastBlockNumber == rpc.LatestBlockNumber { |
| 157 | + lastBlockNumber = headBlockNumber |
| 158 | + } else if pendingBlock == nil && lastBlockNumber > headBlockNumber { |
| 159 | + return nil, nil, 0, 0, errRequestBeyondHead |
| 160 | + } |
| 161 | + if maxHistory != 0 { |
| 162 | + // limit retrieval to the given number of latest blocks |
| 163 | + if tooOldCount := int64(headBlockNumber) - int64(maxHistory) - int64(lastBlockNumber) + int64(blockCount); tooOldCount > 0 { |
| 164 | + // tooOldCount is the number of requested blocks that are too old to be served |
| 165 | + if int64(blockCount) > tooOldCount { |
| 166 | + blockCount -= int(tooOldCount) |
| 167 | + } else { |
| 168 | + return nil, nil, 0, 0, nil |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + // ensure not trying to retrieve before genesis |
| 173 | + if rpc.BlockNumber(blockCount) > lastBlockNumber+1 { |
| 174 | + blockCount = int(lastBlockNumber + 1) |
| 175 | + } |
| 176 | + return pendingBlock, pendingReceipts, lastBlockNumber, blockCount, nil |
| 177 | +} |
| 178 | + |
| 179 | +// FeeHistory returns data relevant for fee estimation based on the specified range of blocks. |
| 180 | +// The range can be specified either with absolute block numbers or ending with the latest |
| 181 | +// or pending block. Backends may or may not support gathering data from the pending block |
| 182 | +// or blocks older than a certain age (specified in maxHistory). The first block of the |
| 183 | +// actually processed range is returned to avoid ambiguity when parts of the requested range |
| 184 | +// are not available or when the head has changed during processing this request. |
| 185 | +// Three arrays are returned based on the processed blocks: |
| 186 | +// - reward: the requested percentiles of effective priority fees per gas of transactions in each |
| 187 | +// block, sorted in ascending order and weighted by gas used. |
| 188 | +// - baseFee: base fee per gas in the given block |
| 189 | +// - gasUsedRatio: gasUsed/gasLimit in the given block |
| 190 | +// Note: baseFee includes the next block after the newest of the returned range, because this |
| 191 | +// value can be derived from the newest block. |
| 192 | +func (oracle *Oracle) FeeHistory(ctx context.Context, blockCount int, lastBlockNumber rpc.BlockNumber, rewardPercentiles []float64) (firstBlockNumber rpc.BlockNumber, reward [][]*big.Int, baseFee []*big.Int, gasUsedRatio []float64, err error) { |
| 193 | + if blockCount < 1 { |
| 194 | + // returning with no data and no error means there are no retrievable blocks |
| 195 | + return |
| 196 | + } |
| 197 | + if blockCount > maxBlockCount { |
| 198 | + blockCount = maxBlockCount |
| 199 | + } |
| 200 | + for i, p := range rewardPercentiles { |
| 201 | + if p < 0 || p > 100 || (i > 0 && p < rewardPercentiles[i-1]) { |
| 202 | + return 0, nil, nil, nil, errInvalidPercentiles |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + processBlocks := len(rewardPercentiles) != 0 |
| 207 | + // limit retrieval to maxHistory if set |
| 208 | + var maxHistory int |
| 209 | + if processBlocks { |
| 210 | + maxHistory = oracle.maxBlockHistory |
| 211 | + } else { |
| 212 | + maxHistory = oracle.maxHeaderHistory |
| 213 | + } |
| 214 | + |
| 215 | + var ( |
| 216 | + pendingBlock *types.Block |
| 217 | + pendingReceipts types.Receipts |
| 218 | + ) |
| 219 | + if pendingBlock, pendingReceipts, lastBlockNumber, blockCount, err = oracle.resolveBlockRange(ctx, lastBlockNumber, blockCount, maxHistory); err != nil || blockCount == 0 { |
| 220 | + return |
| 221 | + } |
| 222 | + firstBlockNumber = lastBlockNumber + 1 - rpc.BlockNumber(blockCount) |
| 223 | + |
| 224 | + processNext := int64(firstBlockNumber) |
| 225 | + resultCh := make(chan *blockFees, blockCount) |
| 226 | + threadCount := 4 |
| 227 | + if blockCount < threadCount { |
| 228 | + threadCount = blockCount |
| 229 | + } |
| 230 | + for i := 0; i < threadCount; i++ { |
| 231 | + go func() { |
| 232 | + for { |
| 233 | + blockNumber := rpc.BlockNumber(atomic.AddInt64(&processNext, 1) - 1) |
| 234 | + if blockNumber > lastBlockNumber { |
| 235 | + return |
| 236 | + } |
| 237 | + |
| 238 | + bf := &blockFees{blockNumber: blockNumber} |
| 239 | + if pendingBlock != nil && blockNumber >= rpc.BlockNumber(pendingBlock.NumberU64()) { |
| 240 | + bf.block, bf.receipts = pendingBlock, pendingReceipts |
| 241 | + } else { |
| 242 | + if processBlocks { |
| 243 | + bf.block, bf.err = oracle.backend.BlockByNumber(ctx, blockNumber) |
| 244 | + if bf.block != nil { |
| 245 | + bf.receipts, bf.err = oracle.backend.GetReceipts(ctx, bf.block.Hash()) |
| 246 | + } |
| 247 | + } else { |
| 248 | + bf.header, bf.err = oracle.backend.HeaderByNumber(ctx, blockNumber) |
| 249 | + } |
| 250 | + } |
| 251 | + if bf.block != nil { |
| 252 | + bf.header = bf.block.Header() |
| 253 | + } |
| 254 | + if bf.header != nil { |
| 255 | + oracle.processBlock(bf, rewardPercentiles) |
| 256 | + } |
| 257 | + // send to resultCh even if empty to guarantee that blockCount items are sent in total |
| 258 | + resultCh <- bf |
| 259 | + } |
| 260 | + }() |
| 261 | + } |
| 262 | + |
| 263 | + reward = make([][]*big.Int, blockCount) |
| 264 | + baseFee = make([]*big.Int, blockCount+1) |
| 265 | + gasUsedRatio = make([]float64, blockCount) |
| 266 | + firstMissing := blockCount |
| 267 | + |
| 268 | + for ; blockCount > 0; blockCount-- { |
| 269 | + bf := <-resultCh |
| 270 | + if bf.err != nil { |
| 271 | + return 0, nil, nil, nil, bf.err |
| 272 | + } |
| 273 | + i := int(bf.blockNumber - firstBlockNumber) |
| 274 | + if bf.header != nil { |
| 275 | + reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = bf.reward, bf.baseFee, bf.nextBaseFee, bf.gasUsedRatio |
| 276 | + } else { |
| 277 | + // getting no block and no error means we are requesting into the future (might happen because of a reorg) |
| 278 | + if i < firstMissing { |
| 279 | + firstMissing = i |
| 280 | + } |
| 281 | + } |
| 282 | + } |
| 283 | + if firstMissing == 0 { |
| 284 | + return 0, nil, nil, nil, nil |
| 285 | + } |
| 286 | + if processBlocks { |
| 287 | + reward = reward[:firstMissing] |
| 288 | + } else { |
| 289 | + reward = nil |
| 290 | + } |
| 291 | + baseFee, gasUsedRatio = baseFee[:firstMissing+1], gasUsedRatio[:firstMissing] |
| 292 | + return |
| 293 | +} |
0 commit comments