github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/internal/ethapi/transaction_args.go (about) 1 // Copyright 2021 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ethapi 18 19 import ( 20 "bytes" 21 "context" 22 "errors" 23 "fmt" 24 "math/big" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/common/hexutil" 28 "github.com/ethereum/go-ethereum/common/math" 29 "github.com/ethereum/go-ethereum/core" 30 "github.com/ethereum/go-ethereum/core/state" 31 "github.com/ethereum/go-ethereum/core/types" 32 "github.com/ethereum/go-ethereum/log" 33 "github.com/ethereum/go-ethereum/rpc" 34 ) 35 36 // TransactionArgs represents the arguments to construct a new transaction 37 // or a message call. 38 type TransactionArgs struct { 39 From *common.Address `json:"from"` 40 To *common.Address `json:"to"` 41 Gas *hexutil.Uint64 `json:"gas"` 42 GasPrice *hexutil.Big `json:"gasPrice"` 43 MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"` 44 MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"` 45 Value *hexutil.Big `json:"value"` 46 Nonce *hexutil.Uint64 `json:"nonce"` 47 48 // We accept "data" and "input" for backwards-compatibility reasons. 49 // "input" is the newer name and should be preferred by clients. 50 // Issue detail: https://github.com/ethereum/go-ethereum/issues/15628 51 Data *hexutil.Bytes `json:"data"` 52 Input *hexutil.Bytes `json:"input"` 53 54 // Introduced by AccessListTxType transaction. 55 AccessList *types.AccessList `json:"accessList,omitempty"` 56 ChainID *hexutil.Big `json:"chainId,omitempty"` 57 } 58 59 // from retrieves the transaction sender address. 60 func (args *TransactionArgs) from() common.Address { 61 if args.From == nil { 62 return common.Address{} 63 } 64 return *args.From 65 } 66 67 // data retrieves the transaction calldata. Input field is preferred. 68 func (args *TransactionArgs) data() []byte { 69 if args.Input != nil { 70 return *args.Input 71 } 72 if args.Data != nil { 73 return *args.Data 74 } 75 return nil 76 } 77 78 // setDefaults fills in default values for unspecified tx fields. 79 func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error { 80 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 81 return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 82 } 83 // After london, default to 1559 unless gasPrice is set 84 head := b.CurrentHeader() 85 // If user specifies both maxPriorityfee and maxFee, then we do not 86 // need to consult the chain for defaults. It's definitely a London tx. 87 if args.MaxPriorityFeePerGas == nil || args.MaxFeePerGas == nil { 88 // In this clause, user left some fields unspecified. 89 if b.ChainConfig().IsLondon(head.Number) && args.GasPrice == nil { 90 if args.MaxPriorityFeePerGas == nil { 91 tip, err := b.SuggestGasTipCap(ctx) 92 if err != nil { 93 return err 94 } 95 args.MaxPriorityFeePerGas = (*hexutil.Big)(tip) 96 } 97 if args.MaxFeePerGas == nil { 98 gasFeeCap := new(big.Int).Add( 99 (*big.Int)(args.MaxPriorityFeePerGas), 100 new(big.Int).Mul(head.BaseFee, big.NewInt(2)), 101 ) 102 args.MaxFeePerGas = (*hexutil.Big)(gasFeeCap) 103 } 104 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 105 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 106 } 107 } else { 108 if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { 109 return errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet") 110 } 111 if args.GasPrice == nil { 112 price, err := b.SuggestGasTipCap(ctx) 113 if err != nil { 114 return err 115 } 116 if b.ChainConfig().IsLondon(head.Number) { 117 // The legacy tx gas price suggestion should not add 2x base fee 118 // because all fees are consumed, so it would result in a spiral 119 // upwards. 120 price.Add(price, head.BaseFee) 121 } 122 args.GasPrice = (*hexutil.Big)(price) 123 } 124 } 125 } else { 126 // Both maxPriorityfee and maxFee set by caller. Sanity-check their internal relation 127 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 128 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 129 } 130 } 131 if args.Value == nil { 132 args.Value = new(hexutil.Big) 133 } 134 if args.Nonce == nil { 135 nonce, err := b.GetPoolNonce(ctx, args.from()) 136 if err != nil { 137 return err 138 } 139 args.Nonce = (*hexutil.Uint64)(&nonce) 140 } 141 if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { 142 return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`) 143 } 144 if args.To == nil && len(args.data()) == 0 { 145 return errors.New(`contract creation without any data provided`) 146 } 147 // Estimate the gas usage if necessary. 148 if args.Gas == nil { 149 // These fields are immutable during the estimation, safe to 150 // pass the pointer directly. 151 data := args.data() 152 callArgs := TransactionArgs{ 153 From: args.From, 154 To: args.To, 155 GasPrice: args.GasPrice, 156 MaxFeePerGas: args.MaxFeePerGas, 157 MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, 158 Value: args.Value, 159 Data: (*hexutil.Bytes)(&data), 160 AccessList: args.AccessList, 161 } 162 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 163 estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) 164 if err != nil { 165 return err 166 } 167 args.Gas = &estimated 168 log.Trace("Estimate gas usage automatically", "gas", args.Gas) 169 } 170 // If chain id is provided, ensure it matches the local chain id. Otherwise, set the local 171 // chain id as the default. 172 want := b.ChainConfig().ChainID 173 if args.ChainID != nil { 174 if have := (*big.Int)(args.ChainID); have.Cmp(want) != 0 { 175 return fmt.Errorf("chainId does not match node's (have=%v, want=%v)", have, want) 176 } 177 } else { 178 args.ChainID = (*hexutil.Big)(want) 179 } 180 return nil 181 } 182 183 // ToMessage converts the transaction arguments to the Message type used by the 184 // core evm. This method is used in calls and traces that do not require a real 185 // live transaction. 186 func (args *TransactionArgs) ToMessage(globalGasCap uint64, header *types.Header, state *state.StateDB) (types.Message, error) { 187 baseFee := header.BaseFee 188 189 // Reject invalid combinations of pre- and post-1559 fee styles 190 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 191 return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 192 } 193 // Set sender address or use zero address if none specified. 194 addr := args.from() 195 196 // Set default gas & gas price if none were set 197 gas := globalGasCap 198 if gas == 0 { 199 gas = uint64(math.MaxUint64 / 2) 200 } 201 if args.Gas != nil { 202 gas = uint64(*args.Gas) 203 } 204 if globalGasCap != 0 && globalGasCap < gas { 205 log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 206 gas = globalGasCap 207 } 208 var ( 209 gasPrice *big.Int 210 gasFeeCap *big.Int 211 gasTipCap *big.Int 212 ) 213 if baseFee == nil { 214 // If there's no basefee, then it must be a non-1559 execution 215 gasPrice = new(big.Int) 216 if args.GasPrice != nil { 217 gasPrice = args.GasPrice.ToInt() 218 } 219 gasFeeCap, gasTipCap = gasPrice, gasPrice 220 } else { 221 // A basefee is provided, necessitating 1559-type execution 222 if args.GasPrice != nil { 223 // User specified the legacy gas field, convert to 1559 gas typing 224 gasPrice = args.GasPrice.ToInt() 225 gasFeeCap, gasTipCap = gasPrice, gasPrice 226 } else { 227 // User specified 1559 gas feilds (or none), use those 228 gasFeeCap = new(big.Int) 229 if args.MaxFeePerGas != nil { 230 gasFeeCap = args.MaxFeePerGas.ToInt() 231 } 232 gasTipCap = new(big.Int) 233 if args.MaxPriorityFeePerGas != nil { 234 gasTipCap = args.MaxPriorityFeePerGas.ToInt() 235 } 236 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes 237 gasPrice = new(big.Int) 238 if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 239 gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) 240 } 241 } 242 } 243 value := new(big.Int) 244 if args.Value != nil { 245 value = args.Value.ToInt() 246 } 247 data := args.data() 248 var accessList types.AccessList 249 if args.AccessList != nil { 250 accessList = *args.AccessList 251 } 252 msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true) 253 254 // Arbitrum: raise the gas cap to ignore L1 costs so that it's compute-only 255 if core.InterceptRPCGasCap != nil && state != nil { 256 // ToMessage recurses once to allow ArbOS to intercept the result for all callers 257 // ArbOS uses this to modify globalGasCap so that the cap will ignore this tx's specific L1 data costs 258 core.InterceptRPCGasCap(&globalGasCap, msg, header, state) 259 return args.ToMessage(globalGasCap, header, nil) // we pass a nil to avoid another recursion 260 } 261 return msg, nil 262 } 263 264 // Raises the vanilla gas cap by the tx's l1 data costs in l2 terms. This creates a new gas cap that after 265 // data payments are made, equals the original vanilla cap for the remaining, L2-specific work the tx does. 266 func (args *TransactionArgs) L2OnlyGasCap(gasCap uint64, header *types.Header, state *state.StateDB) (uint64, error) { 267 msg, err := args.ToMessage(gasCap, header, nil) 268 if err != nil { 269 return 0, err 270 } 271 core.InterceptRPCGasCap(&gasCap, msg, header, state) 272 return gasCap, nil 273 } 274 275 // toTransaction converts the arguments to a transaction. 276 // This assumes that setDefaults has been called. 277 func (args *TransactionArgs) toTransaction() *types.Transaction { 278 var data types.TxData 279 switch { 280 case args.MaxFeePerGas != nil: 281 al := types.AccessList{} 282 if args.AccessList != nil { 283 al = *args.AccessList 284 } 285 data = &types.DynamicFeeTx{ 286 To: args.To, 287 ChainID: (*big.Int)(args.ChainID), 288 Nonce: uint64(*args.Nonce), 289 Gas: uint64(*args.Gas), 290 GasFeeCap: (*big.Int)(args.MaxFeePerGas), 291 GasTipCap: (*big.Int)(args.MaxPriorityFeePerGas), 292 Value: (*big.Int)(args.Value), 293 Data: args.data(), 294 AccessList: al, 295 } 296 case args.AccessList != nil: 297 data = &types.AccessListTx{ 298 To: args.To, 299 ChainID: (*big.Int)(args.ChainID), 300 Nonce: uint64(*args.Nonce), 301 Gas: uint64(*args.Gas), 302 GasPrice: (*big.Int)(args.GasPrice), 303 Value: (*big.Int)(args.Value), 304 Data: args.data(), 305 AccessList: *args.AccessList, 306 } 307 default: 308 data = &types.LegacyTx{ 309 To: args.To, 310 Nonce: uint64(*args.Nonce), 311 Gas: uint64(*args.Gas), 312 GasPrice: (*big.Int)(args.GasPrice), 313 Value: (*big.Int)(args.Value), 314 Data: args.data(), 315 } 316 } 317 return types.NewTx(data) 318 } 319 320 // ToTransaction converts the arguments to a transaction. 321 // This assumes that setDefaults has been called. 322 func (args *TransactionArgs) ToTransaction() *types.Transaction { 323 return args.toTransaction() 324 }