Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize BLOBBASEFEE #10628

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func (b *SimulatedBackend) callContract(_ context.Context, call ethereum.CallMsg

txContext := core.NewEVMTxContext(msg)
header := block.Header()
evmContext := core.NewEVMBlockContext(header, core.GetHashFn(header, b.getHeader), b.m.Engine, nil)
evmContext := core.NewEVMBlockContext(header, core.GetHashFn(header, b.getHeader), b.m.Engine, nil, b.m.ChainConfig)
// Create a new environment which holds all relevant information
// about the transaction and calling mechanisms.
vmEnv := vm.NewEVM(evmContext, txContext, statedb, b.m.ChainConfig, vm.Config{})
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/exec3/trace_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewTraceWorker(tx kv.TemporalTx, cc *chain.Config, engine consensus.EngineR

func (e *TraceWorker) ChangeBlock(header *types.Header) {
e.blockNum = header.Number.Uint64()
blockCtx := transactions.NewEVMBlockContext(e.engine, header, true /* requireCanonical */, e.tx, e.headerReader)
blockCtx := transactions.NewEVMBlockContext(e.engine, header, true /* requireCanonical */, e.tx, e.headerReader, e.evm.ChainConfig())
e.blockCtx = &blockCtx
e.blockHash = header.Hash()
e.header = header
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/exec3/trace_worker2.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func CustomTraceMapReduce(fromBlock, toBlock uint64, consumer TraceConsumer, ctx
defer getHashFnMute.Unlock()
return f(n)
}
blockContext := core.NewEVMBlockContext(header, getHashFn, cfg.Engine, nil /* author */)
blockContext := core.NewEVMBlockContext(header, getHashFn, cfg.Engine, nil /* author */, chainConfig)

rules := chainConfig.Rules(blockNum, b.Time())
for txIndex := -1; txIndex <= len(txs); txIndex++ {
Expand Down
8 changes: 4 additions & 4 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/polygon/bor/borcfg"

Expand Down Expand Up @@ -66,7 +65,7 @@ type eip1559Calculator struct{}
func (f eip1559Calculator) CurrentFees(chainConfig *chain.Config, db kv.Getter) (baseFee, blobFee, minBlobGasPrice, blockGasLimit uint64, err error) {
hash := rawdb.ReadHeadHeaderHash(db)

if hash == (libcommon.Hash{}) {
if hash == (common.Hash{}) {
return 0, 0, 0, 0, fmt.Errorf("can't get head header hash")
}

Expand All @@ -86,9 +85,10 @@ func (f eip1559Calculator) CurrentFees(chainConfig *chain.Config, db kv.Getter)
if currentHeader.ExcessBlobGas != nil {
excessBlobGas := CalcExcessBlobGas(chainConfig, currentHeader)
b, err := GetBlobGasPrice(chainConfig, excessBlobGas)
if err == nil {
blobFee = b.Uint64()
if err != nil {
return 0, 0, 0, 0, err
}
blobFee = b.Uint64()
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func SysCallContract(contract libcommon.Address, data []byte, chainConfig *chain
author = &state.SystemAddress
txContext = NewEVMTxContext(msg)
}
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), engine, author)
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), engine, author, chainConfig)
evm := vm.NewEVM(blockContext, txContext, ibs, chainConfig, vmConfig)

ret, _, err := evm.Call(
Expand All @@ -297,7 +297,7 @@ func SysCallContract(contract libcommon.Address, data []byte, chainConfig *chain
}

// SysCreate is a special (system) contract creation methods for genesis constructors.
func SysCreate(contract libcommon.Address, data []byte, chainConfig chain.Config, ibs *state.IntraBlockState, header *types.Header) (result []byte, err error) {
func SysCreate(contract libcommon.Address, data []byte, chainConfig *chain.Config, ibs *state.IntraBlockState, header *types.Header) (result []byte, err error) {
msg := types.NewMessage(
contract,
nil, // to
Expand All @@ -313,8 +313,8 @@ func SysCreate(contract libcommon.Address, data []byte, chainConfig chain.Config
// Create a new context to be used in the EVM environment
author := &contract
txContext := NewEVMTxContext(msg)
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), nil, author)
evm := vm.NewEVM(blockContext, txContext, ibs, &chainConfig, vmConfig)
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), nil, author, chainConfig)
evm := vm.NewEVM(blockContext, txContext, ibs, chainConfig, vmConfig)

ret, _, err := evm.SysCreate(
vm.AccountRef(msg.From()),
Expand Down
35 changes: 20 additions & 15 deletions core/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import (

"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/merge"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
)

// NewEVMBlockContext creates a new context for use in the EVM.
func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libcommon.Hash, engine consensus.EngineReader, author *libcommon.Address) evmtypes.BlockContext {
func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libcommon.Hash,
engine consensus.EngineReader, author *libcommon.Address, config *chain.Config) evmtypes.BlockContext {
// If we don't have an explicit author (i.e. not mining), extract from the header
var beneficiary libcommon.Address
if author == nil {
Expand All @@ -55,10 +57,13 @@ func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libco
*prevRandDao = header.MixDigest
}

var excessBlobGas *uint64
var blobBaseFee *uint256.Int
if header.ExcessBlobGas != nil {
excessBlobGas = new(uint64)
*excessBlobGas = *header.ExcessBlobGas
var err error
blobBaseFee, err = misc.GetBlobGasPrice(config, *header.ExcessBlobGas)
if err != nil {
panic(err)
}
}

var transferFunc evmtypes.TransferFunc
Expand All @@ -68,17 +73,17 @@ func NewEVMBlockContext(header *types.Header, blockHashFunc func(n uint64) libco
transferFunc = Transfer
}
return evmtypes.BlockContext{
CanTransfer: CanTransfer,
Transfer: transferFunc,
GetHash: blockHashFunc,
Coinbase: beneficiary,
BlockNumber: header.Number.Uint64(),
Time: header.Time,
Difficulty: new(big.Int).Set(header.Difficulty),
BaseFee: &baseFee,
GasLimit: header.GasLimit,
PrevRanDao: prevRandDao,
ExcessBlobGas: excessBlobGas,
CanTransfer: CanTransfer,
Transfer: transferFunc,
GetHash: blockHashFunc,
Coinbase: beneficiary,
BlockNumber: header.Number.Uint64(),
Time: header.Time,
Difficulty: new(big.Int).Set(header.Difficulty),
BaseFee: &baseFee,
GasLimit: header.GasLimit,
PrevRanDao: prevRandDao,
BlobBaseFee: blobBaseFee,
}
}

Expand Down
18 changes: 10 additions & 8 deletions core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ import (
"embed"
"encoding/json"
"fmt"
"math/big"
"os"
"slices"

"github.com/c2h5oh/datasize"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/config3"
"github.com/ledgerwatch/erigon-lib/kv/temporal"
state2 "github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/log/v3"
"golang.org/x/sync/errgroup"
"math/big"
"os"
"slices"

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/chain/networkname"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon-lib/config3"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
"github.com/ledgerwatch/erigon-lib/kv/rawdbv3"
"github.com/ledgerwatch/erigon-lib/kv/temporal"
state2 "github.com/ledgerwatch/erigon-lib/state"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus/ethash"
"github.com/ledgerwatch/erigon/consensus/merge"
Expand Down Expand Up @@ -601,7 +603,7 @@ func GenesisToBlock(g *types.Genesis, tmpDir string, logger log.Logger) (*types.
}

if len(account.Constructor) > 0 {
if _, err = SysCreate(addr, account.Constructor, *g.Config, statedb, head); err != nil {
if _, err = SysCreate(addr, account.Constructor, g.Config, statedb, head); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func ApplyTransaction(config *chain.Config, blockHashFunc func(n uint64) libcomm
// about the transaction and calling mechanisms.
cfg.SkipAnalysis = SkipAnalysis(config, header.Number.Uint64())

blockContext := NewEVMBlockContext(header, blockHashFunc, engine, author)
blockContext := NewEVMBlockContext(header, blockHashFunc, engine, author, config)
vmenv := vm.NewEVM(blockContext, evmtypes.TxContext{}, ibs, config, cfg)

return applyTransaction(config, engine, gp, ibs, stateWriter, header, tx, usedGas, usedBlobGas, vmenv, cfg)
Expand Down
20 changes: 6 additions & 14 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

cmath "github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/crypto"
Expand Down Expand Up @@ -204,13 +203,10 @@ func (st *StateTransition) buyGas(gasBailout bool) error {
// compute blob fee for eip-4844 data blobs if any
blobGasVal := new(uint256.Int)
if st.evm.ChainRules().IsCancun {
if st.evm.Context.ExcessBlobGas == nil {
blobGasPrice := st.evm.Context.BlobBaseFee
if blobGasPrice == nil {
return fmt.Errorf("%w: Cancun is active but ExcessBlobGas is nil", ErrInternalFailure)
}
blobGasPrice, err := misc.GetBlobGasPrice(st.evm.ChainConfig(), *st.evm.Context.ExcessBlobGas)
if err != nil {
return err
}
blobGasVal, overflow = blobGasVal.MulOverflow(blobGasPrice, new(uint256.Int).SetUint64(st.msg.BlobGas()))
if overflow {
return fmt.Errorf("%w: overflow converting blob gas: %v", ErrInsufficientFunds, blobGasVal)
Expand Down Expand Up @@ -313,18 +309,14 @@ func (st *StateTransition) preCheck(gasBailout bool) error {
}
}
if st.msg.BlobGas() > 0 && st.evm.ChainRules().IsCancun {
if st.evm.Context.ExcessBlobGas == nil {
blobGasPrice := st.evm.Context.BlobBaseFee
if blobGasPrice == nil {
return fmt.Errorf("%w: Cancun is active but ExcessBlobGas is nil", ErrInternalFailure)
}
blobGasPrice, err := misc.GetBlobGasPrice(st.evm.ChainConfig(), *st.evm.Context.ExcessBlobGas)
if err != nil {
return err
}
maxFeePerBlobGas := st.msg.MaxFeePerBlobGas()
if blobGasPrice.Cmp(maxFeePerBlobGas) > 0 {
return fmt.Errorf("%w: address %v, maxFeePerBlobGas: %v blobGasPrice: %v, excessBlobGas: %v",
ErrMaxFeePerBlobGas,
st.msg.From().Hex(), st.msg.MaxFeePerBlobGas(), blobGasPrice, st.evm.Context.ExcessBlobGas)
return fmt.Errorf("%w: address %v, maxFeePerBlobGas: %v < blobGasPrice: %v",
ErrMaxFeePerBlobGas, st.msg.From().Hex(), st.msg.MaxFeePerBlobGas(), blobGasPrice)
}
}

Expand Down
7 changes: 1 addition & 6 deletions core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

libcommon "github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/params"
)

Expand Down Expand Up @@ -303,11 +302,7 @@ func enable6780(jt *JumpTable) {

// opBlobBaseFee implements the BLOBBASEFEE opcode
func opBlobBaseFee(pc *uint64, interpreter *EVMInterpreter, callContext *ScopeContext) ([]byte, error) {
excessBlobGas := interpreter.evm.Context.ExcessBlobGas
blobBaseFee, err := misc.GetBlobGasPrice(interpreter.evm.ChainConfig(), *excessBlobGas)
if err != nil {
return nil, err
}
blobBaseFee := interpreter.evm.Context.BlobBaseFee
callContext.Stack.Push(blobBaseFee)
return nil, nil
}
Expand Down
18 changes: 9 additions & 9 deletions core/vm/evmtypes/evmtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type BlockContext struct {
GetHash GetHashFunc

// Block information
Coinbase common.Address // Provides information for COINBASE
GasLimit uint64 // Provides information for GASLIMIT
MaxGasLimit bool // Use GasLimit override for 2^256-1 (to be compatible with OpenEthereum's trace_call)
BlockNumber uint64 // Provides information for NUMBER
Time uint64 // Provides information for TIME
Difficulty *big.Int // Provides information for DIFFICULTY
BaseFee *uint256.Int // Provides information for BASEFEE
PrevRanDao *common.Hash // Provides information for PREVRANDAO
ExcessBlobGas *uint64 // Provides information for handling data blobs
Coinbase common.Address // Provides information for COINBASE
GasLimit uint64 // Provides information for GASLIMIT
MaxGasLimit bool // Use GasLimit override for 2^256-1 (to be compatible with OpenEthereum's trace_call)
BlockNumber uint64 // Provides information for NUMBER
Time uint64 // Provides information for TIME
Difficulty *big.Int // Provides information for DIFFICULTY
BaseFee *uint256.Int // Provides information for BASEFEE
PrevRanDao *common.Hash // Provides information for PREVRANDAO
BlobBaseFee *uint256.Int // Provides information for BLOBBASEFEE
}

// TxContext provides the EVM with information about a transaction.
Expand Down
4 changes: 2 additions & 2 deletions eth/stagedsync/exec3.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ Loop:
defer getHashFnMute.Unlock()
return f(n)
}
blockContext := core.NewEVMBlockContext(header, getHashFn, engine, nil /* author */)
blockContext := core.NewEVMBlockContext(header, getHashFn, engine, nil /* author */, chainConfig)
if parallel {
select {
case err := <-rwLoopErrCh:
Expand Down Expand Up @@ -1445,7 +1445,7 @@ func reconstituteStep(last bool,
defer getHashFnMute.Unlock()
return f(n)
}
blockContext := core.NewEVMBlockContext(header, getHashFn, engine, nil /* author */)
blockContext := core.NewEVMBlockContext(header, getHashFn, engine, nil /* author */, chainConfig)
rules := chainConfig.Rules(bn, b.Time())

for txIndex := -1; txIndex <= len(txs); txIndex++ {
Expand Down
26 changes: 13 additions & 13 deletions eth/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import (
"crypto/ecdsa"
"crypto/rand"
"encoding/json"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"math/big"
"testing"

"github.com/holiman/uint256"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutil"

"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
Expand All @@ -35,8 +38,6 @@ import (
"github.com/ledgerwatch/erigon/turbo/stages/mock"
"github.com/stretchr/testify/require"

"github.com/holiman/uint256"

// Force-load native and js packages, to trigger registration
"github.com/ledgerwatch/erigon/eth/tracers"
_ "github.com/ledgerwatch/erigon/eth/tracers/js"
Expand Down Expand Up @@ -70,18 +71,17 @@ func TestPrestateTracerCreate2(t *testing.T) {
Origin: origin,
GasPrice: uint256.NewInt(1),
}
excessBlobGas := uint64(50000)
context := evmtypes.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Coinbase: libcommon.Address{},
BlockNumber: 8000000,
Time: 5,
Difficulty: big.NewInt(0x30000),
GasLimit: uint64(6000000),
ExcessBlobGas: &excessBlobGas,
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Coinbase: libcommon.Address{},
BlockNumber: 8000000,
Time: 5,
Difficulty: big.NewInt(0x30000),
GasLimit: uint64(6000000),
BaseFee: uint256.NewInt(0),
BlobBaseFee: uint256.NewInt(50000),
}
context.BaseFee = uint256.NewInt(0)
alloc := types.GenesisAlloc{}

// The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
Expand Down
2 changes: 1 addition & 1 deletion tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (t *StateTest) RunNoVerify(tx kv.RwTx, subtest StateSubtest, vmconfig vm.Co
// Prepare the EVM.
txContext := core.NewEVMTxContext(msg)
header := block.HeaderNoCopy()
context := core.NewEVMBlockContext(header, core.GetHashFn(header, nil), nil, &t.json.Env.Coinbase)
context := core.NewEVMBlockContext(header, core.GetHashFn(header, nil), nil, &t.json.Env.Coinbase, config)
context.GetHash = vmTestBlockHash
if baseFee != nil {
context.BaseFee = new(uint256.Int)
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
return nil, err
}

blockCtx := transactions.NewEVMBlockContext(engine, header, stateBlockNumberOrHash.RequireCanonical, tx, api._blockReader)
blockCtx := transactions.NewEVMBlockContext(engine, header, stateBlockNumberOrHash.RequireCanonical, tx, api._blockReader, chainConfig)
txCtx := core.NewEVMTxContext(firstMsg)
// Get a new instance of the EVM
evm := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, vm.Config{Debug: false})
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func (api *APIImpl) CreateAccessList(ctx context.Context, args ethapi2.CallArgs,
// Apply the transaction with the access list tracer
tracer := logger.NewAccessListTracer(accessList, excl, state)
config := vm.Config{Tracer: tracer, Debug: true, NoBaseFee: true}
blockCtx := transactions.NewEVMBlockContext(engine, header, bNrOrHash.RequireCanonical, tx, api._blockReader)
blockCtx := transactions.NewEVMBlockContext(engine, header, bNrOrHash.RequireCanonical, tx, api._blockReader, chainConfig)
txCtx := core.NewEVMTxContext(msg)

evm := vm.NewEVM(blockCtx, txCtx, state, chainConfig, config)
Expand Down
Loading
Loading