github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/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/unicornultrafoundation/go-u2u/common" 27 "github.com/unicornultrafoundation/go-u2u/common/hexutil" 28 "github.com/unicornultrafoundation/go-u2u/common/math" 29 "github.com/unicornultrafoundation/go-u2u/core/types" 30 "github.com/unicornultrafoundation/go-u2u/log" 31 "github.com/unicornultrafoundation/go-u2u/rpc" 32 33 "github.com/unicornultrafoundation/go-u2u/gossip/gasprice" 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 (arg *TransactionArgs) from() common.Address { 61 if arg.From == nil { 62 return common.Address{} 63 } 64 return *arg.From 65 } 66 67 // data retrieves the transaction calldata. Input field is preferred. 68 func (arg *TransactionArgs) data() []byte { 69 if arg.Input != nil { 70 return *arg.Input 71 } 72 if arg.Data != nil { 73 return *arg.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.CurrentBlock().Header() 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 := b.SuggestGasTipCap(ctx, gasprice.AsDefaultCertainty) 92 args.MaxPriorityFeePerGas = (*hexutil.Big)(tip) 93 } 94 if args.MaxFeePerGas == nil { 95 gasFeeCap := new(big.Int).Add( 96 (*big.Int)(args.MaxPriorityFeePerGas), 97 b.MinGasPrice(), 98 ) 99 args.MaxFeePerGas = (*hexutil.Big)(gasFeeCap) 100 } 101 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 102 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 103 } 104 } else { 105 if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil { 106 return errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet") 107 } 108 if args.GasPrice == nil { 109 price := b.SuggestGasTipCap(ctx, gasprice.AsDefaultCertainty) 110 price.Add(price, b.MinGasPrice()) 111 args.GasPrice = (*hexutil.Big)(price) 112 } 113 } 114 } else { 115 // Both maxPriorityfee and maxFee set by caller. Sanity-check their internal relation 116 if args.MaxFeePerGas.ToInt().Cmp(args.MaxPriorityFeePerGas.ToInt()) < 0 { 117 return fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", args.MaxFeePerGas, args.MaxPriorityFeePerGas) 118 } 119 } 120 if args.Value == nil { 121 args.Value = new(hexutil.Big) 122 } 123 if args.Nonce == nil { 124 nonce, err := b.GetPoolNonce(ctx, args.from()) 125 if err != nil { 126 return err 127 } 128 args.Nonce = (*hexutil.Uint64)(&nonce) 129 } 130 if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) { 131 return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`) 132 } 133 if args.To == nil && len(args.data()) == 0 { 134 return errors.New(`contract creation without any data provided`) 135 } 136 // Estimate the gas usage if necessary. 137 if args.Gas == nil { 138 // These fields are immutable during the estimation, safe to 139 // pass the pointer directly. 140 callArgs := TransactionArgs{ 141 From: args.From, 142 To: args.To, 143 GasPrice: args.GasPrice, 144 MaxFeePerGas: args.MaxFeePerGas, 145 MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, 146 Value: args.Value, 147 Data: args.Data, 148 AccessList: args.AccessList, 149 } 150 pendingBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) 151 estimated, err := DoEstimateGas(ctx, b, callArgs, pendingBlockNr, b.RPCGasCap()) 152 if err != nil { 153 return err 154 } 155 args.Gas = &estimated 156 log.Trace("Estimate gas usage automatically", "gas", args.Gas) 157 } 158 if args.ChainID == nil { 159 id := (*hexutil.Big)(b.ChainConfig().ChainID) 160 args.ChainID = id 161 } 162 return nil 163 } 164 165 // ToMessage converts the transaction arguments to the Message type used by the 166 // core evm. This method is used in calls and traces that do not require a real 167 // live transaction. 168 func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int) (types.Message, error) { 169 // Reject invalid combinations of pre- and post-1559 fee styles 170 if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { 171 return types.Message{}, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") 172 } 173 // Set sender address or use zero address if none specified. 174 addr := args.from() 175 176 // Set default gas & gas price if none were set 177 gas := globalGasCap 178 if gas == 0 { 179 gas = uint64(math.MaxUint64 / 2) 180 } 181 if args.Gas != nil { 182 gas = uint64(*args.Gas) 183 } 184 if globalGasCap != 0 && globalGasCap < gas { 185 log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) 186 gas = globalGasCap 187 } 188 var ( 189 gasPrice *big.Int 190 gasFeeCap *big.Int 191 gasTipCap *big.Int 192 ) 193 if baseFee == nil { 194 // If there's no basefee, then it must be a non-1559 execution 195 gasPrice = new(big.Int) 196 if args.GasPrice != nil { 197 gasPrice = args.GasPrice.ToInt() 198 } 199 gasFeeCap, gasTipCap = gasPrice, gasPrice 200 } else { 201 // A basefee is provided, necessitating 1559-type execution 202 if args.GasPrice != nil { 203 // User specified the legacy gas field, convert to 1559 gas typing 204 gasPrice = args.GasPrice.ToInt() 205 gasFeeCap, gasTipCap = gasPrice, gasPrice 206 } else { 207 // User specified 1559 gas feilds (or none), use those 208 gasFeeCap = new(big.Int) 209 if args.MaxFeePerGas != nil { 210 gasFeeCap = args.MaxFeePerGas.ToInt() 211 } 212 gasTipCap = new(big.Int) 213 if args.MaxPriorityFeePerGas != nil { 214 gasTipCap = args.MaxPriorityFeePerGas.ToInt() 215 } 216 // Backfill the legacy gasPrice for EVM execution, unless we're all zeroes 217 gasPrice = new(big.Int) 218 if gasFeeCap.BitLen() > 0 || gasTipCap.BitLen() > 0 { 219 gasPrice = math.BigMin(new(big.Int).Add(gasTipCap, baseFee), gasFeeCap) 220 } 221 } 222 } 223 value := new(big.Int) 224 if args.Value != nil { 225 value = args.Value.ToInt() 226 } 227 data := args.data() 228 var accessList types.AccessList 229 if args.AccessList != nil { 230 accessList = *args.AccessList 231 } 232 msg := types.NewMessage(addr, args.To, 0, value, gas, gasPrice, gasFeeCap, gasTipCap, data, accessList, true) 233 return msg, nil 234 } 235 236 // toTransaction converts the arguments to a transaction. 237 // This assumes that setDefaults has been called. 238 func (args *TransactionArgs) toTransaction() *types.Transaction { 239 var data types.TxData 240 switch { 241 case args.MaxFeePerGas != nil: 242 al := types.AccessList{} 243 if args.AccessList != nil { 244 al = *args.AccessList 245 } 246 data = &types.DynamicFeeTx{ 247 To: args.To, 248 ChainID: (*big.Int)(args.ChainID), 249 Nonce: uint64(*args.Nonce), 250 Gas: uint64(*args.Gas), 251 GasFeeCap: (*big.Int)(args.MaxFeePerGas), 252 GasTipCap: (*big.Int)(args.MaxPriorityFeePerGas), 253 Value: (*big.Int)(args.Value), 254 Data: args.data(), 255 AccessList: al, 256 } 257 case args.AccessList != nil: 258 data = &types.AccessListTx{ 259 To: args.To, 260 ChainID: (*big.Int)(args.ChainID), 261 Nonce: uint64(*args.Nonce), 262 Gas: uint64(*args.Gas), 263 GasPrice: (*big.Int)(args.GasPrice), 264 Value: (*big.Int)(args.Value), 265 Data: args.data(), 266 AccessList: *args.AccessList, 267 } 268 default: 269 data = &types.LegacyTx{ 270 To: args.To, 271 Nonce: uint64(*args.Nonce), 272 Gas: uint64(*args.Gas), 273 GasPrice: (*big.Int)(args.GasPrice), 274 Value: (*big.Int)(args.Value), 275 Data: args.data(), 276 } 277 } 278 return types.NewTx(data) 279 } 280 281 // ToTransaction converts the arguments to a transaction. 282 // This assumes that setDefaults has been called. 283 func (args *TransactionArgs) ToTransaction() *types.Transaction { 284 return args.toTransaction() 285 }