2023-04-04 11:25:28 +03:00
|
|
|
// Copyright 2023 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/>.
|
|
|
|
|
2023-07-27 14:11:09 +03:00
|
|
|
package eip4844
|
2023-04-04 11:25:28 +03:00
|
|
|
|
|
|
|
import (
|
2023-05-31 10:21:13 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-04-04 11:25:28 +03:00
|
|
|
"math/big"
|
|
|
|
|
2023-05-31 10:21:13 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-04-04 11:25:28 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-07-27 16:53:28 +03:00
|
|
|
minBlobGasPrice = big.NewInt(params.BlobTxMinBlobGasprice)
|
|
|
|
blobGaspriceUpdateFraction = big.NewInt(params.BlobTxBlobGaspriceUpdateFraction)
|
2023-04-04 11:25:28 +03:00
|
|
|
)
|
|
|
|
|
2023-07-27 16:53:28 +03:00
|
|
|
// VerifyEIP4844Header verifies the presence of the excessBlobGas field and that
|
|
|
|
// if the current block contains no transactions, the excessBlobGas is updated
|
2023-05-31 10:21:13 +03:00
|
|
|
// accordingly.
|
|
|
|
func VerifyEIP4844Header(parent, header *types.Header) error {
|
|
|
|
// Verify the header is not malformed
|
2023-07-27 16:53:28 +03:00
|
|
|
if header.ExcessBlobGas == nil {
|
|
|
|
return errors.New("header is missing excessBlobGas")
|
2023-05-31 10:21:13 +03:00
|
|
|
}
|
2023-07-27 16:53:28 +03:00
|
|
|
if header.BlobGasUsed == nil {
|
|
|
|
return errors.New("header is missing blobGasUsed")
|
2023-05-31 10:21:13 +03:00
|
|
|
}
|
2023-08-01 10:07:25 +03:00
|
|
|
// Verify that the blob gas used remains within reasonable limits.
|
2023-08-24 00:16:14 +03:00
|
|
|
if *header.BlobGasUsed > params.MaxBlobGasPerBlock {
|
|
|
|
return fmt.Errorf("blob gas used %d exceeds maximum allowance %d", *header.BlobGasUsed, params.MaxBlobGasPerBlock)
|
2023-05-31 10:21:13 +03:00
|
|
|
}
|
2023-07-27 16:53:28 +03:00
|
|
|
if *header.BlobGasUsed%params.BlobTxBlobGasPerBlob != 0 {
|
2023-08-01 10:07:25 +03:00
|
|
|
return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, params.BlobTxBlobGasPerBlob)
|
2023-05-31 10:21:13 +03:00
|
|
|
}
|
2023-07-27 16:53:28 +03:00
|
|
|
// Verify the excessBlobGas is correct based on the parent header
|
2023-05-31 10:21:13 +03:00
|
|
|
var (
|
2023-07-27 16:53:28 +03:00
|
|
|
parentExcessBlobGas uint64
|
|
|
|
parentBlobGasUsed uint64
|
2023-05-31 10:21:13 +03:00
|
|
|
)
|
2023-07-27 16:53:28 +03:00
|
|
|
if parent.ExcessBlobGas != nil {
|
|
|
|
parentExcessBlobGas = *parent.ExcessBlobGas
|
|
|
|
parentBlobGasUsed = *parent.BlobGasUsed
|
2023-04-04 11:25:28 +03:00
|
|
|
}
|
2023-07-27 16:53:28 +03:00
|
|
|
expectedExcessBlobGas := CalcExcessBlobGas(parentExcessBlobGas, parentBlobGasUsed)
|
|
|
|
if *header.ExcessBlobGas != expectedExcessBlobGas {
|
|
|
|
return fmt.Errorf("invalid excessBlobGas: have %d, want %d, parent excessBlobGas %d, parent blobDataUsed %d",
|
|
|
|
*header.ExcessBlobGas, expectedExcessBlobGas, parentExcessBlobGas, parentBlobGasUsed)
|
2023-05-31 10:21:13 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-01 10:07:25 +03:00
|
|
|
// CalcExcessBlobGas calculates the excess blob gas after applying the set of
|
|
|
|
// blobs on top of the excess blob gas.
|
2023-07-27 16:53:28 +03:00
|
|
|
func CalcExcessBlobGas(parentExcessBlobGas uint64, parentBlobGasUsed uint64) uint64 {
|
|
|
|
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
|
|
|
|
if excessBlobGas < params.BlobTxTargetBlobGasPerBlock {
|
2023-05-31 10:21:13 +03:00
|
|
|
return 0
|
|
|
|
}
|
2023-07-27 16:53:28 +03:00
|
|
|
return excessBlobGas - params.BlobTxTargetBlobGasPerBlock
|
2023-05-31 10:21:13 +03:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:07:25 +03:00
|
|
|
// CalcBlobFee calculates the blobfee from the header's excess blob gas field.
|
2023-07-27 16:53:28 +03:00
|
|
|
func CalcBlobFee(excessBlobGas uint64) *big.Int {
|
|
|
|
return fakeExponential(minBlobGasPrice, new(big.Int).SetUint64(excessBlobGas), blobGaspriceUpdateFraction)
|
2023-04-04 11:25:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fakeExponential approximates factor * e ** (numerator / denominator) using
|
|
|
|
// Taylor expansion.
|
|
|
|
func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
|
|
|
|
var (
|
|
|
|
output = new(big.Int)
|
|
|
|
accum = new(big.Int).Mul(factor, denominator)
|
|
|
|
)
|
|
|
|
for i := 1; accum.Sign() > 0; i++ {
|
|
|
|
output.Add(output, accum)
|
|
|
|
|
|
|
|
accum.Mul(accum, numerator)
|
|
|
|
accum.Div(accum, denominator)
|
|
|
|
accum.Div(accum, big.NewInt(int64(i)))
|
|
|
|
}
|
|
|
|
return output.Div(output, denominator)
|
|
|
|
}
|