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