-
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
9dc7949
commit 81e15f6
Showing
5 changed files
with
124 additions
and
97 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
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,14 @@ | ||
package helpers | ||
|
||
import ( | ||
"github.com/pokt-network/pocket/runtime" | ||
"github.com/pokt-network/pocket/shared/modules" | ||
) | ||
|
||
var ( | ||
GenesisPath = runtime.GetEnv("GENESIS_PATH", "build/config/genesis.json") | ||
RpcHost string | ||
|
||
//P2PMod is initialized in order to broadcast a message to the local network | ||
P2PMod modules.P2PModule | ||
) |
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,20 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const BusCLICtxKey cliContextKey = "bus" | ||
|
||
// NOTE: this is required by the linter, otherwise a simple string constant would have been enough | ||
type cliContextKey string | ||
|
||
func SetValueInCLIContext(cmd *cobra.Command, key cliContextKey, value any) { | ||
cmd.SetContext(context.WithValue(cmd.Context(), key, value)) | ||
} | ||
|
||
func GetValueFromCLIContext[T any](cmd *cobra.Command, key cliContextKey) (T, bool) { | ||
value, ok := cmd.Context().Value(key).(T) | ||
return value, ok | ||
} |
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,75 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pokt-network/pocket/app/client/cli/flags" | ||
"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 | ||
flags.ConfigPath = runtime.GetEnv("CONFIG_PATH", "build/config/config1.json") | ||
rpcURL := fmt.Sprintf("http://%s:%s", RpcHost, defaults.DefaultRPCPort) | ||
|
||
runtimeMgr := runtime.NewManagerFromFiles( | ||
flags.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") | ||
} | ||
} |
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