github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/cmd/geth/chaincmd_test.go (about) 1 package main 2 3 import ( 4 "io" 5 "net/http" 6 "net/http/httptest" 7 "os" 8 "testing" 9 ) 10 11 func TestChainInit(t *testing.T) { 12 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 13 w.WriteHeader(200) 14 f, err := os.Open("testdata/init.json") 15 if err != nil { 16 panic(err) 17 } 18 defer f.Close() 19 io.Copy(w, f) 20 })) 21 22 tests := []struct { 23 name string 24 url string 25 hash string 26 errorMsg string 27 }{ 28 { 29 "no genesis hash specified", 30 server.URL, 31 "", 32 "Must specify a genesis hash argument if the genesis path argument is an URL", 33 }, 34 { 35 "invalid genesis hash specified", 36 server.URL, 37 "not hex yo", 38 "Error decoding genesis hash", 39 }, 40 { 41 "bad URL", 42 "https://honk", 43 "0x1234", 44 "Failed to fetch genesis file", 45 }, 46 { 47 "mis-matched hashes", 48 server.URL, 49 "0x1234", 50 "Genesis hashes do not match", 51 }, 52 } 53 for _, tt := range tests { 54 t.Run(tt.name, func(t *testing.T) { 55 datadir := tmpdir(t) 56 geth := runGeth(t, "init", tt.url, tt.hash, "--datadir", datadir) 57 geth.ExpectRegexp(tt.errorMsg) 58 }) 59 } 60 61 t.Run("URL and hash args OK", func(t *testing.T) { 62 datadir := tmpdir(t) 63 geth := runGeth(t, "init", server.URL, "0x1f0201852c30e203a701ac283aeafafaf55b2ad3ae2f4e8f15c61e761434fb62", "--datadir", datadir) 64 geth.ExpectExit() 65 geth = runGeth(t, "dump-chain-cfg", "--datadir", datadir) 66 geth.ExpectRegexp("\"muirGlacierBlock\": 500") 67 }) 68 69 t.Run("file arg OK", func(t *testing.T) { 70 datadir := tmpdir(t) 71 geth := runGeth(t, "init", "testdata/init.json", "--datadir", datadir) 72 geth.ExpectExit() 73 geth = runGeth(t, "dump-chain-cfg", "--datadir", datadir) 74 geth.ExpectRegexp("\"muirGlacierBlock\": 500") 75 }) 76 } 77 78 func TestDumpChainCfg(t *testing.T) { 79 datadir := tmpdir(t) 80 geth := runGeth(t, "init", "testdata/init.json", "--datadir", datadir) 81 geth.ExpectExit() 82 geth = runGeth(t, "dump-chain-cfg", "--datadir", datadir) 83 geth.Expect(`{ 84 "chainId": 69, 85 "homesteadBlock": 0, 86 "eip150Block": 0, 87 "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000", 88 "eip155Block": 0, 89 "eip158Block": 0, 90 "byzantiumBlock": 0, 91 "constantinopleBlock": 0, 92 "petersburgBlock": 0, 93 "istanbulBlock": 0, 94 "muirGlacierBlock": 500, 95 "clique": { 96 "period": 0, 97 "epoch": 30000 98 } 99 }`) 100 }