Skip to content

Commit

Permalink
Fee Market Updates (ethereum#17)
Browse files Browse the repository at this point in the history
* Add running blob txs count to block header

- and update the blob fee rules to the full version
  • Loading branch information
Inphi authored Sep 19, 2022
1 parent e5291ef commit d8e92d1
Show file tree
Hide file tree
Showing 32 changed files with 336 additions and 152 deletions.
2 changes: 2 additions & 0 deletions cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type header struct {
MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
ExcessBlobs uint64 `json:"excessBlobs" rlp:"optional"`
}

type headerMarshaling struct {
Expand Down Expand Up @@ -129,6 +130,7 @@ func (i *bbInput) ToBlock() *types.Block {
Extra: i.Header.Extra,
MixDigest: i.Header.MixDigest,
BaseFee: i.Header.BaseFee,
ExcessBlobs: i.Header.ExcessBlobs,
}

// Fill optional values.
Expand Down
6 changes: 6 additions & 0 deletions cmd/evm/internal/t8ntool/gen_header.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions cmd/evm/internal/t8ntool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,16 @@ func Transaction(ctx *cli.Context) error {
} else {
r.Address = sender
}
// Check intrinsic gas
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), len(tx.BlobVersionedHashes()), tx.To() == nil,
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int))); err != nil {
// Check intrinsic gas assuming no excess blobs
// NOTE: We set excess_blobs prestate to zero. So this may not accurately compute the
// intrinsic gas unless the tool is updated to take in an excess_blobs parameter.

rules := core.IntrinsicGasChainRules{
Homestead: chainConfig.IsHomestead(new(big.Int)),
EIP2028: chainConfig.IsIstanbul(new(big.Int)),
EIP4844: chainConfig.IsSharding(new(big.Int)),
}
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), len(tx.DataHashes()), 0, tx.To() == nil, rules); err != nil {
r.Error = err
results = append(results, r)
continue
Expand Down
3 changes: 3 additions & 0 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
// The block reward is no longer handled here. It's done by the
// external consensus engine.
header.Root = state.IntermediateRoot(true)
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
header.ExcessBlobs = misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs)))
}
}

// FinalizeAndAssemble implements consensus.Engine, setting the final state and
Expand Down
4 changes: 4 additions & 0 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,9 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
// No block rewards in PoA, so the state remains as is and uncles are dropped
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
header.UncleHash = types.CalcUncleHash(nil)
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
header.ExcessBlobs = misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs)))
}
}

// FinalizeAndAssemble implements consensus.Engine, ensuring no uncles are set,
Expand Down Expand Up @@ -743,6 +746,7 @@ func encodeSigHeader(w io.Writer, header *types.Header) {
if header.BaseFee != nil {
enc = append(enc, header.BaseFee)
}
enc = append(enc, header.ExcessBlobs)
if err := rlp.Encode(w, enc); err != nil {
panic("can't encode: " + err.Error())
}
Expand Down
4 changes: 4 additions & 0 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(chain.Config(), state, header, uncles)
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
header.ExcessBlobs = misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs)))
}
}

// FinalizeAndAssemble implements consensus.Engine, accumulating the block and
Expand Down Expand Up @@ -635,6 +638,7 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
if header.BaseFee != nil {
enc = append(enc, header.BaseFee)
}
enc = append(enc, header.ExcessBlobs)
rlp.Encode(hasher, enc)
hasher.Sum(hash[:0])
return hash
Expand Down
49 changes: 49 additions & 0 deletions consensus/misc/eip4844.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 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"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)

// CalcExcessBlobTransactions calculates the number of blobs above the target
func CalcExcessBlobTransactions(parent *types.Header, blobs uint64) uint64 {
adjusted := parent.ExcessBlobs + blobs
if adjusted < params.TargetBlobsPerBlock {
return 0
}
return adjusted - params.TargetBlobsPerBlock
}

// FakeExponential approximates 2 ** (num / denom)
func FakeExponential(num uint64, denom uint64) uint64 {
cofactor := uint64(math.Exp2(float64(num / denom)))
fractional := num % denom
return cofactor + (fractional*cofactor*2+
(uint64(math.Pow(float64(fractional), 2))*cofactor)/denom)/(denom*3)
}

func CountBlobs(txs []*types.Transaction) int {
var count int
for _, tx := range txs {
count += len(tx.DataHashes())
}
return count
}
7 changes: 7 additions & 0 deletions core/beacon/gen_ed.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/beacon/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ExecutableDataV1 struct {
Timestamp uint64 `json:"timestamp" gencodec:"required"`
ExtraData []byte `json:"extraData" gencodec:"required"`
BaseFeePerGas *big.Int `json:"baseFeePerGas" gencodec:"required"`
ExcessBlobs uint64 `json:"excessBlobs" gencodec:"required"`
BlockHash common.Hash `json:"blockHash" gencodec:"required"`
Transactions [][]byte `json:"transactions" gencodec:"required"`
}
Expand All @@ -76,6 +77,7 @@ type executableDataMarshaling struct {
GasUsed hexutil.Uint64
Timestamp hexutil.Uint64
BaseFeePerGas *hexutil.Big
ExcessBlobs hexutil.Uint64
ExtraData hexutil.Bytes
LogsBloom hexutil.Bytes
Transactions []hexutil.Bytes
Expand Down Expand Up @@ -178,6 +180,7 @@ func ExecutableDataToBlock(params ExecutableDataV1) (*types.Block, error) {
GasUsed: params.GasUsed,
Time: params.Timestamp,
BaseFee: params.BaseFeePerGas,
ExcessBlobs: params.ExcessBlobs,
Extra: params.ExtraData,
MixDigest: params.Random,
}
Expand All @@ -201,6 +204,7 @@ func BlockToExecutableData(block *types.Block) *ExecutableDataV1 {
GasLimit: block.GasLimit(),
GasUsed: block.GasUsed(),
BaseFeePerGas: block.BaseFee(),
ExcessBlobs: block.ExcessBlobs(),
Timestamp: block.Time(),
ReceiptsRoot: block.ReceiptHash(),
LogsBloom: block.Bloom().Bytes(),
Expand Down
7 changes: 6 additions & 1 deletion core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
return func(i int, gen *BlockGen) {
toaddr := common.Address{}
data := make([]byte, nbytes)
gas, _ := IntrinsicGas(data, nil, 0, false, false, false)
rules := IntrinsicGasChainRules{
Homestead: false,
EIP2028: false,
EIP4844: false,
}
gas, _ := IntrinsicGas(data, nil, 0, 0, false, rules)
signer := types.MakeSigner(gen.config, big.NewInt(int64(i)))
gasPrice := big.NewInt(0)
if gen.header.BaseFee != nil {
Expand Down
1 change: 1 addition & 0 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common
Time: new(big.Int).SetUint64(header.Time),
Difficulty: new(big.Int).Set(header.Difficulty),
BaseFee: baseFee,
ExcessBlobs: header.ExcessBlobs,
GasLimit: header.GasLimit,
Random: random,
}
Expand Down
58 changes: 32 additions & 26 deletions core/gen_genesis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d8e92d1

Please sign in to comment.