|
| 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 eip4844 |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "math/big" |
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/core/types" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + BlobTxBytesPerFieldElement uint64 = 32 // Size in bytes of a field element |
| 29 | + BlobTxFieldElementsPerBlob uint64 = 4096 // Number of field elements stored in a single data blob |
| 30 | + BlobTxHashVersion uint64 = 0x01 // Version byte of the commitment hash |
| 31 | + BlobTxBlobGasPerBlob uint64 = 1 << 17 // Gas consumption of a single data blob (== blob byte size) |
| 32 | + BlobTxMinBlobGasprice uint64 = 1 // Minimum gas price for data blobs |
| 33 | + BlobTxBlobGaspriceUpdateFraction uint64 = 3338477 // Controls the maximum rate of change for blob gas price |
| 34 | + BlobTxPointEvaluationPrecompileGas uint64 = 50000 // Gas price for the point evaluation precompile. |
| 35 | + |
| 36 | + BlobTxTargetBlobGasPerBlock = 3 * BlobTxBlobGasPerBlob // Target consumable blob gas for data blobs per block (for 1559-like pricing) |
| 37 | + MaxBlobGasPerBlock = 6 * BlobTxBlobGasPerBlob // Maximum consumable blob gas for data blobs per block |
| 38 | + |
| 39 | + minBlobGasPrice = big.NewInt(int64(BlobTxMinBlobGasprice)) |
| 40 | + blobGaspriceUpdateFraction = big.NewInt(int64(BlobTxBlobGaspriceUpdateFraction)) |
| 41 | +) |
| 42 | + |
| 43 | +// VerifyEIP4844Header verifies the presence of the excessBlobGas field and that |
| 44 | +// if the current block contains no transactions, the excessBlobGas is updated |
| 45 | +// accordingly. |
| 46 | +func VerifyEIP4844Header(parent, header *types.Header) error { |
| 47 | + // Verify the header is not malformed |
| 48 | + if header.ExcessBlobGas == nil { |
| 49 | + return errors.New("header is missing excessBlobGas") |
| 50 | + } |
| 51 | + if header.BlobGasUsed == nil { |
| 52 | + return errors.New("header is missing blobGasUsed") |
| 53 | + } |
| 54 | + // Verify that the blob gas used remains within reasonable limits. |
| 55 | + if *header.BlobGasUsed > MaxBlobGasPerBlock { |
| 56 | + return fmt.Errorf("blob gas used %d exceeds maximum allowance %d", *header.BlobGasUsed, MaxBlobGasPerBlock) |
| 57 | + } |
| 58 | + if *header.BlobGasUsed%BlobTxBlobGasPerBlob != 0 { |
| 59 | + return fmt.Errorf("blob gas used %d not a multiple of blob gas per blob %d", header.BlobGasUsed, BlobTxBlobGasPerBlob) |
| 60 | + } |
| 61 | + // Verify the excessBlobGas is correct based on the parent header |
| 62 | + var ( |
| 63 | + parentExcessBlobGas uint64 |
| 64 | + parentBlobGasUsed uint64 |
| 65 | + ) |
| 66 | + if parent.ExcessBlobGas != nil { |
| 67 | + parentExcessBlobGas = *parent.ExcessBlobGas |
| 68 | + parentBlobGasUsed = *parent.BlobGasUsed |
| 69 | + } |
| 70 | + expectedExcessBlobGas := CalcExcessBlobGas(parentExcessBlobGas, parentBlobGasUsed) |
| 71 | + if *header.ExcessBlobGas != expectedExcessBlobGas { |
| 72 | + return fmt.Errorf("invalid excessBlobGas: have %d, want %d, parent excessBlobGas %d, parent blobDataUsed %d", |
| 73 | + *header.ExcessBlobGas, expectedExcessBlobGas, parentExcessBlobGas, parentBlobGasUsed) |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// CalcExcessBlobGas calculates the excess blob gas after applying the set of |
| 79 | +// blobs on top of the excess blob gas. |
| 80 | +func CalcExcessBlobGas(parentExcessBlobGas uint64, parentBlobGasUsed uint64) uint64 { |
| 81 | + excessBlobGas := parentExcessBlobGas + parentBlobGasUsed |
| 82 | + if excessBlobGas < BlobTxTargetBlobGasPerBlock { |
| 83 | + return 0 |
| 84 | + } |
| 85 | + return excessBlobGas - BlobTxTargetBlobGasPerBlock |
| 86 | +} |
| 87 | + |
| 88 | +// CalcBlobFee calculates the blobfee from the header's excess blob gas field. |
| 89 | +func CalcBlobFee(excessBlobGas uint64) *big.Int { |
| 90 | + return fakeExponential(minBlobGasPrice, new(big.Int).SetUint64(excessBlobGas), blobGaspriceUpdateFraction) |
| 91 | +} |
| 92 | + |
| 93 | +// fakeExponential approximates factor * e ** (numerator / denominator) using |
| 94 | +// Taylor expansion. |
| 95 | +func fakeExponential(factor, numerator, denominator *big.Int) *big.Int { |
| 96 | + var ( |
| 97 | + output = new(big.Int) |
| 98 | + accum = new(big.Int).Mul(factor, denominator) |
| 99 | + ) |
| 100 | + for i := 1; accum.Sign() > 0; i++ { |
| 101 | + output.Add(output, accum) |
| 102 | + |
| 103 | + accum.Mul(accum, numerator) |
| 104 | + accum.Div(accum, denominator) |
| 105 | + accum.Div(accum, big.NewInt(int64(i))) |
| 106 | + } |
| 107 | + return output.Div(output, denominator) |
| 108 | +} |
0 commit comments