github.com/vipernet-xyz/tm@v0.34.24/cmd/tendermint/commands/debug/util.go (about)

     1  package debug
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  
    12  	cfg "github.com/vipernet-xyz/tm/config"
    13  	rpchttp "github.com/vipernet-xyz/tm/rpc/client/http"
    14  )
    15  
    16  // dumpStatus gets node status state dump from the Tendermint RPC and writes it
    17  // to file. It returns an error upon failure.
    18  func dumpStatus(rpc *rpchttp.HTTP, dir, filename string) error {
    19  	status, err := rpc.Status(context.Background())
    20  	if err != nil {
    21  		return fmt.Errorf("failed to get node status: %w", err)
    22  	}
    23  
    24  	return writeStateJSONToFile(status, dir, filename)
    25  }
    26  
    27  // dumpNetInfo gets network information state dump from the Tendermint RPC and
    28  // writes it to file. It returns an error upon failure.
    29  func dumpNetInfo(rpc *rpchttp.HTTP, dir, filename string) error {
    30  	netInfo, err := rpc.NetInfo(context.Background())
    31  	if err != nil {
    32  		return fmt.Errorf("failed to get node network information: %w", err)
    33  	}
    34  
    35  	return writeStateJSONToFile(netInfo, dir, filename)
    36  }
    37  
    38  // dumpConsensusState gets consensus state dump from the Tendermint RPC and
    39  // writes it to file. It returns an error upon failure.
    40  func dumpConsensusState(rpc *rpchttp.HTTP, dir, filename string) error {
    41  	consDump, err := rpc.DumpConsensusState(context.Background())
    42  	if err != nil {
    43  		return fmt.Errorf("failed to get node consensus dump: %w", err)
    44  	}
    45  
    46  	return writeStateJSONToFile(consDump, dir, filename)
    47  }
    48  
    49  // copyWAL copies the Tendermint node's WAL file. It returns an error if the
    50  // WAL file cannot be read or copied.
    51  func copyWAL(conf *cfg.Config, dir string) error {
    52  	walPath := conf.Consensus.WalFile()
    53  	walFile := filepath.Base(walPath)
    54  
    55  	return copyFile(walPath, filepath.Join(dir, walFile))
    56  }
    57  
    58  // copyConfig copies the Tendermint node's config file. It returns an error if
    59  // the config file cannot be read or copied.
    60  func copyConfig(home, dir string) error {
    61  	configFile := "config.toml"
    62  	configPath := filepath.Join(home, "config", configFile)
    63  
    64  	return copyFile(configPath, filepath.Join(dir, configFile))
    65  }
    66  
    67  func dumpProfile(dir, addr, profile string, debug int) error {
    68  	endpoint := fmt.Sprintf("%s/debug/pprof/%s?debug=%d", addr, profile, debug)
    69  
    70  	//nolint:gosec,nolintlint
    71  	resp, err := http.Get(endpoint)
    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  }