github.com/ethereum/go-ethereum@v1.16.1/cmd/evm/internal/t8ntool/transition.go (about) 1 // Copyright 2020 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 "io" 24 "math/big" 25 "os" 26 "path/filepath" 27 28 "github.com/ethereum/go-ethereum/common" 29 "github.com/ethereum/go-ethereum/common/hexutil" 30 "github.com/ethereum/go-ethereum/consensus/misc/eip1559" 31 "github.com/ethereum/go-ethereum/core/state" 32 "github.com/ethereum/go-ethereum/core/tracing" 33 "github.com/ethereum/go-ethereum/core/types" 34 "github.com/ethereum/go-ethereum/core/vm" 35 "github.com/ethereum/go-ethereum/eth/tracers" 36 "github.com/ethereum/go-ethereum/eth/tracers/logger" 37 "github.com/ethereum/go-ethereum/log" 38 "github.com/ethereum/go-ethereum/params" 39 "github.com/ethereum/go-ethereum/tests" 40 "github.com/urfave/cli/v2" 41 ) 42 43 const ( 44 ErrorEVM = 2 45 ErrorConfig = 3 46 ErrorMissingBlockhash = 4 47 48 ErrorJson = 10 49 ErrorIO = 11 50 ErrorRlp = 12 51 52 stdinSelector = "stdin" 53 ) 54 55 type NumberedError struct { 56 errorCode int 57 err error 58 } 59 60 func NewError(errorCode int, err error) *NumberedError { 61 return &NumberedError{errorCode, err} 62 } 63 64 func (n *NumberedError) Error() string { 65 return fmt.Sprintf("ERROR(%d): %v", n.errorCode, n.err.Error()) 66 } 67 68 func (n *NumberedError) ExitCode() int { 69 return n.errorCode 70 } 71 72 // compile-time conformance test 73 var ( 74 _ cli.ExitCoder = (*NumberedError)(nil) 75 ) 76 77 type input struct { 78 Alloc types.GenesisAlloc `json:"alloc,omitempty"` 79 Env *stEnv `json:"env,omitempty"` 80 Txs []*txWithKey `json:"txs,omitempty"` 81 TxRlp string `json:"txsRlp,omitempty"` 82 } 83 84 func Transition(ctx *cli.Context) error { 85 baseDir, err := createBasedir(ctx) 86 if err != nil { 87 return NewError(ErrorIO, fmt.Errorf("failed creating output basedir: %v", err)) 88 } 89 // We need to load three things: alloc, env and transactions. May be either in 90 // stdin input or in files. 91 // Check if anything needs to be read from stdin 92 var ( 93 prestate Prestate 94 txIt txIterator // txs to apply 95 allocStr = ctx.String(InputAllocFlag.Name) 96 97 envStr = ctx.String(InputEnvFlag.Name) 98 txStr = ctx.String(InputTxsFlag.Name) 99 inputData = &input{} 100 ) 101 // Figure out the prestate alloc 102 if allocStr == stdinSelector || envStr == stdinSelector || txStr == stdinSelector { 103 decoder := json.NewDecoder(os.Stdin) 104 if err := decoder.Decode(inputData); err != nil { 105 return NewError(ErrorJson, fmt.Errorf("failed unmarshalling stdin: %v", err)) 106 } 107 } 108 if allocStr != stdinSelector { 109 if err := readFile(allocStr, "alloc", &inputData.Alloc); err != nil { 110 return err 111 } 112 } 113 prestate.Pre = inputData.Alloc 114 115 // Set the block environment 116 if envStr != stdinSelector { 117 var env stEnv 118 if err := readFile(envStr, "env", &env); err != nil { 119 return err 120 } 121 inputData.Env = &env 122 } 123 prestate.Env = *inputData.Env 124 125 vmConfig := vm.Config{} 126 // Construct the chainconfig 127 var chainConfig *params.ChainConfig 128 if cConf, extraEips, err := tests.GetChainConfig(ctx.String(ForknameFlag.Name)); err != nil { 129 return NewError(ErrorConfig, fmt.Errorf("failed constructing chain configuration: %v", err)) 130 } else { 131 chainConfig = cConf 132 vmConfig.ExtraEips = extraEips 133 } 134 135 // Set the chain id 136 chainConfig.ChainID = big.NewInt(ctx.Int64(ChainIDFlag.Name)) 137 138 if txIt, err = loadTransactions(txStr, inputData, chainConfig); err != nil { 139 return err 140 } 141 if err := applyLondonChecks(&prestate.Env, chainConfig); err != nil { 142 return err 143 } 144 if err := applyShanghaiChecks(&prestate.Env, chainConfig); err != nil { 145 return err 146 } 147 if err := applyMergeChecks(&prestate.Env, chainConfig); err != nil { 148 return err 149 } 150 if err := applyCancunChecks(&prestate.Env, chainConfig); err != nil { 151 return err 152 } 153 154 // Configure tracer 155 if ctx.IsSet(TraceTracerFlag.Name) { // Custom tracing 156 config := json.RawMessage(ctx.String(TraceTracerConfigFlag.Name)) 157 tracer, err := tracers.DefaultDirectory.New(ctx.String(TraceTracerFlag.Name), 158 nil, config, chainConfig) 159 if err != nil { 160 return NewError(ErrorConfig, fmt.Errorf("failed instantiating tracer: %v", err)) 161 } 162 vmConfig.Tracer = newResultWriter(baseDir, tracer) 163 } else if ctx.Bool(TraceFlag.Name) { // JSON opcode tracing 164 logConfig := &logger.Config{ 165 DisableStack: ctx.Bool(TraceDisableStackFlag.Name), 166 EnableMemory: ctx.Bool(TraceEnableMemoryFlag.Name), 167 EnableReturnData: ctx.Bool(TraceEnableReturnDataFlag.Name), 168 } 169 if ctx.Bool(TraceEnableCallFramesFlag.Name) { 170 vmConfig.Tracer = newFileWriter(baseDir, func(out io.Writer) *tracing.Hooks { 171 return logger.NewJSONLoggerWithCallFrames(logConfig, out) 172 }) 173 } else { 174 vmConfig.Tracer = newFileWriter(baseDir, func(out io.Writer) *tracing.Hooks { 175 return logger.NewJSONLogger(logConfig, out) 176 }) 177 } 178 } 179 // Run the test and aggregate the result 180 s, result, body, err := prestate.Apply(vmConfig, chainConfig, txIt, ctx.Int64(RewardFlag.Name)) 181 if err != nil { 182 return err 183 } 184 // Dump the execution result 185 collector := make(Alloc) 186 s.DumpToCollector(collector, nil) 187 return dispatchOutput(ctx, baseDir, result, collector, body) 188 } 189 190 func applyLondonChecks(env *stEnv, chainConfig *params.ChainConfig) error { 191 if !chainConfig.IsLondon(big.NewInt(int64(env.Number))) { 192 return nil 193 } 194 // Sanity check, to not `panic` in state_transition 195 if env.BaseFee != nil { 196 // Already set, base fee has precedent over parent base fee. 197 return nil 198 } 199 if env.ParentBaseFee == nil || env.Number == 0 { 200 return NewError(ErrorConfig, errors.New("EIP-1559 config but missing 'parentBaseFee' in env section")) 201 } 202 env.BaseFee = eip1559.CalcBaseFee(chainConfig, &types.Header{ 203 Number: new(big.Int).SetUint64(env.Number - 1), 204 BaseFee: env.ParentBaseFee, 205 GasUsed: env.ParentGasUsed, 206 GasLimit: env.ParentGasLimit, 207 }) 208 return nil 209 } 210 211 func applyShanghaiChecks(env *stEnv, chainConfig *params.ChainConfig) error { 212 if !chainConfig.IsShanghai(big.NewInt(int64(env.Number)), env.Timestamp) { 213 return nil 214 } 215 if env.Withdrawals == nil { 216 return NewError(ErrorConfig, errors.New("Shanghai config but missing 'withdrawals' in env section")) 217 } 218 return nil 219 } 220 221 func applyMergeChecks(env *stEnv, chainConfig *params.ChainConfig) error { 222 isMerged := chainConfig.TerminalTotalDifficulty != nil && chainConfig.TerminalTotalDifficulty.BitLen() == 0 223 if !isMerged { 224 // pre-merge: If difficulty was not provided by caller, we need to calculate it. 225 if env.Difficulty != nil { 226 // already set 227 return nil 228 } 229 switch { 230 case env.ParentDifficulty == nil: 231 return NewError(ErrorConfig, errors.New("currentDifficulty was not provided, and cannot be calculated due to missing parentDifficulty")) 232 case env.Number == 0: 233 return NewError(ErrorConfig, errors.New("currentDifficulty needs to be provided for block number 0")) 234 case env.Timestamp <= env.ParentTimestamp: 235 return NewError(ErrorConfig, fmt.Errorf("currentDifficulty cannot be calculated -- currentTime (%d) needs to be after parent time (%d)", 236 env.Timestamp, env.ParentTimestamp)) 237 } 238 env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp, 239 env.ParentTimestamp, env.ParentDifficulty, env.ParentUncleHash) 240 return nil 241 } 242 // post-merge: 243 // - random must be supplied 244 // - difficulty must be zero 245 switch { 246 case env.Random == nil: 247 return NewError(ErrorConfig, errors.New("post-merge requires currentRandom to be defined in env")) 248 case env.Difficulty != nil && env.Difficulty.BitLen() != 0: 249 return NewError(ErrorConfig, errors.New("post-merge difficulty must be zero (or omitted) in env")) 250 } 251 env.Difficulty = nil 252 return nil 253 } 254 255 func applyCancunChecks(env *stEnv, chainConfig *params.ChainConfig) error { 256 if !chainConfig.IsCancun(big.NewInt(int64(env.Number)), env.Timestamp) { 257 env.ParentBeaconBlockRoot = nil // un-set it if it has been set too early 258 return nil 259 } 260 // Post-cancun 261 // We require EIP-4788 beacon root to be set in the env 262 if env.ParentBeaconBlockRoot == nil { 263 return NewError(ErrorConfig, errors.New("post-cancun env requires parentBeaconBlockRoot to be set")) 264 } 265 return nil 266 } 267 268 type Alloc map[common.Address]types.Account 269 270 func (g Alloc) OnRoot(common.Hash) {} 271 272 func (g Alloc) OnAccount(addr *common.Address, dumpAccount state.DumpAccount) { 273 if addr == nil { 274 return 275 } 276 balance, _ := new(big.Int).SetString(dumpAccount.Balance, 0) 277 var storage map[common.Hash]common.Hash 278 if dumpAccount.Storage != nil { 279 storage = make(map[common.Hash]common.Hash, len(dumpAccount.Storage)) 280 for k, v := range dumpAccount.Storage { 281 storage[k] = common.HexToHash(v) 282 } 283 } 284 genesisAccount := types.Account{ 285 Code: dumpAccount.Code, 286 Storage: storage, 287 Balance: balance, 288 Nonce: dumpAccount.Nonce, 289 } 290 g[*addr] = genesisAccount 291 } 292 293 // saveFile marshals the object to the given file 294 func saveFile(baseDir, filename string, data interface{}) error { 295 b, err := json.MarshalIndent(data, "", " ") 296 if err != nil { 297 return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err)) 298 } 299 location := filepath.Join(baseDir, filename) 300 if err = os.WriteFile(location, b, 0644); err != nil { 301 return NewError(ErrorIO, fmt.Errorf("failed writing output: %v", err)) 302 } 303 log.Info("Wrote file", "file", location) 304 return nil 305 } 306 307 // dispatchOutput writes the output data to either stderr or stdout, or to the specified 308 // files 309 func dispatchOutput(ctx *cli.Context, baseDir string, result *ExecutionResult, alloc Alloc, body hexutil.Bytes) error { 310 stdOutObject := make(map[string]interface{}) 311 stdErrObject := make(map[string]interface{}) 312 dispatch := func(baseDir, fName, name string, obj interface{}) error { 313 switch fName { 314 case "stdout": 315 stdOutObject[name] = obj 316 case "stderr": 317 stdErrObject[name] = obj 318 case "": 319 // don't save 320 default: // save to file 321 if err := saveFile(baseDir, fName, obj); err != nil { 322 return err 323 } 324 } 325 return nil 326 } 327 if err := dispatch(baseDir, ctx.String(OutputAllocFlag.Name), "alloc", alloc); err != nil { 328 return err 329 } 330 if err := dispatch(baseDir, ctx.String(OutputResultFlag.Name), "result", result); err != nil { 331 return err 332 } 333 if err := dispatch(baseDir, ctx.String(OutputBodyFlag.Name), "body", body); err != nil { 334 return err 335 } 336 if len(stdOutObject) > 0 { 337 b, err := json.MarshalIndent(stdOutObject, "", " ") 338 if err != nil { 339 return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err)) 340 } 341 os.Stdout.Write(b) 342 os.Stdout.WriteString("\n") 343 } 344 if len(stdErrObject) > 0 { 345 b, err := json.MarshalIndent(stdErrObject, "", " ") 346 if err != nil { 347 return NewError(ErrorJson, fmt.Errorf("failed marshalling output: %v", err)) 348 } 349 os.Stderr.Write(b) 350 os.Stderr.WriteString("\n") 351 } 352 return nil 353 }