Skip to content

Commit 773cfb2

Browse files
change from excess blobs to excess data gas (#24)
1 parent 82cf924 commit 773cfb2

File tree

30 files changed

+469
-369
lines changed

30 files changed

+469
-369
lines changed

cmd/evm/internal/t8ntool/block.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ import (
3838

3939
//go:generate go run github.com/fjl/gencodec -type header -field-override headerMarshaling -out gen_header.go
4040
type header struct {
41-
ParentHash common.Hash `json:"parentHash"`
42-
OmmerHash *common.Hash `json:"sha3Uncles"`
43-
Coinbase *common.Address `json:"miner"`
44-
Root common.Hash `json:"stateRoot" gencodec:"required"`
45-
TxHash *common.Hash `json:"transactionsRoot"`
46-
ReceiptHash *common.Hash `json:"receiptsRoot"`
47-
Bloom types.Bloom `json:"logsBloom"`
48-
Difficulty *big.Int `json:"difficulty"`
49-
Number *big.Int `json:"number" gencodec:"required"`
50-
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
51-
GasUsed uint64 `json:"gasUsed"`
52-
Time uint64 `json:"timestamp" gencodec:"required"`
53-
Extra []byte `json:"extraData"`
54-
MixDigest common.Hash `json:"mixHash"`
55-
Nonce *types.BlockNonce `json:"nonce"`
56-
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
57-
ExcessBlobs *uint64 `json:"excessBlobs" rlp:"optional"`
41+
ParentHash common.Hash `json:"parentHash"`
42+
OmmerHash *common.Hash `json:"sha3Uncles"`
43+
Coinbase *common.Address `json:"miner"`
44+
Root common.Hash `json:"stateRoot" gencodec:"required"`
45+
TxHash *common.Hash `json:"transactionsRoot"`
46+
ReceiptHash *common.Hash `json:"receiptsRoot"`
47+
Bloom types.Bloom `json:"logsBloom"`
48+
Difficulty *big.Int `json:"difficulty"`
49+
Number *big.Int `json:"number" gencodec:"required"`
50+
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
51+
GasUsed uint64 `json:"gasUsed"`
52+
Time uint64 `json:"timestamp" gencodec:"required"`
53+
Extra []byte `json:"extraData"`
54+
MixDigest common.Hash `json:"mixHash"`
55+
Nonce *types.BlockNonce `json:"nonce"`
56+
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
57+
ExcessDataGas *big.Int `json:"excessDataGas" rlp:"optional"`
5858
}
5959

6060
type headerMarshaling struct {
@@ -115,22 +115,22 @@ func (c *cliqueInput) UnmarshalJSON(input []byte) error {
115115
// ToBlock converts i into a *types.Block
116116
func (i *bbInput) ToBlock() *types.Block {
117117
header := &types.Header{
118-
ParentHash: i.Header.ParentHash,
119-
UncleHash: types.EmptyUncleHash,
120-
Coinbase: common.Address{},
121-
Root: i.Header.Root,
122-
TxHash: types.EmptyRootHash,
123-
ReceiptHash: types.EmptyRootHash,
124-
Bloom: i.Header.Bloom,
125-
Difficulty: common.Big0,
126-
Number: i.Header.Number,
127-
GasLimit: i.Header.GasLimit,
128-
GasUsed: i.Header.GasUsed,
129-
Time: i.Header.Time,
130-
Extra: i.Header.Extra,
131-
MixDigest: i.Header.MixDigest,
132-
BaseFee: i.Header.BaseFee,
133-
ExcessBlobs: i.Header.ExcessBlobs,
118+
ParentHash: i.Header.ParentHash,
119+
UncleHash: types.EmptyUncleHash,
120+
Coinbase: common.Address{},
121+
Root: i.Header.Root,
122+
TxHash: types.EmptyRootHash,
123+
ReceiptHash: types.EmptyRootHash,
124+
Bloom: i.Header.Bloom,
125+
Difficulty: common.Big0,
126+
Number: i.Header.Number,
127+
GasLimit: i.Header.GasLimit,
128+
GasUsed: i.Header.GasUsed,
129+
Time: i.Header.Time,
130+
Extra: i.Header.Extra,
131+
MixDigest: i.Header.MixDigest,
132+
BaseFee: i.Header.BaseFee,
133+
ExcessDataGas: i.Header.ExcessDataGas,
134134
}
135135

136136
// Fill optional values.

cmd/evm/internal/t8ntool/gen_header.go

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/evm/internal/t8ntool/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func Transaction(ctx *cli.Context) error {
147147
EIP2028: chainConfig.IsIstanbul(new(big.Int)),
148148
EIP4844: chainConfig.IsSharding(new(big.Int)),
149149
}
150-
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), len(tx.DataHashes()), 0, tx.To() == nil, rules); err != nil {
150+
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, rules); err != nil {
151151
r.Error = err
152152
results = append(results, r)
153153
continue

consensus/beacon/consensus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
345345
header.Root = state.IntermediateRoot(true)
346346
if chain.Config().IsSharding(header.Number) {
347347
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
348-
header.SetExcessBlobs(misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs))))
348+
header.SetExcessDataGas(misc.CalcExcessDataGas(parent.ExcessDataGas, misc.CountBlobs(txs)))
349349
}
350350
}
351351
}

