github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/cmd/evm/internal/t8ntool/transaction.go (about) 1 // Copyright 2021 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package t8ntool 18 19 import ( 20 "encoding/json" 21 "errors" 22 "fmt" 23 "math/big" 24 "os" 25 "strings" 26 27 "github.com/ethxdao/go-ethereum/common" 28 "github.com/ethxdao/go-ethereum/common/hexutil" 29 "github.com/ethxdao/go-ethereum/core" 30 "github.com/ethxdao/go-ethereum/core/types" 31 "github.com/ethxdao/go-ethereum/log" 32 "github.com/ethxdao/go-ethereum/params" 33 "github.com/ethxdao/go-ethereum/rlp" 34 "github.com/ethxdao/go-ethereum/tests" 35 ) 36 37 type result struct { 38 Error error 39 Address common.Address 40 Hash common.Hash 41 IntrinsicGas uint64 42 } 43 44 // MarshalJSON marshals as JSON with a hash. 45 func (r *result) MarshalJSON() ([]byte, error) { 46 type xx struct { 47 Error string `json:"error,omitempty"` 48 Address *common.Address `json:"address,omitempty"` 49 Hash *common.Hash `json:"hash,omitempty"` 50 IntrinsicGas hexutil.Uint64 `json:"intrinsicGas,omitempty"` 51 } 52 var out xx 53 if r.Error != nil { 54 out.Error = r.Error.Error() 55 } 56 if r.Address != (common.Address{}) { 57 out.Address = &r.Address 58 } 59 if r.Hash != (common.Hash{}) { 60 out.Hash = &r.Hash 61 } 62 out.IntrinsicGas = hexutil.Uint64(r.IntrinsicGas) 63 return json.Marshal(out) 64 } 65 66 func Transaction(ctx *cli.Context) error { 67 // Configure the go-ethereum logger 68 glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false))) 69 glogger.Verbosity(log.Lvl(ctx.Int(VerbosityFlag.Name))) 70 log.Root().SetHandler(glogger) 71 72 var ( 73 err error 74 ) 75 // We need to load the transactions. May be either in stdin input or in files. 76 // Check if anything needs to be read from stdin 77 var ( 78 txStr = ctx.String(InputTxsFlag.Name) 79 inputData = &input{} 80 chainConfig *params.ChainConfig 81 ) 82 // Construct the chainconfig 83 if cConf, _, err := tests.GetChainConfig(ctx.String(ForknameFlag.Name)); err != nil { 84 return NewError(ErrorConfig, fmt.Errorf("failed constructing chain configuration: %v", err)) 85 } else { 86 chainConfig = cConf 87 } 88 // Set the chain id 89 chainConfig.ChainID = big.NewInt(ctx.Int64(ChainIDFlag.Name)) 90 var body hexutil.Bytes 91 if txStr == stdinSelector { 92 decoder := json.NewDecoder(os.Stdin) 93 if err := decoder.Decode(inputData); err != nil { 94 return NewError(ErrorJson, fmt.Errorf("failed unmarshaling stdin: %v", err)) 95 } 96 // Decode the body of already signed transactions 97 body = common.FromHex(inputData.TxRlp) 98 } else { 99 // Read input from file 100 inFile, err := os.Open(txStr) 101 if err != nil { 102 return NewError(ErrorIO, fmt.Errorf("failed reading txs file: %v", err)) 103 } 104 defer inFile.Close() 105 decoder := json.NewDecoder(inFile) 106 if strings.HasSuffix(txStr, ".rlp") { 107 if err := decoder.Decode(&body); err != nil { 108 return err 109 } 110 } else { 111 return NewError(ErrorIO, errors.New("only rlp supported")) 112 } 113 } 114 signer := types.MakeSigner(chainConfig, new(big.Int)) 115 // We now have the transactions in 'body', which is supposed to be an 116 // rlp list of transactions 117 it, err := rlp.NewListIterator([]byte(body)) 118 if err != nil { 119 return err 120 } 121 var results []result 122 for it.Next() { 123 if err := it.Err(); err != nil { 124 return NewError(ErrorIO, err) 125 } 126 var tx types.Transaction 127 err := rlp.DecodeBytes(it.Value(), &tx) 128 if err != nil { 129 results = append(results, result{Error: err}) 130 continue 131 } 132 r := result{Hash: tx.Hash()} 133 if sender, err := types.Sender(signer, &tx); err != nil { 134 r.Error = err 135 results = append(results, r) 136 continue 137 } else { 138 r.Address = sender 139 } 140 // Check intrinsic gas 141 if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, 142 chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int))); err != nil { 143 r.Error = err 144 results = append(results, r) 145 continue 146 } else { 147 r.IntrinsicGas = gas 148 if tx.Gas() < gas { 149 r.Error = fmt.Errorf("%w: have %d, want %d", core.ErrIntrinsicGas, tx.Gas(), gas) 150 results = append(results, r) 151 continue 152 } 153 } 154 // Validate <256bit fields 155 switch { 156 case tx.Nonce()+1 < tx.Nonce(): 157 r.Error = errors.New("nonce exceeds 2^64-1") 158 case tx.Value().BitLen() > 256: 159 r.Error = errors.New("value exceeds 256 bits") 160 case tx.GasPrice().BitLen() > 256: 161 r.Error = errors.New("gasPrice exceeds 256 bits") 162 case tx.GasTipCap().BitLen() > 256: 163 r.Error = errors.New("maxPriorityFeePerGas exceeds 256 bits") 164 case tx.GasFeeCap().BitLen() > 256: 165 r.Error = errors.New("maxFeePerGas exceeds 256 bits") 166 case tx.GasFeeCap().Cmp(tx.GasTipCap()) < 0: 167 r.Error = errors.New("maxFeePerGas < maxPriorityFeePerGas") 168 case new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas())).BitLen() > 256: 169 r.Error = errors.New("gas * gasPrice exceeds 256 bits") 170 case new(big.Int).Mul(tx.GasFeeCap(), new(big.Int).SetUint64(tx.Gas())).BitLen() > 256: 171 r.Error = errors.New("gas * maxFeePerGas exceeds 256 bits") 172 } 173 results = append(results, r) 174 } 175 out, err := json.MarshalIndent(results, "", " ") 176 fmt.Println(string(out)) 177 return err 178 }