Skip to content

Commit

Permalink
update: state pruning & optimized config setup.
Browse files Browse the repository at this point in the history
* update: state pruning & optimized config setup.

* chore: pruning cleanup.

* chore: README and constants.

* update: internal version handling.
  • Loading branch information
christopherbrumm authored Jan 24, 2024
1 parent c3ed9af commit c745cbb
Show file tree
Hide file tree
Showing 21 changed files with 644 additions and 293 deletions.
170 changes: 65 additions & 105 deletions .github/README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VERSION := v0.2.3
VERSION := v0.3.0

ldflags := $(LDFLAGS)
ldflags += -X main.Version=$(VERSION)
ldflags += -X main.version=$(VERSION)
ldflags := $(strip $(ldflags))

BUILD_FLAGS := -ldflags '$(ldflags)'
Expand Down
17 changes: 9 additions & 8 deletions cmd/supervysor/backup.go → cmd/supervysor/commands/backup.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package main
package commands

import (
"fmt"
"path/filepath"

"github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers"

"github.com/KYVENetwork/supervysor/utils"

"github.com/KYVENetwork/supervysor/store"

"github.com/KYVENetwork/supervysor/backup"
"github.com/KYVENetwork/supervysor/cmd/supervysor/helpers"
"github.com/spf13/cobra"
)

var (
compressionType string
destPath string
maxBackups int
)

func init() {
backupCmd.Flags().StringVar(&home, "home", "", "path to home directory (e.g. /root/.osmosisd)")
if err := backupCmd.MarkFlagRequired("home"); err != nil {
Expand All @@ -28,12 +25,16 @@ func init() {
backupCmd.Flags().StringVar(&compressionType, "compression", "", "compression type to compress backup directory ['tar.gz', 'zip', '']")

backupCmd.Flags().IntVar(&maxBackups, "max-backups", 0, "number of kept backups (set 0 to keep all)")

backupCmd.Flags().BoolVar(&optOut, "opt-out", false, "disable the collection of anonymous usage data")
}

var backupCmd = &cobra.Command{
Use: "backup",
Short: "Backup data directory",
Run: func(cmd *cobra.Command, args []string) {
utils.TrackBackupEvent(optOut)

backupDir, err := helpers.GetBackupDir()
if err != nil {
logger.Error("failed to get ksync home directory", "err", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"cosmossdk.io/log"

"github.com/spf13/viper"

Expand All @@ -15,6 +19,8 @@ import (
cfg "github.com/tendermint/tendermint/config"
)

var logger = log.NewLogger(os.Stdout)

func CreateDestPath(backupDir string, latestHeight int64) (string, error) {
if err := os.Mkdir(filepath.Join(backupDir, strconv.FormatInt(latestHeight, 10)), 0o755); err != nil {
return "", fmt.Errorf("error creating backup directory: %v", err)
Expand Down Expand Up @@ -76,6 +82,45 @@ func GetBackupDir() (string, error) {
return backupDir, nil
}

func GetHomePathFromBinary(binaryPath string) string {
cmdPath, err := exec.LookPath(binaryPath)
if err != nil {
logger.Error(fmt.Sprintf("failed to lookup binary path: %s", err.Error()))
os.Exit(1)
}

startArgs := make([]string, 0)

// if we run with cosmovisor we start with the cosmovisor run command
if strings.HasSuffix(binaryPath, "cosmovisor") {
startArgs = append(startArgs, "run")
}

out, err := exec.Command(cmdPath, startArgs...).Output()
if err != nil {
logger.Error("failed to get output of binary")
os.Exit(1)
}

// here we search for a specific line in the binary output when simply
// executed without arguments. In the output, the default home path
// is printed, which is parsed and used by KSYNC
for _, line := range strings.Split(string(out), "\n") {
if strings.Contains(line, "--home") {
if strings.Count(line, "\"") != 2 {
logger.Error(fmt.Sprintf("did not found default home path in help line: %s", line))
os.Exit(1)
}

return strings.Split(line, "\"")[1]
}
}

logger.Error("did not found default home path in entire binary output")
os.Exit(1)
return ""
}

func GetSupervysorDir() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
Expand Down
73 changes: 35 additions & 38 deletions cmd/supervysor/init.go → cmd/supervysor/commands/init.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
package main
package commands

import (
"errors"
"fmt"
"os"
"strings"

"golang.org/x/exp/slices"
"github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers"

"github.com/KYVENetwork/supervysor/cmd/supervysor/helpers"
"github.com/KYVENetwork/supervysor/types"
"github.com/KYVENetwork/supervysor/utils"

"golang.org/x/exp/slices"

"github.com/KYVENetwork/supervysor/settings"
"github.com/KYVENetwork/supervysor/types"

"github.com/pelletier/go-toml/v2"

"github.com/spf13/cobra"
)

var (
abciEndpoint string
binary string
chainId string
fallbackEndpoints string
home string
metrics bool
metricsPort int
poolId int
seeds string
pruningInterval int

cfg types.SupervysorConfig
)

func init() {
initCmd.Flags().StringVar(&binary, "binary", "", "path to chain binaries or cosmovisor (e.g. /root/go/bin/cosmovisor)")
initCmd.Flags().StringVarP(&binary, "binary", "b", "", "path to chain binaries or cosmovisor (e.g. /root/go/bin/cosmovisor)")
if err := initCmd.MarkFlagRequired("binary"); err != nil {
panic(fmt.Errorf("flag 'binary-path' should be required: %w", err))
}

initCmd.Flags().StringVar(&home, "home", "", "path to home directory (e.g. /root/.osmosisd)")
if err := initCmd.MarkFlagRequired("home"); err != nil {
panic(fmt.Errorf("flag 'home-path' should be required: %w", err))
}

initCmd.Flags().StringVar(&config, "config", "", "path to config directory (default: ~/.supervysor/)")

initCmd.Flags().IntVar(&poolId, "pool-id", 0, "KYVE pool-id")
if err := initCmd.MarkFlagRequired("pool-id"); err != nil {
Expand All @@ -55,11 +42,15 @@ func init() {

initCmd.Flags().StringVar(&chainId, "chain-id", "kyve-1", "KYVE chain-id")

initCmd.Flags().BoolVar(&optOut, "opt-out", false, "disable the collection of anonymous usage data")

initCmd.Flags().StringVar(&fallbackEndpoints, "fallback-endpoints", "", "additional endpoints to query KYVE pool height")

initCmd.Flags().IntVar(&pruningInterval, "pruning-interval", 24, "block-pruning interval (hours)")

initCmd.Flags().BoolVar(&metrics, "metrics", true, "exposing Prometheus metrics (true or false)")
initCmd.Flags().BoolVar(&statePruning, "state-pruning", true, "state pruning enabled")

initCmd.Flags().BoolVar(&metrics, "metrics", false, "exposing Prometheus metrics (true or false)")

initCmd.Flags().IntVar(&metricsPort, "metrics-port", 26660, "port for metrics server")

Expand All @@ -76,25 +67,31 @@ var initCmd = &cobra.Command{
return fmt.Errorf("not supported chain-id")
}

// if no home path was given get the default one
if home == "" {
logger.Error("home directory can not be empty")
return fmt.Errorf("empty home directory path")
home = helpers.GetHomePathFromBinary(binary)
}

if pruningInterval <= 6 {
logger.Error("pruning-interval should be higher than 6 hours")
}

utils.TrackInitEvent(chainId, optOut)

if err := settings.InitializeSettings(binary, home, poolId, false, seeds, chainId, fallbackEndpoints); err != nil {
logger.Error("could not initialize settings", "err", err)
return err
}
logger.Info("successfully initialized settings")

configPath, err := helpers.GetSupervysorDir()
if err != nil {
logger.Error("could not get supervysor directory path", "err", err)
return err
if config == "" {
configPath, err = helpers.GetSupervysorDir()
if err != nil {
logger.Error("could not get supervysor directory path", "err", err)
return err
}
} else {
configPath = config
}

if _, err = os.Stat(configPath + "/config.toml"); err == nil {
Expand All @@ -109,7 +106,7 @@ var initCmd = &cobra.Command{
}
logger.Info("initializing supverysor...")

config := types.SupervysorConfig{
supervysorConfig := types.SupervysorConfig{
ABCIEndpoint: abciEndpoint,
BinaryPath: binary,
ChainId: chainId,
Expand All @@ -123,9 +120,10 @@ var initCmd = &cobra.Command{
PoolId: poolId,
PruningInterval: pruningInterval,
Seeds: seeds,
StatePruning: statePruning,
StateRequests: false,
}
b, err := toml.Marshal(config)
b, err := toml.Marshal(supervysorConfig)
if err != nil {
logger.Error("could not unmarshal config", "err", err)
return err
Expand All @@ -137,7 +135,7 @@ var initCmd = &cobra.Command{
return err
}

_, err = getSupervysorConfig()
_, err = getSupervysorConfig(configPath)
if err != nil {
logger.Error("could not load config file", "err", err)
return err
Expand All @@ -153,13 +151,12 @@ var initCmd = &cobra.Command{
}

// getSupervysorConfig returns the supervysor config.toml file.
func getSupervysorConfig() (*types.SupervysorConfig, error) {
configPath, err := helpers.GetSupervysorDir()
if err != nil {
return nil, fmt.Errorf("could not get supervysor directory path: %s", err)
func getSupervysorConfig(configPath string) (*types.SupervysorConfig, error) {
if !strings.HasSuffix(configPath, "/config.toml") {
configPath += "/config.toml"
}

data, err := os.ReadFile(configPath + "/config.toml")
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("could not find config. Please initialize again: %s", err)
}
Expand Down
38 changes: 38 additions & 0 deletions cmd/supervysor/commands/prune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package commands

import (
"fmt"

"github.com/KYVENetwork/supervysor/utils"

"github.com/KYVENetwork/supervysor/store"
"github.com/spf13/cobra"
)

func init() {
pruneCmd.Flags().StringVar(&home, "home", "", "path to home directory (e.g. /root/.osmosisd)")
if err := pruneCmd.MarkFlagRequired("home"); err != nil {
panic(fmt.Errorf("flag 'home' should be required: %w", err))
}

pruneCmd.Flags().Int64Var(&untilHeight, "until-height", 0, "prune blocks until specified height (excluding)")
if err := pruneCmd.MarkFlagRequired("until-height"); err != nil {
panic(fmt.Errorf("flag 'until-height' should be required: %w", err))
}

pruneCmd.Flags().BoolVar(&statePruning, "state-pruning", true, "state pruning enabled")

pruneCmd.Flags().BoolVar(&optOut, "opt-out", false, "disable the collection of anonymous usage data")
}

var pruneCmd = &cobra.Command{
Use: "prune",
Short: "Prune blocks and optionally state from base height until a specific height",
Run: func(cmd *cobra.Command, args []string) {
utils.TrackPruneEvent(optOut)

if err := store.Prune(home, untilHeight, statePruning, logger); err != nil {
logger.Error(err.Error())
}
},
}
Loading

0 comments on commit c745cbb

Please sign in to comment.