diff --git a/core/state_transition.go b/core/state_transition.go index bd4d9af1160..3f0649e9a44 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -219,49 +219,42 @@ func (st *StateTransition) buyGas(gasBailout bool) error { } } - balanceCheck := gasVal - if st.gasFeeCap != nil { - balanceCheck = st.sharedBuyGasBalance.SetUint64(st.msg.Gas()) - balanceCheck, overflow = balanceCheck.MulOverflow(balanceCheck, st.gasFeeCap) - if overflow { - return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex()) - } - balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, st.value) - if overflow { - return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex()) - } - if st.evm.ChainRules().IsCancun { - maxBlobFee, overflow := new(uint256.Int).MulOverflow(st.msg.MaxFeePerBlobGas(), new(uint256.Int).SetUint64(st.msg.BlobGas())) + if !gasBailout { + balanceCheck := gasVal + if st.gasFeeCap != nil { + balanceCheck = st.sharedBuyGasBalance.SetUint64(st.msg.Gas()) + balanceCheck, overflow = balanceCheck.MulOverflow(balanceCheck, st.gasFeeCap) if overflow { return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex()) } - balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, maxBlobFee) + balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, st.value) if overflow { return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex()) } + if st.evm.ChainRules().IsCancun { + maxBlobFee, overflow := new(uint256.Int).MulOverflow(st.msg.MaxFeePerBlobGas(), new(uint256.Int).SetUint64(st.msg.BlobGas())) + if overflow { + return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex()) + } + balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, maxBlobFee) + if overflow { + return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex()) + } + } } - } - var subBalance = false - if have, want := st.state.GetBalance(st.msg.From()), balanceCheck; have.Cmp(want) < 0 { - if !gasBailout { + if have, want := st.state.GetBalance(st.msg.From()), balanceCheck; have.Cmp(want) < 0 { return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From().Hex(), have, want) } - } else { - subBalance = true + st.state.SubBalance(st.msg.From(), gasVal) + st.state.SubBalance(st.msg.From(), blobGasVal) } + if err := st.gp.SubGas(st.msg.Gas()); err != nil { - if !gasBailout { - return err - } + return err } st.gasRemaining += st.msg.Gas() st.initialGas = st.msg.Gas() st.evm.BlobFee = blobGasVal - - if subBalance { - st.state.SubBalance(st.msg.From(), gasVal) - st.state.SubBalance(st.msg.From(), blobGasVal) - } return nil } @@ -426,7 +419,7 @@ func (st *StateTransition) TransitionDb(refunds bool, gasBailout bool) (*Executi st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1) ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), st.data, st.gasRemaining, st.value, bailout) } - if refunds { + if refunds && !gasBailout { if rules.IsLondon { // After EIP-3529: refunds are capped to gasUsed / 5 st.refundGas(params.RefundQuotientEIP3529)