Skip to content

Commit

Permalink
feat(cmd): adding network flag, deprecating CELESTIA_PRIVATE_GENESIS,…
Browse files Browse the repository at this point in the history
… changing default store extension
  • Loading branch information
distractedm1nd committed Sep 12, 2022
1 parent 8657edc commit 0a341f5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
38 changes: 21 additions & 17 deletions cmd/cel-key/node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@ package main

import (
"fmt"
"strings"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/celestiaorg/celestia-node/params"

sdkflags "github.com/cosmos/cosmos-sdk/client/flags"
)

var (
nodeDirKey = "node.type"

bridgeDir = "~/.celestia-bridge/keys"
fullDir = "~/.celestia-full/keys"
lightDir = "~/.celestia-light/keys"
nodeDirKey = "node.type"
nodeNetworkKey = "node.network"
)

func DirectoryFlags() *flag.FlagSet {
flags := &flag.FlagSet{}
flags.String(nodeDirKey, "", "Sets key utility to use the node type's directory (e.g. "+
"~/.celestia-light if --node.type light is passed).")
defaultNetwork := string(params.DefaultNetwork())

flags.String(
nodeDirKey,
"",
"Sets key utility to use the node type's directory (e.g. "+
"~/.celestia-light-"+strings.ToLower(defaultNetwork)+" if --node.type light is passed).")
flags.String(
nodeNetworkKey,
defaultNetwork,
"Sets key utility to use the node network's directory (e.g. "+
"~/.celestia-light-mynetwork if --node.network MyNetwork is passed).")

return flags
}
Expand All @@ -31,17 +41,11 @@ func ParseDirectoryFlags(cmd *cobra.Command) error {
return nil
}

network := cmd.Flag(nodeNetworkKey).Value.String()
switch nodeType {
case "bridge":
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, bridgeDir); err != nil {
return err
}
case "full":
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, fullDir); err != nil {
return err
}
case "light":
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, lightDir); err != nil {
case "bridge", "full", "light":
keyPath := fmt.Sprintf("~/.celestia-%s-%s", nodeType, strings.ToLower(network))
if err := cmd.Flags().Set(sdkflags.FlagKeyringDir, keyPath); err != nil {
return err
}
default:
Expand Down
12 changes: 8 additions & 4 deletions cmd/cel-shed/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var headerCmd = &cobra.Command{
}

var headerStoreInit = &cobra.Command{
Use: "store-init [node-type] [height]",
Use: "store-init [node-type] [network] [height]",
Short: `Forcefully initialize header store head to be of the given height. Requires the node being stopped.
Custom store path is not supported yet.`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
if len(args) != 3 {
return fmt.Errorf("not enough arguments")
}

Expand All @@ -35,12 +35,16 @@ Custom store path is not supported yet.`,
return fmt.Errorf("invalid node-type")
}

height, err := strconv.Atoi(args[1])
network := args[1]

height, err := strconv.Atoi(args[2])
if err != nil {
return fmt.Errorf("invalid height: %w", err)
}

s, err := node.OpenStore(fmt.Sprintf("~/.celestia-%s", strings.ToLower(tp.String())))
s, err := node.OpenStore(
fmt.Sprintf("~/.celestia-%s-%s", strings.ToLower(tp.String()), strings.ToLower(network)),
)
if err != nil {
return err
}
Expand Down
27 changes: 23 additions & 4 deletions cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/celestiaorg/celestia-node/params"
"strings"

"github.com/spf13/cobra"
Expand All @@ -12,8 +13,9 @@ import (
)

var (
nodeStoreFlag = "node.store"
nodeConfigFlag = "node.config"
nodeStoreFlag = "node.store"
nodeConfigFlag = "node.config"
nodeNetworkFlag = "node.network"
)

// NodeFlags gives a set of hardcoded Node package flags.
Expand All @@ -22,21 +24,38 @@ func NodeFlags(tp node.Type) *flag.FlagSet {

flags.String(
nodeStoreFlag,
fmt.Sprintf("~/.celestia-%s", strings.ToLower(tp.String())),
"",
"The path to root/home directory of your Celestia Node Store",
)
flags.String(
nodeConfigFlag,
"",
"Path to a customized node config TOML file",
)
flags.String(
nodeNetworkFlag,
"",
"The name of the network to connect to",
)

return flags
}

// ParseNodeFlags parses Node flags from the given cmd and applies values to Env.
func ParseNodeFlags(ctx context.Context, cmd *cobra.Command) (context.Context, error) {
ctx = WithStorePath(ctx, cmd.Flag(nodeStoreFlag).Value.String())
network := cmd.Flag(nodeNetworkFlag).Value.String()
if network != "" {
params.SetDefaultNetwork(params.Network(network))
} else {
network = string(params.DefaultNetwork())
}

store := cmd.Flag(nodeStoreFlag).Value.String()
if store == "" {
tp := NodeType(ctx)
store = fmt.Sprintf("~/.celestia-%s-%s", strings.ToLower(tp.String()), strings.ToLower(network))
}
ctx = WithStorePath(ctx, store)

nodeConfig := cmd.Flag(nodeConfigFlag).Value.String()
if nodeConfig != "" {
Expand Down
14 changes: 6 additions & 8 deletions params/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
)

const (
EnvCustomNetwork = "CELESTIA_CUSTOM"
EnvPrivateGenesis = "CELESTIA_PRIVATE_GENESIS"
EnvCustomNetwork = "CELESTIA_CUSTOM"
)

// defaultNetwork defines a default network for the Celestia Node.
Expand All @@ -19,6 +18,10 @@ func DefaultNetwork() Network {
return defaultNetwork
}

func SetDefaultNetwork(net Network) {
defaultNetwork = net
}

func init() {
// check if custom network option set
// format: CELESTIA_CUSTOM=<netID>:<genesisHash>:<bootstrapPeerList>
Expand All @@ -32,7 +35,7 @@ func init() {
panic("params: must provide at least <network_ID> to use a custom network")
}
netID := params[0]
defaultNetwork = Network(netID)
SetDefaultNetwork(Network(netID))
networksList[defaultNetwork] = struct{}{}
// check if genesis hash provided and register it if exists
if len(params) >= 2 {
Expand All @@ -52,9 +55,4 @@ func init() {
bootstrapList[Network(netID)] = bs
}
}
// check if private network option set
if genesis, ok := os.LookupEnv(EnvPrivateGenesis); ok {
defaultNetwork = Private
genesisList[Private] = strings.ToUpper(genesis)
}
}
1 change: 0 additions & 1 deletion params/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import "fmt"

// GenesisFor reports a hash of a genesis block for a given network.
// Genesis is strictly defined and can't be modified.
// To run a custom genesis private network use CELESTIA_PRIVATE_GENESIS env var.
func GenesisFor(net Network) (string, error) {
if err := net.Validate(); err != nil {
return "", err
Expand Down
1 change: 0 additions & 1 deletion params/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const (
// Mamaki testnet. See: celestiaorg/networks.
Mamaki Network = "mamaki"
// Private can be used to set up any private network, including local testing setups.
// Use CELESTIA_PRIVATE_GENESIS env var to enable Private by specifying its genesis block hash.
Private Network = "private"
)

Expand Down

0 comments on commit 0a341f5

Please sign in to comment.