Skip to content

Commit c745cbb

Browse files
update: state pruning & optimized config setup.
* update: state pruning & optimized config setup. * chore: pruning cleanup. * chore: README and constants. * update: internal version handling.
1 parent c3ed9af commit c745cbb

File tree

21 files changed

+644
-293
lines changed

21 files changed

+644
-293
lines changed

.github/README.md

Lines changed: 65 additions & 105 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
VERSION := v0.2.3
1+
VERSION := v0.3.0
22

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

77
BUILD_FLAGS := -ldflags '$(ldflags)'
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
package main
1+
package commands
22

33
import (
44
"fmt"
55
"path/filepath"
66

7+
"github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers"
8+
9+
"github.com/KYVENetwork/supervysor/utils"
10+
711
"github.com/KYVENetwork/supervysor/store"
812

913
"github.com/KYVENetwork/supervysor/backup"
10-
"github.com/KYVENetwork/supervysor/cmd/supervysor/helpers"
1114
"github.com/spf13/cobra"
1215
)
1316

14-
var (
15-
compressionType string
16-
destPath string
17-
maxBackups int
18-
)
19-
2017
func init() {
2118
backupCmd.Flags().StringVar(&home, "home", "", "path to home directory (e.g. /root/.osmosisd)")
2219
if err := backupCmd.MarkFlagRequired("home"); err != nil {
@@ -28,12 +25,16 @@ func init() {
2825
backupCmd.Flags().StringVar(&compressionType, "compression", "", "compression type to compress backup directory ['tar.gz', 'zip', '']")
2926

3027
backupCmd.Flags().IntVar(&maxBackups, "max-backups", 0, "number of kept backups (set 0 to keep all)")
28+
29+
backupCmd.Flags().BoolVar(&optOut, "opt-out", false, "disable the collection of anonymous usage data")
3130
}
3231

3332
var backupCmd = &cobra.Command{
3433
Use: "backup",
3534
Short: "Backup data directory",
3635
Run: func(cmd *cobra.Command, args []string) {
36+
utils.TrackBackupEvent(optOut)
37+
3738
backupDir, err := helpers.GetBackupDir()
3839
if err != nil {
3940
logger.Error("failed to get ksync home directory", "err", err)

cmd/supervysor/helpers/helpers.go renamed to cmd/supervysor/commands/helpers/helpers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import (
44
"fmt"
55
"net/http"
66
"os"
7+
"os/exec"
78
"path/filepath"
89
"strconv"
10+
"strings"
11+
12+
"cosmossdk.io/log"
913

1014
"github.com/spf13/viper"
1115

@@ -15,6 +19,8 @@ import (
1519
cfg "github.com/tendermint/tendermint/config"
1620
)
1721

22+
var logger = log.NewLogger(os.Stdout)
23+
1824
func CreateDestPath(backupDir string, latestHeight int64) (string, error) {
1925
if err := os.Mkdir(filepath.Join(backupDir, strconv.FormatInt(latestHeight, 10)), 0o755); err != nil {
2026
return "", fmt.Errorf("error creating backup directory: %v", err)
@@ -76,6 +82,45 @@ func GetBackupDir() (string, error) {
7682
return backupDir, nil
7783
}
7884

85+
func GetHomePathFromBinary(binaryPath string) string {
86+
cmdPath, err := exec.LookPath(binaryPath)
87+
if err != nil {
88+
logger.Error(fmt.Sprintf("failed to lookup binary path: %s", err.Error()))
89+
os.Exit(1)
90+
}
91+
92+
startArgs := make([]string, 0)
93+
94+
// if we run with cosmovisor we start with the cosmovisor run command
95+
if strings.HasSuffix(binaryPath, "cosmovisor") {
96+
startArgs = append(startArgs, "run")
97+
}
98+
99+
out, err := exec.Command(cmdPath, startArgs...).Output()
100+
if err != nil {
101+
logger.Error("failed to get output of binary")
102+
os.Exit(1)
103+
}
104+
105+
// here we search for a specific line in the binary output when simply
106+
// executed without arguments. In the output, the default home path
107+
// is printed, which is parsed and used by KSYNC
108+
for _, line := range strings.Split(string(out), "\n") {
109+
if strings.Contains(line, "--home") {
110+
if strings.Count(line, "\"") != 2 {
111+
logger.Error(fmt.Sprintf("did not found default home path in help line: %s", line))
112+
os.Exit(1)
113+
}
114+
115+
return strings.Split(line, "\"")[1]
116+
}
117+
}
118+
119+
logger.Error("did not found default home path in entire binary output")
120+
os.Exit(1)
121+
return ""
122+
}
123+
79124
func GetSupervysorDir() (string, error) {
80125
home, err := os.UserHomeDir()
81126
if err != nil {
Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,34 @@
1-
package main
1+
package commands
22

33
import (
44
"errors"
55
"fmt"
66
"os"
7+
"strings"
78

8-
"golang.org/x/exp/slices"
9+
"github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers"
910

10-
"github.com/KYVENetwork/supervysor/cmd/supervysor/helpers"
11-
"github.com/KYVENetwork/supervysor/types"
11+
"github.com/KYVENetwork/supervysor/utils"
12+
13+
"golang.org/x/exp/slices"
1214

1315
"github.com/KYVENetwork/supervysor/settings"
16+
"github.com/KYVENetwork/supervysor/types"
1417

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

1720
"github.com/spf13/cobra"
1821
)
1922

20-
var (
21-
abciEndpoint string
22-
binary string
23-
chainId string
24-
fallbackEndpoints string
25-
home string
26-
metrics bool
27-
metricsPort int
28-
poolId int
29-
seeds string
30-
pruningInterval int
31-
32-
cfg types.SupervysorConfig
33-
)
34-
3523
func init() {
36-
initCmd.Flags().StringVar(&binary, "binary", "", "path to chain binaries or cosmovisor (e.g. /root/go/bin/cosmovisor)")
24+
initCmd.Flags().StringVarP(&binary, "binary", "b", "", "path to chain binaries or cosmovisor (e.g. /root/go/bin/cosmovisor)")
3725
if err := initCmd.MarkFlagRequired("binary"); err != nil {
3826
panic(fmt.Errorf("flag 'binary-path' should be required: %w", err))
3927
}
4028

4129
initCmd.Flags().StringVar(&home, "home", "", "path to home directory (e.g. /root/.osmosisd)")
42-
if err := initCmd.MarkFlagRequired("home"); err != nil {
43-
panic(fmt.Errorf("flag 'home-path' should be required: %w", err))
44-
}
30+
31+
initCmd.Flags().StringVar(&config, "config", "", "path to config directory (default: ~/.supervysor/)")
4532

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

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

45+
initCmd.Flags().BoolVar(&optOut, "opt-out", false, "disable the collection of anonymous usage data")
46+
5847
initCmd.Flags().StringVar(&fallbackEndpoints, "fallback-endpoints", "", "additional endpoints to query KYVE pool height")
5948

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

62-
initCmd.Flags().BoolVar(&metrics, "metrics", true, "exposing Prometheus metrics (true or false)")
51+
initCmd.Flags().BoolVar(&statePruning, "state-pruning", true, "state pruning enabled")
52+
53+
initCmd.Flags().BoolVar(&metrics, "metrics", false, "exposing Prometheus metrics (true or false)")
6354

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

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

70+
// if no home path was given get the default one
7971
if home == "" {
80-
logger.Error("home directory can not be empty")
81-
return fmt.Errorf("empty home directory path")
72+
home = helpers.GetHomePathFromBinary(binary)
8273
}
8374

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

79+
utils.TrackInitEvent(chainId, optOut)
80+
8881
if err := settings.InitializeSettings(binary, home, poolId, false, seeds, chainId, fallbackEndpoints); err != nil {
8982
logger.Error("could not initialize settings", "err", err)
9083
return err
9184
}
9285
logger.Info("successfully initialized settings")
9386

94-
configPath, err := helpers.GetSupervysorDir()
95-
if err != nil {
96-
logger.Error("could not get supervysor directory path", "err", err)
97-
return err
87+
if config == "" {
88+
configPath, err = helpers.GetSupervysorDir()
89+
if err != nil {
90+
logger.Error("could not get supervysor directory path", "err", err)
91+
return err
92+
}
93+
} else {
94+
configPath = config
9895
}
9996

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

112-
config := types.SupervysorConfig{
109+
supervysorConfig := types.SupervysorConfig{
113110
ABCIEndpoint: abciEndpoint,
114111
BinaryPath: binary,
115112
ChainId: chainId,
@@ -123,9 +120,10 @@ var initCmd = &cobra.Command{
123120
PoolId: poolId,
124121
PruningInterval: pruningInterval,
125122
Seeds: seeds,
123+
StatePruning: statePruning,
126124
StateRequests: false,
127125
}
128-
b, err := toml.Marshal(config)
126+
b, err := toml.Marshal(supervysorConfig)
129127
if err != nil {
130128
logger.Error("could not unmarshal config", "err", err)
131129
return err
@@ -137,7 +135,7 @@ var initCmd = &cobra.Command{
137135
return err
138136
}
139137

140-
_, err = getSupervysorConfig()
138+
_, err = getSupervysorConfig(configPath)
141139
if err != nil {
142140
logger.Error("could not load config file", "err", err)
143141
return err
@@ -153,13 +151,12 @@ var initCmd = &cobra.Command{
153151
}
154152

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

162-
data, err := os.ReadFile(configPath + "/config.toml")
159+
data, err := os.ReadFile(configPath)
163160
if err != nil {
164161
return nil, fmt.Errorf("could not find config. Please initialize again: %s", err)
165162
}

cmd/supervysor/commands/prune.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/KYVENetwork/supervysor/utils"
7+
8+
"github.com/KYVENetwork/supervysor/store"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func init() {
13+
pruneCmd.Flags().StringVar(&home, "home", "", "path to home directory (e.g. /root/.osmosisd)")
14+
if err := pruneCmd.MarkFlagRequired("home"); err != nil {
15+
panic(fmt.Errorf("flag 'home' should be required: %w", err))
16+
}
17+
18+
pruneCmd.Flags().Int64Var(&untilHeight, "until-height", 0, "prune blocks until specified height (excluding)")
19+
if err := pruneCmd.MarkFlagRequired("until-height"); err != nil {
20+
panic(fmt.Errorf("flag 'until-height' should be required: %w", err))
21+
}
22+
23+
pruneCmd.Flags().BoolVar(&statePruning, "state-pruning", true, "state pruning enabled")
24+
25+
pruneCmd.Flags().BoolVar(&optOut, "opt-out", false, "disable the collection of anonymous usage data")
26+
}
27+
28+
var pruneCmd = &cobra.Command{
29+
Use: "prune",
30+
Short: "Prune blocks and optionally state from base height until a specific height",
31+
Run: func(cmd *cobra.Command, args []string) {
32+
utils.TrackPruneEvent(optOut)
33+
34+
if err := store.Prune(home, untilHeight, statePruning, logger); err != nil {
35+
logger.Error(err.Error())
36+
}
37+
},
38+
}

0 commit comments

Comments
 (0)