From 2a9acb45a1315aa9ec1146b5c0b95cb7d9e4271a Mon Sep 17 00:00:00 2001 From: Hlib Kanunnikov Date: Mon, 23 Sep 2024 16:11:41 +0200 Subject: [PATCH] fix(node): fixup CELESTIA_HOME (#3759) This mainly fixes the usage of `CELESTIA_HOME` env var. Previously, it worked like `XDG_HOME` in the form of `$CELESTIA_HOME/.celestia--`. 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. --- cmd/cel-key/node_types.go | 40 ++++++++++++++++++--------------------- cmd/flags_node.go | 2 +- nodebuilder/store.go | 25 ++++++++++++------------ nodebuilder/store_test.go | 8 ++++---- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/cmd/cel-key/node_types.go b/cmd/cel-key/node_types.go index a70889a263..470ef596e7 100644 --- a/cmd/cel-key/node_types.go +++ b/cmd/cel-key/node_types.go @@ -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" ) @@ -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 } diff --git a/cmd/flags_node.go b/cmd/flags_node.go index bc86cb4a27..a6736f8e85 100644 --- a/cmd/flags_node.go +++ b/cmd/flags_node.go @@ -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 } diff --git a/nodebuilder/store.go b/nodebuilder/store.go index 9d8af29075..e9a33d122e 100644 --- a/nodebuilder/store.go +++ b/nodebuilder/store.go @@ -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 } @@ -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 } diff --git a/nodebuilder/store_test.go b/nodebuilder/store_test.go index a7968a6d72..45a9c3960a 100644 --- a/nodebuilder/store_test.go +++ b/nodebuilder/store_test.go @@ -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 @@ -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 } @@ -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