github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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/types" 30 "github.com/ethereum/go-ethereum/log" 31 "github.com/ethereum/go-ethereum/rpc" 32 ) 33 34 // TransactionArgs represents the arguments to construct a new transaction 35 // or a message call. 36 type TransactionArgs struct { 37 From *common.Address `json:"from"` 38 To *common.Address `json:"to"` 39 Gas *hexutil.Uint64 `json:"gas"` 40 GasPrice *hexutil.Big `json:"gasPrice"` 41 MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"` 42 MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"` 43 Value *hexutil.Big `json:"value"` 44 Nonce *hexutil.Uint64 `json:"nonce"` 45 46 // We accept "data" and "input" for backwards-compatibility reasons. 47 // "input" is the newer name and should be preferred by clients. 48 // Issue detail: https://github.com/ethereum/go-ethereum/issues/15628 49 Data *hexutil.Bytes `json:"data"` 50 Input *hexutil.Bytes `json:"input"` 51 52 // Introduced by AccessListTxType transaction. 53 AccessList *types.AccessList `json:"accessList,omitempty"` 54 ChainID *hexutil.Big `json:"chainId,omitempty"` 55 } 56 57 // from retrieves the transaction sender address. 58 func (args *TransactionArgs) from() common.Address { 59 if args.From == nil { 60 return common.Address{} 61 } 62 63 return *args.From 64 } 65 66 // data retrieves the transaction calldata. Input field is preferred. 67 func (args *TransactionArgs) data() []byte { 68 if args.Input != nil { 69 return *args.Input 70 } 71 72 if args.Data != nil { 73 return *args.Data 74 } 75 76 return nil 77 } 78 79 // setDefaults fills in default values for unspecified tx fields. 80 func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error { 81 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 82 return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 83 } 84 85 // After london, default to 1559 unless gasPrice is set 86 head := b.CurrentHeader() 87 88 // If user specifies both maxPriorityfee and maxFee, then we do not 89 // need to consult the chain for defaults. It's definitely a London tx. 90 if args.MaxPriorityFeePerGas == nil || args.MaxFeePerGas == nil { 91 // In this clause, user left some fields unspecified. 92 if b.ChainConfig().IsLondon(head.Number) && args.GasPrice == nil { 93 if args.MaxPriorityFeePerGas == nil { 94 tip, err := b.SuggestGasTipCap(ctx) 95 if err != nil { 96 return err 97 } 98 99 args.MaxPriorityFeePerGas = (*hexutil.Big)(tip) 100 } 101 if args.MaxFeePerGas == nil { 102 gasFeeCap := new(big.Int).Add( 103 (*big.Int)(args.MaxPriorityFeePerGas), 104 new(big.Int).Mul(head.BaseFee, big.NewInt(2)), 105 ) 106 107 args.MaxFeePerGas = (*hexutil.Big)(gasFeeCap) 108 } 109 110 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 111 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 112 } 113 } else { 114 if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { 115 return errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet") 116 } 117 118 if args.GasPrice == nil { 119 price, err := b.SuggestGasTipCap(ctx) 120 if err != nil { 121 return err 122 } 123 124 if b.ChainConfig().IsLondon(head.Number) { 125 // The legacy tx gas price suggestion should not add 2x base fee 126 // because all fees are consumed, so it would result in a spiral 127 // upwards. 128 price.Add(price, head.BaseFee) 129 } 130 131 args.GasPrice = (*hexutil.Big)(price) 132 } 133 } 134 } else { 135 // Both maxPriorityfee and maxFee set by caller. Sanity-check their internal relation 136 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 137 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 138 } 139 } 140 141 if args.Value == nil { 142 args.Value = new(hexutil.Big) 143 } 144 145 if args.Nonce == nil { 146 nonce, err := b.GetPoolNonce(ctx, args.from()) 147 if err != nil { 148 return err 149 } 150 151 args.Nonce = (*hexutil.Uint64)(&nonce) 152 } 153 154 if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { 155 return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`) 156 } 157 158 if args.To == nil && len(args.data()) == 0 { 159 return errors.New(`contract creation without any data provided`) 160 } 161 162 // Estimate the gas usage if necessary. 163 if args.Gas == nil { 164 // These fields are immutable during the estimation, safe to 165 // pass the pointer directly. 166 data := args.data() 167 callArgs := TransactionArgs{ 168 From: args.From, 169 To: args.To, 170 GasPrice: args.GasPrice, 171 MaxFeePerGas: args.MaxFeePerGas, 172 MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, 173 Value: args.Value, 174 Data: (*hexutil.Bytes)(&data), 175 AccessList: args.AccessList, 176 } 177 178 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 179 180 estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) 181 if err != nil { 182 return err 183 } 184 185 args.Gas = &estimated 186 log.Trace("Estimate gas usage automatically", "gas", args.Gas) 187 } 188 189 if args.ChainID == nil { 190 id := (*hexutil.Big)(b.ChainConfig().ChainID) 191 args.ChainID = id 192 } 193 194 return nil 195 } 196 197 // ToMessage converts the transaction arguments to the Message type used by the 198 // core evm. This method is used in calls and traces that do not require a real 199 // live transaction. 200 func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (types.Message, error) { 201 // Reject invalid combinations of pre- and post-1559 fee styles 202 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 203 return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 204 } 205 206 // Set sender address or use zero address if none specified. 207 addr := args.from() 208 209 // Gas set for system calls 210 systemCallGas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) 211 212 // Set default gas & gas price if none were set 213 gas := globalGasCap 214 if gas == 0 { 215 gas = uint64(math.MaxUint64 / 2) 216 } 217 218 if args.Gas != nil && *args.Gas != systemCallGas { 219 gas = uint64(*args.Gas) 220 } 221 222 if globalGasCap != 0 && globalGasCap < gas { 223 log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 224 gas = globalGasCap 225 } 226 227 var ( 228 gasPrice *big.Int 229 gasFeeCap *big.Int 230 gasTipCap *big.Int 231 ) 232 233 if baseFee == nil { 234 // If there's no basefee, then it must be a non-1559 execution 235 gasPrice = new(big.Int) 236 if args.GasPrice != nil { 237 gasPrice = args.GasPrice.ToInt() 238 } 239 240 gasFeeCap, gasTipCap = gasPrice, gasPrice 241 } else { 242 // A basefee is provided, necessitating 1559-type execution 243 if args.GasPrice != nil { 244 // User specified the legacy gas field, convert to 1559 gas typing 245 gasPrice = args.GasPrice.ToInt() 246 gasFeeCap, gasTipCap = gasPrice, gasPrice 247 } else { 248 // User specified 1559 gas feilds (or none), use those 249 gasFeeCap = new(big.Int) 250 251 if args.MaxFeePerGas != nil { 252 gasFeeCap = args.MaxFeePerGas.ToInt() 253 } 254 255 gasTipCap = new(big.Int) 256 257 if args.MaxPriorityFeePerGas != nil { 258 gasTipCap = args.MaxPriorityFeePerGas.ToInt() 259 } 260 261 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes 262 gasPrice = new(big.Int) 263 264 if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 265 gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) 266 } 267 } 268 } 269 270 value := new(big.Int) 271 if args.Value != nil { 272 value = args.Value.ToInt() 273 } 274 275 data := args.data() 276 277 var accessList types.AccessList 278 if args.AccessList != nil { 279 accessList = *args.AccessList 280 } 281 282 msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true) 283 284 return msg, nil 285 } 286 287 // toTransaction converts the arguments to a transaction. 288 // This assumes that setDefaults has been called. 289 func (args *TransactionArgs) toTransaction() *types.Transaction { 290 var data types.TxData 291 292 switch { 293 case args.MaxFeePerGas != nil: 294 al := types.AccessList{} 295 if args.AccessList != nil { 296 al = *args.AccessList 297 } 298 data = &types.DynamicFeeTx{ 299 To: args.To, 300 ChainID: (*big.Int)(args.ChainID), 301 Nonce: uint64(*args.Nonce), 302 Gas: uint64(*args.Gas), 303 GasFeeCap: (*big.Int)(args.MaxFeePerGas), 304 GasTipCap: (*big.Int)(args.MaxPriorityFeePerGas), 305 Value: (*big.Int)(args.Value), 306 Data: args.data(), 307 AccessList: al, 308 } 309 case args.AccessList != nil: 310 data = &types.AccessListTx{ 311 To: args.To, 312 ChainID: (*big.Int)(args.ChainID), 313 Nonce: uint64(*args.Nonce), 314 Gas: uint64(*args.Gas), 315 GasPrice: (*big.Int)(args.GasPrice), 316 Value: (*big.Int)(args.Value), 317 Data: args.data(), 318 AccessList: *args.AccessList, 319 } 320 default: 321 data = &types.LegacyTx{ 322 To: args.To, 323 Nonce: uint64(*args.Nonce), 324 Gas: uint64(*args.Gas), 325 GasPrice: (*big.Int)(args.GasPrice), 326 Value: (*big.Int)(args.Value), 327 Data: args.data(), 328 } 329 } 330 331 return types.NewTx(data) 332 } 333 334 // ToTransaction converts the arguments to a transaction. 335 // This assumes that setDefaults has been called. 336 func (args *TransactionArgs) ToTransaction() *types.Transaction { 337 return args.toTransaction() 338 }