Skip to content

Commit

Permalink
Applying diff
Browse files Browse the repository at this point in the history
  • Loading branch information
Olshansk committed Jul 12, 2023
1 parent 021d90f commit 6ce8435
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ rebuild_client_start: docker_check ## Rebuild and run a client daemon which is o

.PHONY: client_connect
client_connect: docker_check ## Connect to the running client debugging daemon
docker exec -it client /bin/bash -c "POCKET_P2P_IS_CLIENT_ONLY=true go run -tags=debug app/client/*.go debug --remote_cli_url=http://validator1:50832"
docker exec -it client /bin/bash -c "POCKET_REMOTE_CLI_URL=http://validator1:50832 go run -tags=debug app/client/*.go debug_ui"

.PHONY: build_and_watch
build_and_watch: ## Continous build Pocket's main entrypoint as files change
Expand Down Expand Up @@ -526,7 +526,7 @@ localnet_client_debug: ## Opens a `client debug` cli to interact with blockchain

.PHONY: localnet_shell
localnet_shell: ## Opens a shell in the pod that has the `client` cli available. The binary updates automatically whenever the code changes (i.e. hot reloads).
kubectl exec -it deploy/dev-cli-client --container pocket -- /bin/bash
kubectl exec -it deploy/dev-cli-client --container pocket -- /bin/bash -c "export POCKET_REMOTE_CLI_URL=http://pocket-validators:50832; bash"

.PHONY: localnet_logs_validators
localnet_logs_validators: ## Outputs logs from all validators
Expand Down
1 change: 0 additions & 1 deletion app/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ var rootCmd = &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// by this time, the config path should be set
cfg = configs.ParseConfig(flags.ConfigPath)

// set final `remote_cli_url` value; order of precedence: flag > env var > config > default
flags.RemoteCLIURL = viper.GetString("remote_cli_url")
return nil
Expand Down
68 changes: 60 additions & 8 deletions app/client/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ var (
)

func init() {
dbg := NewDebugCommand()
dbg.AddCommand(NewDebugSubCommands()...)
dbgUI := newDebugUICommand()
dbgUI.AddCommand(newDebugUISubCommands()...)
rootCmd.AddCommand(dbgUI)

dbg := newDebugCommand()
dbg.AddCommand(debugCommands()...)
rootCmd.AddCommand(dbg)
}

