github.com/calmw/ethereum@v0.1.1/consensus/misc/eip4844.go (about)

     1  // Copyright 2023 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 misc
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/calmw/ethereum/params"
    23  )
    24  
    25  var (
    26  	minDataGasPrice            = big.NewInt(params.BlobTxMinDataGasprice)
    27  	dataGaspriceUpdateFraction = big.NewInt(params.BlobTxDataGaspriceUpdateFraction)
    28  )
    29  
    30  // CalcBlobFee calculates the blobfee from the header's excess data gas field.
    31  func CalcBlobFee(excessDataGas *big.Int) *big.Int {
    32  	// If this block does not yet have EIP-4844 enabled, return the starting fee
    33  	if excessDataGas == nil {
    34  		return big.NewInt(params.BlobTxMinDataGasprice)
    35  	}
    36  	return fakeExponential(minDataGasPrice, excessDataGas, dataGaspriceUpdateFraction)
    37  }
    38  
    39  // fakeExponential approximates factor * e ** (numerator / denominator) using
    40  // Taylor expansion.
    41  func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
    42  	var (
    43  		output = new(big.Int)
    44  		accum  = new(big.Int).Mul(factor, denominator)
    45  	)
    46  	for i := 1; accum.Sign() > 0; i++ {
    47  		output.Add(output, accum)
    48  
    49  		accum.Mul(accum, numerator)
    50  		accum.Div(accum, denominator)
    51  		accum.Div(accum, big.NewInt(int64(i)))
    52  	}
    53  	return output.Div(output, denominator)
    54  }