Skip to content

Commit

Permalink
change from excess blobs to excess data gas (ethereum#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-bayardo authored Oct 6, 2022
1 parent 82cf924 commit 773cfb2
Show file tree
Hide file tree
Showing 30 changed files with 469 additions and 369 deletions.
66 changes: 33 additions & 33 deletions cmd/evm/internal/t8ntool/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ import (

//go:generate go run github.com/fjl/gencodec -type header -field-override headerMarshaling -out gen_header.go
type header struct {
ParentHash common.Hash `json:"parentHash"`
OmmerHash *common.Hash `json:"sha3Uncles"`
Coinbase *common.Address `json:"miner"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash *common.Hash `json:"transactionsRoot"`
ReceiptHash *common.Hash `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"`
Difficulty *big.Int `json:"difficulty"`
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed"`
Time uint64 `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData"`
MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
ExcessBlobs *uint64 `json:"excessBlobs" rlp:"optional"`
ParentHash common.Hash `json:"parentHash"`
OmmerHash *common.Hash `json:"sha3Uncles"`
Coinbase *common.Address `json:"miner"`
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash *common.Hash `json:"transactionsRoot"`
ReceiptHash *common.Hash `json:"receiptsRoot"`
Bloom types.Bloom `json:"logsBloom"`
Difficulty *big.Int `json:"difficulty"`
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed"`
Time uint64 `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData"`
MixDigest common.Hash `json:"mixHash"`
Nonce *types.BlockNonce `json:"nonce"`
BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`
ExcessDataGas *big.Int `json:"excessDataGas" rlp:"optional"`
}

type headerMarshaling struct {
Expand Down Expand Up @@ -115,22 +115,22 @@ func (c *cliqueInput) UnmarshalJSON(input []byte) error {
// ToBlock converts i into a *types.Block
func (i *bbInput) ToBlock() *types.Block {
header := &types.Header{
ParentHash: i.Header.ParentHash,
UncleHash: types.EmptyUncleHash,
Coinbase: common.Address{},
Root: i.Header.Root,
TxHash: types.EmptyRootHash,
ReceiptHash: types.EmptyRootHash,
Bloom: i.Header.Bloom,
Difficulty: common.Big0,
Number: i.Header.Number,
GasLimit: i.Header.GasLimit,
GasUsed: i.Header.GasUsed,
Time: i.Header.Time,
Extra: i.Header.Extra,
MixDigest: i.Header.MixDigest,
BaseFee: i.Header.BaseFee,
ExcessBlobs: i.Header.ExcessBlobs,
ParentHash: i.Header.ParentHash,
UncleHash: types.EmptyUncleHash,
Coinbase: common.Address{},
Root: i.Header.Root,
TxHash: types.EmptyRootHash,
ReceiptHash: types.EmptyRootHash,
Bloom: i.Header.Bloom,
Difficulty: common.Big0,
Number: i.Header.Number,
GasLimit: i.Header.GasLimit,
GasUsed: i.Header.GasUsed,
Time: i.Header.Time,
Extra: i.Header.Extra,
MixDigest: i.Header.MixDigest,
BaseFee: i.Header.BaseFee,
ExcessDataGas: i.Header.ExcessDataGas,
}

// Fill optional values.
Expand Down
74 changes: 37 additions & 37 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.

2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func Transaction(ctx *cli.Context) error {
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 {
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, rules); err != nil {
r.Error = err
results = append(results, r)
continue
Expand Down
2 changes: 1 addition & 1 deletion consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
header.Root = state.IntermediateRoot(true)
if chain.Config().IsSharding(header.Number) {
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
header.SetExcessBlobs(misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs))))
header.SetExcessDataGas(misc.CalcExcessDataGas(parent.ExcessDataGas, misc.CountBlobs(txs)))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
return err
}
if !chain.Config().IsSharding(header.Number) {
if header.ExcessBlobs != nil {
return fmt.Errorf("invalid excessBlobs before fork: have %d, want <nil>", *header.ExcessBlobs)
if header.ExcessDataGas != nil {
return fmt.Errorf("invalid excessDataGas before fork: have %v, want <nil>", header.ExcessDataGas)
}
} else if err := misc.VerifyEip4844Header(chain.Config(), parent, header); err != nil {
// Verify the header's EIP-4844 attributes.
Expand Down Expand Up @@ -578,7 +578,7 @@ func (c *Clique) Finalize(chain consensus.ChainHeaderReader, header *types.Heade
header.UncleHash = types.CalcUncleHash(nil)
if chain.Config().IsSharding(header.Number) {
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
header.SetExcessBlobs(misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs))))
header.SetExcessDataGas(misc.CalcExcessDataGas(parent.ExcessDataGas, misc.CountBlobs(txs)))
}
}
}
Expand Down Expand Up @@ -756,8 +756,8 @@ func encodeSigHeader(w io.Writer, header *types.Header) {
if header.BaseFee != nil {
enc = append(enc, header.BaseFee)
}
if header.ExcessBlobs != nil {
enc = append(enc, header.ExcessBlobs)
if header.ExcessDataGas != nil {
enc = append(enc, header.ExcessDataGas)
}
if err := rlp.Encode(w, enc); err != nil {
panic("can't encode: " + err.Error())
Expand Down
10 changes: 5 additions & 5 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainHeaderReader, header, pa
return err
}
if !chain.Config().IsSharding(header.Number) {
if header.ExcessBlobs != nil {
return fmt.Errorf("invalid excessBlobs before fork: have %d, expected 'nil'", *header.ExcessBlobs)
if header.ExcessDataGas != nil {
return fmt.Errorf("invalid excessDataGas before fork: have %v, expected 'nil'", header.ExcessDataGas)
}
} else if err := misc.VerifyEip4844Header(chain.Config(), parent, header); err != nil {
// Verify the header's EIP-4844 attributes.
Expand Down Expand Up @@ -611,7 +611,7 @@ func (ethash *Ethash) Finalize(chain consensus.ChainHeaderReader, header *types.
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
if chain.Config().IsSharding(header.Number) {
if parent := chain.GetHeaderByHash(header.ParentHash); parent != nil {
header.SetExcessBlobs(misc.CalcExcessBlobTransactions(parent, uint64(misc.CountBlobs(txs))))
header.SetExcessDataGas(misc.CalcExcessDataGas(parent.ExcessDataGas, misc.CountBlobs(txs)))
}
}
}
Expand Down Expand Up @@ -648,8 +648,8 @@ func (ethash *Ethash) SealHash(header *types.Header) (hash common.Hash) {
if header.BaseFee != nil {
enc = append(enc, header.BaseFee)
}
if header.ExcessBlobs != nil {
enc = append(enc, header.ExcessBlobs)
if header.ExcessDataGas != nil {
enc = append(enc, header.ExcessDataGas)
}
rlp.Encode(hasher, enc)
hasher.Sum(hash[:0])
Expand Down
46 changes: 28 additions & 18 deletions consensus/misc/eip4844.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@ package misc

import (
"fmt"
"math"
"math/big"

"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 {
var excessBlobs uint64
if parent.ExcessBlobs != nil {
excessBlobs = *parent.ExcessBlobs
// CalcExcessDataGas implements calc_excess_data_gas from EIP-4844
func CalcExcessDataGas(parentExcessDataGas *big.Int, newBlobs int) *big.Int {
excessDataGas := new(big.Int)
if parentExcessDataGas != nil {
excessDataGas.Set(parentExcessDataGas)
}
adjusted := excessBlobs + blobs
if adjusted < params.TargetBlobsPerBlock {
return 0
consumedGas := big.NewInt(params.DataGasPerBlob)
consumedGas.Mul(consumedGas, big.NewInt(int64(newBlobs)))

excessDataGas.Add(excessDataGas, consumedGas)
targetGas := big.NewInt(params.TargetDataGasPerBlock)
if excessDataGas.Cmp(targetGas) < 0 {
return new(big.Int)
}
return adjusted - params.TargetBlobsPerBlock
return new(big.Int).Set(excessDataGas.Sub(excessDataGas, targetGas))
}

// 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)
// FakeExponential approximates factor * e ** (num / denom) using a taylor expansion
// as described in the EIP-4844 spec.
func FakeExponential(factor, num, denom *big.Int) *big.Int {
output := new(big.Int)
numAccum := new(big.Int).Mul(factor, denom)
for i := 1; numAccum.Sign() > 0; i++ {
output.Add(output, numAccum)
numAccum.Mul(numAccum, num)
iBig := big.NewInt(int64(i))
numAccum.Div(numAccum, iBig.Mul(iBig, denom))
}
return output.Div(output, denom)
}

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

// VerifyEip4844Header verifies that the header is not malformed
func VerifyEip4844Header(config *params.ChainConfig, parent, header *types.Header) error {
if header.ExcessBlobs == nil {
return fmt.Errorf("header is missing excessBlobs")
if header.ExcessDataGas == nil {
return fmt.Errorf("header is missing excessDataGas")
}
return nil
}
Loading

0 comments on commit 773cfb2

Please sign in to comment.