github.com/devwanda/aphelion-staking@v0.33.9/cmd/tendermint/commands/debug/util.go (about)

     1  package debug
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	cfg "github.com/devwanda/aphelion-staking/config"
    14  	rpchttp "github.com/devwanda/aphelion-staking/rpc/client/http"
    15  )
    16  
    17  // dumpStatus gets node status state dump from the Tendermint 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()
    21  	if err != nil {
    22  		return errors.Wrap(err, "failed to get node status")
    23  	}
    24  
    25  	return writeStateJSONToFile(status, dir, filename)
    26  }
    27  
    28  // dumpNetInfo gets network information state dump from the Tendermint 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()
    32  	if err != nil {
    33  		return errors.Wrap(err, "failed to get node network information")
    34  	}
    35  
    36  	return writeStateJSONToFile(netInfo, dir, filename)
    37  }
    38  
    39  // dumpConsensusState gets consensus state dump from the Tendermint 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()
    43  	if err != nil {
    44  		return errors.Wrap(err, "failed to get node consensus dump")
    45  	}
    46  
    47  	return writeStateJSONToFile(consDump, dir, filename)
    48  }
    49  
    50  // copyWAL copies the Tendermint 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 Tendermint 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 := http.Get(endpoint) // nolint: gosec
    72  	if err != nil {
    73  		return errors.Wrapf(err, "failed to query for %s profile", profile)
    74  	}
    75  	defer resp.Body.Close()
    76  
    77  	body, err := ioutil.ReadAll(resp.Body)
    78  	if err != nil {
    79  		return errors.Wrapf(err, "failed to read %s profile response body", profile)
    80  	}
    81  
    82  	return ioutil.WriteFile(path.Join(dir, fmt.Sprintf("%s.out", profile)), body, os.ModePerm)
    83  }