@@ -28,8 +28,10 @@ import (
2828
2929 "github.com/ethereum/go-ethereum/common"
3030 "github.com/ethereum/go-ethereum/consensus/misc/eip1559"
31+ "github.com/ethereum/go-ethereum/consensus/misc/eip4844"
3132 "github.com/ethereum/go-ethereum/core/types"
3233 "github.com/ethereum/go-ethereum/log"
34+ "github.com/ethereum/go-ethereum/params"
3335 "github.com/ethereum/go-ethereum/rpc"
3436)
3537
@@ -63,9 +65,11 @@ type cacheKey struct {
6365
6466// processedFees contains the results of a processed block.
6567type processedFees struct {
66- reward []* big.Int
67- baseFee , nextBaseFee * big.Int
68- gasUsedRatio float64
68+ reward []* big.Int
69+ baseFee , nextBaseFee * big.Int
70+ gasUsedRatio float64
71+ blobGasUsedRatio float64
72+ blobBaseFee , nextBlobBaseFee * big.Int
6973}
7074
7175// txGasAndReward is sorted in ascending order based on reward
@@ -78,16 +82,31 @@ type txGasAndReward struct {
7882// the block field filled in, retrieves the block from the backend if not present yet and
7983// fills in the rest of the fields.
8084func (oracle * Oracle ) processBlock (bf * blockFees , percentiles []float64 ) {
81- chainconfig := oracle .backend .ChainConfig ()
85+ config := oracle .backend .ChainConfig ()
86+
87+ // Fill in base fee and next base fee.
8288 if bf .results .baseFee = bf .header .BaseFee ; bf .results .baseFee == nil {
8389 bf .results .baseFee = new (big.Int )
8490 }
85- if chainconfig .IsLondon (big .NewInt (int64 (bf .blockNumber + 1 ))) {
86- bf .results .nextBaseFee = eip1559 .CalcBaseFee (chainconfig , bf .header )
91+ if config .IsLondon (big .NewInt (int64 (bf .blockNumber + 1 ))) {
92+ bf .results .nextBaseFee = eip1559 .CalcBaseFee (config , bf .header )
8793 } else {
8894 bf .results .nextBaseFee = new (big.Int )
8995 }
96+ // Fill in blob base fee and next blob base fee.
97+ if excessBlobGas := bf .header .ExcessBlobGas ; excessBlobGas != nil {
98+ bf .results .blobBaseFee = eip4844 .CalcBlobFee (* excessBlobGas )
99+ bf .results .nextBlobBaseFee = eip4844 .CalcBlobFee (eip4844 .CalcExcessBlobGas (* excessBlobGas , * bf .header .BlobGasUsed ))
100+ } else {
101+ bf .results .blobBaseFee = new (big.Int )
102+ bf .results .nextBlobBaseFee = new (big.Int )
103+ }
104+ // Compute gas used ratio for normal and blob gas.
90105 bf .results .gasUsedRatio = float64 (bf .header .GasUsed ) / float64 (bf .header .GasLimit )
106+ if blobGasUsed := bf .header .BlobGasUsed ; blobGasUsed != nil {
107+ bf .results .blobGasUsedRatio = float64 (* blobGasUsed ) / params .MaxBlobGasPerBlock
108+ }
109+
91110 if len (percentiles ) == 0 {
92111 // rewards were not requested, return null
93112 return
@@ -203,17 +222,19 @@ func (oracle *Oracle) resolveBlockRange(ctx context.Context, reqEnd rpc.BlockNum
203222// or blocks older than a certain age (specified in maxHistory). The first block of the
204223// actually processed range is returned to avoid ambiguity when parts of the requested range
205224// are not available or when the head has changed during processing this request.
206- // Three arrays are returned based on the processed blocks:
225+ // Five arrays are returned based on the processed blocks:
207226// - reward: the requested percentiles of effective priority fees per gas of transactions in each
208227// block, sorted in ascending order and weighted by gas used.
209228// - baseFee: base fee per gas in the given block
210229// - gasUsedRatio: gasUsed/gasLimit in the given block
230+ // - blobBaseFee: the blob base fee per gas in the given block
231+ // - blobGasUsedRatio: blobGasUsed/blobGasLimit in the given block
211232//
212- // Note: baseFee includes the next block after the newest of the returned range, because this
213- // value can be derived from the newest block.
214- func (oracle * Oracle ) FeeHistory (ctx context.Context , blocks uint64 , unresolvedLastBlock rpc.BlockNumber , rewardPercentiles []float64 ) (* big.Int , [][]* big.Int , []* big.Int , []float64 , error ) {
233+ // Note: baseFee and blobBaseFee both include the next block after the newest of the returned range,
234+ // because this value can be derived from the newest block.
235+ func (oracle * Oracle ) FeeHistory (ctx context.Context , blocks uint64 , unresolvedLastBlock rpc.BlockNumber , rewardPercentiles []float64 ) (* big.Int , [][]* big.Int , []* big.Int , []float64 , [] * big. Int , [] float64 , error ) {
215236 if blocks < 1 {
216- return common .Big0 , nil , nil , nil , nil // returning with no data and no error means there are no retrievable blocks
237+ return common .Big0 , nil , nil , nil , nil , nil , nil // returning with no data and no error means there are no retrievable blocks
217238 }
218239 maxFeeHistory := oracle .maxHeaderHistory
219240 if len (rewardPercentiles ) != 0 {
@@ -225,10 +246,10 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
225246 }
226247 for i , p := range rewardPercentiles {
227248 if p < 0 || p > 100 {
228- return common .Big0 , nil , nil , nil , fmt .Errorf ("%w: %f" , errInvalidPercentile , p )
249+ return common .Big0 , nil , nil , nil , nil , nil , fmt .Errorf ("%w: %f" , errInvalidPercentile , p )
229250 }
230251 if i > 0 && p <= rewardPercentiles [i - 1 ] {
231- return common .Big0 , nil , nil , nil , fmt .Errorf ("%w: #%d:%f >= #%d:%f" , errInvalidPercentile , i - 1 , rewardPercentiles [i - 1 ], i , p )
252+ return common .Big0 , nil , nil , nil , nil , nil , fmt .Errorf ("%w: #%d:%f >= #%d:%f" , errInvalidPercentile , i - 1 , rewardPercentiles [i - 1 ], i , p )
232253 }
233254 }
234255 var (
@@ -238,7 +259,7 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
238259 )
239260 pendingBlock , pendingReceipts , lastBlock , blocks , err := oracle .resolveBlockRange (ctx , unresolvedLastBlock , blocks )
240261 if err != nil || blocks == 0 {
241- return common .Big0 , nil , nil , nil , err
262+ return common .Big0 , nil , nil , nil , nil , nil , err
242263 }
243264 oldestBlock := lastBlock + 1 - blocks
244265
@@ -295,19 +316,22 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
295316 }()
296317 }
297318 var (
298- reward = make ([][]* big.Int , blocks )
299- baseFee = make ([]* big.Int , blocks + 1 )
300- gasUsedRatio = make ([]float64 , blocks )
301- firstMissing = blocks
319+ reward = make ([][]* big.Int , blocks )
320+ baseFee = make ([]* big.Int , blocks + 1 )
321+ gasUsedRatio = make ([]float64 , blocks )
322+ blobGasUsedRatio = make ([]float64 , blocks )
323+ blobBaseFee = make ([]* big.Int , blocks + 1 )
324+ firstMissing = blocks
302325 )
303326 for ; blocks > 0 ; blocks -- {
304327 fees := <- results
305328 if fees .err != nil {
306- return common .Big0 , nil , nil , nil , fees .err
329+ return common .Big0 , nil , nil , nil , nil , nil , fees .err
307330 }
308331 i := fees .blockNumber - oldestBlock
309332 if fees .results .baseFee != nil {
310333 reward [i ], baseFee [i ], baseFee [i + 1 ], gasUsedRatio [i ] = fees .results .reward , fees .results .baseFee , fees .results .nextBaseFee , fees .results .gasUsedRatio
334+ blobGasUsedRatio [i ], blobBaseFee [i ], blobBaseFee [i + 1 ] = fees .results .blobGasUsedRatio , fees .results .blobBaseFee , fees .results .nextBlobBaseFee
311335 } else {
312336 // getting no block and no error means we are requesting into the future (might happen because of a reorg)
313337 if i < firstMissing {
@@ -316,13 +340,14 @@ func (oracle *Oracle) FeeHistory(ctx context.Context, blocks uint64, unresolvedL
316340 }
317341 }
318342 if firstMissing == 0 {
319- return common .Big0 , nil , nil , nil , nil
343+ return common .Big0 , nil , nil , nil , nil , nil , nil
320344 }
321345 if len (rewardPercentiles ) != 0 {
322346 reward = reward [:firstMissing ]
323347 } else {
324348 reward = nil
325349 }
326350 baseFee , gasUsedRatio = baseFee [:firstMissing + 1 ], gasUsedRatio [:firstMissing ]
327- return new (big.Int ).SetUint64 (oldestBlock ), reward , baseFee , gasUsedRatio , nil
351+ blobBaseFee , blobGasUsedRatio = blobBaseFee [:firstMissing + 1 ], blobGasUsedRatio [:firstMissing ]
352+ return new (big.Int ).SetUint64 (oldestBlock ), reward , baseFee , gasUsedRatio , blobBaseFee , blobGasUsedRatio , nil
328353}
0 commit comments