Skip to content

core/types: reduce allocations in effectiveGasTip #31602

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

Closed
Closed
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
32 changes: 19 additions & 13 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,43 +356,49 @@ func (tx *Transaction) GasTipCapIntCmp(other *big.Int) int {
// the actual negative value, _and_ ErrGasFeeCapTooLow
func (tx *Transaction) EffectiveGasTip(baseFee *big.Int) (*big.Int, error) {
if baseFee == nil {
return tx.GasTipCap(), nil
return tx.inner.gasTipCap(), nil
}
out, err := tx.effectiveGasTip(baseFee)
return new(big.Int).Set(out), err
}

// effectiveGasTip returns the effective miner gasTipCap for the given base fee but does not allocate.
// Note: if the effective gasTipCap is negative, this method returns both error
// the actual negative value, _and_ ErrGasFeeCapTooLow
// Note: please copy the return value before modifying.
// Note: assumes basefee to be non-nil.
func (tx *Transaction) effectiveGasTip(baseFee *big.Int) (*big.Int, error) {
var err error
gasFeeCap := tx.GasFeeCap()
gasFeeCap := tx.inner.gasFeeCap()
if gasFeeCap.Cmp(baseFee) < 0 {
err = ErrGasFeeCapTooLow
}
gasFeeCap = gasFeeCap.Sub(gasFeeCap, baseFee)
gasFeeCap = new(big.Int).Sub(gasFeeCap, baseFee)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to get rid of this allocation, but I can't


gasTipCap := tx.GasTipCap()
gasTipCap := tx.inner.gasTipCap()
if gasTipCap.Cmp(gasFeeCap) < 0 {
return gasTipCap, err
}
return gasFeeCap, err
}

// EffectiveGasTipValue is identical to EffectiveGasTip, but does not return an
// error in case the effective gasTipCap is negative
func (tx *Transaction) EffectiveGasTipValue(baseFee *big.Int) *big.Int {
effectiveTip, _ := tx.EffectiveGasTip(baseFee)
return effectiveTip
}

// EffectiveGasTipCmp compares the effective gasTipCap of two transactions assuming the given base fee.
func (tx *Transaction) EffectiveGasTipCmp(other *Transaction, baseFee *big.Int) int {
if baseFee == nil {
return tx.GasTipCapCmp(other)
}
return tx.EffectiveGasTipValue(baseFee).Cmp(other.EffectiveGasTipValue(baseFee))
effectiveGasTip, _ := tx.effectiveGasTip(baseFee)
otherEffectiveGasTip, _ := other.effectiveGasTip(baseFee)
return effectiveGasTip.Cmp(otherEffectiveGasTip)
}

// EffectiveGasTipIntCmp compares the effective gasTipCap of a transaction to the given gasTipCap.
func (tx *Transaction) EffectiveGasTipIntCmp(other *big.Int, baseFee *big.Int) int {
if baseFee == nil {
return tx.GasTipCapIntCmp(other)
}
return tx.EffectiveGasTipValue(baseFee).Cmp(other)
effectiveGasTip, _ := tx.effectiveGasTip(baseFee)
return effectiveGasTip.Cmp(other)
}

// BlobGas returns the blob gas limit of the transaction for blob transactions, 0 otherwise.
Expand Down
3 changes: 2 additions & 1 deletion eth/tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ func (t *jsTracer) OnTxStart(env *tracing.VMContext, tx *types.Transaction, from
t.activePrecompiles = vm.ActivePrecompiles(rules)
t.ctx["block"] = t.vm.ToValue(t.env.BlockNumber.Uint64())
t.ctx["gas"] = t.vm.ToValue(tx.Gas())
gasPriceBig, err := t.toBig(t.vm, tx.EffectiveGasTipValue(env.BaseFee).String())
gasTip, _ := tx.EffectiveGasTip(env.BaseFee)
gasPriceBig, err := t.toBig(t.vm, gasTip.String())
if err != nil {
t.err = err
return
Expand Down