add a bool flag to bypass gas estimation in setDefaults

This commit is contained in:
colinlyguo 2024-01-29 11:52:15 +08:00
parent 76cad9bae5
commit 942453152e
No known key found for this signature in database
GPG key ID: 82E123BE1B68288B
2 changed files with 13 additions and 17 deletions

View file

@ -451,7 +451,7 @@ func (s *PersonalAccountAPI) signTransaction(ctx context.Context, args *Transact
return nil, err
}
// Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil {
if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err
}
// Assemble the transaction and sign with the wallet
@ -1511,19 +1511,9 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
if db == nil || err != nil {
return nil, 0, nil, err
}
// If the gas amount is not set, default to RPC gas cap.
if args.Gas == nil {
if b.RPCGasCap() != 0 {
tmp := hexutil.Uint64(b.RPCGasCap())
args.Gas = &tmp
} else {
tmp := hexutil.Uint64(math.MaxUint64 / 2)
args.Gas = &tmp
}
}
// Ensure any missing fields are filled, extract the recipient and input data
if err := args.setDefaults(ctx, b); err != nil {
if err := args.setDefaults(ctx, b, true); err != nil {
return nil, 0, nil, err
}
var to common.Address
@ -1828,7 +1818,7 @@ func (s *TransactionAPI) SendTransaction(ctx context.Context, args TransactionAr
}
// Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil {
if err := args.setDefaults(ctx, s.b, false); err != nil {
return common.Hash{}, err
}
// Assemble the transaction and sign with the wallet
@ -1846,7 +1836,7 @@ func (s *TransactionAPI) SendTransaction(ctx context.Context, args TransactionAr
// processing (signing + broadcast).
func (s *TransactionAPI) FillTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
// Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil {
if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err
}
// Assemble the transaction and obtain rlp
@ -1916,7 +1906,7 @@ func (s *TransactionAPI) SignTransaction(ctx context.Context, args TransactionAr
if args.Nonce == nil {
return nil, errors.New("nonce not specified")
}
if err := args.setDefaults(ctx, s.b); err != nil {
if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err
}
// Before actually sign the transaction, ensure the transaction fee is reasonable.
@ -1965,7 +1955,7 @@ func (s *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs, g
if sendArgs.Nonce == nil {
return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
}
if err := sendArgs.setDefaults(ctx, s.b); err != nil {
if err := sendArgs.setDefaults(ctx, s.b, false); err != nil {
return common.Hash{}, err
}
matchTx := sendArgs.toTransaction()

View file

@ -81,7 +81,7 @@ func (args *TransactionArgs) data() []byte {
}
// setDefaults fills in default values for unspecified tx fields.
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, infiniteGas bool) error {
if err := args.setFeeDefaults(ctx, b); err != nil {
return err
}
@ -107,6 +107,12 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
if args.To == nil && len(args.data()) == 0 {
return errors.New(`contract creation without any data provided`)
}
// Assign a very high gas limit for cases where an accurate gas limit is not critical,
// but need to ensure that gas is sufficient.
if infiniteGas {
tmp := hexutil.Uint64(math.MaxUint64 / 2)
args.Gas = &tmp
}
// Estimate the gas usage if necessary.
if args.Gas == nil {
// These fields are immutable during the estimation, safe to