github.com/line/ostracon@v1.0.10-0.20230328032236-7f20145f065d/cmd/ostracon/commands/debug/util.go (about) 1 package debug 2 3 import ( 4 "context" 5 "fmt" 6 "io" 7 "os" 8 "path" 9 "path/filepath" 10 "time" 11 12 cfg "github.com/line/ostracon/config" 13 "github.com/line/ostracon/libs/net" 14 rpchttp "github.com/line/ostracon/rpc/client/http" 15 ) 16 17 // dumpStatus gets node status state dump from the Ostracon RPC and writes it 18 // to file. It returns an error upon failure. 19 func dumpStatus(rpc *rpchttp.HTTP, dir, filename string) error { 20 status, err := rpc.Status(context.Background()) 21 if err != nil { 22 return fmt.Errorf("failed to get node status: %w", err) 23 } 24 25 return writeStateJSONToFile(status, dir, filename) 26 } 27 28 // dumpNetInfo gets network information state dump from the Ostracon RPC and 29 // writes it to file. It returns an error upon failure. 30 func dumpNetInfo(rpc *rpchttp.HTTP, dir, filename string) error { 31 netInfo, err := rpc.NetInfo(context.Background()) 32 if err != nil { 33 return fmt.Errorf("failed to get node network information: %w", err) 34 } 35 36 return writeStateJSONToFile(netInfo, dir, filename) 37 } 38 39 // dumpConsensusState gets consensus state dump from the Ostracon RPC and 40 // writes it to file. It returns an error upon failure. 41 func dumpConsensusState(rpc *rpchttp.HTTP, dir, filename string) error { 42 consDump, err := rpc.DumpConsensusState(context.Background()) 43 if err != nil { 44 return fmt.Errorf("failed to get node consensus dump: %w", err) 45 } 46 47 return writeStateJSONToFile(consDump, dir, filename) 48 } 49 50 // copyWAL copies the Ostracon node's WAL file. It returns an error if the 51 // WAL file cannot be read or copied. 52 func copyWAL(conf *cfg.Config, dir string) error { 53 walPath := conf.Consensus.WalFile() 54 walFile := filepath.Base(walPath) 55 56 return copyFile(walPath, filepath.Join(dir, walFile)) 57 } 58 59 // copyConfig copies the Ostracon node's config file. It returns an error if 60 // the config file cannot be read or copied. 61 func copyConfig(home, dir string) error { 62 configFile := "config.toml" 63 configPath := filepath.Join(home, "config", configFile) 64 65 return copyFile(configPath, filepath.Join(dir, configFile)) 66 } 67 68 func dumpProfile(dir, addr, profile string, debug int) error { 69 endpoint := fmt.Sprintf("%s/debug/pprof/%s?debug=%d", addr, profile, debug) 70 71 resp, err := net.HttpGet(endpoint, time.Duration(frequency)*time.Second) 72 if err != nil { 73 return fmt.Errorf("failed to query for %s profile: %w", profile, err) 74 } 75 defer resp.Body.Close() 76 77 body, err := io.ReadAll(resp.Body) 78 if err != nil { 79 return fmt.Errorf("failed to read %s profile response body: %w", profile, err) 80 } 81 82 return os.WriteFile(path.Join(dir, fmt.Sprintf("%s.out", profile)), body, os.ModePerm) 83 }