Skip to content

Commit

Permalink
addressing a few last nits around data gas (ethereum#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-bayardo authored Oct 8, 2022
1 parent dc3740c commit ca62042
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 31 deletions.
4 changes: 4 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ var (
// by a transaction is higher than what's left in the block.
ErrGasLimitReached = errors.New("gas limit reached")

// ErrDataGasLimitReached is returned by the gas pool if the amount of data gas required by a
// transaction is higher than what's left in the block.
ErrDataGasLimitReached = errors.New("data gas limit reached")

// ErrInsufficientFundsForTransfer is returned if the transaction sender doesn't
// have enough funds for transfer(topmost call only).
ErrInsufficientFundsForTransfer = errors.New("insufficient funds for transfer")
Expand Down
2 changes: 1 addition & 1 deletion core/gaspool.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (gp *GasPool) AddDataGas(amount uint64) *GasPool {
// error otherwise.
func (gp *GasPool) SubDataGas(amount uint64) error {
if gp.dataGas < amount {
return ErrGasLimitReached
return ErrDataGasLimitReached
}
gp.dataGas -= amount
return nil
Expand Down
3 changes: 2 additions & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
ret, st.gas, vmerr = st.evm.Call(sender, st.to(), st.data, st.gas, st.value)
}

// TODO: Also refund datagas if tx is rejected
// Note that unlike regular gas, data fee gas is not refunded if the tx is reverted, per
// EIP-4844 spec.
if !rules.IsLondon {
// Before EIP-3529: refunds were capped to gasUsed / 2
st.refundGas(params.RefundQuotient)
Expand Down
11 changes: 10 additions & 1 deletion eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,21 @@ func (b *EthAPIBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
return nil
}

func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, excessDataGas *big.Int, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
func (b *EthAPIBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
vmError := func() error { return nil }
if vmConfig == nil {
vmConfig = b.eth.blockchain.GetVMConfig()
}
txContext := core.NewEVMTxContext(msg)
var excessDataGas *big.Int
ph, err := b.HeaderByHash(ctx, header.ParentHash)

if err != nil {
return nil, vmError, err
}
if ph != nil {
excessDataGas = ph.ExcessDataGas
}
context := core.NewEVMBlockContext(header, excessDataGas, b.eth.BlockChain(), nil)
return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig), vmError, nil
}
Expand Down
20 changes: 2 additions & 18 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,15 +953,7 @@ func DoCall(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash
if err != nil {
return nil, err
}
var excessDataGas *big.Int
ph, err := b.HeaderByHash(ctx, header.ParentHash)
if err != nil {
return nil, err
}
if ph != nil {
excessDataGas = ph.ExcessDataGas
}
evm, vmError, err := b.GetEVM(ctx, msg, state, header, excessDataGas, &vm.Config{NoBaseFee: true})
evm, vmError, err := b.GetEVM(ctx, msg, state, header, &vm.Config{NoBaseFee: true})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1454,15 +1446,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
// Apply the transaction with the access list tracer
tracer := logger.NewAccessListTracer(accessList, args.from(), to, precompiles)
config := vm.Config{Tracer: tracer, Debug: true, NoBaseFee: true}
var excessDataGas *big.Int
ph, err := b.HeaderByHash(ctx, header.ParentHash)
if err != nil {
return nil, 0, nil, err
}
if ph != nil {
excessDataGas = ph.ExcessDataGas
}
vmenv, _, err := b.GetEVM(ctx, msg, statedb, header, excessDataGas, &config)
vmenv, _, err := b.GetEVM(ctx, msg, statedb, header, &config)
if err != nil {
return nil, 0, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Backend interface {
PendingBlockAndReceipts() (*types.Block, types.Receipts)
GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error)
GetTd(ctx context.Context, hash common.Hash) *big.Int
GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, excessDataGas *big.Int, vmConfig *vm.Config) (*vm.EVM, func() error, error)
GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error)
SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
SubscribeChainSideEvent(ch chan<- core.ChainSideEvent) event.Subscription
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/transaction_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (b *backendMock) GetLogs(ctx context.Context, blockHash common.Hash, number
return nil, nil
}
func (b *backendMock) GetTd(ctx context.Context, hash common.Hash) *big.Int { return nil }
func (b *backendMock) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, excessDataGas *big.Int, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
func (b *backendMock) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
return nil, nil, nil
}
func (b *backendMock) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { return nil }
Expand Down
10 changes: 9 additions & 1 deletion les/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,19 @@ func (b *LesApiBackend) GetTd(ctx context.Context, hash common.Hash) *big.Int {
return nil
}

func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, excessDataGas *big.Int, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
func (b *LesApiBackend) GetEVM(ctx context.Context, msg core.Message, state *state.StateDB, header *types.Header, vmConfig *vm.Config) (*vm.EVM, func() error, error) {
if vmConfig == nil {
vmConfig = new(vm.Config)
}
txContext := core.NewEVMTxContext(msg)
var excessDataGas *big.Int
ph, err := b.HeaderByHash(ctx, header.ParentHash)
if err != nil {
return nil, state.Error, err
}
if ph != nil {
excessDataGas = ph.ExcessDataGas
}
context := core.NewEVMBlockContext(header, excessDataGas, b.eth.blockchain, nil)
return vm.NewEVM(context, txContext, state, b.eth.chainConfig, *vmConfig), state.Error, nil
}
Expand Down
7 changes: 0 additions & 7 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ type environment struct {
txs []*types.Transaction
receipts []*types.Receipt
uncles map[common.Hash]*types.Header
numBlobs int
}

// copy creates a deep copy of environment.
Expand Down Expand Up @@ -837,19 +836,13 @@ func (w *worker) updateSnapshot(env *environment) {
func (w *worker) commitTransaction(env *environment, tx *types.Transaction) ([]*types.Log, error) {
snap := env.state.Snapshot()

txBlobCount := len(tx.DataHashes())
if env.numBlobs+txBlobCount > params.MaxBlobsPerBlock {
return nil, errMaxBlobsReached
}

receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &env.coinbase, env.gasPool, env.state, env.header, env.excessDataGas, tx, &env.header.GasUsed, *w.chain.GetVMConfig())
if err != nil {
env.state.RevertToSnapshot(snap)
return nil, err
}
env.txs = append(env.txs, tx)
env.receipts = append(env.receipts, receipt)
env.numBlobs += txBlobCount

return receipt.Logs, nil
}
Expand Down

0 comments on commit ca62042

Please sign in to comment.