internal/ethapi: move check to ToTransaction

This commit is contained in:
MariusVanDerWijden 2026-05-27 11:07:09 +02:00
parent 0268f2e03d
commit 5966f20f8a
No known key found for this signature in database
4 changed files with 34 additions and 54 deletions

View file

@ -986,7 +986,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
} }
var ( var (
msg = args.ToMessage(blockContext.BaseFee, true) msg = args.ToMessage(blockContext.BaseFee, true)
tx = args.ToTransaction(types.DynamicFeeTxType) tx, _ = args.ToTransaction(types.DynamicFeeTxType)
traceConfig *TraceConfig traceConfig *TraceConfig
) )
// Lower the basefee to 0 to avoid breaking EVM // Lower the basefee to 0 to avoid breaking EVM

View file

@ -1371,7 +1371,12 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
} }
res, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit)) res, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
if err != nil { if err != nil {
return nil, 0, nil, fmt.Errorf("failed to apply transaction: %v err: %v", args.ToTransaction(types.LegacyTxType).Hash(), err) var hash common.Hash
tx, err := args.ToTransaction(types.LegacyTxType)
if err == nil {
hash = tx.Hash()
}
return nil, 0, nil, fmt.Errorf("failed to apply transaction: %v err: %v", hash, err)
} }
if tracer.Equal(prevTracer) { if tracer.Equal(prevTracer) {
return accessList, res.UsedGas, res.Err, nil return accessList, res.UsedGas, res.Err, nil
@ -1642,7 +1647,10 @@ func (api *TransactionAPI) SendTransaction(ctx context.Context, args Transaction
return common.Hash{}, err return common.Hash{}, err
} }
// Assemble the transaction and sign with the wallet // Assemble the transaction and sign with the wallet
tx := args.ToTransaction(types.DynamicFeeTxType) tx, err := args.ToTransaction(types.DynamicFeeTxType)
if err != nil {
return common.Hash{}, err
}
signed, err := wallet.SignTx(account, tx, api.b.ChainConfig().ChainID) signed, err := wallet.SignTx(account, tx, api.b.ChainConfig().ChainID)
if err != nil { if err != nil {
@ -1664,7 +1672,10 @@ func (api *TransactionAPI) FillTransaction(ctx context.Context, args Transaction
return nil, err return nil, err
} }
// Assemble the transaction and obtain rlp // Assemble the transaction and obtain rlp
tx := args.ToTransaction(types.DynamicFeeTxType) tx, err := args.ToTransaction(types.DynamicFeeTxType)
if err != nil {
return nil, err
}
data, err := tx.MarshalBinary() data, err := tx.MarshalBinary()
if err != nil { if err != nil {
return nil, err return nil, err
@ -1860,7 +1871,10 @@ func (api *TransactionAPI) SignTransaction(ctx context.Context, args Transaction
return nil, err return nil, err
} }
// Before actually sign the transaction, ensure the transaction fee is reasonable. // Before actually sign the transaction, ensure the transaction fee is reasonable.
tx := args.ToTransaction(types.DynamicFeeTxType) tx, err := args.ToTransaction(types.DynamicFeeTxType)
if err != nil {
return nil, err
}
if err := checkTxFee(tx.GasPrice(), tx.Gas(), api.b.RPCTxFeeCap()); err != nil { if err := checkTxFee(tx.GasPrice(), tx.Gas(), api.b.RPCTxFeeCap()); err != nil {
return nil, err return nil, err
} }
@ -1916,7 +1930,10 @@ func (api *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs,
if err := sendArgs.setDefaults(ctx, api.b, sidecarConfig{}); err != nil { if err := sendArgs.setDefaults(ctx, api.b, sidecarConfig{}); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
matchTx := sendArgs.ToTransaction(types.DynamicFeeTxType) matchTx, err := sendArgs.ToTransaction(types.DynamicFeeTxType)
if err != nil {
return common.Hash{}, err
}
// Before replacing the old transaction, ensure the _new_ transaction fee is reasonable. // Before replacing the old transaction, ensure the _new_ transaction fee is reasonable.
price := matchTx.GasPrice() price := matchTx.GasPrice()
@ -1946,7 +1963,11 @@ func (api *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs,
if gasLimit != nil && *gasLimit != 0 { if gasLimit != nil && *gasLimit != 0 {
sendArgs.Gas = gasLimit sendArgs.Gas = gasLimit
} }
signedTx, err := api.sign(sendArgs.from(), sendArgs.ToTransaction(types.DynamicFeeTxType)) tx, err := sendArgs.ToTransaction(types.DynamicFeeTxType)
if err != nil {
return common.Hash{}, err
}
signedTx, err := api.sign(sendArgs.from(), tx)
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
} }

View file

@ -279,7 +279,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
return nil, nil, nil, err return nil, nil, nil, err
} }
var ( var (
tx = call.ToTransaction(types.DynamicFeeTxType) tx, _ = call.ToTransaction(types.DynamicFeeTxType)
txHash = tx.Hash() txHash = tx.Hash()
) )
txes[i] = tx txes[i] = tx

View file

@ -74,24 +74,6 @@ type TransactionArgs struct {
AuthorizationList []types.SetCodeAuthorization `json:"authorizationList"` AuthorizationList []types.SetCodeAuthorization `json:"authorizationList"`
} }
func (args *TransactionArgs) inferTxType(defaultType int) int {
usedType := types.LegacyTxType
switch {
case args.AuthorizationList != nil || defaultType == types.SetCodeTxType:
usedType = types.SetCodeTxType
case args.BlobHashes != nil || defaultType == types.BlobTxType:
usedType = types.BlobTxType
case args.MaxFeePerGas != nil || defaultType == types.DynamicFeeTxType:
usedType = types.DynamicFeeTxType
case args.AccessList != nil || defaultType == types.AccessListTxType:
usedType = types.AccessListTxType
}
if args.GasPrice != nil {
usedType = types.LegacyTxType
}
return usedType
}
// from retrieves the transaction sender address. // from retrieves the transaction sender address.
func (args *TransactionArgs) from() common.Address { func (args *TransactionArgs) from() common.Address {
if args.From == nil { if args.From == nil {
@ -196,32 +178,6 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, config
} else { } else {
args.ChainID = (*hexutil.Big)(want) args.ChainID = (*hexutil.Big)(want)
} }
// Validate explicit transaction type if provided
if args.Type != nil {
requested := int(*args.Type)
// Validate supported types
switch requested {
case types.LegacyTxType,
types.AccessListTxType,
types.DynamicFeeTxType,
types.BlobTxType,
types.SetCodeTxType:
// ok
default:
return fmt.Errorf("unsupported transaction type: %d", requested)
}
inferred := args.inferTxType(types.LegacyTxType)
if requested != inferred {
return fmt.Errorf(
"transaction type mismatch (requested=%d inferred=%d)",
requested, inferred,
)
}
}
return nil return nil
} }
@ -544,7 +500,7 @@ func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck bool) *c
// ToTransaction converts the arguments to a transaction. // ToTransaction converts the arguments to a transaction.
// This assumes that setDefaults has been called. // This assumes that setDefaults has been called.
func (args *TransactionArgs) ToTransaction(defaultType int) *types.Transaction { func (args *TransactionArgs) ToTransaction(defaultType int) (*types.Transaction, error) {
usedType := types.LegacyTxType usedType := types.LegacyTxType
switch { switch {
case args.AuthorizationList != nil || defaultType == types.SetCodeTxType: case args.AuthorizationList != nil || defaultType == types.SetCodeTxType:
@ -560,6 +516,9 @@ func (args *TransactionArgs) ToTransaction(defaultType int) *types.Transaction {
if args.GasPrice != nil { if args.GasPrice != nil {
usedType = types.LegacyTxType usedType = types.LegacyTxType
} }
if usedType != int(*args.Type) {
return nil, fmt.Errorf("wrong tx type used, requested: %v, used: %v", args.Type, usedType)
}
var data types.TxData var data types.TxData
switch usedType { switch usedType {
case types.SetCodeTxType: case types.SetCodeTxType:
@ -649,7 +608,7 @@ func (args *TransactionArgs) ToTransaction(defaultType int) *types.Transaction {
Data: args.data(), Data: args.data(),
} }
} }
return types.NewTx(data) return types.NewTx(data), nil
} }
// IsEIP4844 returns an indicator if the args contains EIP4844 fields. // IsEIP4844 returns an indicator if the args contains EIP4844 fields.