Skip to content

Commit 2a9acb4

Browse files
authored
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-<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.
1 parent 35cd3dd commit 2a9acb4

File tree

4 files changed

+36
-39
lines changed

4 files changed

+36
-39
lines changed

cmd/cel-key/node_types.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
flag "github.com/spf13/pflag"
1111

1212
"github.com/celestiaorg/celestia-node/nodebuilder"
13+
nodemod "github.com/celestiaorg/celestia-node/nodebuilder/node"
1314
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
1415
)
1516

@@ -42,32 +43,27 @@ func ParseDirectoryFlags(cmd *cobra.Command) error {
4243
return nil
4344
}
4445

45-
nodeType := cmd.Flag(nodeDirKey).Value.String()
46-
if nodeType == "" {
47-
return errors.New("no node type provided")
46+
nodeTypeStr := cmd.Flag(nodeDirKey).Value.String()
47+
nodeType := nodemod.ParseType(nodeTypeStr)
48+
if !nodeType.IsValid() {
49+
return errors.New("no or invalid node type provided")
4850
}
4951

50-
network := cmd.Flag(networkKey).Value.String()
51-
if net, err := p2p.Network(network).Validate(); err == nil {
52-
network = string(net)
53-
} else {
54-
fmt.Println("WARNING: unknown network specified: ", network)
52+
network, err := p2p.ParseNetwork(cmd)
53+
if err != nil {
54+
return err
5555
}
56-
switch nodeType {
57-
case "bridge", "full", "light":
58-
path, err := nodebuilder.DefaultNodeStorePath(nodeType, network)
59-
if err != nil {
60-
return err
61-
}
6256

63-
keyPath := fmt.Sprintf("%s/keys", path)
64-
fmt.Println("using directory: ", keyPath)
65-
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, keyPath); err != nil {
66-
return err
67-
}
68-
default:
69-
return fmt.Errorf("node type %s is not valid. Provided node types: (bridge || full || light)",
70-
nodeType)
57+
path, err := nodebuilder.DefaultNodeStorePath(nodeType, network)
58+
if err != nil {
59+
return err
7160
}
61+
62+
keyPath := fmt.Sprintf("%s/keys", path)
63+
fmt.Println("using directory: ", keyPath)
64+
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, keyPath); err != nil {
65+
return err
66+
}
67+
7268
return nil
7369
}

cmd/flags_node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ParseNodeFlags(ctx context.Context, cmd *cobra.Command, network p2p.Network
4242
if store == "" {
4343
tp := NodeType(ctx)
4444
var err error
45-
store, err = nodebuilder.DefaultNodeStorePath(tp.String(), network.String())
45+
store, err = nodebuilder.DefaultNodeStorePath(tp, network)
4646
if err != nil {
4747
return ctx, err
4848
}

nodebuilder/store.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func DiscoverOpened() (string, error) {
168168

169169
for _, n := range defaultNetwork {
170170
for _, tp := range nodeTypes {
171-
path, err := DefaultNodeStorePath(tp.String(), n.String())
171+
path, err := DefaultNodeStorePath(tp, n)
172172
if err != nil {
173173
return "", err
174174
}
@@ -185,25 +185,26 @@ func DiscoverOpened() (string, error) {
185185

186186
// DefaultNodeStorePath constructs the default node store path using the given
187187
// node type and network.
188-
var DefaultNodeStorePath = func(tp, network string) (string, error) {
188+
var DefaultNodeStorePath = func(tp nodemod.Type, network p2p.Network) (string, error) {
189189
home := os.Getenv("CELESTIA_HOME")
190+
if home != "" {
191+
return home, nil
192+
}
190193

191-
if home == "" {
192-
var err error
193-
home, err = os.UserHomeDir()
194-
if err != nil {
195-
return "", err
196-
}
194+
home, err := os.UserHomeDir()
195+
if err != nil {
196+
return "", err
197197
}
198-
if network == p2p.Mainnet.String() {
199-
return fmt.Sprintf("%s/.celestia-%s", home, strings.ToLower(tp)), nil
198+
199+
if network == p2p.Mainnet {
200+
return fmt.Sprintf("%s/.celestia-%s", home, strings.ToLower(tp.String())), nil
200201
}
201202
// only include network name in path for testnets and custom networks
202203
return fmt.Sprintf(
203204
"%s/.celestia-%s-%s",
204205
home,
205-
strings.ToLower(tp),
206-
strings.ToLower(network),
206+
strings.ToLower(tp.String()),
207+
strings.ToLower(network.String()),
207208
), nil
208209
}
209210

nodebuilder/store_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestDiscoverOpened(t *testing.T) {
167167
t.Run("single open store", func(t *testing.T) {
168168
_, dir := initAndOpenStore(t, node.Full)
169169

170-
mockDefaultNodeStorePath := func(t, n string) (string, error) {
170+
mockDefaultNodeStorePath := func(t node.Type, n p2p.Network) (string, error) {
171171
return dir, nil
172172
}
173173
DefaultNodeStorePath = mockDefaultNodeStorePath
@@ -193,8 +193,8 @@ func TestDiscoverOpened(t *testing.T) {
193193
}
194194
}
195195

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

219219
t.Run("no opened store", func(t *testing.T) {
220220
dir := t.TempDir()
221-
mockDefaultNodeStorePath := func(t, n string) (string, error) {
221+
mockDefaultNodeStorePath := func(t node.Type, n p2p.Network) (string, error) {
222222
return dir, nil
223223
}
224224
DefaultNodeStorePath = mockDefaultNodeStorePath

0 commit comments

Comments
 (0)