Skip to content

Commit fb6ae07

Browse files
s1naMariusVanDerWijden
authored andcommitted
consensus: implement EIP-7918
1 parent 778430a commit fb6ae07

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

consensus/misc/eip4844/eip4844.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,27 @@ 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+
return excessBlobGas - targetGas
84+
}
85+
// EIP-7918.
86+
var (
87+
reservePrice = new(big.Int).Mul(parent.BaseFee, big.NewInt(params.BlobBaseCost))
88+
blobPrice = calcBlobPrice(config, parent)
89+
)
90+
if reservePrice.Cmp(blobPrice) > 0 {
91+
max := MaxBlobsPerBlock(config, headTimestamp)
92+
scaledExcess := parentBlobGasUsed * uint64(max-target) / uint64(max)
93+
return parentExcessBlobGas + scaledExcess
94+
}
7995
return excessBlobGas - targetGas
8096
}
8197

@@ -177,3 +193,9 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
177193
}
178194
return output.Div(output, denominator)
179195
}
196+
197+
// calcBlobPrice calculates the blob price based for a block.
198+
func calcBlobPrice(config *params.ChainConfig, header *types.Header) *big.Int {
199+
blobBaseFee := CalcBlobFee(config, header)
200+
return new(big.Int).Mul(blobBaseFee, big.NewInt(params.BlobTxBlobGasPerBlob))
201+
}

consensus/misc/eip4844/eip4844_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,56 @@ func TestFakeExponential(t *testing.T) {
127127
}
128128
}
129129
}
130+
131+
func TestCalcExcessBlobGasEIP7918(t *testing.T) {
132+
// TODO: replace with a test config that has Osaka enabled.
133+
c := *params.MainnetChainConfig
134+
cfg := &c
135+
cfg.OsakaTime = new(uint64)
136+
pragueSchedule := *cfg.BlobScheduleConfig.Prague
137+
cfg.BlobScheduleConfig.Osaka = &pragueSchedule
138+
139+
var (
140+
targetBlobs = targetBlobsPerBlock(cfg, *cfg.CancunTime)
141+
blobGasPerBlob = uint64(params.BlobTxBlobGasPerBlob)
142+
blobGasTarget = uint64(targetBlobs) * blobGasPerBlob
143+
)
144+
145+
makeHeader := func(
146+
parentExcess uint64,
147+
parentBaseFee uint64,
148+
blobsUsed int,
149+
) *types.Header {
150+
blobGasUsed := uint64(blobsUsed) * blobGasPerBlob
151+
return &types.Header{
152+
BaseFee: big.NewInt(int64(parentBaseFee)),
153+
ExcessBlobGas: &parentExcess,
154+
BlobGasUsed: &blobGasUsed,
155+
}
156+
}
157+
158+
tests := []struct {
159+
name string
160+
header *types.Header
161+
wantExcessGas uint64
162+
}{
163+
{
164+
name: "BelowReservePrice",
165+
header: makeHeader(0, 1_000_000_000, targetBlobs),
166+
wantExcessGas: blobGasTarget * 3 / 9,
167+
},
168+
{
169+
name: "AboveReservePrice",
170+
header: makeHeader(0, 1, targetBlobs),
171+
wantExcessGas: 0,
172+
},
173+
}
174+
175+
for _, tc := range tests {
176+
got := CalcExcessBlobGas(cfg, tc.header, *cfg.CancunTime)
177+
if got != tc.wantExcessGas {
178+
t.Fatalf("%s: excess-blob-gas mismatch – have %d, want %d",
179+
tc.name, got, tc.wantExcessGas)
180+
}
181+
}
182+
}

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 << 14 // 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)