// NewDebugSubCommands builds out the list of debug subcommands by matching the
// newDebugUISubCommands builds out the list of debug subcommands by matching the
// handleSelect dispatch to the appropriate command.
// * To add a debug subcommand, you must add it to the `items` array and then
// write a function handler to match for it in `handleSelect`.
func NewDebugSubCommands() []*cobra.Command {
func newDebugUISubCommands() []*cobra.Command {
commands := make([]*cobra.Command, len(items))
for idx, promptItem := range items {
commands[idx] = &cobra.Command{
Expand All @@ -66,17 +70,65 @@ func NewDebugSubCommands() []*cobra.Command {
return commands
}

// NewDebugCommand returns the cobra CLI for the Debug command.
func NewDebugCommand() *cobra.Command {
// newDebugUICommand returns the cobra CLI for the Debug UI interface.
func newDebugUICommand() *cobra.Command {
return &cobra.Command{
Use: "debug",
Short: "Debug utility for rapid development",
Use: "debug_ui",
Short: "Debug selection ui for rapid development",
Args: cobra.MaximumNArgs(0),
PersistentPreRunE: helpers.P2PDependenciesPreRunE,
RunE: runDebug,
}
}

// newDebugCommand returns the cobra CLI for the Debug command.
func newDebugCommand() *cobra.Command {
return &cobra.Command{
Use: "debug",
Short: "Debug utility for rapid development",
Args: cobra.MaximumNArgs(1),
PersistentPreRunE: helpers.P2PDependenciesPreRunE,
}
}

func debugCommands() []*cobra.Command {
cmds := []*cobra.Command{
{
Use: "TriggerView",
Short: "Trigger the next view in consensus",
Long: "Sends a message to all visible nodes on the network to start the next view (height/step/round) in consensus",
Aliases: []string{"triggerView"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
m := &messaging.DebugMessage{
Action: messaging.DebugMessageAction_DEBUG_CONSENSUS_TRIGGER_NEXT_VIEW,
Type: messaging.DebugMessageRoutingType_DEBUG_MESSAGE_TYPE_BROADCAST,
Message: nil,
}
broadcastDebugMessage(cmd, m)
return nil
},
},
{
Use: "TogglePacemakerMode",
Short: "Toggle the pacemaker",
Long: "Toggle the consensus pacemaker either on or off so the chain progresses on its own or loses liveness",
Aliases: []string{"togglePaceMaker"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
m := &messaging.DebugMessage{
Action: messaging.DebugMessageAction_DEBUG_CONSENSUS_TOGGLE_PACE_MAKER_MODE,
Type: messaging.DebugMessageRoutingType_DEBUG_MESSAGE_TYPE_BROADCAST,
Message: nil,
}
broadcastDebugMessage(cmd, m)
return nil
},
},
}
return cmds
}

func runDebug(cmd *cobra.Command, args []string) (err error) {
for {
if selection, err := promptGetInput(); err == nil {
Expand Down
7 changes: 7 additions & 0 deletions app/client/cli/helpers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package helpers

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/pokt-network/pocket/app/client/cli/flags"
"github.com/pokt-network/pocket/logger"
"github.com/pokt-network/pocket/p2p"
rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc"
rpcPSP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/runtime/configs"
"github.com/pokt-network/pocket/shared/modules"
)

Expand All @@ -18,6 +20,11 @@ func P2PDependenciesPreRunE(cmd *cobra.Command, _ []string) error {
// TECHDEBT: this is to keep backwards compatibility with localnet
flags.ConfigPath = runtime.GetEnv("CONFIG_PATH", "build/config/config.validator1.json")

// by this time, the config path should be set
configs.ParseConfig(flags.ConfigPath)
// set final `remote_cli_url` value; order of precedence: flag > env var > config > default
flags.RemoteCLIURL = viper.GetString("remote_cli_url")

runtimeMgr := runtime.NewManagerFromFiles(
flags.ConfigPath, genesisPath,
runtime.WithClientDebugMode(),
Expand Down
2 changes: 2 additions & 0 deletions build/deployments/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ services:
security_opt:
- "seccomp:unconfined"
environment:
# BUG: The `SERVICER1_SERVICER_ENABLED` env var is not currnetly visible in the `command` above and needs to be investigate
- SERVICER1_SERVICER_ENABLED=true
- POCKET_RPC_USE_CORS=true
# Uncomment to enable DLV debugging
# - DEBUG_PORT=7085
Expand Down
5 changes: 1 addition & 4 deletions build/localnet/manifests/cli-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ spec:
memory: "512Mi"
cpu: "4"
env:
- name: POCKET_P2P_IS_CLIENT_ONLY
value: "true"
- name: CONFIG_PATH
value: "/var/pocket/config/config.json"
- name: GENESIS_PATH
Expand Down Expand Up @@ -76,8 +74,7 @@ spec:
# Any host that is visible and connected to the cluster can be arbitrarily selected as the RPC host
- name: POCKET_REMOTE_CLI_URL
value: http://full-node-001-pocket:50832
# TECHDEBT(#678): debug client requires hostname to participate
# in P2P networking.
# TECHDEBT(#678): debug client requires hostname to participate in P2P networking.
- name: POCKET_P2P_HOSTNAME
value: "127.0.0.1"
volumeMounts:
Expand Down
2 changes: 2 additions & 0 deletions consensus/e2e_tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,12 +771,14 @@ func waitForNodeToReceiveMissingBlock(
// TODO(#352): implement this function.
// waitForNodeToCatchUp waits for given node to node to catch up to the target height by sending the requested block.
func waitForNodeToCatchUp(

t *testing.T,
clck *clock.Mock,
eventsChannel modules.EventsChannel,
unsyncedNode *shared.Node,
blockResponse *anypb.Any,
targetHeight uint64,

) error {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions e2e/tests/steps_init_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build e2e

// IN_THIS_PR: Figure out how to make this work: //go:build e2e
package e2e

import (
Expand All @@ -23,8 +22,8 @@ import (
var e2eLogger = pocketLogger.Global.CreateLoggerForModule("e2e")

const (
// defines the host & port scheme that LocalNet uses for naming validators.
// e.g. validator-001 thru validator-999
// Each actor is represented e.g. validator-001 thru validator-999
// Defines the host & port scheme that LocalNet uses for naming actors.
validatorServiceURLTmpl = "validator-%s-pocket:%d"
// validatorA maps to suffix ID 001 and is also used by the cluster-manager
// though it has no special permissions.
Expand All @@ -43,6 +42,7 @@ type rootSuite struct {
// clientset is the kubernetes API we acquire from the user's $HOME/.kube/config
clientset *kubernetes.Clientset
// validator holds command results between runs and reports errors to the test suite
// TECHDEBT: Rename `validator` to something more appropriate
validator *validatorPod
// validatorA maps to suffix ID 001 of the kube pod that we use as our control agent
}
Expand Down Expand Up @@ -148,9 +148,7 @@ func (s *rootSuite) unstakeValidator() {
}

// getPrivateKey generates a new keypair from the private hex key that we get from the clientset
func (s *rootSuite) getPrivateKey(
validatorId string,
) cryptoPocket.PrivateKey {
func (s *rootSuite) getPrivateKey(validatorId string) cryptoPocket.PrivateKey {
privHexString := s.validatorKeys[validatorId]
privateKey, err := cryptoPocket.NewPrivateKey(privHexString)
require.NoErrorf(s, err, "failed to extract privkey")
Expand All @@ -161,6 +159,8 @@ func (s *rootSuite) getPrivateKey(
// getClientset uses the default path `$HOME/.kube/config` to build a kubeconfig
// and then connects to that cluster and returns a *Clientset or an error
func getClientset(t gocuke.TestingT) (*kubernetes.Clientset, error) {
t.Helper()

userHomeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get home dir: %w", err)
Expand Down
27 changes: 26 additions & 1 deletion p2p/background/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package background
import (
"context"
"fmt"
"time"

dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
libp2pHost "github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
libp2pPeer "github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/multierr"
"google.golang.org/protobuf/proto"
Expand All @@ -32,6 +34,12 @@ var (
_ backgroundRouterFactory = &backgroundRouter{}
)

// TECHDEBT: Make these values configurable
const (
connectMaxRetries = 5
connectRetryTimeout = time.Second * 2
)

type backgroundRouterFactory = modules.FactoryWithConfig[typesP2P.Router, *config.BackgroundConfig]

// backgroundRouter implements `typesP2P.Router` for use with all P2P participants.
Expand Down Expand Up @@ -342,13 +350,30 @@ func (rtr *backgroundRouter) bootstrap(ctx context.Context) error {
return nil
}

if err := rtr.host.Connect(ctx, libp2pAddrInfo); err != nil {
if err := rtr.connectWithRetry(ctx, libp2pAddrInfo); err != nil {
return fmt.Errorf("connecting to peer: %w", err)
}
}
return nil
}

// connectWithRetry attempts to connect to the given peer, retrying up to connectMaxRetries times
// and waiting connectRetryTimeout between each attempt.
func (rtr *backgroundRouter) connectWithRetry(ctx context.Context, libp2pAddrInfo peer.AddrInfo) error {
var err error
for i := 0; i < connectMaxRetries; i++ {
err = rtr.host.Connect(ctx, libp2pAddrInfo)
if err == nil {
return nil
}

fmt.Printf("Failed to connect (attempt %d), retrying in %v...\n", i+1, connectRetryTimeout)
time.Sleep(connectRetryTimeout)
}

return fmt.Errorf("failed to connect after %d attempts, last error: %w", 5, err)
}

// topicValidator is used in conjunction with libp2p-pubsub's notion of "topic
// validaton". It is used for arbitrary and concurrent pre-propagation validation
// of messages.
Expand Down
1 change: 1 addition & 0 deletions persistence/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (p *persistenceModule) TransactionExists(transactionHash string) (bool, err
}
return false, err
}

return true, nil
}

Expand Down
60 changes: 52 additions & 8 deletions shared/k8s/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import (
)

//nolint:gosec // G101 Not a credential
const privateKeysSecretResourceName = "validators-private-keys"
const kubernetesServiceAccountNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
const defaultNamespace = "default"
const (
privateKeysSecretResourceNameValidators = "validators-private-keys"
privateKeysSecretResourceNameServicers = "servicers-private-keys"
privateKeysSecretResourceNameFisherman = "fisherman-private-keys"
privateKeysSecretResourceNameApplications = "applications-private-keys"
kubernetesServiceAccountNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
defaultNamespace = "default"
)

var CurrentNamespace = ""

Expand All @@ -34,20 +39,59 @@ func init() {
}

// FetchValidatorPrivateKeys returns a map corresponding to the data section of
// the validator private keys k8s secret (yaml), located at `privateKeysSecretResourceName`.
// the validator private keys Kubernetes secret.
func FetchValidatorPrivateKeys(clientset *kubernetes.Clientset) (map[string]string, error) {
validatorKeysMap := make(map[string]string)
return fetchPrivateKeys(clientset, privateKeysSecretResourceNameValidators, "validators")
}

// FetchServicerPrivateKeys returns a map corresponding to the data section of
// the servicer private keys Kubernetes secret.
func FetchServicerPrivateKeys(clientset *kubernetes.Clientset) (map[string]string, error) {
return fetchPrivateKeys(clientset, privateKeysSecretResourceNameServicers, "servicers")
}

// FetchFishermanPrivateKeys returns a map corresponding to the data section of
// the fisherman private keys Kubernetes secret.
func FetchFishermanPrivateKeys(clientset *kubernetes.Clientset) (map[string]string, error) {
return fetchPrivateKeys(clientset, privateKeysSecretResourceNameFisherman, "fisherman")
}

// FetchApplicationPrivateKeys returns a map corresponding to the data section of
// the application private keys Kubernetes secret.
func FetchApplicationPrivateKeys(clientset *kubernetes.Clientset) (map[string]string, error) {
return fetchPrivateKeys(clientset, privateKeysSecretResourceNameApplications, "applications")
}

// fetchPrivateKeys returns a map corresponding to the data section of
// the private keys Kubernetes secret for the specified resource name and actor.
func fetchPrivateKeys(clientset *kubernetes.Clientset, resourceName string, actor string) (map[string]string, error) {
privateKeysMap := make(map[string]string)

privateKeysSecretResourceName := ""
switch actor {
case "validators":
privateKeysSecretResourceName = privateKeysSecretResourceNameValidators
case "servicers":
privateKeysSecretResourceName = privateKeysSecretResourceNameServicers
case "fisherman":
privateKeysSecretResourceName = privateKeysSecretResourceNameFisherman
case "applications":
privateKeysSecretResourceName = privateKeysSecretResourceNameApplications
default:
return nil, fmt.Errorf("unknown actor: %s", actor)
}

privateKeysSecret, err := clientset.CoreV1().Secrets(CurrentNamespace).Get(context.TODO(), privateKeysSecretResourceName, metav1.GetOptions{})
if err != nil {
panic(err)
}

for id, privHexString := range privateKeysSecret.Data {
// it's safe to cast []byte to string here
validatorKeysMap[id] = string(privHexString)
// It's safe to cast []byte to string here
privateKeysMap[id] = string(privHexString)
}
return validatorKeysMap, nil

return privateKeysMap, nil
}

func getNamespace() (string, error) {
Expand Down
1 change: 1 addition & 0 deletions shared/modules/persistence_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type PersistenceModule interface {
// Indexer operations
GetTxIndexer() indexer.TxIndexer
TransactionExists(transactionHash string) (bool, error)
// TransactionExistsInStateTree(transactionHash string) (bool, error)

// TreeStore operations
GetTreeStore() TreeStoreModule
Expand Down
Loading

0 comments on commit 6ce8435

Please sign in to comment.