Skip to content

Commit ffb4e6f

Browse files
s1nafjl
andauthored
consensus/misc/eip4844: implement EIP-7918 (#31965)
https://eips.ethereum.org/EIPS/eip-7918 --------- Co-authored-by: Felix Lange <[email protected]>
1 parent 84f2932 commit ffb4e6f

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

consensus/misc/eip4844/eip4844.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,30 @@ func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTim
7171
parentExcessBlobGas = *parent.ExcessBlobGas
7272
parentBlobGasUsed = *parent.BlobGasUsed
7373
}
74-
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
75-
targetGas := uint64(targetBlobsPerBlock(config, headTimestamp)) * params.BlobTxBlobGasPerBlob
74+
var (
75+
excessBlobGas = parentExcessBlobGas + parentBlobGasUsed
76+
target = targetBlobsPerBlock(config, headTimestamp)
77+
targetGas = uint64(target) * params.BlobTxBlobGasPerBlob
78+
)
7679
if excessBlobGas < targetGas {
7780
return 0
7881
}
82+
if !config.IsOsaka(config.LondonBlock, headTimestamp) {
83+
// Pre-Osaka, we use the formula defined by EIP-4844.
84+
return excessBlobGas - targetGas
85+
}
86+
87+
// EIP-7918 (post-Osaka) introduces a different formula for computing excess.
88+
var (
89+
baseCost = big.NewInt(params.BlobBaseCost)
90+
reservePrice = baseCost.Mul(baseCost, parent.BaseFee)
91+
blobPrice = calcBlobPrice(config, parent)
92+
)
93+
if reservePrice.Cmp(blobPrice) > 0 {
94+
max := MaxBlobsPerBlock(config, headTimestamp)
95+
scaledExcess := parentBlobGasUsed * uint64(max-target) / uint64(max)
96+
return parentExcessBlobGas + scaledExcess
97+
}
7998
return excessBlobGas - targetGas
8099
}
81100

@@ -177,3 +196,9 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
177196
}
178197
return output.Div(output, denominator)
179198
}
199+
200+
// calcBlobPrice calculates the blob price for a block.
201+
func calcBlobPrice(config *params.ChainConfig, header *types.Header) *big.Int {
202+
blobBaseFee := CalcBlobFee(config, header)
203+
return new(big.Int).Mul(blobBaseFee, big.NewInt(params.BlobTxBlobGasPerBlob))
204+
}

consensus/misc/eip4844/eip4844_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,43 @@ func TestFakeExponential(t *testing.T) {
127127
}
128128
}
129129
}
130+
131+
func TestCalcExcessBlobGasEIP7918(t *testing.T) {
132+
var (
133+
cfg = params.MergedTestChainConfig
134+
targetBlobs = targetBlobsPerBlock(cfg, *cfg.CancunTime)
135+
blobGasTarget = uint64(targetBlobs) * params.BlobTxBlobGasPerBlob
136+
)
137+
makeHeader := func(parentExcess, parentBaseFee uint64, blobsUsed int) *types.Header {
138+
blobGasUsed := uint64(blobsUsed) * params.BlobTxBlobGasPerBlob
139+
return &types.Header{
140+
BaseFee: big.NewInt(int64(parentBaseFee)),
141+
ExcessBlobGas: &parentExcess,
142+
BlobGasUsed: &blobGasUsed,
143+
}
144+
}
145+
146+
tests := []struct {
147+
name string
148+
header *types.Header
149+
wantExcessGas uint64
150+
}{
151+
{
152+
name: "BelowReservePrice",
153+
header: makeHeader(0, 1_000_000_000, targetBlobs),
154+
wantExcessGas: blobGasTarget * 3 / 9,
155+
},
156+
{
157+
name: "AboveReservePrice",
158+
header: makeHeader(0, 1, targetBlobs),
159+
wantExcessGas: 0,
160+
},
161+
}
162+
for _, tc := range tests {
163+
got := CalcExcessBlobGas(cfg, tc.header, *cfg.CancunTime)
164+
if got != tc.wantExcessGas {
165+
t.Fatalf("%s: excess-blob-gas mismatch – have %d, want %d",
166+
tc.name, got, tc.wantExcessGas)
167+
}
168+
}
169+
}

params/protocol_params.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ const (
173173
BlobTxBlobGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size)
174174
BlobTxMinBlobGasprice = 1 // Minimum gas price for data blobs
175175
BlobTxPointEvaluationPrecompileGas = 50000 // Gas price for the point evaluation precompile.
176+
BlobBaseCost = 1 << 13 // Base execution gas cost for a blob.
176177

177178
HistoryServeWindow = 8192 // Number of blocks to serve historical block hashes for, EIP-2935.
178179
)

0 commit comments

Comments
 (0)