-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c78193d
commit 1bc00bc
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |