erigon-pulse/consensus/misc/eip4844.go
racytech 6588bca40b
eip-4844: NewMessage now expectes maxFeePerDataGas & GetPayloadV3 impl (#7365)
types.NewMessage now expects maxFeePerDataGas param, which will be used
in transaction verification (preCheck). GetPayloadV3 method added to
EngineAPI. Some cosmetic changes applied.
2023-04-23 18:27:05 +01:00

85 lines
2.8 KiB
Go

// Copyright 2021 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package misc
import (
"fmt"
"math/big"
"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/params"
)
// CalcExcessDataGas implements calc_excess_data_gas from EIP-4844
func CalcExcessDataGas(parentExcessDataGas *big.Int, newBlobs int) *big.Int {
excessDataGas := new(big.Int)
if parentExcessDataGas != nil {
excessDataGas.Set(parentExcessDataGas)
}
consumedGas := big.NewInt(params.DataGasPerBlob)
consumedGas.Mul(consumedGas, big.NewInt(int64(newBlobs)))
excessDataGas.Add(excessDataGas, consumedGas)
targetGas := big.NewInt(params.TargetDataGasPerBlock)
if excessDataGas.Cmp(targetGas) < 0 {
return new(big.Int)
}
return new(big.Int).Set(excessDataGas.Sub(excessDataGas, targetGas))
}
// FakeExponential approximates factor * e ** (num / denom) using a taylor expansion
// as described in the EIP-4844 spec.
func FakeExponential(factor, num, denom *big.Int) *big.Int {
output := new(big.Int)
numAccum := new(big.Int).Mul(factor, denom)
for i := 1; numAccum.Sign() > 0; i++ {
output.Add(output, numAccum)
numAccum.Mul(numAccum, num)
iBig := big.NewInt(int64(i))
numAccum.Div(numAccum, iBig.Mul(iBig, denom))
}
return output.Div(output, denom)
}
// CountBlobs returns the number of blob transactions in txs
func CountBlobs(txs []types.Transaction) int {
var count int
for _, tx := range txs {
count += len(tx.GetDataHashes())
}
return count
}
// VerifyEip4844Header verifies that the header is not malformed
func VerifyEip4844Header(config *chain.Config, parent, header *types.Header) error {
if header.ExcessDataGas == nil {
return fmt.Errorf("header is missing excessDataGas")
}
return nil
}
// GetDataGasPrice implements get_data_gas_price from EIP-4844
func GetDataGasPrice(excessDataGas *big.Int) *big.Int {
return FakeExponential(big.NewInt(params.MinDataGasPrice), excessDataGas, big.NewInt(params.DataGasPriceUpdateFraction))
}
func GetDataGasUsed(numBlobs int) uint64 {
return uint64(numBlobs) * params.DataGasPerBlob
}