Skip to content

Commit a2ab520

Browse files
s1nafjl
authored andcommitted
consensus: implement EIP-7918
1 parent 819e9b4 commit a2ab520

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
@@ -70,11 +70,27 @@ func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTim
7070
parentExcessBlobGas = *parent.ExcessBlobGas
7171
parentBlobGasUsed = *parent.BlobGasUsed
7272
}
73-
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
74-
targetGas := uint64(targetBlobsPerBlock(config, headTimestamp)) * params.BlobTxBlobGasPerBlob
73+
var (
74+
excessBlobGas = parentExcessBlobGas + parentBlobGasUsed
75+
target = targetBlobsPerBlock(config, headTimestamp)
76+
targetGas = uint64(target) * params.BlobTxBlobGasPerBlob
77+
)
7578
if excessBlobGas < targetGas {
7679
return 0
7780
}
81+
if !config.IsOsaka(config.LondonBlock, headTimestamp) {
82+
return excessBlobGas - targetGas
83+
}
84+
// EIP-7918.
85+
var (
86+
reservePrice = new(big.Int).Mul(parent.BaseFee, big.NewInt(params.BlobBaseCost))
87+
blobPrice = calcBlobPrice(config, parent)
88+
)
89+
if reservePrice.Cmp(blobPrice) > 0 {
90+
max := MaxBlobsPerBlock(config, headTimestamp)
91+
scaledExcess := parentBlobGasUsed * uint64(max-target) / uint64(max)
92+
return parentExcessBlobGas + scaledExcess
93+
}
7894
return excessBlobGas - targetGas
7995
}
8096

@@ -185,3 +201,9 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
185201
}
186202
return output.Div(output, denominator)
187203
}
204+
205+
// calcBlobPrice calculates the blob price based for a block.
206+
func calcBlobPrice(config *params.ChainConfig, header *types.Header) *big.Int {
207+
blobBaseFee := CalcBlobFee(config, header)
208+
return new(big.Int).Mul(blobBaseFee, big.NewInt(params.BlobTxBlobGasPerBlob))
209+
}

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
@@ -174,6 +174,7 @@ const (
174174
BlobTxBlobGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size)
175175
BlobTxMinBlobGasprice = 1 // Minimum gas price for data blobs
176176
BlobTxPointEvaluationPrecompileGas = 50000 // Gas price for the point evaluation precompile.
177+
BlobBaseCost = 1 << 14 // Base execution gas cost for a blob.
177178

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

0 commit comments

Comments
 (0)