Skip to content

Commit

Permalink
fixup: refactor cli helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jun 6, 2023
1 parent c78193d commit 1bc00bc
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions app/client/cli/setup.go
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 1bc00bc

Please sign in to comment.