Skip to content

Commit

Permalink
[P2P] Refactor/consolidate P2P modules (#576)
Browse files Browse the repository at this point in the history
## @Reviewer

This PR may be more digestible / reviewable on a commit-by-commit basis.
Commits are organized logically and any given line is only modified in a
single commit, with few exceptions.

---

## Description

Re-consolidates Libp2p and P2P modules and types.

## Issue

Fixes #554

## Type of change

Please mark the relevant option(s):

- [ ] New feature, functionality or library
- [ ] Bug fix
- [x] Code health or cleanup
- [ ] Major breaking change
- [ ] Documentation
- [ ] Other <!-- add details here if it a different type of change -->

## List of changes

- Moved peer & url conversion utils to `p2p/utils` package
- Refactor `getPeerIP` to use `net.DefaultResolver` for easier testing
- Moved & refactor libp2p `host.Host` setup util to `p2p/utils`
- Consolidated Libp2p & P2P `modules.Module` implementations
- Consolidated Libp2p & P2P `stdnetwork` `typesP2P.Network`
implementations
- Refactored raintree `typesP2P.Network` implementation to use libp2p
- Moved `shared/p2p` package into `p2p/types` packages
- Removed `Trnasport` interface and implementations
- Removed `ConnectionFactory` type and related members
- Added libp2p `host.Host` mock generator
- Refactor debug CLI post P2P module re-consolidation
- Removed *temporary* `shared/p2p` package
- Removed `runtime/configs.Config#UseLibp2p` field
- Use pod IP for validator DNS resolution tilt localnet
- Add `LIBP2P_DEBUG` env var
- Debug logging improvements
- Set validator `POCKET_P2P_HOSTNAME` env var to the pod IP
- Set validator `p2p.hostname` config value to empty string so that the
env var applies


## Testing

- [x] `make develop_test`
- [x]
[LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md)
w/ all of the steps outlined in the `README`


## Required Checklist

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have tested my changes using the available tooling
- [x] I have updated the corresponding CHANGELOG

### If Applicable Checklist

- [ ] I have updated the corresponding README(s); local and/or global
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added, or updated,
[mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding
README(s)
- [ ] I have added, or updated, documentation and
[mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*`
if I updated `shared/*`README(s)

---------

Co-authored-by: Daniel Olshansky <[email protected]>
Co-authored-by: Dmitry K <[email protected]>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
4 people authored Apr 17, 2023
1 parent a7fd2a6 commit 49a3223
Show file tree
Hide file tree
Showing 62 changed files with 1,353 additions and 1,629 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mockgen: clean_mocks ## Use `mockgen` to generate mocks used for testing purpose
go generate ./${modules_dir}
echo "Mocks generated in ${modules_dir}/mocks"

$(eval DIRS = p2p libp2p persistence)
$(eval DIRS = p2p persistence)
for dir in $(DIRS); do \
echo "Processing $$dir mocks..."; \
find $$dir/types/mocks -type f ! -name "mocks.go" -exec rm {} \;; \
Expand Down
36 changes: 12 additions & 24 deletions app/client/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"os"

"github.com/manifoldco/promptui"
"github.com/pokt-network/pocket/libp2p"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/anypb"

"github.com/pokt-network/pocket/logger"
"github.com/pokt-network/pocket/p2p"
"github.com/pokt-network/pocket/p2p/providers/current_height_provider"
rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc"
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
rpcABP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/runtime/defaults"
"github.com/pokt-network/pocket/shared/messaging"
"github.com/pokt-network/pocket/shared/modules"
sharedP2P "github.com/pokt-network/pocket/shared/p2p"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/anypb"
)

// TECHDEBT: Lowercase variables / constants that do not need to be exported.
Expand Down Expand Up @@ -105,13 +105,17 @@ func NewDebugCommand() *cobra.Command {

setValueInCLIContext(cmd, busCLICtxKey, bus)

// TECHDEBT: simplify after P2P module consolidation.
var err error
p2pMod, err = getP2PModule(runtimeMgr)
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")
}
Expand Down Expand Up @@ -265,7 +269,7 @@ func sendDebugMessage(cmd *cobra.Command, debugMsg *messaging.DebugMessage) {
}

// fetchPeerstore retrieves the providers from the CLI context and uses them to retrieve the address book for the current height
func fetchPeerstore(cmd *cobra.Command) (sharedP2P.Peerstore, error) {
func fetchPeerstore(cmd *cobra.Command) (typesP2P.Peerstore, error) {
bus, ok := getValueFromCLIContext[modules.Bus](cmd, busCLICtxKey)
if !ok || bus == nil {
return nil, errors.New("retrieving bus from CLI context")
Expand Down Expand Up @@ -293,22 +297,6 @@ func fetchPeerstore(cmd *cobra.Command) (sharedP2P.Peerstore, error) {
return pstore, nil
}

func getP2PModule(runtimeMgr *runtime.Manager) (p2pModule modules.P2PModule, err error) {
bus := runtimeMgr.GetBus()

var mod modules.Module
if runtimeMgr.GetConfig().UseLibP2P {
mod, err = libp2p.Create(bus)
} else {
mod, err = p2p.Create(bus)
}
if err != nil {
return nil, err
}

return mod.(modules.P2PModule), nil
}

// sendConsensusNewHeightEventToP2PModule mimicks the consensus module sending a ConsensusNewHeightEvent to the p2p module
// This is necessary because the debug client is not a validator and has no consensus module but it has to update the peerstore
// depending on the changes in the validator set.
Expand Down
4 changes: 4 additions & 0 deletions app/client/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.0.28] - 2023-04-17

- Refactor debug CLI post P2P module re-consolidation

## [0.0.0.27] - 2023-04-07

- Add Query Command
Expand Down
1 change: 1 addition & 0 deletions build/deployments/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ services:
- "seccomp:unconfined"
environment:
- POCKET_RPC_USE_CORS=true
- LIBP2P_DEBUG=info
# Uncomment to enable the pprof server
# - PPROF_ENABLED=true
# Uncomment to enable DLV debugging
Expand Down
6 changes: 6 additions & 0 deletions build/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.0.35] - 2023-04-17

- Removed runtime/configs.Config#UseLibp2p field
- Use pod IP for validator DNS resolution tilt localnet
- Add `LIBP2P_DEBUG` env var

## [0.0.0.34] - 2023-04-14

- Changed LocalNet validators to use the new `pocket-validator` helm chart instead of templating the manifests with `sed`.
Expand Down
4 changes: 4 additions & 0 deletions build/localnet/manifests/cli-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ spec:
# Any host that is visible and connected to the cluster can be arbitrarily selected as the RPC host
- name: RPC_HOST
value: pocket-validators
# TECHDEBT(#678): debug client requires hostname to participate
# in P2P networking.
- name: POCKET_P2P_HOSTNAME
value: "127.0.0.1"
volumeMounts:
# IMPROVE: should probably go in /etc/pocket and have Viper read from there as a default path
- mountPath: /var/pocket/config
Expand Down
6 changes: 6 additions & 0 deletions charts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.0.2] - 2023-04-17

- Removed `runtime/configs.Config#UseLibp2p` field
- Set validator `POCKET_P2P_HOSTNAME` env var to the pod IP
- Set validator `p2p.hostname` config value to empty string so that the env var applies

## [0.0.0.1] - 2023-04-14

- Introduced `pocket-validator` helm chart.
Expand Down
2 changes: 1 addition & 1 deletion charts/pocket-validator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ privateKeySecretKeyRef:
| config.consensus.private_key | string | `""` | |
| config.logger.format | string | `"json"` | |
| config.logger.level | string | `"debug"` | |
| config.p2p.hostname | string | `""` | |
| config.p2p.is_empty_connection_type | bool | `false` | |
| config.p2p.max_mempool_count | int | `100000` | |
| config.p2p.port | int | `42069` | |
Expand All @@ -69,7 +70,6 @@ privateKeySecretKeyRef:
| config.telemetry.address | string | `"0.0.0.0:9000"` | |
| config.telemetry.enabled | bool | `true` | |
| config.telemetry.endpoint | string | `"/metrics"` | |
| config.use_libp2p | bool | `false` | |
| config.utility.max_mempool_transaction_bytes | int | `1073741824` | |
| config.utility.max_mempool_transactions | int | `9000` | |
| externalPostgresql.database | string | `""` | name of the external database |
Expand Down
4 changes: 4 additions & 0 deletions charts/pocket-validator/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ spec:
"until nc -z $(POSTGRES_HOST) $(POSTGRES_PORT); do echo waiting for postgres...; sleep 2; done;",
]
env:
- name: POCKET_P2P_HOSTNAME
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: POSTGRES_HOST
value: {{ include "pocket-validator.postgresqlHost" . }}
- name: POSTGRES_PORT
Expand Down
2 changes: 1 addition & 1 deletion charts/pocket-validator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ privateKeySecretKeyRef:
config:
root_directory: "/go/src/github.com/pocket-network"
private_key: "" # @ignored This value is needed but ignored - use privateKeySecretKeyRef instead
use_libp2p: false
consensus:
max_mempool_bytes: 500000000
pacemaker_config:
Expand All @@ -93,6 +92,7 @@ config:
max_conn_idle_time: 1m
health_check_period: 30s
p2p:
hostname: ""
port: 42069
use_rain_tree: true
is_empty_connection_type: false
Expand Down
11 changes: 5 additions & 6 deletions consensus/debugging.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ func (m *consensusModule) resetToGenesis(_ *messaging.DebugMessage) error {

func (m *consensusModule) printNodeState(_ *messaging.DebugMessage) {
state := m.GetNodeState()
m.logger.Debug().
Fields(map[string]any{
"step": state.Step,
"height": state.Height,
"round": state.Round,
}).Msg("Node state")
m.logger.Debug().Fields(map[string]any{
"step": typesCons.StepToString[typesCons.HotstuffStep(state.Step)],
"height": state.Height,
"round": state.Round,
}).Msg("Node state")
}

func (m *consensusModule) triggerNextView(_ *messaging.DebugMessage) {
Expand Down
4 changes: 4 additions & 0 deletions consensus/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.0.48] - 2023-04-17

- Debug logging improvements

## [0.0.0.47] - 2023-04-17

- Log warnings in `handleStateSyncMessage()`
Expand Down
24 changes: 7 additions & 17 deletions consensus/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,11 @@ func protoHash(m proto.Message) string {

func (m *consensusModule) sendToLeader(msg *typesCons.HotstuffMessage) {
leaderId := m.leaderId
m.logger.Debug().Fields(
map[string]any{
"src": m.nodeId,
"dst": leaderId,
"height": msg.GetHeight(),
"step": msg.GetStep(),
"round": msg.GetRound(),
},
).Msg("✉️ About to try sending hotstuff message ✉️")

loggingFields := hotstuffMsgToLoggingFields(msg)
loggingFields["src"] = m.nodeId
loggingFields["dst"] = leaderId
m.logger.Debug().Fields(loggingFields).Msg("✉️ About to try sending hotstuff message ✉️")

// TODO: This can happen due to a race condition with the pacemaker.
if leaderId == nil {
Expand Down Expand Up @@ -183,13 +179,7 @@ func (m *consensusModule) sendToLeader(msg *typesCons.HotstuffMessage) {
// Star-like (O(n)) broadcast - send to all nodes directly
// INVESTIGATE: Re-evaluate if we should be using our structured broadcast (RainTree O(log3(n))) algorithm instead
func (m *consensusModule) broadcastToValidators(msg *typesCons.HotstuffMessage) {
m.logger.Info().Fields(
map[string]any{
"height": m.CurrentHeight(),
"step": m.step,
"round": m.round,
},
).Msg("📣 Broadcasting message 📣")
m.logger.Info().Fields(hotstuffMsgToLoggingFields(msg)).Msg("📣 Broadcasting message 📣")

anyConsensusMessage, err := codec.GetCodec().ToAny(msg)
if err != nil {
Expand Down Expand Up @@ -301,6 +291,6 @@ func hotstuffMsgToLoggingFields(msg *typesCons.HotstuffMessage) map[string]any {
return map[string]any{
"height": msg.GetHeight(),
"round": msg.GetRound(),
"step": msg.GetStep(),
"step": typesCons.StepToString[msg.GetStep()],
}
}
2 changes: 1 addition & 1 deletion consensus/module_consensus_pacemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (m *consensusModule) ResetRound(isNewHeight bool) {
func (m *consensusModule) ReleaseUtilityUnitOfWork() error {
utilityUnitOfWork := m.utilityUnitOfWork
if utilityUnitOfWork == nil {
m.logger.Debug().Msg("Try to release utilityUnitOfWork is nil...")
m.logger.Debug().Msg("Try to release a nil utilityUnitOfWork... Ideally this should not happen")
return nil
}
if err := utilityUnitOfWork.Release(); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/korovkin/limiter v0.0.0-20230307205149-3d4b2b34c99d
github.com/labstack/echo/v4 v4.9.1
github.com/libp2p/go-libp2p v0.25.1
github.com/libp2p/go-libp2p-pubsub v0.9.2
github.com/looplab/fsm v1.0.1
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/mapstructure v1.5.0
Expand Down Expand Up @@ -110,7 +109,6 @@ require (
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/vault/api v1.9.0 h1:ab7dI6W8DuCY7yCU8blo0UCYl2oHre/dloCmzMWg9w8=
Expand Down Expand Up @@ -529,8 +527,6 @@ github.com/libp2p/go-libp2p v0.25.1 h1:YK+YDCHpYyTvitKWVxa5PfElgIpOONU01X5UcLEwJ
github.com/libp2p/go-libp2p v0.25.1/go.mod h1:xnK9/1d9+jeQCVvi/f1g12KqtVi/jP/SijtKV1hML3g=
github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw=
github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI=
github.com/libp2p/go-libp2p-pubsub v0.9.2 h1:CoWrvqtIbk+8iTLk1yCN8zODMgBSCqRgyVCvHaGJx8Y=
github.com/libp2p/go-libp2p-pubsub v0.9.2/go.mod h1:RYA7aM9jIic5VV47WXu4GkcRxRhrdElWf8xtyli+Dzc=
github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA=
github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0=
github.com/libp2p/go-msgio v0.3.0/go.mod h1:nyRM819GmVaF9LX3l03RMh10QdOroF++NBbxAb0mmDM=
Expand Down
47 changes: 0 additions & 47 deletions libp2p/docs/CHANGELOG.md

This file was deleted.

Loading

0 comments on commit 49a3223

Please sign in to comment.