2023-01-04 21:43:24 +00:00
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
2023-11-01 19:56:14 +00:00
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
|
2023-01-04 21:43:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-11-01 19:56:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2023-01-04 21:43:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-11-01 19:56:14 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-01-04 21:43:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TransactionArgs represents the arguments to construct a new transaction
|
|
|
|
// or a message call.
|
|
|
|
type TransactionArgs struct {
|
|
|
|
From *common.Address `json:"from"`
|
|
|
|
To *common.Address `json:"to"`
|
|
|
|
Gas *hexutil.Uint64 `json:"gas"`
|
|
|
|
GasPrice *hexutil.Big `json:"gasPrice"`
|
|
|
|
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
|
|
|
|
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
|
|
|
|
Value *hexutil.Big `json:"value"`
|
|
|
|
Nonce *hexutil.Uint64 `json:"nonce"`
|
|
|
|
|
|
|
|
// We accept "data" and "input" for backwards-compatibility reasons.
|
|
|
|
// "input" is the newer name and should be preferred by clients.
|
|
|
|
// Issue detail: https://github.com/ethereum/go-ethereum/issues/15628
|
|
|
|
Data *hexutil.Bytes `json:"data"`
|
|
|
|
Input *hexutil.Bytes `json:"input"`
|
|
|
|
|
|
|
|
// Introduced by AccessListTxType transaction.
|
|
|
|
AccessList *types.AccessList `json:"accessList,omitempty"`
|
|
|
|
ChainID *hexutil.Big `json:"chainId,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// from retrieves the transaction sender address.
|
|
|
|
func (args *TransactionArgs) from() common.Address {
|
|
|
|
if args.From == nil {
|
|
|
|
return common.Address{}
|
|
|
|
}
|
|
|
|
return *args.From
|
|
|
|
}
|
|
|
|
|
|
|
|
// data retrieves the transaction calldata. Input field is preferred.
|
|
|
|
func (args *TransactionArgs) data() []byte {
|
|
|
|
if args.Input != nil {
|
|
|
|
return *args.Input
|
|
|
|
}
|
|
|
|
if args.Data != nil {
|
|
|
|
return *args.Data
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-01 19:56:14 +00:00
|
|
|
|
|
|
|
// ToMessage converts the transaction arguments to the Message type used by the
|
|
|
|
// core evm. This method is used in calls and traces that do not require a real
|
|
|
|
// live transaction.
|
|
|
|
func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (*core.Message, error) {
|
|
|
|
// Reject invalid combinations of pre- and post-1559 fee styles
|
|
|
|
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
|
|
|
|
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
|
|
|
|
}
|
|
|
|
// Set sender address or use zero address if none specified.
|
|
|
|
addr := args.from()
|
|
|
|
|
|
|
|
// Set default gas & gas price if none were set
|
|
|
|
gas := globalGasCap
|
|
|
|
if gas == 0 {
|
|
|
|
gas = uint64(math.MaxUint64 / 2)
|
|
|
|
}
|
|
|
|
if args.Gas != nil {
|
|
|
|
gas = uint64(*args.Gas)
|
|
|
|
}
|
|
|
|
if globalGasCap != 0 && globalGasCap < gas {
|
|
|
|
logrus.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap)
|
|
|
|
gas = globalGasCap
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
gasPrice *big.Int
|
|
|
|
gasFeeCap *big.Int
|
|
|
|
gasTipCap *big.Int
|
|
|
|
)
|
|
|
|
if baseFee == nil {
|
|
|
|
// If there's no basefee, then it must be a non-1559 execution
|
|
|
|
gasPrice = new(big.Int)
|
|
|
|
if args.GasPrice != nil {
|
|
|
|
gasPrice = args.GasPrice.ToInt()
|
|
|
|
}
|
|
|
|
gasFeeCap, gasTipCap = gasPrice, gasPrice
|
|
|
|
} else {
|
|
|
|
// A basefee is provided, necessitating 1559-type execution
|
|
|
|
if args.GasPrice != nil {
|
|
|
|
// User specified the legacy gas field, convert to 1559 gas typing
|
|
|
|
gasPrice = args.GasPrice.ToInt()
|
|
|
|
gasFeeCap, gasTipCap = gasPrice, gasPrice
|
|
|
|
} else {
|
|
|
|
// User specified 1559 gas fields (or none), use those
|
|
|
|
gasFeeCap = new(big.Int)
|
|
|
|
if args.MaxFeePerGas != nil {
|
|
|
|
gasFeeCap = args.MaxFeePerGas.ToInt()
|
|
|
|
}
|
|
|
|
gasTipCap = new(big.Int)
|
|
|
|
if args.MaxPriorityFeePerGas != nil {
|
|
|
|
gasTipCap = args.MaxPriorityFeePerGas.ToInt()
|
|
|
|
}
|
|
|
|
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
|
|
|
|
gasPrice = new(big.Int)
|
|
|
|
if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 {
|
|
|
|
gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value := new(big.Int)
|
|
|
|
if args.Value != nil {
|
|
|
|
value = args.Value.ToInt()
|
|
|
|
}
|
|
|
|
data := args.data()
|
|
|
|
var accessList types.AccessList
|
|
|
|
if args.AccessList != nil {
|
|
|
|
accessList = *args.AccessList
|
|
|
|
}
|
|
|
|
msg := &core.Message{
|
|
|
|
From: addr,
|
|
|
|
To: args.To,
|
|
|
|
Value: value,
|
|
|
|
GasLimit: gas,
|
|
|
|
GasPrice: gasPrice,
|
|
|
|
GasFeeCap: gasFeeCap,
|
|
|
|
GasTipCap: gasTipCap,
|
|
|
|
Data: data,
|
|
|
|
AccessList: accessList,
|
|
|
|
SkipAccountChecks: true,
|
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|