Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): fixup CELESTIA_HOME #3759

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading