internal/ethapi: minor cleanups

This commit is contained in:
MariusVanDerWijden 2025-08-22 10:28:54 +02:00
parent 76e3a3686f
commit 38d1d128d9
2 changed files with 33 additions and 54 deletions

View file

@ -1501,10 +1501,7 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction
} }
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
config := setDefaultConfig{ if err := args.setDefaults(ctx, api.b, sidecarConfig{}); err != nil {
skipGasEstimation: false,
}
if err := args.setDefaults(ctx, api.b, config); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
// Assemble the transaction and sign with the wallet // Assemble the transaction and sign with the wallet
@ -1522,8 +1519,7 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction
// processing (signing + broadcast). // processing (signing + broadcast).
func (api *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) { func (api *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
config := setDefaultConfig{ config := sidecarConfig{
skipGasEstimation: false,
blobSidecarAllowed: true, blobSidecarAllowed: true,
blobSidecarVersion: types.BlobSidecarVersion0, blobSidecarVersion: types.BlobSidecarVersion0,
} }
@ -1600,10 +1596,6 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction
if args.Nonce == nil { if args.Nonce == nil {
return nil, errors.New("nonce not specified") return nil, errors.New("nonce not specified")
} }
config := setDefaultConfig{
skipGasEstimation: false,
blobSidecarAllowed: true,
}
sidecarVersion := types.BlobSidecarVersion0 sidecarVersion := types.BlobSidecarVersion0
if len(args.Blobs) > 0 { if len(args.Blobs) > 0 {
chainHead := api.b.CurrentHeader() chainHead := api.b.CurrentHeader()
@ -1612,8 +1604,11 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction
sidecarVersion = types.BlobSidecarVersion1 sidecarVersion = types.BlobSidecarVersion1
} }
} }
config.blobSidecarVersion = sidecarVersion
config := sidecarConfig{
blobSidecarAllowed: true,
blobSidecarVersion: sidecarVersion,
}
if err := args.setDefaults(ctx, api.b, config); err != nil { if err := args.setDefaults(ctx, api.b, config); err != nil {
return nil, err return nil, err
} }
@ -1671,10 +1666,7 @@ func (api *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs,
if sendArgs.Nonce == nil { if sendArgs.Nonce == nil {
return common.Hash{}, errors.New("missing transaction nonce in transaction spec") return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
} }
config := setDefaultConfig{ if err := sendArgs.setDefaults(ctx, api.b, sidecarConfig{}); err != nil {
skipGasEstimation: false,
}
if err := sendArgs.setDefaults(ctx, api.b, config); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
matchTx := sendArgs.ToTransaction(types.LegacyTxType) matchTx := sendArgs.ToTransaction(types.LegacyTxType)

View file

@ -91,21 +91,16 @@ func (args *TransactionArgs) data() []byte {
return nil return nil
} }
// setDefaultConfig defines the options for deriving missing fields of transactions. // sidecarConfig defines the options for deriving missing fields of transactions.
type setDefaultConfig struct { type sidecarConfig struct {
// This configures whether blobs are allowed to be passed and // This configures whether blobs are allowed to be passed and
// the associated sidecar version should be attached. // the associated sidecar version should be attached.
blobSidecarAllowed bool blobSidecarAllowed bool
blobSidecarVersion byte 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. // 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 { if err := args.setBlobTxSidecar(ctx, config); err != nil {
return err return err
} }
@ -146,13 +141,6 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config
} }
if args.Gas == nil { 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 // These fields are immutable during the estimation, safe to
// pass the pointer directly. // pass the pointer directly.
data := args.data() data := args.data()
@ -176,7 +164,6 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config
args.Gas = &estimated args.Gas = &estimated
log.Trace("Estimate gas usage automatically", "gas", args.Gas) 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 // If chain id is provided, ensure it matches the local chain id. Otherwise, set the local
// chain id as the default. // chain id as the default.
@ -292,7 +279,7 @@ func (args *TransactionArgs) setLondonFeeDefaults(ctx context.Context, head *typ
} }
// setBlobTxSidecar adds the blob tx // 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. // No blobs, we're done.
if args.Blobs == nil { if args.Blobs == nil {
return 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`) 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) n := len(args.Blobs)
if args.BlobHashes != nil && len(args.BlobHashes) != n { 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) return fmt.Errorf("number of blobs and hashes mismatch (have=%d, want=%d)", len(args.BlobHashes), n)