diff --git a/app/client/cli/setup.go b/app/client/cli/setup.go new file mode 100644 index 000000000..4e947a8a4 --- /dev/null +++ b/app/client/cli/setup.go @@ -0,0 +1,77 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/pokt-network/pocket/logger" + "github.com/pokt-network/pocket/p2p" + rpc2 "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc" + "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc" + "github.com/pokt-network/pocket/runtime" + "github.com/pokt-network/pocket/runtime/defaults" + "github.com/pokt-network/pocket/shared/modules" +) + +// P2PDependenciesPreRunE initializes peerstore & current height providers, and a +// p2p module with consumes them. Everything is registered to the bus. +func P2PDependenciesPreRunE(cmd *cobra.Command, _ []string) error { + // TECHDEBT: this is to keep backwards compatibility with localnet + configPath = runtime.GetEnv("CONFIG_PATH", "build/config/config1.json") + rpcURL := fmt.Sprintf("http://%s:%s", rpcHost, defaults.DefaultRPCPort) + + runtimeMgr := runtime.NewManagerFromFiles( + configPath, genesisPath, + runtime.WithClientDebugMode(), + runtime.WithRandomPK(), + ) + + bus := runtimeMgr.GetBus() + SetValueInCLIContext(cmd, BusCLICtxKey, bus) + + setupPeerstoreProvider(*runtimeMgr, rpcURL) + setupCurrentHeightProvider(*runtimeMgr, rpcURL) + setupAndStartP2PModule(*runtimeMgr) + + return nil +} + +func setupPeerstoreProvider(rm runtime.Manager, rpcURL string) { + bus := rm.GetBus() + modulesRegistry := bus.GetModulesRegistry() + pstoreProvider := rpc.NewRPCPeerstoreProvider( + rpc.WithP2PConfig(rm.GetConfig().P2P), + rpc.WithCustomRPCURL(rpcURL), + ) + modulesRegistry.RegisterModule(pstoreProvider) +} + +func setupCurrentHeightProvider(rm runtime.Manager, rpcURL string) { + bus := rm.GetBus() + modulesRegistry := bus.GetModulesRegistry() + currentHeightProvider := rpc2.NewRPCCurrentHeightProvider( + rpc2.WithCustomRPCURL(rpcURL), + ) + modulesRegistry.RegisterModule(currentHeightProvider) +} + +func setupAndStartP2PModule(rm runtime.Manager) { + bus := rm.GetBus() + mod, err := p2p.Create(bus) + if err != nil { + logger.Global.Fatal().Err(err).Msg("Failed to create p2p module") + } + + var ok bool + p2pMod, ok = mod.(modules.P2PModule) + if !ok { + logger.Global.Fatal().Msgf("unexpected P2P module type: %T", mod) + } + + if err := p2pMod.Start(); err != nil { + logger.Global.Fatal().Err(err).Msg("Failed to start p2p module") + } +} + +const BusCLICtxKey = "bus"