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

feat(cmd): Provide ability to specify network to user #1073

Merged
merged 3 commits into from
Sep 13, 2022
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
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 {
distractedm1nd marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions cmd/celestia/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (
func init() {
bridgeCmd.AddCommand(
cmdnode.Init(
cmdnode.NodeFlags(node.Bridge),
cmdnode.NodeFlags(),
cmdnode.P2PFlags(),
cmdnode.CoreFlags(),
cmdnode.MiscFlags(),
cmdnode.RPCFlags(),
cmdnode.KeyFlags(),
),
cmdnode.Start(
cmdnode.NodeFlags(node.Bridge),
cmdnode.NodeFlags(),
cmdnode.P2PFlags(),
cmdnode.CoreFlags(),
cmdnode.MiscFlags(),
Expand Down
4 changes: 2 additions & 2 deletions cmd/celestia/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func init() {
fullCmd.AddCommand(
cmdnode.Init(
cmdnode.NodeFlags(node.Full),
cmdnode.NodeFlags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand All @@ -25,7 +25,7 @@ func init() {
cmdnode.KeyFlags(),
),
cmdnode.Start(
cmdnode.NodeFlags(node.Full),
cmdnode.NodeFlags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand Down
4 changes: 2 additions & 2 deletions cmd/celestia/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func init() {
lightCmd.AddCommand(
cmdnode.Init(
cmdnode.NodeFlags(node.Light),
cmdnode.NodeFlags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand All @@ -25,7 +25,7 @@ func init() {
cmdnode.KeyFlags(),
),
cmdnode.Start(
cmdnode.NodeFlags(node.Light),
cmdnode.NodeFlags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand Down
32 changes: 27 additions & 5 deletions cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,56 @@ import (
flag "github.com/spf13/pflag"

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

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.
func NodeFlags(tp node.Type) *flag.FlagSet {
func NodeFlags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
nodeStoreFlag,
fmt.Sprintf("~/.celestia-%s", strings.ToLower(tp.String())),
"",
distractedm1nd marked this conversation as resolved.
Show resolved Hide resolved
"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,
"",
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
"The name of the network to connect to, e.g. "+params.ListProvidedNetworks(),
)

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 != "" {
err := params.SetDefaultNetwork(params.Network(network))
if err != nil {
return ctx, err
}
} 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
16 changes: 9 additions & 7 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,14 @@ func DefaultNetwork() Network {
return defaultNetwork
}

func SetDefaultNetwork(net Network) error {
if err := net.Validate(); err != nil {
return err
}
defaultNetwork = net
return nil
}

Wondertan marked this conversation as resolved.
Show resolved Hide resolved
func init() {
// check if custom network option set
// format: CELESTIA_CUSTOM=<netID>:<genesisHash>:<bootstrapPeerList>
Expand Down Expand Up @@ -52,9 +59,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
14 changes: 13 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 All @@ -40,3 +39,16 @@ var networksList = map[Network]struct{}{
Mamaki: {},
Private: {},
}

// ListProvidedNetworks provides a string listing all known long-standing networks for things like command hints.
func ListProvidedNetworks() string {
var networks string
for net := range networksList {
// "private" network isn't really a choosable option, so skip
if net != Private {
networks += string(net) + ", "
}
}
// chop off trailing ", "
return networks[:len(networks)-2]
distractedm1nd marked this conversation as resolved.
Show resolved Hide resolved
}