github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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/tirogen/go-ethereum/common" 27 "github.com/tirogen/go-ethereum/common/hexutil" 28 "github.com/tirogen/go-ethereum/common/math" 29 "github.com/tirogen/go-ethereum/core/types" 30 "github.com/tirogen/go-ethereum/log" 31 "github.com/tirogen/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/tirogen/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 return *args.From 63 } 64 65 // data retrieves the transaction calldata. Input field is preferred. 66 func (args *TransactionArgs) data() []byte { 67 if args.Input != nil { 68 return *args.Input 69 } 70 if args.Data != nil { 71 return *args.Data 72 } 73 return nil 74 } 75 76 // setDefaults fills in default values for unspecified tx fields. 77 func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error { 78 if err := args.setFeeDefaults(ctx, b); err != nil { 79 return err 80 } 81 if args.Value == nil { 82 args.Value = new(hexutil.Big) 83 } 84 if args.Nonce == nil { 85 nonce, err := b.GetPoolNonce(ctx, args.from()) 86 if err != nil { 87 return err 88 } 89 args.Nonce = (*hexutil.Uint64)(&nonce) 90 } 91 if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { 92 return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`) 93 } 94 if args.To == nil && len(args.data()) == 0 { 95 return errors.New(`contract creation without any data provided`) 96 } 97 // Estimate the gas usage if necessary. 98 if args.Gas == nil { 99 // These fields are immutable during the estimation, safe to 100 // pass the pointer directly. 101 data := args.data() 102 callArgs := TransactionArgs{ 103 From: args.From, 104 To: args.To, 105 GasPrice: args.GasPrice, 106 MaxFeePerGas: args.MaxFeePerGas, 107 MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, 108 Value: args.Value, 109 Data: (*hexutil.Bytes)(&data), 110 AccessList: args.AccessList, 111 } 112 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 113 estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) 114 if err != nil { 115 return err 116 } 117 args.Gas = &estimated 118 log.Trace("Estimate gas usage automatically", "gas", args.Gas) 119 } 120 // If chain id is provided, ensure it matches the local chain id. Otherwise, set the local 121 // chain id as the default. 122 want := b.ChainConfig().ChainID 123 if args.ChainID != nil { 124 if have := (*big.Int)(args.ChainID); have.Cmp(want) != 0 { 125 return fmt.Errorf("chainId does not match node's (have=%v, want=%v)", have, want) 126 } 127 } else { 128 args.ChainID = (*hexutil.Big)(want) 129 } 130 return nil 131 } 132 133 // setFeeDefaults fills in default fee values for unspecified tx fields. 134 func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) error { 135 // If both gasPrice and at least one of the EIP-1559 fee parameters are specified, error. 136 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 137 return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 138 } 139 // If the tx has completely specified a fee mechanism, no default is needed. This allows users 140 // who are not yet synced past London to get defaults for other tx values. See 141 // https://github.com/tirogen/go-ethereum/pull/23274 for more information. 142 eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil 143 if (args.GasPrice != nil && !eip1559ParamsSet) || (args.GasPrice == nil && eip1559ParamsSet) { 144 // Sanity check the EIP-1559 fee parameters if present. 145 if args.GasPrice == nil && args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 146 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 147 } 148 return nil 149 } 150 // Now attempt to fill in default value depending on whether London is active or not. 151 head := b.CurrentHeader() 152 if b.ChainConfig().IsLondon(head.Number) { 153 // London is active, set maxPriorityFeePerGas and maxFeePerGas. 154 if err := args.setLondonFeeDefaults(ctx, head, b); err != nil { 155 return err 156 } 157 } else { 158 if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { 159 return fmt.Errorf("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active") 160 } 161 // London not active, set gas price. 162 price, err := b.SuggestGasTipCap(ctx) 163 if err != nil { 164 return err 165 } 166 args.GasPrice = (*hexutil.Big)(price) 167 } 168 return nil 169 } 170 171 // setLondonFeeDefaults fills in reasonable default fee values for unspecified fields. 172 func (args *TransactionArgs) setLondonFeeDefaults(ctx context.Context, head *types.Header, b Backend) error { 173 // Set maxPriorityFeePerGas if it is missing. 174 if args.MaxPriorityFeePerGas == nil { 175 tip, err := b.SuggestGasTipCap(ctx) 176 if err != nil { 177 return err 178 } 179 args.MaxPriorityFeePerGas = (*hexutil.Big)(tip) 180 } 181 // Set maxFeePerGas if it is missing. 182 if args.MaxFeePerGas == nil { 183 // Set the max fee to be 2 times larger than the previous block's base fee. 184 // The additional slack allows the tx to not become invalidated if the base 185 // fee is rising. 186 val := new(big.Int).Add( 187 args.MaxPriorityFeePerGas.ToInt(), 188 new(big.Int).Mul(head.BaseFee, big.NewInt(2)), 189 ) 190 args.MaxFeePerGas = (*hexutil.Big)(val) 191 } 192 // Both EIP-1559 fee parameters are now set; sanity check them. 193 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 194 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 195 } 196 return nil 197 } 198 199 // ToMessage converts the transaction arguments to the Message type used by the 200 // core evm. This method is used in calls and traces that do not require a real 201 // live transaction. 202 func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (types.Message, error) { 203 // Reject invalid combinations of pre- and post-1559 fee styles 204 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 205 return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 206 } 207 // Set sender address or use zero address if none specified. 208 addr := args.from() 209 210 // Set default gas & gas price if none were set 211 gas := globalGasCap 212 if gas == 0 { 213 gas = uint64(math.MaxUint64 / 2) 214 } 215 if args.Gas != nil { 216 gas = uint64(*args.Gas) 217 } 218 if globalGasCap != 0 && globalGasCap < gas { 219 log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 220 gas = globalGasCap 221 } 222 var ( 223 gasPrice *big.Int 224 gasFeeCap *big.Int 225 gasTipCap *big.Int 226 ) 227 if baseFee == nil { 228 // If there's no basefee, then it must be a non-1559 execution 229 gasPrice = new(big.Int) 230 if args.GasPrice != nil { 231 gasPrice = args.GasPrice.ToInt() 232 } 233 gasFeeCap, gasTipCap = gasPrice, gasPrice 234 } else { 235 // A basefee is provided, necessitating 1559-type execution 236 if args.GasPrice != nil { 237 // User specified the legacy gas field, convert to 1559 gas typing 238 gasPrice = args.GasPrice.ToInt() 239 gasFeeCap, gasTipCap = gasPrice, gasPrice 240 } else { 241 // User specified 1559 gas fields (or none), use those 242 gasFeeCap = new(big.Int) 243 if args.MaxFeePerGas != nil { 244 gasFeeCap = args.MaxFeePerGas.ToInt() 245 } 246 gasTipCap = new(big.Int) 247 if args.MaxPriorityFeePerGas != nil { 248 gasTipCap = args.MaxPriorityFeePerGas.ToInt() 249 } 250 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes 251 gasPrice = new(big.Int) 252 if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 253 gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) 254 } 255 } 256 } 257 value := new(big.Int) 258 if args.Value != nil { 259 value = args.Value.ToInt() 260 } 261 data := args.data() 262 var accessList types.AccessList 263 if args.AccessList != nil { 264 accessList = *args.AccessList 265 } 266 msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true) 267 return msg, nil 268 } 269 270 // toTransaction converts the arguments to a transaction. 271 // This assumes that setDefaults has been called. 272 func (args *TransactionArgs) toTransaction() *types.Transaction { 273 var data types.TxData 274 switch { 275 case args.MaxFeePerGas != nil: 276 al := types.AccessList{} 277 if args.AccessList != nil { 278 al = *args.AccessList 279 } 280 data = &types.DynamicFeeTx{ 281 To: args.To, 282 ChainID: (*big.Int)(args.ChainID), 283 Nonce: uint64(*args.Nonce), 284 Gas: uint64(*args.Gas), 285 GasFeeCap: (*big.Int)(args.MaxFeePerGas), 286 GasTipCap: (*big.Int)(args.MaxPriorityFeePerGas), 287 Value: (*big.Int)(args.Value), 288 Data: args.data(), 289 AccessList: al, 290 } 291 case args.AccessList != nil: 292 data = &types.AccessListTx{ 293 To: args.To, 294 ChainID: (*big.Int)(args.ChainID), 295 Nonce: uint64(*args.Nonce), 296 Gas: uint64(*args.Gas), 297 GasPrice: (*big.Int)(args.GasPrice), 298 Value: (*big.Int)(args.Value), 299 Data: args.data(), 300 AccessList: *args.AccessList, 301 } 302 default: 303 data = &types.LegacyTx{ 304 To: args.To, 305 Nonce: uint64(*args.Nonce), 306 Gas: uint64(*args.Gas), 307 GasPrice: (*big.Int)(args.GasPrice), 308 Value: (*big.Int)(args.Value), 309 Data: args.data(), 310 } 311 } 312 return types.NewTx(data) 313 } 314 315 // ToTransaction converts the arguments to a transaction. 316 // This assumes that setDefaults has been called. 317 func (args *TransactionArgs) ToTransaction() *types.Transaction { 318 return args.toTransaction() 319 }