Skip to content

Commit d2cf493

Browse files
karalaberoberto-bayardoholiman
authored
consensus/misc, params: add EIP-4844 blobfee conversions (#27041)
* consensus/misc, params: add EIP-4844 blobfee conversions * consensus/misc: pull in fakeExponential test cases * consensus/misc: reuse bigints * consensus/misc: nit renames, additional larger testcase --------- Co-authored-by: Roberto Bayardo <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 91faf2c commit d2cf493

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

consensus/misc/eip4844.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package misc
18+
19+
import (
20+
"math/big"
21+
22+
"github.com/ethereum/go-ethereum/params"
23+
)
24+
25+
var (
26+
minDataGasPrice = big.NewInt(params.BlobTxMinDataGasprice)
27+
dataGaspriceUpdateFraction = big.NewInt(params.BlobTxDataGaspriceUpdateFraction)
28+
)
29+
30+
// CalcBlobFee calculates the blobfee from the header's excess data gas field.
31+
func CalcBlobFee(excessDataGas *big.Int) *big.Int {
32+
// If this block does not yet have EIP-4844 enabled, return the starting fee
33+
if excessDataGas == nil {
34+
return big.NewInt(params.BlobTxMinDataGasprice)
35+
}
36+
return fakeExponential(minDataGasPrice, excessDataGas, dataGaspriceUpdateFraction)
37+
}
38+
39+
// fakeExponential approximates factor * e ** (numerator / denominator) using
40+
// Taylor expansion.
41+
func fakeExponential(factor, numerator, denominator *big.Int) *big.Int {
42+
var (
43+
output = new(big.Int)
44+
accum = new(big.Int).Mul(factor, denominator)
45+
)
46+
for i := 1; accum.Sign() > 0; i++ {
47+
output.Add(output, accum)
48+
49+
accum.Mul(accum, numerator)
50+
accum.Div(accum, denominator)
51+
accum.Div(accum, big.NewInt(int64(i)))
52+
}
53+
return output.Div(output, denominator)
54+
}

consensus/misc/eip4844_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package misc
18+
19+
import (
20+
"fmt"
21+
"math/big"
22+
"testing"
23+
24+
"github.com/ethereum/go-ethereum/params"
25+
)
26+
27+
func TestCalcBlobFee(t *testing.T) {
28+
tests := []struct {
29+
excessDataGas int64
30+
blobfee int64
31+
}{
32+
{0, 1},
33+
{1542706, 1},
34+
{1542707, 2},
35+
{10 * 1024 * 1024, 111},
36+
}
37+
have := CalcBlobFee(nil)
38+
if have.Int64() != params.BlobTxMinDataGasprice {
39+
t.Errorf("nil test: blobfee mismatch: have %v, want %v", have, params.BlobTxMinDataGasprice)
40+
}
41+
for i, tt := range tests {
42+
have := CalcBlobFee(big.NewInt(tt.excessDataGas))
43+
if have.Int64() != tt.blobfee {
44+
t.Errorf("test %d: blobfee mismatch: have %v want %v", i, have, tt.blobfee)
45+
}
46+
}
47+
}
48+
49+
func TestFakeExponential(t *testing.T) {
50+
tests := []struct {
51+
factor int64
52+
numerator int64
53+
denominator int64
54+
want int64
55+
}{
56+
// When numerator == 0 the return value should always equal the value of factor
57+
{1, 0, 1, 1},
58+
{38493, 0, 1000, 38493},
59+
{0, 1234, 2345, 0}, // should be 0
60+
{1, 2, 1, 6}, // approximate 7.389
61+
{1, 4, 2, 6},
62+
{1, 3, 1, 16}, // approximate 20.09
63+
{1, 6, 2, 18},
64+
{1, 4, 1, 49}, // approximate 54.60
65+
{1, 8, 2, 50},
66+
{10, 8, 2, 542}, // approximate 540.598
67+
{11, 8, 2, 596}, // approximate 600.58
68+
{1, 5, 1, 136}, // approximate 148.4
69+
{1, 5, 2, 11}, // approximate 12.18
70+
{2, 5, 2, 23}, // approximate 24.36
71+
{1, 50000000, 2225652, 5709098764},
72+
}
73+
for i, tt := range tests {
74+
f, n, d := big.NewInt(tt.factor), big.NewInt(tt.numerator), big.NewInt(tt.denominator)
75+
original := fmt.Sprintf("%d %d %d", f, n, d)
76+
have := fakeExponential(f, n, d)
77+
if have.Int64() != tt.want {
78+
t.Errorf("test %d: fake exponential mismatch: have %v want %v", i, have, tt.want)
79+
}
80+
later := fmt.Sprintf("%d %d %d", f, n, d)
81+
if original != later {
82+
t.Errorf("test %d: fake exponential modified arguments: have\n%v\nwant\n%v", i, later, original)
83+
}
84+
}
85+
}

params/protocol_params.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ const (
159159
// up to half the consumed gas could be refunded. Redefined as 1/5th in EIP-3529
160160
RefundQuotient uint64 = 2
161161
RefundQuotientEIP3529 uint64 = 5
162+
163+
BlobTxMinDataGasprice = 1 // Minimum gas price for data blobs
164+
BlobTxDataGaspriceUpdateFraction = 2225652 // Controls the maximum rate of change for data gas price
162165
)
163166

164167
// Gas discount table for BLS12-381 G1 and G2 multi exponentiation operations

0 commit comments

Comments
 (0)