Skip to content

Commit d8e92d1

Browse files
authored
Fee Market Updates (#17)
* Add running blob txs count to block header - and update the blob fee rules to the full version
1 parent e5291ef commit d8e92d1

32 files changed

+336
-152
lines changed

cmd/evm/internal/t8ntool/block.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type header struct {
5454
MixDigest common.Hash `json:"mixHash"`
5555
Nonce *types.BlockNonce `json:"nonce"`
5656
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
57+
ExcessBlobs uint64 `json:"excessBlobs" rlp:"optional"`
5758
}
5859

5960
type headerMarshaling struct {
@@ -129,6 +130,7 @@ func (i *bbInput) ToBlock() *types.Block {
129130
Extra: i.Header.Extra,
130131
MixDigest: i.Header.MixDigest,
131132
BaseFee: i.Header.BaseFee,
133+
ExcessBlobs: i.Header.ExcessBlobs,
132134
}
133135

134136
// Fill optional values.

cmd/evm/internal/t8ntool/gen_header.go

Lines changed: 6 additions & 0 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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,16 @@ func Transaction(ctx *cli.Context) error {
138138
} else {
139139
r.Address = sender
140140
}
141-
// Check intrinsic gas
142-
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), len(tx.BlobVersionedHashes()), tx.To() == nil,
143-
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int))); err != nil {
141+
// Check intrinsic gas assuming no excess blobs
142+
// NOTE: We set excess_blobs prestate to zero. So this may not accurately compute the
143+
// intrinsic gas unless the tool is updated to take in an excess_blobs parameter.
144+
145+
rules := core.IntrinsicGasChainRules{
146+
Homestead: chainConfig.IsHomestead(new(big.Int)),
147+
EIP2028: chainConfig.IsIstanbul(new(big.Int)),
148+
EIP4844: chainConfig.IsSharding(new(big.Int)),
149+
}
150+
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), len(tx.DataHashes()), 0, tx.To() == nil, rules); err != nil {
144151
r.Error = err
145152
results = append(results, r)
146153
continue

consensus/beacon/consensus.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
318318
// The block reward is no longer handled here. It's done by the
319319
// external consensus engine.
320320
header.Root = state.IntermediateRoot(true)
321+
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
322+
header.ExcessBlobs = misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs)))
323+
}
321324
}
322325

323326
// FinalizeAndAssemble implements consensus.Engine, setting the final state and

consensus/clique/clique.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
568568
// No block rewards in PoA, so the state remains as is and uncles are dropped
569569
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
570570
header.UncleHash = types.CalcUncleHash(nil)
571+
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
572+
header.ExcessBlobs = misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs)))
573+
}
571574
}
572575

573576
// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
@@ -743,6 +746,7 @@ func encodeSigHeader(w io.Writer, header *types.Header) {
743746
if header.BaseFee != nil {
744747
enc = append(enc, header.BaseFee)
745748
}
749+
enc = append(enc, header.ExcessBlobs)
746750
if err := rlp.Encode(w, enc); err != nil {
747751
panic("can't encode: " + err.Error())
748752
}

consensus/ethash/consensus.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
601601
// Accumulate any block and uncle rewards and commit the final state root
602602
accumulateRewards(chain.Config(), state, header, uncles)
603603
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
604+
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
605+
header.ExcessBlobs = misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs)))
606+
}
604607
}
605608

606609
// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
@@ -635,6 +638,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
635638
if header.BaseFee != nil {
636639
enc = append(enc, header.BaseFee)
637640
}
641+
enc = append(enc, header.ExcessBlobs)
638642
rlp.Encode(hasher, enc)
639643
hasher.Sum(hash[:0])
640644
return hash

consensus/misc/eip4844.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 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"
21+
22+
"github.com/ethereum/go-ethereum/core/types"
23+
"github.com/ethereum/go-ethereum/params"
24+
)
25+
26+
// CalcExcessBlobTransactions calculates the number of blobs above the target
27+
func CalcExcessBlobTransactions(parent *types.Header, blobs uint64) uint64 {
28+
adjusted := parent.ExcessBlobs + blobs
29+
if adjusted < params.TargetBlobsPerBlock {
30+
return 0
31+
}
32+
return adjusted - params.TargetBlobsPerBlock
33+
}
34+
35+
// FakeExponential approximates 2 ** (num / denom)
36+
func FakeExponential(num uint64, denom uint64) uint64 {
37+
cofactor := uint64(math.Exp2(float64(num / denom)))
38+
fractional := num % denom
39+
return cofactor + (fractional*cofactor*2+
40+
(uint64(math.Pow(float64(fractional), 2))*cofactor)/denom)/(denom*3)
41+
}
42+
43+
func CountBlobs(txs []*types.Transaction) int {
44+
var count int
45+
for _, tx := range txs {
46+
count += len(tx.DataHashes())
47+
}
48+
return count
49+
}

core/beacon/gen_ed.go

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

core/beacon/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type ExecutableDataV1 struct {
6565
Timestamp uint64 `json:"timestamp" gencodec:"required"`
6666
ExtraData []byte `json:"extraData" gencodec:"required"`
6767
BaseFeePerGas *big.Int `json:"baseFeePerGas" gencodec:"required"`
68+
ExcessBlobs uint64 `json:"excessBlobs" gencodec:"required"`
6869
BlockHash common.Hash `json:"blockHash" gencodec:"required"`
6970
Transactions [][]byte `json:"transactions" gencodec:"required"`
7071
}
@@ -76,6 +77,7 @@ type executableDataMarshaling struct {
7677
GasUsed hexutil.Uint64
7778
Timestamp hexutil.Uint64
7879
BaseFeePerGas *hexutil.Big
80+
ExcessBlobs hexutil.Uint64
7981
ExtraData hexutil.Bytes
8082
LogsBloom hexutil.Bytes
8183
Transactions []hexutil.Bytes
@@ -178,6 +180,7 @@ func ExecutableDataToBlock(params ExecutableDataV1) (*types.Block, error) {
178180
GasUsed: params.GasUsed,
179181
Time: params.Timestamp,
180182
BaseFee: params.BaseFeePerGas,
183+
ExcessBlobs: params.ExcessBlobs,
181184
Extra: params.ExtraData,
182185
MixDigest: params.Random,
183186
}
@@ -201,6 +204,7 @@ func BlockToExecutableData(block *types.Block) *ExecutableDataV1 {
201204
GasLimit: block.GasLimit(),
202205
GasUsed: block.GasUsed(),
203206
BaseFeePerGas: block.BaseFee(),
207+
ExcessBlobs: block.ExcessBlobs(),
204208
Timestamp: block.Time(),
205209
ReceiptsRoot: block.ReceiptHash(),
206210
LogsBloom: block.Bloom().Bytes(),

core/bench_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
8383
return func(i int, gen *BlockGen) {
8484
toaddr := common.Address{}
8585
data := make([]byte, nbytes)
86-
gas, _ := IntrinsicGas(data, nil, 0, false, false, false)
86+
rules := IntrinsicGasChainRules{
87+
Homestead: false,
88+
EIP2028: false,
89+
EIP4844: false,
90+
}
91+
gas, _ := IntrinsicGas(data, nil, 0, 0, false, rules)
8792
signer := types.MakeSigner(gen.config, big.NewInt(int64(i)))
8893
gasPrice := big.NewInt(0)
8994
if gen.header.BaseFee != nil {

0 commit comments

Comments
 (0)