Skip to content

Commit 38d1d12

Browse files
internal/ethapi: minor cleanups
1 parent 76e3a36 commit 38d1d12

File tree

2 files changed

+33
-54
lines changed

2 files changed

+33
-54
lines changed

internal/ethapi/api.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,10 +1501,7 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction
15011501
}
15021502

15031503
// Set some sanity defaults and terminate on failure
1504-
config := setDefaultConfig{
1505-
skipGasEstimation: false,
1506-
}
1507-
if err := args.setDefaults(ctx, api.b, config); err != nil {
1504+
if err := args.setDefaults(ctx, api.b, sidecarConfig{}); err != nil {
15081505
return common.Hash{}, err
15091506
}
15101507
// Assemble the transaction and sign with the wallet
@@ -1522,8 +1519,7 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction
15221519
// processing (signing + broadcast).
15231520
func (api *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
15241521
// Set some sanity defaults and terminate on failure
1525-
config := setDefaultConfig{
1526-
skipGasEstimation: false,
1522+
config := sidecarConfig{
15271523
blobSidecarAllowed: true,
15281524
blobSidecarVersion: types.BlobSidecarVersion0,
15291525
}
@@ -1600,10 +1596,6 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction
16001596
if args.Nonce == nil {
16011597
return nil, errors.New("nonce not specified")
16021598
}
1603-
config := setDefaultConfig{
1604-
skipGasEstimation: false,
1605-
blobSidecarAllowed: true,
1606-
}
16071599
sidecarVersion := types.BlobSidecarVersion0
16081600
if len(args.Blobs) > 0 {
16091601
chainHead := api.b.CurrentHeader()
@@ -1612,8 +1604,11 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction
16121604
sidecarVersion = types.BlobSidecarVersion1
16131605
}
16141606
}
1615-
config.blobSidecarVersion = sidecarVersion
16161607

1608+
config := sidecarConfig{
1609+
blobSidecarAllowed: true,
1610+
blobSidecarVersion: sidecarVersion,
1611+
}
16171612
if err := args.setDefaults(ctx, api.b, config); err != nil {
16181613
return nil, err
16191614
}
@@ -1671,10 +1666,7 @@ func (api *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs,
16711666
if sendArgs.Nonce == nil {
16721667
return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
16731668
}
1674-
config := setDefaultConfig{
1675-
skipGasEstimation: false,
1676-
}
1677-
if err := sendArgs.setDefaults(ctx, api.b, config); err != nil {
1669+
if err := sendArgs.setDefaults(ctx, api.b, sidecarConfig{}); err != nil {
16781670
return common.Hash{}, err
16791671
}
16801672
matchTx := sendArgs.ToTransaction(types.LegacyTxType)

internal/ethapi/transaction_args.go

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,16 @@ func (args *TransactionArgs) data() []byte {
9191
return nil
9292
}
9393

94-
// setDefaultConfig defines the options for deriving missing fields of transactions.
95-
type setDefaultConfig struct {
94+
// sidecarConfig defines the options for deriving missing fields of transactions.
95+
type sidecarConfig struct {
9696
// This configures whether blobs are allowed to be passed and
9797
// the associated sidecar version should be attached.
9898
blobSidecarAllowed bool
9999
blobSidecarVersion byte
100-
101-
// skipGasEstimation is the flag whether the gas estimation is skipped.
102-
// this flag should only be used in the scenarios that a precise gas limit
103-
// is not critical, e.g., in non-transaction calls.
104-
skipGasEstimation bool
105100
}
106101

107102
// setDefaults fills in default values for unspecified tx fields.
108-
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config setDefaultConfig) error {
103+
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config sidecarConfig) error {
109104
if err := args.setBlobTxSidecar(ctx, config); err != nil {
110105
return err
111106
}
@@ -146,36 +141,28 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config
146141
}
147142

148143
if args.Gas == nil {
149-
if config.skipGasEstimation {
150-
gas := hexutil.Uint64(b.RPCGasCap())
151-
if gas == 0 {
152-
gas = hexutil.Uint64(math.MaxUint64 / 2)
153-
}
154-
args.Gas = &gas
155-
} else {
156-
// These fields are immutable during the estimation, safe to
157-
// pass the pointer directly.
158-
data := args.data()
159-
callArgs := TransactionArgs{
160-
From: args.From,
161-
To: args.To,
162-
GasPrice: args.GasPrice,
163-
MaxFeePerGas: args.MaxFeePerGas,
164-
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
165-
Value: args.Value,
166-
Data: (*hexutil.Bytes)(&data),
167-
AccessList: args.AccessList,
168-
BlobFeeCap: args.BlobFeeCap,
169-
BlobHashes: args.BlobHashes,
170-
}
171-
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
172-
estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, nil, b.RPCGasCap())
173-
if err != nil {
174-
return err
175-
}
176-
args.Gas = &estimated
177-
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
144+
// These fields are immutable during the estimation, safe to
145+
// pass the pointer directly.
146+
data := args.data()
147+
callArgs := TransactionArgs{
148+
From: args.From,
149+
To: args.To,
150+
GasPrice: args.GasPrice,
151+
MaxFeePerGas: args.MaxFeePerGas,
152+
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
153+
Value: args.Value,
154+
Data: (*hexutil.Bytes)(&data),
155+
AccessList: args.AccessList,
156+
BlobFeeCap: args.BlobFeeCap,
157+
BlobHashes: args.BlobHashes,
158+
}
159+
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
160+
estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, nil, b.RPCGasCap())
161+
if err != nil {
162+
return err
178163
}
164+
args.Gas = &estimated
165+
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
179166
}
180167

181168
// 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
292279
}
293280

294281
// setBlobTxSidecar adds the blob tx
295-
func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config setDefaultConfig) error {
282+
func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config sidecarConfig) error {
296283
// No blobs, we're done.
297284
if args.Blobs == nil {
298285
return nil
@@ -311,7 +298,7 @@ func (args *TransactionArgs) setBlobTxSidecar(ctx context.Context, config setDef
311298
return errors.New(`blob commitments provided while proofs were not`)
312299
}
313300

314-
// len(blobs) == len(commitments) == len(proofs) == len(hashes)
301+
// len(blobs) == len(commitments) == len(hashes)
315302
n := len(args.Blobs)
316303
if args.BlobHashes != nil && len(args.BlobHashes) != n {
317304
return fmt.Errorf("number of blobs and hashes mismatch (have=%d, want=%d)", len(args.BlobHashes), n)

0 commit comments

Comments
 (0)