github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/cmd/evm/t8n_test.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 main 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "os" 23 "reflect" 24 "strings" 25 "testing" 26 27 "github.com/docker/docker/pkg/reexec" 28 "github.com/electroneum/electroneum-sc/cmd/evm/internal/t8ntool" 29 "github.com/electroneum/electroneum-sc/internal/cmdtest" 30 ) 31 32 func TestMain(m *testing.M) { 33 // Run the app if we've been exec'd as "ethkey-test" in runEthkey. 34 reexec.Register("evm-test", func() { 35 if err := app.Run(os.Args); err != nil { 36 fmt.Fprintln(os.Stderr, err) 37 os.Exit(1) 38 } 39 os.Exit(0) 40 }) 41 // check if we have been reexec'd 42 if reexec.Init() { 43 return 44 } 45 os.Exit(m.Run()) 46 } 47 48 type testT8n struct { 49 *cmdtest.TestCmd 50 } 51 52 type t8nInput struct { 53 inAlloc string 54 inTxs string 55 inEnv string 56 stFork string 57 stReward string 58 } 59 60 func (args *t8nInput) get(base string) []string { 61 var out []string 62 if opt := args.inAlloc; opt != "" { 63 out = append(out, "--input.alloc") 64 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 65 } 66 if opt := args.inTxs; opt != "" { 67 out = append(out, "--input.txs") 68 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 69 } 70 if opt := args.inEnv; opt != "" { 71 out = append(out, "--input.env") 72 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 73 } 74 if opt := args.stFork; opt != "" { 75 out = append(out, "--state.fork", opt) 76 } 77 if opt := args.stReward; opt != "" { 78 out = append(out, "--state.reward", opt) 79 } 80 return out 81 } 82 83 type t8nOutput struct { 84 alloc bool 85 result bool 86 body bool 87 } 88 89 func (args *t8nOutput) get() (out []string) { 90 if args.body { 91 out = append(out, "--output.body", "stdout") 92 } else { 93 out = append(out, "--output.body", "") // empty means ignore 94 } 95 if args.result { 96 out = append(out, "--output.result", "stdout") 97 } else { 98 out = append(out, "--output.result", "") 99 } 100 if args.alloc { 101 out = append(out, "--output.alloc", "stdout") 102 } else { 103 out = append(out, "--output.alloc", "") 104 } 105 return out 106 } 107 108 func TestT8n(t *testing.T) { 109 tt := new(testT8n) 110 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 111 for i, tc := range []struct { 112 base string 113 input t8nInput 114 output t8nOutput 115 expExitCode int 116 expOut string 117 }{ 118 { // Test exit (3) on bad config 119 base: "./testdata/1", 120 input: t8nInput{ 121 "alloc.json", "txs.json", "env.json", "Frontier+1346", "", 122 }, 123 output: t8nOutput{alloc: true, result: true}, 124 expExitCode: 3, 125 }, 126 { 127 base: "./testdata/1", 128 input: t8nInput{ 129 "alloc.json", "txs.json", "env.json", "Byzantium", "", 130 }, 131 output: t8nOutput{alloc: true, result: true}, 132 expOut: "exp.json", 133 }, 134 { // blockhash test 135 base: "./testdata/3", 136 input: t8nInput{ 137 "alloc.json", "txs.json", "env.json", "Berlin", "", 138 }, 139 output: t8nOutput{alloc: true, result: true}, 140 expOut: "exp.json", 141 }, 142 { // missing blockhash test 143 base: "./testdata/4", 144 input: t8nInput{ 145 "alloc.json", "txs.json", "env.json", "Berlin", "", 146 }, 147 output: t8nOutput{alloc: true, result: true}, 148 expExitCode: 4, 149 }, 150 { // Uncle test 151 base: "./testdata/5", 152 input: t8nInput{ 153 "alloc.json", "txs.json", "env.json", "Byzantium", "0x80", 154 }, 155 output: t8nOutput{alloc: true, result: true}, 156 expOut: "exp.json", 157 }, 158 { // Sign json transactions 159 base: "./testdata/13", 160 input: t8nInput{ 161 "alloc.json", "txs.json", "env.json", "London", "", 162 }, 163 output: t8nOutput{body: true}, 164 expOut: "exp.json", 165 }, 166 { // Already signed transactions 167 base: "./testdata/13", 168 input: t8nInput{ 169 "alloc.json", "signed_txs.rlp", "env.json", "London", "", 170 }, 171 output: t8nOutput{result: true}, 172 expOut: "exp2.json", 173 }, 174 { // Difficulty calculation - no uncles 175 base: "./testdata/14", 176 input: t8nInput{ 177 "alloc.json", "txs.json", "env.json", "London", "", 178 }, 179 output: t8nOutput{result: true}, 180 expOut: "exp.json", 181 }, 182 { // Difficulty calculation - with uncles 183 base: "./testdata/14", 184 input: t8nInput{ 185 "alloc.json", "txs.json", "env.uncles.json", "London", "", 186 }, 187 output: t8nOutput{result: true}, 188 expOut: "exp2.json", 189 }, 190 { // Difficulty calculation - with ommers + Berlin 191 base: "./testdata/14", 192 input: t8nInput{ 193 "alloc.json", "txs.json", "env.uncles.json", "Berlin", "", 194 }, 195 output: t8nOutput{result: true}, 196 expOut: "exp_berlin.json", 197 }, 198 { // Difficulty calculation on arrow glacier 199 base: "./testdata/19", 200 input: t8nInput{ 201 "alloc.json", "txs.json", "env.json", "London", "", 202 }, 203 output: t8nOutput{result: true}, 204 expOut: "exp_london.json", 205 }, 206 { // Difficulty calculation on arrow glacier 207 base: "./testdata/19", 208 input: t8nInput{ 209 "alloc.json", "txs.json", "env.json", "ArrowGlacier", "", 210 }, 211 output: t8nOutput{result: true}, 212 expOut: "exp_arrowglacier.json", 213 }, 214 { // Sign unprotected (pre-EIP155) transaction 215 base: "./testdata/23", 216 input: t8nInput{ 217 "alloc.json", "txs.json", "env.json", "Berlin", "", 218 }, 219 output: t8nOutput{result: true}, 220 expOut: "exp.json", 221 }, 222 { // Test post-merge transition 223 base: "./testdata/24", 224 input: t8nInput{ 225 "alloc.json", "txs.json", "env.json", "Merged", "", 226 }, 227 output: t8nOutput{alloc: true, result: true}, 228 expOut: "exp.json", 229 }, 230 { // Test post-merge transition where input is missing random 231 base: "./testdata/24", 232 input: t8nInput{ 233 "alloc.json", "txs.json", "env-missingrandom.json", "Merged", "", 234 }, 235 output: t8nOutput{alloc: false, result: false}, 236 expExitCode: 3, 237 }, 238 } { 239 args := []string{"t8n"} 240 args = append(args, tc.output.get()...) 241 args = append(args, tc.input.get(tc.base)...) 242 var qArgs []string // quoted args for debugging purposes 243 for _, arg := range args { 244 if len(arg) == 0 { 245 qArgs = append(qArgs, `""`) 246 } else { 247 qArgs = append(qArgs, arg) 248 } 249 } 250 tt.Logf("args: %v\n", strings.Join(qArgs, " ")) 251 tt.Run("evm-test", args...) 252 // Compare the expected output, if provided 253 if tc.expOut != "" { 254 want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut)) 255 if err != nil { 256 t.Fatalf("test %d: could not read expected output: %v", i, err) 257 } 258 have := tt.Output() 259 ok, err := cmpJson(have, want) 260 switch { 261 case err != nil: 262 t.Fatalf("test %d, json parsing failed: %v", i, err) 263 case !ok: 264 t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want)) 265 } 266 } 267 tt.WaitExit() 268 if have, want := tt.ExitStatus(), tc.expExitCode; have != want { 269 t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want) 270 } 271 } 272 } 273 274 type t9nInput struct { 275 inTxs string 276 stFork string 277 } 278 279 func (args *t9nInput) get(base string) []string { 280 var out []string 281 if opt := args.inTxs; opt != "" { 282 out = append(out, "--input.txs") 283 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 284 } 285 if opt := args.stFork; opt != "" { 286 out = append(out, "--state.fork", opt) 287 } 288 return out 289 } 290 291 func TestT9n(t *testing.T) { 292 tt := new(testT8n) 293 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 294 for i, tc := range []struct { 295 base string 296 input t9nInput 297 expExitCode int 298 expOut string 299 }{ 300 { // London txs on homestead 301 base: "./testdata/15", 302 input: t9nInput{ 303 inTxs: "signed_txs.rlp", 304 stFork: "Homestead", 305 }, 306 expOut: "exp.json", 307 }, 308 { // London txs on London 309 base: "./testdata/15", 310 input: t9nInput{ 311 inTxs: "signed_txs.rlp", 312 stFork: "London", 313 }, 314 expOut: "exp2.json", 315 }, 316 { // An RLP list (a blockheader really) 317 base: "./testdata/15", 318 input: t9nInput{ 319 inTxs: "blockheader.rlp", 320 stFork: "London", 321 }, 322 expOut: "exp3.json", 323 }, 324 { // Transactions with too low gas 325 base: "./testdata/16", 326 input: t9nInput{ 327 inTxs: "signed_txs.rlp", 328 stFork: "London", 329 }, 330 expOut: "exp.json", 331 }, 332 { // Transactions with value exceeding 256 bits 333 base: "./testdata/17", 334 input: t9nInput{ 335 inTxs: "signed_txs.rlp", 336 stFork: "London", 337 }, 338 expOut: "exp.json", 339 }, 340 { // Invalid RLP 341 base: "./testdata/18", 342 input: t9nInput{ 343 inTxs: "invalid.rlp", 344 stFork: "London", 345 }, 346 expExitCode: t8ntool.ErrorIO, 347 }, 348 } { 349 args := []string{"t9n"} 350 args = append(args, tc.input.get(tc.base)...) 351 352 tt.Run("evm-test", args...) 353 tt.Logf("args:\n go run . %v\n", strings.Join(args, " ")) 354 // Compare the expected output, if provided 355 if tc.expOut != "" { 356 want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut)) 357 if err != nil { 358 t.Fatalf("test %d: could not read expected output: %v", i, err) 359 } 360 have := tt.Output() 361 ok, err := cmpJson(have, want) 362 switch { 363 case err != nil: 364 t.Logf(string(have)) 365 t.Fatalf("test %d, json parsing failed: %v", i, err) 366 case !ok: 367 t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want)) 368 } 369 } 370 tt.WaitExit() 371 if have, want := tt.ExitStatus(), tc.expExitCode; have != want { 372 t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want) 373 } 374 } 375 } 376 377 type b11rInput struct { 378 inEnv string 379 inOmmersRlp string 380 inTxsRlp string 381 inClique string 382 ethash bool 383 ethashMode string 384 ethashDir string 385 } 386 387 func (args *b11rInput) get(base string) []string { 388 var out []string 389 if opt := args.inEnv; opt != "" { 390 out = append(out, "--input.header") 391 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 392 } 393 if opt := args.inOmmersRlp; opt != "" { 394 out = append(out, "--input.ommers") 395 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 396 } 397 if opt := args.inTxsRlp; opt != "" { 398 out = append(out, "--input.txs") 399 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 400 } 401 if opt := args.inClique; opt != "" { 402 out = append(out, "--seal.clique") 403 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 404 } 405 if args.ethash { 406 out = append(out, "--seal.ethash") 407 } 408 if opt := args.ethashMode; opt != "" { 409 out = append(out, "--seal.ethash.mode") 410 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 411 } 412 if opt := args.ethashDir; opt != "" { 413 out = append(out, "--seal.ethash.dir") 414 out = append(out, fmt.Sprintf("%v/%v", base, opt)) 415 } 416 out = append(out, "--output.block") 417 out = append(out, "stdout") 418 return out 419 } 420 421 func TestB11r(t *testing.T) { 422 tt := new(testT8n) 423 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 424 for i, tc := range []struct { 425 base string 426 input b11rInput 427 expExitCode int 428 expOut string 429 }{ 430 { // unsealed block 431 base: "./testdata/20", 432 input: b11rInput{ 433 inEnv: "header.json", 434 inOmmersRlp: "ommers.json", 435 inTxsRlp: "txs.rlp", 436 }, 437 expOut: "exp.json", 438 }, 439 { // ethash test seal 440 base: "./testdata/21", 441 input: b11rInput{ 442 inEnv: "header.json", 443 inOmmersRlp: "ommers.json", 444 inTxsRlp: "txs.rlp", 445 }, 446 expOut: "exp.json", 447 }, 448 { // clique test seal 449 base: "./testdata/21", 450 input: b11rInput{ 451 inEnv: "header.json", 452 inOmmersRlp: "ommers.json", 453 inTxsRlp: "txs.rlp", 454 inClique: "clique.json", 455 }, 456 expOut: "exp-clique.json", 457 }, 458 { // block with ommers 459 base: "./testdata/22", 460 input: b11rInput{ 461 inEnv: "header.json", 462 inOmmersRlp: "ommers.json", 463 inTxsRlp: "txs.rlp", 464 }, 465 expOut: "exp.json", 466 }, 467 } { 468 args := []string{"b11r"} 469 args = append(args, tc.input.get(tc.base)...) 470 471 tt.Run("evm-test", args...) 472 tt.Logf("args:\n go run . %v\n", strings.Join(args, " ")) 473 // Compare the expected output, if provided 474 if tc.expOut != "" { 475 want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut)) 476 if err != nil { 477 t.Fatalf("test %d: could not read expected output: %v", i, err) 478 } 479 have := tt.Output() 480 ok, err := cmpJson(have, want) 481 switch { 482 case err != nil: 483 t.Logf(string(have)) 484 t.Fatalf("test %d, json parsing failed: %v", i, err) 485 case !ok: 486 t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want)) 487 } 488 } 489 tt.WaitExit() 490 if have, want := tt.ExitStatus(), tc.expExitCode; have != want { 491 t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want) 492 } 493 } 494 } 495 496 // cmpJson compares the JSON in two byte slices. 497 func cmpJson(a, b []byte) (bool, error) { 498 var j, j2 interface{} 499 if err := json.Unmarshal(a, &j); err != nil { 500 return false, err 501 } 502 if err := json.Unmarshal(b, &j2); err != nil { 503 return false, err 504 } 505 return reflect.DeepEqual(j2, j), nil 506 }