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

refactor!(params): Moving params to nodebuilder and adding config #1168

Merged
merged 3 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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/p2p"
)

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

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

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

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"

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

// NOTE: We should always ensure that the added Flags below are parsed somewhere, like in the PersistentPreRun func on
Expand Down Expand Up @@ -48,7 +47,13 @@ var bridgeCmd = &cobra.Command{

ctx = cmdnode.WithNodeType(ctx, node.Bridge)

ctx, err = cmdnode.ParseNodeFlags(ctx, cmd)
parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/celestia/full.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ package main
import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"

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

// NOTE: We should always ensure that the added Flags below are parsed somewhere, like in the PersistentPreRun func on
Expand Down Expand Up @@ -56,7 +55,13 @@ var fullCmd = &cobra.Command{

ctx = cmdnode.WithNodeType(ctx, node.Full)

ctx, err = cmdnode.ParseNodeFlags(ctx, cmd)
parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/celestia/light.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ package main
import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"

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

// NOTE: We should always ensure that the added Flags below are parsed somewhere, like in the PersistentPreRun func on
Expand Down Expand Up @@ -56,8 +55,14 @@ var lightCmd = &cobra.Command{

ctx = cmdnode.WithNodeType(ctx, node.Light)

parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

// loads existing config into the environment
ctx, err = cmdnode.ParseNodeFlags(ctx, cmd)
ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

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

var log = logging.Logger("cmd")
Expand All @@ -17,6 +18,11 @@ func NodeType(ctx context.Context) node.Type {
return ctx.Value(nodeTypeKey{}).(node.Type)
}

// Network reads the node type from the context.
func Network(ctx context.Context) p2p.Network {
return ctx.Value(networkKey{}).(p2p.Network)
}

// StorePath reads the store path from the context.
func StorePath(ctx context.Context) string {
return ctx.Value(storePathKey{}).(string)
Expand All @@ -37,6 +43,11 @@ func WithNodeType(ctx context.Context, tp node.Type) context.Context {
return context.WithValue(ctx, nodeTypeKey{}, tp)
}

// WithNetwork sets the network in the given context.
func WithNetwork(ctx context.Context, network p2p.Network) context.Context {
return context.WithValue(ctx, networkKey{}, network)
}

// WithStorePath sets Store Path in the given context.
func WithStorePath(ctx context.Context, storePath string) context.Context {
return context.WithValue(ctx, storePathKey{}, storePath)
Expand Down Expand Up @@ -67,4 +78,5 @@ type (
configKey struct{}
storePathKey struct{}
nodeTypeKey struct{}
networkKey struct{}
)
26 changes: 5 additions & 21 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/p2p"
)

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,16 @@ 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())
}

func ParseNodeFlags(ctx context.Context, cmd *cobra.Command, network p2p.Network) (context.Context, error) {
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(network)))
}
ctx = WithStorePath(ctx, store)

Expand Down
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Options passed on start override configuration options only on start and are not
// override config with all modifiers passed on start
cfg := NodeConfig(ctx)

nd, err := nodebuilder.NewWithConfig(NodeType(ctx), store, &cfg, NodeOptions(ctx)...)
nd, err := nodebuilder.NewWithConfig(NodeType(ctx), Network(ctx), store, &cfg, NodeOptions(ctx)...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion das/daser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestDASer_stopsAfter_BEFP(t *testing.T) {
// 15 headers from the past and 15 future headers
mockGet, sub, _ := createDASerSubcomponents(t, bServ, 15, 15)

// create fraud share and break one header
// create fraud service and break one header
f := fraud.NewProofService(ps, net.Hosts()[0], mockGet.GetByHeight, ds, false)
require.NoError(t, f.Start(ctx))
mockGet.headers[1] = header.CreateFraudExtHeader(t, mockGet.headers[1], bServ)
Expand Down
7 changes: 3 additions & 4 deletions fraud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"

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

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"

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

// 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", p2p.DefaultNetwork))

// ProofService is responsible for validating and propagating Fraud Proofs.
// It implements the Service interface.
Expand Down
4 changes: 2 additions & 2 deletions header/p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

"github.com/celestiaorg/celestia-node/header"
p2p_pb "github.com/celestiaorg/celestia-node/header/p2p/pb"
"github.com/celestiaorg/celestia-node/params"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

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

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

// Exchange enables sending outbound ExtendedHeaderRequests to the network as well as
// handling inbound ExtendedHeaderRequests from the network.
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/p2p"
)

// 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 p2p.Bootstrappers) (infos []peer.AddrInfo, err error) {
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
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 p2p.Network) (tmbytes.HexBytes, error) {
if cfg.TrustedHash == "" {
gen, err := params.GenesisFor(net)
gen, err := p2p.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 @@ -13,12 +13,12 @@ import (
"github.com/celestiaorg/celestia-node/header/p2p"
"github.com/celestiaorg/celestia-node/header/store"
"github.com/celestiaorg/celestia-node/header/sync"
"github.com/celestiaorg/celestia-node/params"
modp2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

// newP2PExchange constructs new Exchange for headers.
func newP2PExchange(cfg Config) func(params.Bootstrappers, host.Host) (header.Exchange, error) {
return func(bpeers params.Bootstrappers, host host.Host) (header.Exchange, error) {
func newP2PExchange(cfg Config) func(modp2p.Bootstrappers, host.Host) (header.Exchange, error) {
return func(bpeers modp2p.Bootstrappers, host host.Host) (header.Exchange, error) {
peers, err := cfg.trustedPeers(bpeers)
if err != nil {
return nil, err
Expand All @@ -45,7 +45,7 @@ type initStore header.Store
func newInitStore(
lc fx.Lifecycle,
cfg Config,
net params.Network,
net modp2p.Network,
s header.Store,
ex header.Exchange,
) (initStore, error) {
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,7 +13,7 @@ import (
"github.com/celestiaorg/celestia-node/header/sync"
fraudServ "github.com/celestiaorg/celestia-node/nodebuilder/fraud"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/params"
modp2p "github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

var log = logging.Logger("header-module")
Expand All @@ -25,7 +25,7 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
baseComponents := fx.Options(
fx.Supply(*cfg),
fx.Error(cfgErr),
fx.Supply(params.BlockTime),
fx.Supply(modp2p.BlockTime),
fx.Provide(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 @@ -8,7 +8,7 @@ import (
"github.com/celestiaorg/celestia-node/libs/fslock"
"github.com/celestiaorg/celestia-node/libs/utils"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/params"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

// 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(p2p.EnvCustomNetwork); ok {
return fmt.Errorf("cannot run a custom network over an already-existing node store")
}
return nil
Expand Down
9 changes: 4 additions & 5 deletions nodebuilder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ 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 ConstructModule(tp node.Type, cfg *Config, store Store) fx.Option {
func ConstructModule(tp node.Type, network p2p.Network, cfg *Config, store Store) fx.Option {
baseComponents := fx.Options(
fx.Provide(params.DefaultNetwork),
fx.Provide(params.BootstrappersFor),
fx.Supply(tp),
fx.Supply(network),
fx.Provide(p2p.BootstrappersFor),
fx.Provide(func(lc fx.Lifecycle) context.Context {
return fxutil.WithLifecycle(context.Background(), lc)
}),
Expand All @@ -42,7 +42,6 @@ func ConstructModule(tp node.Type, cfg *Config, store Store) fx.Option {

return fx.Module(
"node",
fx.Supply(tp),
baseComponents,
)
}
Loading