From a4ad86b28099625fb9d98eb07e890a8d09f6c9f6 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Thu, 10 Apr 2025 13:04:32 +0200 Subject: [PATCH] core/types: reduce allocations --- core/types/transaction.go | 31 ++++++++++++++++++------------- eth/tracers/js/goja.go | 3 ++- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/core/types/transaction.go b/core/types/transaction.go index 16da5f0522..5c55fbcd0a 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -355,36 +355,40 @@ func (tx *Transaction) GasTipCapIntCmp(other *big.Int) int { // Note: if the effective gasTipCap is negative, this method returns both error // the actual negative value, _and_ ErrGasFeeCapTooLow func (tx *Transaction) EffectiveGasTip(baseFee *big.Int) (*big.Int, error) { + 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. +func (tx *Transaction) effectiveGasTip(baseFee *big.Int) (*big.Int, error) { if baseFee == nil { - return tx.GasTipCap(), nil + return tx.inner.gasTipCap(), nil } 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) gasTipCap := tx.inner.gasTipCap() if gasTipCap.Cmp(gasFeeCap) < 0 { - return gasFeeCap.Set(gasTipCap), err + 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. @@ -392,7 +396,8 @@ func (tx *Transaction) EffectiveGasTipIntCmp(other *big.Int, baseFee *big.Int) i 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. diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index 227ea57226..d1e65bf7f4 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -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