consensus/clique/clique.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
346346
return err
347347
}
348348
if !chain.Config().IsSharding(header.Number) {
349-
if header.ExcessBlobs != nil {
350-
return fmt.Errorf("invalid excessBlobs before fork: have %d, want <nil>", *header.ExcessBlobs)
349+
if header.ExcessDataGas != nil {
350+
return fmt.Errorf("invalid excessDataGas before fork: have %v, want <nil>", header.ExcessDataGas)
351351
}
352352
} else if err := misc.VerifyEip4844Header(chain.Config(), parent, header); err != nil {
353353
// Verify the header's EIP-4844 attributes.
@@ -578,7 +578,7 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
578578
header.UncleHash = types.CalcUncleHash(nil)
579579
if chain.Config().IsSharding(header.Number) {
580580
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
581-
header.SetExcessBlobs(misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs))))
581+
header.SetExcessDataGas(misc.CalcExcessDataGas(parent.ExcessDataGas, misc.CountBlobs(txs)))
582582
}
583583
}
584584
}
@@ -756,8 +756,8 @@ func encodeSigHeader(w io.Writer, header *types.Header) {
756756
if header.BaseFee != nil {
757757
enc = append(enc, header.BaseFee)
758758
}
759-
if header.ExcessBlobs != nil {
760-
enc = append(enc, header.ExcessBlobs)
759+
if header.ExcessDataGas != nil {
760+
enc = append(enc, header.ExcessDataGas)
761761
}
762762
if err := rlp.Encode(w, enc); err != nil {
763763
panic("can't encode: " + err.Error())

consensus/ethash/consensus.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, pa
307307
return err
308308
}
309309
if !chain.Config().IsSharding(header.Number) {
310-
if header.ExcessBlobs != nil {
311-
return fmt.Errorf("invalid excessBlobs before fork: have %d, expected 'nil'", *header.ExcessBlobs)
310+
if header.ExcessDataGas != nil {
311+
return fmt.Errorf("invalid excessDataGas before fork: have %v, expected 'nil'", header.ExcessDataGas)
312312
}
313313
} else if err := misc.VerifyEip4844Header(chain.Config(), parent, header); err != nil {
314314
// Verify the header's EIP-4844 attributes.
@@ -611,7 +611,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
611611
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
612612
if chain.Config().IsSharding(header.Number) {
613613
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
614-
header.SetExcessBlobs(misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs))))
614+
header.SetExcessDataGas(misc.CalcExcessDataGas(parent.ExcessDataGas, misc.CountBlobs(txs)))
615615
}
616616
}
617617
}
@@ -648,8 +648,8 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
648648
if header.BaseFee != nil {
649649
enc = append(enc, header.BaseFee)
650650
}
651-
if header.ExcessBlobs != nil {
652-
enc = append(enc, header.ExcessBlobs)
651+
if header.ExcessDataGas != nil {
652+
enc = append(enc, header.ExcessDataGas)
653653
}
654654
rlp.Encode(hasher, enc)
655655
hasher.Sum(hash[:0])

consensus/misc/eip4844.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,41 @@ package misc
1818

1919
import (
2020
"fmt"
21-
"math"
21+
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/core/types"
2424
"github.com/ethereum/go-ethereum/params"
2525
)
2626

27-
// CalcExcessBlobTransactions calculates the number of blobs above the target
28-
func CalcExcessBlobTransactions(parent *types.Header, blobs uint64) uint64 {
29-
var excessBlobs uint64
30-
if parent.ExcessBlobs != nil {
31-
excessBlobs = *parent.ExcessBlobs
27+
// CalcExcessDataGas implements calc_excess_data_gas from EIP-4844
28+
func CalcExcessDataGas(parentExcessDataGas *big.Int, newBlobs int) *big.Int {
29+
excessDataGas := new(big.Int)
30+
if parentExcessDataGas != nil {
31+
excessDataGas.Set(parentExcessDataGas)
3232
}
33-
adjusted := excessBlobs + blobs
34-
if adjusted < params.TargetBlobsPerBlock {
35-
return 0
33+
consumedGas := big.NewInt(params.DataGasPerBlob)
34+
consumedGas.Mul(consumedGas, big.NewInt(int64(newBlobs)))
35+
36+
excessDataGas.Add(excessDataGas, consumedGas)
37+
targetGas := big.NewInt(params.TargetDataGasPerBlock)
38+
if excessDataGas.Cmp(targetGas) < 0 {
39+
return new(big.Int)
3640
}
37-
return adjusted - params.TargetBlobsPerBlock
41+
return new(big.Int).Set(excessDataGas.Sub(excessDataGas, targetGas))
3842
}
3943

40-
// FakeExponential approximates 2 ** (num / denom)
41-
func FakeExponential(num uint64, denom uint64) uint64 {
42-
cofactor := uint64(math.Exp2(float64(num / denom)))
43-
fractional := num % denom
44-
return cofactor + (fractional*cofactor*2+
45-
(uint64(math.Pow(float64(fractional), 2))*cofactor)/denom)/(denom*3)
44+
// FakeExponential approximates factor * e ** (num / denom) using a taylor expansion
45+
// as described in the EIP-4844 spec.
46+
func FakeExponential(factor, num, denom *big.Int) *big.Int {
47+
output := new(big.Int)
48+
numAccum := new(big.Int).Mul(factor, denom)
49+
for i := 1; numAccum.Sign() > 0; i++ {
50+
output.Add(output, numAccum)
51+
numAccum.Mul(numAccum, num)
52+
iBig := big.NewInt(int64(i))
53+
numAccum.Div(numAccum, iBig.Mul(iBig, denom))
54+
}
55+
return output.Div(output, denom)
4656
}
4757

4858
// CountBlobs returns the number of blob transactions in txs
@@ -56,8 +66,8 @@ func CountBlobs(txs []*types.Transaction) int {
5666

5767
// VerifyEip4844Header verifies that the header is not malformed
5868
func VerifyEip4844Header(config *params.ChainConfig, parent, header *types.Header) error {
59-
if header.ExcessBlobs == nil {
60-
return fmt.Errorf("header is missing excessBlobs")
69+
if header.ExcessDataGas == nil {
70+
return fmt.Errorf("header is missing excessDataGas")
6171
}
6272
return nil
6373
}

0 commit comments

Comments
 (0)