Skip to content

Commit

Permalink
refactor(params): Moving params to nodebuilder and adding config
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Sep 26, 2022
1 parent cec6a2d commit 00c4075
Show file tree
Hide file tree
Showing 31 changed files with 152 additions and 90 deletions.
6 changes: 3 additions & 3 deletions cmd/cel-key/node_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

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

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

"github.com/celestiaorg/celestia-node/nodebuilder/network"
)

var (
Expand All @@ -20,7 +20,7 @@ var (

func DirectoryFlags() *flag.FlagSet {
flags := &flag.FlagSet{}
defaultNetwork := string(params.DefaultNetwork())
defaultNetwork := string(network.DefaultNetwork())

flags.String(
nodeDirKey,
Expand Down
3 changes: 3 additions & 0 deletions cmd/celestia/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

Expand All @@ -14,6 +15,7 @@ func init() {
bridgeCmd.AddCommand(
cmdnode.Init(
cmdnode.NodeFlags(),
network.Flags(),
cmdnode.P2PFlags(),
cmdnode.CoreFlags(),
cmdnode.MiscFlags(),
Expand All @@ -22,6 +24,7 @@ func init() {
),
cmdnode.Start(
cmdnode.NodeFlags(),
network.Flags(),
cmdnode.P2PFlags(),
cmdnode.CoreFlags(),
cmdnode.MiscFlags(),
Expand Down
3 changes: 3 additions & 0 deletions cmd/celestia/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

Expand All @@ -15,6 +16,7 @@ func init() {
fullCmd.AddCommand(
cmdnode.Init(
cmdnode.NodeFlags(),
network.Flags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand All @@ -26,6 +28,7 @@ func init() {
),
cmdnode.Start(
cmdnode.NodeFlags(),
network.Flags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand Down
3 changes: 3 additions & 0 deletions cmd/celestia/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

Expand All @@ -15,6 +16,7 @@ func init() {
lightCmd.AddCommand(
cmdnode.Init(
cmdnode.NodeFlags(),
network.Flags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand All @@ -26,6 +28,7 @@ func init() {
),
cmdnode.Start(
cmdnode.NodeFlags(),
network.Flags(),
cmdnode.P2PFlags(),
cmdnode.HeadersFlags(),
cmdnode.MiscFlags(),
Expand Down
27 changes: 9 additions & 18 deletions cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
flag "github.com/spf13/pflag"

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

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

// NodeFlags gives a set of hardcoded Node package flags.
Expand All @@ -34,31 +33,21 @@ func NodeFlags() *flag.FlagSet {
"",
"Path to a customized node config TOML file",
)
flags.String(
nodeNetworkFlag,
"",
"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) {
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())
parsedNetwork, err := network.ParseFlags(cmd)
if err != nil {
return ctx, err
}

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

Expand All @@ -70,6 +59,7 @@ func ParseNodeFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
return ctx, fmt.Errorf("cmd: while parsing '%s': %w", nodeConfigFlag, err)
}

cfg.Network.Network = parsedNetwork
ctx = WithNodeConfig(ctx, cfg)
} else {
// check if config already exists at the store path and load it
Expand All @@ -80,6 +70,7 @@ func ParseNodeFlags(ctx context.Context, cmd *cobra.Command) (context.Context, e
}
cfg, err := nodebuilder.LoadConfig(filepath.Join(expanded, "config.toml"))
if err == nil {
cfg.Network.Network = parsedNetwork
ctx = WithNodeConfig(ctx, cfg)
}
}
Expand Down
4 changes: 2 additions & 2 deletions fraud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/libp2p/go-libp2p-core/protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"

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

// fraudRequests is the amount of external requests that will be tried to get fraud proofs from other peers.
const fraudRequests = 5

var fraudProtocolID = protocol.ID(fmt.Sprintf("/fraud/v0.0.1/%s", params.DefaultNetwork()))
var fraudProtocolID = protocol.ID(fmt.Sprintf("/fraud/v0.0.1/%s", network.DefaultNetwork()))

// ProofService is responsible for validating and propagating Fraud Proofs.
// It implements the Service interface.
Expand Down
7 changes: 4 additions & 3 deletions header/p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
"github.com/libp2p/go-libp2p-core/protocol"
tmbytes "github.com/tendermint/tendermint/libs/bytes"

"github.com/celestiaorg/go-libp2p-messenger/serde"

"github.com/celestiaorg/celestia-node/header"
p2p_pb "github.com/celestiaorg/celestia-node/header/p2p/pb"
header_pb "github.com/celestiaorg/celestia-node/header/pb"
"github.com/celestiaorg/celestia-node/params"
"github.com/celestiaorg/go-libp2p-messenger/serde"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
)

var log = logging.Logger("header/p2p")
Expand All @@ -26,7 +27,7 @@ var log = logging.Logger("header/p2p")
// gossipsub topic.
const PubSubTopic = "header-sub"

var exchangeProtocolID = protocol.ID(fmt.Sprintf("/header-ex/v0.0.2/%s", params.DefaultNetwork()))
var exchangeProtocolID = protocol.ID(fmt.Sprintf("/header-ex/v0.0.2/%s", network.DefaultNetwork()))

// Exchange enables sending outbound ExtendedHeaderRequests to the network as well as
// handling inbound ExtendedHeaderRequests from the network.
Expand Down
27 changes: 15 additions & 12 deletions nodebuilder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/share"
Expand All @@ -21,12 +22,13 @@ type ConfigLoader func() (*Config, error)
// Config is main configuration structure for a Node.
// It combines configuration units for all Node subsystems.
type Config struct {
Core core.Config
State state.Config
P2P p2p.Config
RPC rpc.Config
Share share.Config
Header header.Config
Core core.Config
State state.Config
P2P p2p.Config
RPC rpc.Config
Share share.Config
Header header.Config
Network network.Config
}

// DefaultConfig provides a default Config for a given Node Type 'tp'.
Expand All @@ -35,12 +37,13 @@ func DefaultConfig(tp node.Type) *Config {
switch tp {
case node.Bridge, node.Light, node.Full:
return &Config{
Core: core.DefaultConfig(),
State: state.DefaultConfig(),
P2P: p2p.DefaultConfig(),
RPC: rpc.DefaultConfig(),
Share: share.DefaultConfig(),
Header: header.DefaultConfig(),
Core: core.DefaultConfig(),
State: state.DefaultConfig(),
P2P: p2p.DefaultConfig(),
RPC: rpc.DefaultConfig(),
Share: share.DefaultConfig(),
Header: header.DefaultConfig(),
Network: network.DefaultConfig(),
}
default:
panic("node: invalid node type")
Expand Down
8 changes: 4 additions & 4 deletions nodebuilder/header/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/multiformats/go-multiaddr"
tmbytes "github.com/tendermint/tendermint/libs/bytes"

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

// Config contains configuration parameters for header retrieval and management.
Expand All @@ -28,7 +28,7 @@ func DefaultConfig() Config {
}
}

func (cfg *Config) trustedPeers(bpeers params.Bootstrappers) (infos []peer.AddrInfo, err error) {
func (cfg *Config) trustedPeers(bpeers network.Bootstrappers) (infos []peer.AddrInfo, err error) {
if len(cfg.TrustedPeers) == 0 {
log.Infof("No trusted peers in config, initializing with default bootstrappers as trusted peers")
return bpeers, nil
Expand All @@ -49,9 +49,9 @@ func (cfg *Config) trustedPeers(bpeers params.Bootstrappers) (infos []peer.AddrI
return
}

func (cfg *Config) trustedHash(net params.Network) (tmbytes.HexBytes, error) {
func (cfg *Config) trustedHash(net network.Network) (tmbytes.HexBytes, error) {
if cfg.TrustedHash == "" {
gen, err := params.GenesisFor(net)
gen, err := network.GenesisFor(net)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions nodebuilder/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/header/p2p"
"github.com/celestiaorg/celestia-node/header/store"
"github.com/celestiaorg/celestia-node/params"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
)

// P2PExchange constructs new Exchange for headers.
func P2PExchange(cfg Config) func(params.Bootstrappers, host.Host) (header.Exchange, error) {
return func(bpeers params.Bootstrappers, host host.Host) (header.Exchange, error) {
func P2PExchange(cfg Config) func(network.Bootstrappers, host.Host) (header.Exchange, error) {
return func(bpeers network.Bootstrappers, host host.Host) (header.Exchange, error) {
peers, err := cfg.trustedPeers(bpeers)
if err != nil {
return nil, err
Expand All @@ -30,7 +30,7 @@ func P2PExchange(cfg Config) func(params.Bootstrappers, host.Host) (header.Excha
}

// InitStore initializes the store.
func InitStore(ctx context.Context, cfg Config, net params.Network, s header.Store, ex header.Exchange) error {
func InitStore(ctx context.Context, cfg Config, net network.Network, s header.Store, ex header.Exchange) error {
trustedHash, err := cfg.trustedHash(net)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions nodebuilder/header/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/celestiaorg/celestia-node/header/sync"
"github.com/celestiaorg/celestia-node/libs/fxutil"
fraudbuilder "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/params"
headerservice "github.com/celestiaorg/celestia-node/service/header"
)

Expand All @@ -27,7 +27,7 @@ func Module(tp node.Type, cfg *Config) fx.Option {
baseComponents := fx.Options(
fx.Supply(*cfg),
fx.Error(cfgErr),
fx.Supply(params.BlockTime),
fx.Supply(network.BlockTime),
fx.Provide(headerservice.NewHeaderService),
fx.Provide(fx.Annotate(
store.NewStore,
Expand Down
4 changes: 2 additions & 2 deletions nodebuilder/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/celestiaorg/celestia-node/libs/fslock"
"github.com/celestiaorg/celestia-node/libs/utils"
"github.com/celestiaorg/celestia-node/nodebuilder/network"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/params"
)

// Init initializes the Node FileSystem Store for the given Node Type 'tp' in the directory under 'path'.
Expand Down Expand Up @@ -104,7 +104,7 @@ func initDir(path string) error {
if utils.Exists(path) {
// if the dir already exists and `CELESTIA_CUSTOM` env var is set,
// fail out to prevent store corruption
if _, ok := os.LookupEnv(params.EnvCustomNetwork); ok {
if _, ok := os.LookupEnv(network.EnvCustomNetwork); ok {
return fmt.Errorf("cannot run a custom network over an already-existing node store")
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions nodebuilder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"go.uber.org/fx"

"github.com/celestiaorg/celestia-node/nodebuilder/network"

"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/daser"
"github.com/celestiaorg/celestia-node/nodebuilder/fraud"
Expand All @@ -14,13 +16,11 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/share"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
"github.com/celestiaorg/celestia-node/params"
)

func Module(tp node.Type, cfg *Config, store Store) fx.Option {
baseComponents := fx.Options(
fx.Provide(params.DefaultNetwork),
fx.Provide(params.BootstrappersFor),
network.Module(&cfg.Network),
fx.Provide(context.Background),
fx.Supply(cfg),
fx.Supply(store.Config),
Expand Down
2 changes: 1 addition & 1 deletion params/bootstrap.go → nodebuilder/network/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package params
package network

import (
logging "github.com/ipfs/go-log/v2"
Expand Down
15 changes: 15 additions & 0 deletions nodebuilder/network/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package network

type Config struct {
Network Network
}

func DefaultConfig() Config {
return Config{
Network: Arabica,
}
}

func (c *Config) Validate() error {
return c.Network.Validate()
}
Loading

0 comments on commit 00c4075

Please sign in to comment.