Implement EIP-7516: BLOBBASEFEE opcode (#8231)

[EIP-7516](https://eips.ethereum.org/EIPS/eip-7516) is part of
[devnet-9](https://notes.ethereum.org/@ethpandaops/dencun-devnet-9)
This commit is contained in:
Andrew Ashikhmin 2023-09-19 10:45:19 +02:00 committed by GitHub
parent 88ddd8caf0
commit 94ec54e7a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 10 deletions

View File

@ -194,15 +194,15 @@ func (st *StateTransition) to() libcommon.Address {
}
func (st *StateTransition) buyGas(gasBailout bool) error {
mgval := st.sharedBuyGas
mgval.SetUint64(st.msg.Gas())
mgval, overflow := mgval.MulOverflow(mgval, st.gasPrice)
gasVal := st.sharedBuyGas
gasVal.SetUint64(st.msg.Gas())
gasVal, overflow := gasVal.MulOverflow(gasVal, st.gasPrice)
if overflow {
return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex())
}
// compute blob fee for eip-4844 data blobs if any
dgval := new(uint256.Int)
blobGasVal := new(uint256.Int)
if st.evm.ChainRules().IsCancun {
if st.evm.Context().ExcessBlobGas == nil {
return fmt.Errorf("%w: Cancun is active but ExcessBlobGas is nil", ErrInternalFailure)
@ -211,16 +211,16 @@ func (st *StateTransition) buyGas(gasBailout bool) error {
if err != nil {
return err
}
_, overflow = dgval.MulOverflow(blobGasPrice, new(uint256.Int).SetUint64(st.msg.BlobGas()))
blobGasVal, overflow = blobGasVal.MulOverflow(blobGasPrice, new(uint256.Int).SetUint64(st.msg.BlobGas()))
if overflow {
return fmt.Errorf("%w: overflow converting blob gas: %v", ErrInsufficientFunds, dgval)
return fmt.Errorf("%w: overflow converting blob gas: %v", ErrInsufficientFunds, blobGasVal)
}
if err := st.gp.SubBlobGas(st.msg.BlobGas()); err != nil {
return err
}
}
balanceCheck := mgval
balanceCheck := gasVal
if st.gasFeeCap != nil {
balanceCheck = st.sharedBuyGasBalance.SetUint64(st.msg.Gas())
balanceCheck, overflow = balanceCheck.MulOverflow(balanceCheck, st.gasFeeCap)
@ -231,7 +231,7 @@ func (st *StateTransition) buyGas(gasBailout bool) error {
if overflow {
return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex())
}
balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, dgval)
balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, blobGasVal)
if overflow {
return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex())
}
@ -253,8 +253,8 @@ func (st *StateTransition) buyGas(gasBailout bool) error {
st.initialGas = st.msg.Gas()
if subBalance {
st.state.SubBalance(st.msg.From(), mgval)
st.state.SubBalance(st.msg.From(), dgval)
st.state.SubBalance(st.msg.From(), gasVal)
st.state.SubBalance(st.msg.From(), blobGasVal)
}
return nil
}

View File

@ -24,10 +24,12 @@ import (
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/params"
)
var activators = map[int]func(*JumpTable){
7516: enable7516,
6780: enable6780,
5656: enable5656,
4844: enable4844,
@ -303,3 +305,25 @@ func enable6780(jt *JumpTable) {
numPush: 0,
}
}
// opBlobBaseFee implements the BLOBBASEFEE opcode
func opBlobBaseFee(pc *uint64, interpreter *EVMInterpreter, callContext *ScopeContext) ([]byte, error) {
excessBlobGas := interpreter.evm.Context().ExcessBlobGas
blobBaseFee, err := misc.GetBlobGasPrice(*excessBlobGas)
if err != nil {
return nil, err
}
callContext.Stack.Push(blobBaseFee)
return nil, nil
}
// enable7516 applies EIP-7516 (BLOBBASEFEE opcode)
// - Adds an opcode that returns the current block's blob base fee.
func enable7516(jt *JumpTable) {
jt[BLOBBASEFEE] = &operation{
execute: opBlobBaseFee,
constantGas: GasQuickStep,
numPop: 0,
numPush: 1,
}
}

View File

@ -104,6 +104,7 @@ func newCancunInstructionSet() JumpTable {
enable4844(&instructionSet) // BLOBHASH opcode
enable5656(&instructionSet) // MCOPY opcode
enable6780(&instructionSet) // SELFDESTRUCT only in same transaction
enable7516(&instructionSet) // BLOBBASEFEE opcode
validateAndFillMaxStack(&instructionSet)
return instructionSet
}

View File

@ -105,6 +105,7 @@ const (
SELFBALANCE OpCode = 0x47
BASEFEE OpCode = 0x48
BLOBHASH OpCode = 0x49
BLOBBASEFEE OpCode = 0x4a
)
// 0x50 range - 'storage' and execution.
@ -282,6 +283,7 @@ var opCodeToString = map[OpCode]string{
SELFBALANCE: "SELFBALANCE",
BASEFEE: "BASEFEE",
BLOBHASH: "BLOBHASH",
BLOBBASEFEE: "BLOBBASEFEE",
// 0x50 range - 'storage' and execution.
POP: "POP",
@ -437,6 +439,7 @@ var stringToOp = map[string]OpCode{
"CHAINID": CHAINID,
"BASEFEE": BASEFEE,
"BLOBHASH": BLOBHASH,
"BLOBBASEFEE": BLOBBASEFEE,
"DELEGATECALL": DELEGATECALL,
"STATICCALL": STATICCALL,
"CODESIZE": CODESIZE,