Skip to content

consensus/misc/eip4844: implement EIP-7918 #31965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions consensus/misc/eip4844/eip4844.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,28 @@ func CalcExcessBlobGas(config *params.ChainConfig, parent *types.Header, headTim
parentExcessBlobGas = *parent.ExcessBlobGas
parentBlobGasUsed = *parent.BlobGasUsed
}
excessBlobGas := parentExcessBlobGas + parentBlobGasUsed
targetGas := uint64(targetBlobsPerBlock(config, headTimestamp)) * params.BlobTxBlobGasPerBlob
var (
excessBlobGas = parentExcessBlobGas + parentBlobGasUsed
target = targetBlobsPerBlock(config, headTimestamp)
targetGas = uint64(target) * params.BlobTxBlobGasPerBlob
)
if excessBlobGas < targetGas {
return 0
}
if !config.IsOsaka(config.LondonBlock, headTimestamp) {
return excessBlobGas - targetGas
}

// EIP-7918 (post-Osaka).
var (
reservePrice = new(big.Int).Mul(parent.BaseFee, big.NewInt(params.BlobBaseCost))
blobPrice = calcBlobPrice(config, parent)
)
if reservePrice.Cmp(blobPrice) > 0 {
max := MaxBlobsPerBlock(config, headTimestamp)
scaledExcess := parentBlobGasUsed * uint64(max-target) / uint64(max)
return parentExcessBlobGas + scaledExcess
}
return excessBlobGas - targetGas
}

Expand Down Expand Up @@ -177,3 +194,9 @@ func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
}
return output.Div(output, denominator)
}

// calcBlobPrice calculates the blob price for a block.
func calcBlobPrice(config *params.ChainConfig, header *types.Header) *big.Int {
blobBaseFee := CalcBlobFee(config, header)
return new(big.Int).Mul(blobBaseFee, big.NewInt(params.BlobTxBlobGasPerBlob))
}
40 changes: 40 additions & 0 deletions consensus/misc/eip4844/eip4844_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,43 @@ func TestFakeExponential(t *testing.T) {
}
}
}

func TestCalcExcessBlobGasEIP7918(t *testing.T) {
var (
cfg = params.MergedTestChainConfig
targetBlobs = targetBlobsPerBlock(cfg, *cfg.CancunTime)
blobGasTarget = uint64(targetBlobs) * params.BlobTxBlobGasPerBlob
)
makeHeader := func(parentExcess, parentBaseFee uint64, blobsUsed int) *types.Header {
blobGasUsed := uint64(blobsUsed) * params.BlobTxBlobGasPerBlob
return &types.Header{
BaseFee: big.NewInt(int64(parentBaseFee)),
ExcessBlobGas: &parentExcess,
BlobGasUsed: &blobGasUsed,
}
}

tests := []struct {
name string
header *types.Header
wantExcessGas uint64
}{
{
name: "BelowReservePrice",
header: makeHeader(0, 1_000_000_000, targetBlobs),
wantExcessGas: blobGasTarget * 3 / 9,
},
{
name: "AboveReservePrice",
header: makeHeader(0, 1, targetBlobs),
wantExcessGas: 0,
},
}
for _, tc := range tests {
got := CalcExcessBlobGas(cfg, tc.header, *cfg.CancunTime)
if got != tc.wantExcessGas {
t.Fatalf("%s: excess-blob-gas mismatch – have %d, want %d",
tc.name, got, tc.wantExcessGas)
}
}
}
1 change: 1 addition & 0 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const (
BlobTxBlobGasPerBlob = 1 << 17 // Gas consumption of a single data blob (== blob byte size)
BlobTxMinBlobGasprice = 1 // Minimum gas price for data blobs
BlobTxPointEvaluationPrecompileGas = 50000 // Gas price for the point evaluation precompile.
BlobBaseCost = 1 << 14 // Base execution gas cost for a blob.

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