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