Skip to content

Commit

Permalink
fix(node): fixup CELESTIA_HOME (#3759)
Browse files Browse the repository at this point in the history
This mainly fixes the usage of `CELESTIA_HOME` env var. Previously, it worked like `XDG_HOME` in the form of `$CELESTIA_HOME/.celestia-<network>-<type>`. This change makes `$CELESTIA_HOME` the actual path to which node data goes.

This is promoted by me logging into the pods on Robusta and being tired of constantly passing the `--node.store` flag while `CELESTIA_HOME` is already set. 

---

Besides, it contains a minor refactor to use respective types for node and network instead of strings.
  • Loading branch information
Wondertan authored Sep 23, 2024
1 parent 35cd3dd commit 2a9acb4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 39 deletions.
40 changes: 18 additions & 22 deletions cmd/cel-key/node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
flag "github.com/spf13/pflag"

"github.com/celestiaorg/celestia-node/nodebuilder"
nodemod "github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

Expand Down Expand Up @@ -42,32 +43,27 @@ func ParseDirectoryFlags(cmd *cobra.Command) error {
return nil
}

nodeType := cmd.Flag(nodeDirKey).Value.String()
if nodeType == "" {
return errors.New("no node type provided")
nodeTypeStr := cmd.Flag(nodeDirKey).Value.String()
nodeType := nodemod.ParseType(nodeTypeStr)
if !nodeType.IsValid() {
return errors.New("no or invalid node type provided")
}

network := cmd.Flag(networkKey).Value.String()
if net, err := p2p.Network(network).Validate(); err == nil {
network = string(net)
} else {
fmt.Println("WARNING: unknown network specified: ", network)
network, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
switch nodeType {
case "bridge", "full", "light":
path, err := nodebuilder.DefaultNodeStorePath(nodeType, network)
if err != nil {
return err
}

keyPath := fmt.Sprintf("%s/keys", path)
fmt.Println("using directory: ", keyPath)
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, keyPath); err != nil {
return err
}
default:
return fmt.Errorf("node type %s is not valid. Provided node types: (bridge || full || light)",
nodeType)
path, err := nodebuilder.DefaultNodeStorePath(nodeType, network)
if err != nil {
return err
}

keyPath := fmt.Sprintf("%s/keys", path)
fmt.Println("using directory: ", keyPath)
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, keyPath); err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ParseNodeFlags(ctx context.Context, cmd *cobra.Command, network p2p.Network
if store == "" {
tp := NodeType(ctx)
var err error
store, err = nodebuilder.DefaultNodeStorePath(tp.String(), network.String())
store, err = nodebuilder.DefaultNodeStorePath(tp, network)
if err != nil {
return ctx, err
}
Expand Down
25 changes: 13 additions & 12 deletions nodebuilder/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func DiscoverOpened() (string, error) {

for _, n := range defaultNetwork {
for _, tp := range nodeTypes {
path, err := DefaultNodeStorePath(tp.String(), n.String())
path, err := DefaultNodeStorePath(tp, n)
if err != nil {
return "", err
}
Expand All @@ -185,25 +185,26 @@ func DiscoverOpened() (string, error) {

// DefaultNodeStorePath constructs the default node store path using the given
// node type and network.
var DefaultNodeStorePath = func(tp, network string) (string, error) {
var DefaultNodeStorePath = func(tp nodemod.Type, network p2p.Network) (string, error) {
home := os.Getenv("CELESTIA_HOME")
if home != "" {
return home, nil
}

if home == "" {
var err error
home, err = os.UserHomeDir()
if err != nil {
return "", err
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
if network == p2p.Mainnet.String() {
return fmt.Sprintf("%s/.celestia-%s", home, strings.ToLower(tp)), nil

if network == p2p.Mainnet {
return fmt.Sprintf("%s/.celestia-%s", home, strings.ToLower(tp.String())), nil
}
// only include network name in path for testnets and custom networks
return fmt.Sprintf(
"%s/.celestia-%s-%s",
home,
strings.ToLower(tp),
strings.ToLower(network),
strings.ToLower(tp.String()),
strings.ToLower(network.String()),
), nil
}

Expand Down
8 changes: 4 additions & 4 deletions nodebuilder/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestDiscoverOpened(t *testing.T) {
t.Run("single open store", func(t *testing.T) {
_, dir := initAndOpenStore(t, node.Full)

mockDefaultNodeStorePath := func(t, n string) (string, error) {
mockDefaultNodeStorePath := func(t node.Type, n p2p.Network) (string, error) {
return dir, nil
}
DefaultNodeStorePath = mockDefaultNodeStorePath
Expand All @@ -193,8 +193,8 @@ func TestDiscoverOpened(t *testing.T) {
}
}

mockDefaultNodeStorePath := func(tp, n string) (string, error) {
key := n + "_" + tp
mockDefaultNodeStorePath := func(tp node.Type, n p2p.Network) (string, error) {
key := n.String() + "_" + tp.String()
if dir, ok := dirMap[key]; ok {
return dir, nil
}
Expand All @@ -218,7 +218,7 @@ func TestDiscoverOpened(t *testing.T) {

t.Run("no opened store", func(t *testing.T) {
dir := t.TempDir()
mockDefaultNodeStorePath := func(t, n string) (string, error) {
mockDefaultNodeStorePath := func(t node.Type, n p2p.Network) (string, error) {
return dir, nil
}
DefaultNodeStorePath = mockDefaultNodeStorePath
Expand Down

0 comments on commit 2a9acb4

Please sign in to comment.