-
Notifications
You must be signed in to change notification settings - Fork 21.3k
consensus/misc, params: add EIP-4844 blobfee conversions #27041
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7c7009a
consensus/misc, params: add EIP-4844 blobfee conversions
karalabe 613705c
consensus/misc: pull in fakeExponential test cases
roberto-bayardo 2673b67
consensus/misc: reuse bigints
holiman 8007a1c
consensus/misc: nit renames, additional larger testcase
holiman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package misc | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
var ( | ||
minDataGasPrice = big.NewInt(params.BlobTxMinDataGasprice) | ||
dataGaspriceUpdateFraction = big.NewInt(params.BlobTxDataGaspriceUpdateFraction) | ||
) | ||
|
||
// CalcBlobFee calculates the blobfee from the header's excess data gas field. | ||
func CalcBlobFee(excessDataGas *big.Int) *big.Int { | ||
// If this block does not yet have EIP-4844 enabled, return the starting fee | ||
if excessDataGas == nil { | ||
return big.NewInt(params.BlobTxMinDataGasprice) | ||
} | ||
return fakeExponential(minDataGasPrice, excessDataGas, dataGaspriceUpdateFraction) | ||
} | ||
|
||
// fakeExponential approximates factor * e ** (numerator / denominator) using | ||
// Taylor expansion. | ||
func fakeExponential(factor, numerator, denominator *big.Int) *big.Int { | ||
var ( | ||
output = new(big.Int) | ||
accum = new(big.Int).Mul(factor, denominator) | ||
) | ||
for i := 1; accum.Sign() > 0; i++ { | ||
output.Add(output, accum) | ||
|
||
accum.Mul(accum, numerator) | ||
accum.Div(accum, denominator) | ||
accum.Div(accum, big.NewInt(int64(i))) | ||
} | ||
return output.Div(output, denominator) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2023 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package misc | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/params" | ||
) | ||
|
||
func TestCalcBlobFee(t *testing.T) { | ||
tests := []struct { | ||
excessDataGas int64 | ||
blobfee int64 | ||
}{ | ||
{0, 1}, | ||
{1542706, 1}, | ||
{1542707, 2}, | ||
{10 * 1024 * 1024, 111}, | ||
} | ||
have := CalcBlobFee(nil) | ||
if have.Int64() != params.BlobTxMinDataGasprice { | ||
t.Errorf("nil test: blobfee mismatch: have %v, want %v", have, params.BlobTxMinDataGasprice) | ||
} | ||
for i, tt := range tests { | ||
have := CalcBlobFee(big.NewInt(tt.excessDataGas)) | ||
if have.Int64() != tt.blobfee { | ||
t.Errorf("test %d: blobfee mismatch: have %v want %v", i, have, tt.blobfee) | ||
} | ||
} | ||
} | ||
|
||
func TestFakeExponential(t *testing.T) { | ||
tests := []struct { | ||
factor int64 | ||
numerator int64 | ||
denominator int64 | ||
want int64 | ||
}{ | ||
// When numerator == 0 the return value should always equal the value of factor | ||
{1, 0, 1, 1}, | ||
{38493, 0, 1000, 38493}, | ||
{0, 1234, 2345, 0}, // should be 0 | ||
{1, 2, 1, 6}, // approximate 7.389 | ||
{1, 4, 2, 6}, | ||
{1, 3, 1, 16}, // approximate 20.09 | ||
holiman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{1, 6, 2, 18}, | ||
{1, 4, 1, 49}, // approximate 54.60 | ||
{1, 8, 2, 50}, | ||
{10, 8, 2, 542}, // approximate 540.598 | ||
{11, 8, 2, 596}, // approximate 600.58 | ||
{1, 5, 1, 136}, // approximate 148.4 | ||
{1, 5, 2, 11}, // approximate 12.18 | ||
{2, 5, 2, 23}, // approximate 24.36 | ||
{1, 50000000, 2225652, 5709098764}, | ||
} | ||
for i, tt := range tests { | ||
f, n, d := big.NewInt(tt.factor), big.NewInt(tt.numerator), big.NewInt(tt.denominator) | ||
original := fmt.Sprintf("%d %d %d", f, n, d) | ||
have := fakeExponential(f, n, d) | ||
if have.Int64() != tt.want { | ||
t.Errorf("test %d: fake exponential mismatch: have %v want %v", i, have, tt.want) | ||
} | ||
later := fmt.Sprintf("%d %d %d", f, n, d) | ||
if original != later { | ||
t.Errorf("test %d: fake exponential modified arguments: have\n%v\nwant\n%v", i, later, original) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.