mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 21:56:43 +00:00
internal/ethapi: minor cleanups
This commit is contained in:
parent
76e3a3686f
commit
38d1d128d9
2 changed files with 33 additions and 54 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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,36 +141,28 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.Gas == nil {
|
if args.Gas == nil {
|
||||||
if config.skipGasEstimation {
|
// These fields are immutable during the estimation, safe to
|
||||||
gas := hexutil.Uint64(b.RPCGasCap())
|
// pass the pointer directly.
|
||||||
if gas == 0 {
|
data := args.data()
|
||||||
gas = hexutil.Uint64(math.MaxUint64 / 2)
|
callArgs := TransactionArgs{
|
||||||
}
|
From: args.From,
|
||||||
args.Gas = &gas
|
To: args.To,
|
||||||
} else {
|
GasPrice: args.GasPrice,
|
||||||
// These fields are immutable during the estimation, safe to
|
MaxFeePerGas: args.MaxFeePerGas,
|
||||||
// pass the pointer directly.
|
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
|
||||||
data := args.data()
|
Value: args.Value,
|
||||||
callArgs := TransactionArgs{
|
Data: (*hexutil.Bytes)(&data),
|
||||||
From: args.From,
|
AccessList: args.AccessList,
|
||||||
To: args.To,
|
BlobFeeCap: args.BlobFeeCap,
|
||||||
GasPrice: args.GasPrice,
|
BlobHashes: args.BlobHashes,
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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
|
// 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
|
// 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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue