From 38d1d128d9319ba25228c63eaf2ec987cd29db47 Mon Sep 17 00:00:00 2001 From: MariusVanDerWijden Date: Fri, 22 Aug 2025 10:28:54 +0200 Subject: [PATCH] internal/ethapi: minor cleanups --- internal/ethapi/api.go | 22 ++++------ internal/ethapi/transaction_args.go | 65 ++++++++++++----------------- 2 files changed, 33 insertions(+), 54 deletions(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 3eb4003f5a..891b0d0b52 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -1501,10 +1501,7 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction } // Set some sanity defaults and terminate on failure - config := setDefaultConfig{ - skipGasEstimation: false, - } - if err := args.setDefaults(ctx, api.b, config); err != nil { + if err := args.setDefaults(ctx, api.b, sidecarConfig{}); err != nil { return common.Hash{}, err } // Assemble the transaction and sign with the wallet @@ -1522,8 +1519,7 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction // processing (signing + broadcast). func (api *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) { // Set some sanity defaults and terminate on failure - config := setDefaultConfig{ - skipGasEstimation: false, + config := sidecarConfig{ blobSidecarAllowed: true, blobSidecarVersion: types.BlobSidecarVersion0, } @@ -1600,10 +1596,6 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction if args.Nonce == nil { return nil, errors.New("nonce not specified") } - config := setDefaultConfig{ - skipGasEstimation: false, - blobSidecarAllowed: true, - } sidecarVersion := types.BlobSidecarVersion0 if len(args.Blobs) > 0 { chainHead := api.b.CurrentHeader() @@ -1612,8 +1604,11 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction sidecarVersion = types.BlobSidecarVersion1 } } - config.blobSidecarVersion = sidecarVersion + config := sidecarConfig{ + blobSidecarAllowed: true, + blobSidecarVersion: sidecarVersion, + } if err := args.setDefaults(ctx, api.b, config); err != nil { return nil, err } @@ -1671,10 +1666,7 @@ func (api *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs, if sendArgs.Nonce == nil { return common.Hash{}, errors.New("missing transaction nonce in transaction spec") } - config := setDefaultConfig{ - skipGasEstimation: false, - } - if err := sendArgs.setDefaults(ctx, api.b, config); err != nil { + if err := sendArgs.setDefaults(ctx, api.b, sidecarConfig{}); err != nil { return common.Hash{}, err } matchTx := sendArgs.ToTransaction(types.LegacyTxType) diff --git a/internal/ethapi/transaction_args.go b/internal/ethapi/transaction_args.go index bb460c3f94..372e7001be 100644 --- a/internal/ethapi/transaction_args.go +++ b/internal/ethapi/transaction_args.go @@ -91,21 +91,16 @@ func (args *TransactionArgs) data() []byte { return nil } -// setDefaultConfig defines the options for deriving missing fields of transactions. -type setDefaultConfig struct { +// sidecarConfig defines the options for deriving missing fields of transactions. +type sidecarConfig struct { // This configures whether blobs are allowed to be passed and // the associated sidecar version should be attached. blobSidecarAllowed bool blobSidecarVersion byte - - // skipGasEstimation is the flag whether the gas estimation is skipped. - // this flag should only be used in the scenarios that a precise gas limit - // is not critical, e.g., in non-transaction calls. - skipGasEstimation bool } // setDefaults fills in default values for unspecified tx fields. -func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config setDefaultConfig) error { +func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config sidecarConfig) error { if err := args.setBlobTxSidecar(ctx, config); err != nil { return err } @@ -146,36 +141,28 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config } if args.Gas == nil { - if config.skipGasEstimation { - gas := hexutil.Uint64(b.RPCGasCap()) - if gas == 0 { - gas = hexutil.Uint64(math.MaxUint64 / 2) - } - args.Gas = &gas - } else { - // These fields are immutable during the estimation, safe to - // pass the pointer directly. - data := args.data() - callArgs := TransactionArgs{ - From: args.From, - To: args.To, - GasPrice: args.GasPrice, - MaxFeePerGas: args.MaxFeePerGas, - MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, - Value: args.Value, - Data: (*hexutil.Bytes)(&data), - AccessList: args.AccessList, - BlobFeeCap: args.BlobFeeCap, - BlobHashes: args.BlobHashes, - } - latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) - estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, nil, b.RPCGasCap()) - if err != nil { - return err - } - args.Gas = &estimated - log.Trace("Estimate gas usage automatically", "gas", args.Gas) + // These fields are immutable during the estimation, safe to + // pass the pointer directly. + data := args.data() + callArgs := TransactionArgs{ + From: args.From, + To: args.To, + GasPrice: args.GasPrice, + MaxFeePerGas: args.MaxFeePerGas, + MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, + Value: args.Value, + Data: (*hexutil.Bytes)(&data), + AccessList: args.AccessList, + BlobFeeCap: args.BlobFeeCap, + BlobHashes: args.BlobHashes, } + latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber) + estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, nil, b.RPCGasCap()) + if err != nil { + return err + } + args.Gas = &estimated + log.Trace("Estimate gas usage automatically", "gas", args.Gas) } // If chain id is provided, ensure it matches the local chain id. Otherwise, set the local @@ -292,7 +279,7 @@ func (args *TransactionArgs) setLondonFeeDefaults(ctx context.Context, head *typ } // setBlobTxSidecar adds the blob tx -func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config setDefaultConfig) error { +func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sidecarConfig) error { // No blobs, we're done. if args.Blobs == nil { return nil @@ -311,7 +298,7 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config setDef return errors.New(`blob commitments provided while proofs were not`) } - // len(blobs) == len(commitments) == len(proofs) == len(hashes) + // len(blobs) == len(commitments) == len(hashes) n := len(args.Blobs) if args.BlobHashes != nil && len(args.BlobHashes) != n { return fmt.Errorf("number of blobs and hashes mismatch (have=%d, want=%d)", len(args.BlobHashes), n)