misc/eip4844: reduce allocations in calcBlobFee

This commit is contained in:
MariusVanDerWijden 2025-12-01 19:54:18 +01:00
parent 449f9300f6
commit 44602e0a21

View file

@ -53,9 +53,9 @@ func (bc *BlobConfig) blobPrice(excessBlobGas uint64) *big.Int {
return new(big.Int).Mul(f, big.NewInt(params.BlobTxBlobGasPerBlob)) return new(big.Int).Mul(f, big.NewInt(params.BlobTxBlobGasPerBlob))
} }
func latestBlobConfig(cfg *params.ChainConfig, time uint64) *BlobConfig { func latestBlobConfig(cfg *params.ChainConfig, time uint64) (BlobConfig, error) {
if cfg.BlobScheduleConfig == nil { if cfg.BlobScheduleConfig == nil {
return nil return BlobConfig{}, errors.New("no blob config")
} }
var ( var (
london = cfg.LondonBlock london = cfg.LondonBlock
@ -80,14 +80,14 @@ func latestBlobConfig(cfg *params.ChainConfig, time uint64) *BlobConfig {
case cfg.IsCancun(london, time) && s.Cancun != nil: case cfg.IsCancun(london, time) && s.Cancun != nil:
bc = s.Cancun bc = s.Cancun
default: default:
return nil return BlobConfig{}, errors.New("no blob config")
} }
return &BlobConfig{ return BlobConfig{
Target: bc.Target, Target: bc.Target,
Max: bc.Max, Max: bc.Max,
UpdateFraction: bc.UpdateFraction, UpdateFraction: bc.UpdateFraction,
} }, nil
} }
// VerifyEIP4844Header verifies the presence of the excessBlobGas field and that // VerifyEIP4844Header verifies the presence of the excessBlobGas field and that
@ -98,8 +98,8 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade
panic("bad header pair") panic("bad header pair")
} }
bcfg := latestBlobConfig(config, header.Time) bcfg, err := latestBlobConfig(config, header.Time)
if bcfg == nil { if err != nil {
panic("called before EIP-4844 is active") panic("called before EIP-4844 is active")
} }
@ -130,11 +130,14 @@ func VerifyEIP4844Header(config *params.ChainConfig, parent, header *types.Heade
// blobs on top of the excess blob gas. // blobs on top of the excess blob gas.
func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTimestamp uint64) uint64 { func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTimestamp uint64) uint64 {
isOsaka := config.IsOsaka(config.LondonBlock, headTimestamp) isOsaka := config.IsOsaka(config.LondonBlock, headTimestamp)
bcfg := latestBlobConfig(config, headTimestamp) bcfg, err := latestBlobConfig(config, headTimestamp)
if err != nil {
panic("calculating excess blob gas on nil blob config")
}
return calcExcessBlobGas(isOsaka, bcfg, parent) return calcExcessBlobGas(isOsaka, bcfg, parent)
} }
func calcExcessBlobGas(isOsaka bool, bcfg *BlobConfig, parent *types.Header) uint64 { func calcExcessBlobGas(isOsaka bool, bcfg BlobConfig, parent *types.Header) uint64 {
var parentExcessBlobGas, parentBlobGasUsed uint64 var parentExcessBlobGas, parentBlobGasUsed uint64
if parent.ExcessBlobGas != nil { if parent.ExcessBlobGas != nil {
parentExcessBlobGas = *parent.ExcessBlobGas parentExcessBlobGas = *parent.ExcessBlobGas
@ -169,8 +172,8 @@ func calcExcessBlobGas(isOsaka bool, bcfg *BlobConfig, parent *types.Header) uin
// CalcBlobFee calculates the blobfee from the header's excess blob gas field. // CalcBlobFee calculates the blobfee from the header's excess blob gas field.
func CalcBlobFee(config *params.ChainConfig, header *types.Header) *big.Int { func CalcBlobFee(config *params.ChainConfig, header *types.Header) *big.Int {
blobConfig := latestBlobConfig(config, header.Time) blobConfig, err := latestBlobConfig(config, header.Time)
if blobConfig == nil { if err != nil {
panic("calculating blob fee on unsupported fork") panic("calculating blob fee on unsupported fork")
} }
return blobConfig.blobBaseFee(*header.ExcessBlobGas) return blobConfig.blobBaseFee(*header.ExcessBlobGas)
@ -178,8 +181,8 @@ func CalcBlobFee(config *params.ChainConfig, header *types.Header) *big.Int {
// MaxBlobsPerBlock returns the max blobs per block for a block at the given timestamp. // MaxBlobsPerBlock returns the max blobs per block for a block at the given timestamp.
func MaxBlobsPerBlock(cfg *params.ChainConfig, time uint64) int { func MaxBlobsPerBlock(cfg *params.ChainConfig, time uint64) int {
blobConfig := latestBlobConfig(cfg, time) blobConfig, err := latestBlobConfig(cfg, time)
if blobConfig == nil { if err != nil {
return 0 return 0
} }
return blobConfig.Max return blobConfig.Max
@ -193,8 +196,8 @@ func MaxBlobGasPerBlock(cfg *params.ChainConfig, time uint64) uint64 {
// LatestMaxBlobsPerBlock returns the latest max blobs per block defined by the // LatestMaxBlobsPerBlock returns the latest max blobs per block defined by the
// configuration, regardless of the currently active fork. // configuration, regardless of the currently active fork.
func LatestMaxBlobsPerBlock(cfg *params.ChainConfig) int { func LatestMaxBlobsPerBlock(cfg *params.ChainConfig) int {
bcfg := latestBlobConfig(cfg, math.MaxUint64) bcfg, err := latestBlobConfig(cfg, math.MaxUint64)
if bcfg == nil { if err != nil {
return 0 return 0
} }
return bcfg.Max return bcfg.Max
@ -202,8 +205,8 @@ func LatestMaxBlobsPerBlock(cfg *params.ChainConfig) int {
// TargetBlobsPerBlock returns the target blobs per block for a block at the given timestamp. // TargetBlobsPerBlock returns the target blobs per block for a block at the given timestamp.
func TargetBlobsPerBlock(cfg *params.ChainConfig, time uint64) int { func TargetBlobsPerBlock(cfg *params.ChainConfig, time uint64) int {
blobConfig := latestBlobConfig(cfg, time) blobConfig, err := latestBlobConfig(cfg, time)
if blobConfig == nil { if err != nil {
return 0 return 0
} }
return blobConfig.Target return blobConfig.Target