github.com/theQRL/go-zond@v0.2.1/zond/gasprice/feehistory.go (about) 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 "encoding/binary" 22 "errors" 23 "fmt" 24 "math" 25 "math/big" 26 "slices" 27 "sync/atomic" 28 29 "github.com/theQRL/go-zond/common" 30 "github.com/theQRL/go-zond/consensus/misc/eip1559" 31 "github.com/theQRL/go-zond/core/types" 32 "github.com/theQRL/go-zond/log" 33 "github.com/theQRL/go-zond/rpc" 34 ) 35 36 var ( 37 errInvalidPercentile = errors.New("invalid reward percentile") 38 errRequestBeyondHead = errors.New("request beyond head block") 39 ) 40 41 const ( 42 // maxBlockFetchers is the max number of goroutines to spin up to pull blocks 43 // for the fee history calculation (mostly relevant for LES). 44 maxBlockFetchers = 4 45 // maxQueryLimit is the max number of requested percentiles. 46 maxQueryLimit = 100 47 ) 48 49 // blockFees represents a single block for processing 50 type blockFees struct { 51 // set by the caller 52 blockNumber uint64 53 header *types.Header 54 block *types.Block // only set if reward percentiles are requested 55 receipts types.Receipts 56 // filled by processBlock 57 results processedFees 58 err error 59 } 60 61 type cacheKey struct { 62 number uint64 63 percentiles string 64 } 65 66 // processedFees contains the results of a processed block. 67 type processedFees struct { 68 reward []*big.Int 69 baseFee, nextBaseFee *big.Int 70 gasUsedRatio float64 71 } 72 73 // txGasAndReward is sorted in ascending order based on reward 74 type txGasAndReward struct { 75 gasUsed uint64 76 reward *big.Int 77 } 78 79 // processBlock takes a blockFees structure with the blockNumber, the header and optionally 80 // the block field filled in, retrieves the block from the backend if not present yet and 81 // fills in the rest of the fields. 82 func (oracle *Oracle) processBlock(bf *blockFees, percentiles []float64) { 83 chainconfig := oracle.backend.ChainConfig() 84 85 // Fill in base fee and next base fee. 86 if bf.results.baseFee = bf.header.BaseFee; bf.results.baseFee == nil { 87 bf.results.baseFee = new(big.Int) 88 } 89 bf.results.nextBaseFee = eip1559.CalcBaseFee(chainconfig, bf.header) 90 // Compute gas used ratio. 91 bf.results.gasUsedRatio = float64(bf.header.GasUsed) / float64(bf.header.GasLimit) 92 if len(percentiles) == 0 { 93 // rewards were not requested, return null 94 return 95 } 96 if bf.block == nil || (bf.receipts == nil && len(bf.block.Transactions()) != 0) { 97 log.Error("Block or receipts are missing while reward percentiles are requested") 98 return 99 } 100 101 bf.results.reward = make([]*big.Int, len(percentiles)) 102 if len(bf.block.Transactions()) == 0 { 103 // return an all zero row if there are no transactions to gather data from 104 for i := range bf.results.reward { 105 bf.results.reward[i] = new(big.Int) 106 } 107 return 108 } 109 110 sorter := make([]txGasAndReward, len(bf.block.Transactions())) 111 for i, tx := range bf.block.Transactions() { 112 reward, _ := tx.EffectiveGasTip(bf.block.BaseFee()) 113 sorter[i] = txGasAndReward{gasUsed: bf.receipts[i].GasUsed, reward: reward} 114 } 115 slices.SortStableFunc(sorter, func(a, b txGasAndReward) int { 116 return a.reward.Cmp(b.reward) 117 }) 118 119 var txIndex int 120 sumGasUsed := sorter[0].gasUsed 121 122 for i, p := range percentiles { 123 thresholdGasUsed := uint64(float64(bf.block.GasUsed()) * p / 100) 124 for sumGasUsed < thresholdGasUsed && txIndex < len(bf.block.Transactions())-1 { 125 txIndex++ 126 sumGasUsed += sorter[txIndex].gasUsed 127 } 128 bf.results.reward[i] = sorter[txIndex].reward 129 } 130 } 131 132 // resolveBlockRange resolves the specified block range to absolute block numbers while also 133 // enforcing backend specific limitations. The pending block and corresponding receipts are 134 // also returned if requested and available. 135 // Note: an error is only returned if retrieving the head header has failed. If there are no 136 // retrievable blocks in the specified range then zero block count is returned with no error. 137 func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNumber, blocks uint64) (*types.Block, []*types.Receipt, uint64, uint64, error) { 138 var ( 139 headBlock *types.Header 140 pendingBlock *types.Block 141 pendingReceipts types.Receipts 142 err error 143 ) 144 145 // Get the chain's current head. 146 if headBlock, err = oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber); err != nil { 147 return nil, nil, 0, 0, err 148 } 149 head := rpc.BlockNumber(headBlock.Number.Uint64()) 150 151 // Fail if request block is beyond the chain's current head. 152 if head < reqEnd { 153 return nil, nil, 0, 0, fmt.Errorf("%w: requested %d, head %d", errRequestBeyondHead, reqEnd, head) 154 } 155 156 // Resolve block tag. 157 if reqEnd < 0 { 158 var ( 159 resolved *types.Header 160 err error 161 ) 162 switch reqEnd { 163 case rpc.PendingBlockNumber: 164 if pendingBlock, pendingReceipts, _ = oracle.backend.Pending(); pendingBlock != nil { 165 resolved = pendingBlock.Header() 166 } else { 167 // Pending block not supported by backend, process only until latest block. 168 resolved = headBlock 169 170 // Update total blocks to return to account for this. 171 blocks-- 172 } 173 case rpc.LatestBlockNumber: 174 // Retrieved above. 175 resolved = headBlock 176 case rpc.SafeBlockNumber: 177 resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.SafeBlockNumber) 178 case rpc.FinalizedBlockNumber: 179 resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.FinalizedBlockNumber) 180 case rpc.EarliestBlockNumber: 181 resolved, err = oracle.backend.HeaderByNumber(ctx, rpc.EarliestBlockNumber) 182 } 183 if resolved == nil || err != nil { 184 return nil, nil, 0, 0, err 185 } 186 // Absolute number resolved. 187 reqEnd = rpc.BlockNumber(resolved.Number.Uint64()) 188 } 189 190 // If there are no blocks to return, short circuit. 191 if blocks == 0 { 192 return nil, nil, 0, 0, nil 193 } 194 // Ensure not trying to retrieve before genesis. 195 if uint64(reqEnd+1) < blocks { 196 blocks = uint64(reqEnd + 1) 197 } 198 return pendingBlock, pendingReceipts, uint64(reqEnd), blocks, nil 199 } 200 201 // FeeHistory returns data relevant for fee estimation based on the specified range of blocks. 202 // The range can be specified either with absolute block numbers or ending with the latest 203 // or pending block. Backends may or may not support gathering data from the pending block 204 // or blocks older than a certain age (specified in maxHistory). The first block of the 205 // actually processed range is returned to avoid ambiguity when parts of the requested range 206 // are not available or when the head has changed during processing this request. 207 // Three arrays are returned based on the processed blocks: 208 // - reward: the requested percentiles of effective priority fees per gas of transactions in each 209 // block, sorted in ascending order and weighted by gas used. 210 // - baseFee: base fee per gas in the given block 211 // - gasUsedRatio: gasUsed/gasLimit in the given block 212 // 213 // Note: baseFee includes the next block after the newest of the returned range, because this 214 // value can be derived from the newest block. 215 func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedLastBlock rpc.BlockNumber, rewardPercentiles []float64) (*big.Int, [][]*big.Int, []*big.Int, []float64, error) { 216 if blocks < 1 { 217 return common.Big0, nil, nil, nil, nil // returning with no data and no error means there are no retrievable blocks 218 } 219 maxFeeHistory := oracle.maxHeaderHistory 220 if len(rewardPercentiles) != 0 { 221 maxFeeHistory = oracle.maxBlockHistory 222 } 223 if len(rewardPercentiles) > maxQueryLimit { 224 return common.Big0, nil, nil, nil, fmt.Errorf("%w: over the query limit %d", errInvalidPercentile, maxQueryLimit) 225 } 226 if blocks > maxFeeHistory { 227 log.Warn("Sanitizing fee history length", "requested", blocks, "truncated", maxFeeHistory) 228 blocks = maxFeeHistory 229 } 230 for i, p := range rewardPercentiles { 231 if p < 0 || p > 100 { 232 return common.Big0, nil, nil, nil, fmt.Errorf("%w: %f", errInvalidPercentile, p) 233 } 234 if i > 0 && p < rewardPercentiles[i-1] { 235 return common.Big0, nil, nil, nil, fmt.Errorf("%w: #%d:%f > #%d:%f", errInvalidPercentile, i-1, rewardPercentiles[i-1], i, p) 236 } 237 } 238 var ( 239 pendingBlock *types.Block 240 pendingReceipts []*types.Receipt 241 err error 242 ) 243 pendingBlock, pendingReceipts, lastBlock, blocks, err := oracle.resolveBlockRange(ctx, unresolvedLastBlock, blocks) 244 if err != nil || blocks == 0 { 245 return common.Big0, nil, nil, nil, err 246 } 247 oldestBlock := lastBlock + 1 - blocks 248 249 var next atomic.Uint64 250 next.Store(oldestBlock) 251 results := make(chan *blockFees, blocks) 252 253 percentileKey := make([]byte, 8*len(rewardPercentiles)) 254 for i, p := range rewardPercentiles { 255 binary.LittleEndian.PutUint64(percentileKey[i*8:(i+1)*8], math.Float64bits(p)) 256 } 257 for i := 0; i < maxBlockFetchers && i < int(blocks); i++ { 258 go func() { 259 for { 260 // Retrieve the next block number to fetch with this goroutine 261 blockNumber := next.Add(1) - 1 262 if blockNumber > lastBlock { 263 return 264 } 265 266 fees := &blockFees{blockNumber: blockNumber} 267 if pendingBlock != nil && blockNumber >= pendingBlock.NumberU64() { 268 fees.block, fees.receipts = pendingBlock, pendingReceipts 269 fees.header = fees.block.Header() 270 oracle.processBlock(fees, rewardPercentiles) 271 results <- fees 272 } else { 273 cacheKey := cacheKey{number: blockNumber, percentiles: string(percentileKey)} 274 275 if p, ok := oracle.historyCache.Get(cacheKey); ok { 276 fees.results = p 277 results <- fees 278 } else { 279 if len(rewardPercentiles) != 0 { 280 fees.block, fees.err = oracle.backend.BlockByNumber(ctx, rpc.BlockNumber(blockNumber)) 281 if fees.block != nil && fees.err == nil { 282 fees.receipts, fees.err = oracle.backend.GetReceipts(ctx, fees.block.Hash()) 283 fees.header = fees.block.Header() 284 } 285 } else { 286 fees.header, fees.err = oracle.backend.HeaderByNumber(ctx, rpc.BlockNumber(blockNumber)) 287 } 288 if fees.header != nil && fees.err == nil { 289 oracle.processBlock(fees, rewardPercentiles) 290 if fees.err == nil { 291 oracle.historyCache.Add(cacheKey, fees.results) 292 } 293 } 294 // send to results even if empty to guarantee that blocks items are sent in total 295 results <- fees 296 } 297 } 298 } 299 }() 300 } 301 var ( 302 reward = make([][]*big.Int, blocks) 303 baseFee = make([]*big.Int, blocks+1) 304 gasUsedRatio = make([]float64, blocks) 305 firstMissing = blocks 306 ) 307 for ; blocks > 0; blocks-- { 308 fees := <-results 309 if fees.err != nil { 310 return common.Big0, nil, nil, nil, fees.err 311 } 312 i := fees.blockNumber - oldestBlock 313 if fees.results.baseFee != nil { 314 reward[i], baseFee[i], baseFee[i+1], gasUsedRatio[i] = fees.results.reward, fees.results.baseFee, fees.results.nextBaseFee, fees.results.gasUsedRatio 315 } else { 316 // getting no block and no error means we are requesting into the future (might happen because of a reorg) 317 if i < firstMissing { 318 firstMissing = i 319 } 320 } 321 } 322 if firstMissing == 0 { 323 return common.Big0, nil, nil, nil, nil 324 } 325 if len(rewardPercentiles) != 0 { 326 reward = reward[:firstMissing] 327 } else { 328 reward = nil 329 } 330 baseFee, gasUsedRatio = baseFee[:firstMissing+1], gasUsedRatio[:firstMissing] 331 return new(big.Int).SetUint64(oldestBlock), reward, baseFee, gasUsedRatio, nil 332 }