Skip to content

Commit 49a3223

Browse files
bryanchriswhiteOlshanskokdasgithub-actions
authored
[P2P] Refactor/consolidate P2P modules (#576)
## @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]>
1 parent a7fd2a6 commit 49a3223

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1353
-1629
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ mockgen: clean_mocks ## Use `mockgen` to generate mocks used for testing purpose
253253
go generate ./${modules_dir}
254254
echo "Mocks generated in ${modules_dir}/mocks"
255255

256-
$(eval DIRS = p2p libp2p persistence)
256+
$(eval DIRS = p2p persistence)
257257
for dir in $(DIRS); do \
258258
echo "Processing $$dir mocks..."; \
259259
find $$dir/types/mocks -type f ! -name "mocks.go" -exec rm {} \;; \

app/client/cli/debug.go

+12-24
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import (
66
"os"
77

88
"github.com/manifoldco/promptui"
9-
"github.com/pokt-network/pocket/libp2p"
9+
"github.com/spf13/cobra"
10+
"google.golang.org/protobuf/types/known/anypb"
11+
1012
"github.com/pokt-network/pocket/logger"
1113
"github.com/pokt-network/pocket/p2p"
1214
"github.com/pokt-network/pocket/p2p/providers/current_height_provider"
1315
rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc"
1416
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
1517
rpcABP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc"
18+
typesP2P "github.com/pokt-network/pocket/p2p/types"
1619
"github.com/pokt-network/pocket/runtime"
1720
"github.com/pokt-network/pocket/runtime/defaults"
1821
"github.com/pokt-network/pocket/shared/messaging"
1922
"github.com/pokt-network/pocket/shared/modules"
20-
sharedP2P "github.com/pokt-network/pocket/shared/p2p"
21-
"github.com/spf13/cobra"
22-
"google.golang.org/protobuf/types/known/anypb"
2323
)
2424

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

106106
setValueInCLIContext(cmd, busCLICtxKey, bus)
107107

108-
// TECHDEBT: simplify after P2P module consolidation.
109-
var err error
110-
p2pMod, err = getP2PModule(runtimeMgr)
108+
mod, err := p2p.Create(bus)
111109
if err != nil {
112110
logger.Global.Fatal().Err(err).Msg("Failed to create p2p module")
113111
}
114112

113+
var ok bool
114+
p2pMod, ok = mod.(modules.P2PModule)
115+
if !ok {
116+
logger.Global.Fatal().Msgf("unexpected P2P module type: %T", mod)
117+
}
118+
115119
if err := p2pMod.Start(); err != nil {
116120
logger.Global.Fatal().Err(err).Msg("Failed to start p2p module")
117121
}
@@ -265,7 +269,7 @@ func sendDebugMessage(cmd *cobra.Command, debugMsg *messaging.DebugMessage) {
265269
}
266270

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

296-
func getP2PModule(runtimeMgr *runtime.Manager) (p2pModule modules.P2PModule, err error) {
297-
bus := runtimeMgr.GetBus()
298-
299-
var mod modules.Module
300-
if runtimeMgr.GetConfig().UseLibP2P {
301-
mod, err = libp2p.Create(bus)
302-
} else {
303-
mod, err = p2p.Create(bus)
304-
}
305-
if err != nil {
306-
return nil, err
307-
}
308-
309-
return mod.(modules.P2PModule), nil
310-
}
311-
312300
// sendConsensusNewHeightEventToP2PModule mimicks the consensus module sending a ConsensusNewHeightEvent to the p2p module
313301
// This is necessary because the debug client is not a validator and has no consensus module but it has to update the peerstore
314302
// depending on the changes in the validator set.

app/client/doc/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.28] - 2023-04-17
11+
12+
- Refactor debug CLI post P2P module re-consolidation
13+
1014
## [0.0.0.27] - 2023-04-07
1115

1216
- Add Query Command

build/deployments/docker-compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ services:
5353
- "seccomp:unconfined"
5454
environment:
5555
- POCKET_RPC_USE_CORS=true
56+
- LIBP2P_DEBUG=info
5657
# Uncomment to enable the pprof server
5758
# - PPROF_ENABLED=true
5859
# Uncomment to enable DLV debugging

build/docs/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.35] - 2023-04-17
11+
12+
- Removed runtime/configs.Config#UseLibp2p field
13+
- Use pod IP for validator DNS resolution tilt localnet
14+
- Add `LIBP2P_DEBUG` env var
15+
1016
## [0.0.0.34] - 2023-04-14
1117

1218
- Changed LocalNet validators to use the new `pocket-validator` helm chart instead of templating the manifests with `sed`.

build/localnet/manifests/cli-client.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ spec:
7676
# Any host that is visible and connected to the cluster can be arbitrarily selected as the RPC host
7777
- name: RPC_HOST
7878
value: pocket-validators
79+
# TECHDEBT(#678): debug client requires hostname to participate
80+
# in P2P networking.
81+
- name: POCKET_P2P_HOSTNAME
82+
value: "127.0.0.1"
7983
volumeMounts:
8084
# IMPROVE: should probably go in /etc/pocket and have Viper read from there as a default path
8185
- mountPath: /var/pocket/config

charts/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.2] - 2023-04-17
11+
12+
- Removed `runtime/configs.Config#UseLibp2p` field
13+
- Set validator `POCKET_P2P_HOSTNAME` env var to the pod IP
14+
- Set validator `p2p.hostname` config value to empty string so that the env var applies
15+
1016
## [0.0.0.1] - 2023-04-14
1117

1218
- Introduced `pocket-validator` helm chart.

charts/pocket-validator/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ privateKeySecretKeyRef:
4545
| config.consensus.private_key | string | `""` | |
4646
| config.logger.format | string | `"json"` | |
4747
| config.logger.level | string | `"debug"` | |
48+
| config.p2p.hostname | string | `""` | |
4849
| config.p2p.is_empty_connection_type | bool | `false` | |
4950
| config.p2p.max_mempool_count | int | `100000` | |
5051
| config.p2p.port | int | `42069` | |
@@ -69,7 +70,6 @@ privateKeySecretKeyRef:
6970
| config.telemetry.address | string | `"0.0.0.0:9000"` | |
7071
| config.telemetry.enabled | bool | `true` | |
7172
| config.telemetry.endpoint | string | `"/metrics"` | |
72-
| config.use_libp2p | bool | `false` | |
7373
| config.utility.max_mempool_transaction_bytes | int | `1073741824` | |
7474
| config.utility.max_mempool_transactions | int | `9000` | |
7575
| externalPostgresql.database | string | `""` | name of the external database |

charts/pocket-validator/templates/statefulset.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ spec:
3535
"until nc -z $(POSTGRES_HOST) $(POSTGRES_PORT); do echo waiting for postgres...; sleep 2; done;",
3636
]
3737
env:
38+
- name: POCKET_P2P_HOSTNAME
39+
valueFrom:
40+
fieldRef:
41+
fieldPath: status.podIP
3842
- name: POSTGRES_HOST
3943
value: {{ include "pocket-validator.postgresqlHost" . }}
4044
- name: POSTGRES_PORT

charts/pocket-validator/values.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ privateKeySecretKeyRef:
7070
config:
7171
root_directory: "/go/src/github.com/pocket-network"
7272
private_key: "" # @ignored This value is needed but ignored - use privateKeySecretKeyRef instead
73-
use_libp2p: false
7473
consensus:
7574
max_mempool_bytes: 500000000
7675
pacemaker_config:
@@ -93,6 +92,7 @@ config:
9392
max_conn_idle_time: 1m
9493
health_check_period: 30s
9594
p2p:
95+
hostname: ""
9696
port: 42069
9797
use_rain_tree: true
9898
is_empty_connection_type: false

consensus/debugging.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ func (m *consensusModule) resetToGenesis(_ *messaging.DebugMessage) error {
5050

5151
func (m *consensusModule) printNodeState(_ *messaging.DebugMessage) {
5252
state := m.GetNodeState()
53-
m.logger.Debug().
54-
Fields(map[string]any{
55-
"step": state.Step,
56-
"height": state.Height,
57-
"round": state.Round,
58-
}).Msg("Node state")
53+
m.logger.Debug().Fields(map[string]any{
54+
"step": typesCons.StepToString[typesCons.HotstuffStep(state.Step)],
55+
"height": state.Height,
56+
"round": state.Round,
57+
}).Msg("Node state")
5958
}
6059

6160
func (m *consensusModule) triggerNextView(_ *messaging.DebugMessage) {

consensus/doc/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.48] - 2023-04-17
11+
12+
- Debug logging improvements
13+
1014
## [0.0.0.47] - 2023-04-17
1115

1216
- Log warnings in `handleStateSyncMessage()`

consensus/helpers.go

+7-17
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,11 @@ func protoHash(m proto.Message) string {
145145

146146
func (m *consensusModule) sendToLeader(msg *typesCons.HotstuffMessage) {
147147
leaderId := m.leaderId
148-
m.logger.Debug().Fields(
149-
map[string]any{
150-
"src": m.nodeId,
151-
"dst": leaderId,
152-
"height": msg.GetHeight(),
153-
"step": msg.GetStep(),
154-
"round": msg.GetRound(),
155-
},
156-
).Msg("✉️ About to try sending hotstuff message ✉️")
148+
149+
loggingFields := hotstuffMsgToLoggingFields(msg)
150+
loggingFields["src"] = m.nodeId
151+
loggingFields["dst"] = leaderId
152+
m.logger.Debug().Fields(loggingFields).Msg("✉️ About to try sending hotstuff message ✉️")
157153

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

194184
anyConsensusMessage, err := codec.GetCodec().ToAny(msg)
195185
if err != nil {
@@ -301,6 +291,6 @@ func hotstuffMsgToLoggingFields(msg *typesCons.HotstuffMessage) map[string]any {
301291
return map[string]any{
302292
"height": msg.GetHeight(),
303293
"round": msg.GetRound(),
304-
"step": msg.GetStep(),
294+
"step": typesCons.StepToString[msg.GetStep()],
305295
}
306296
}

consensus/module_consensus_pacemaker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (m *consensusModule) ResetRound(isNewHeight bool) {
2828
func (m *consensusModule) ReleaseUtilityUnitOfWork() error {
2929
utilityUnitOfWork := m.utilityUnitOfWork
3030
if utilityUnitOfWork == nil {
31-
m.logger.Debug().Msg("Try to release utilityUnitOfWork is nil...")
31+
m.logger.Debug().Msg("Try to release a nil utilityUnitOfWork... Ideally this should not happen")
3232
return nil
3333
}
3434
if err := utilityUnitOfWork.Release(); err != nil {

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ require (
2626
github.com/korovkin/limiter v0.0.0-20230307205149-3d4b2b34c99d
2727
github.com/labstack/echo/v4 v4.9.1
2828
github.com/libp2p/go-libp2p v0.25.1
29-
github.com/libp2p/go-libp2p-pubsub v0.9.2
3029
github.com/looplab/fsm v1.0.1
3130
github.com/manifoldco/promptui v0.9.0
3231
github.com/mitchellh/mapstructure v1.5.0
@@ -110,7 +109,6 @@ require (
110109
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
111110
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
112111
github.com/hashicorp/golang-lru v0.5.4 // indirect
113-
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
114112
github.com/huin/goupnp v1.0.3 // indirect
115113
github.com/imdario/mergo v0.3.6 // indirect
116114
github.com/inconshreveable/mousetrap v1.0.1 // indirect

go.sum

-4
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
387387
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
388388
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
389389
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
390-
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
391-
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
392390
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
393391
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
394392
github.com/hashicorp/vault/api v1.9.0 h1:ab7dI6W8DuCY7yCU8blo0UCYl2oHre/dloCmzMWg9w8=
@@ -529,8 +527,6 @@ github.com/libp2p/go-libp2p v0.25.1 h1:YK+YDCHpYyTvitKWVxa5PfElgIpOONU01X5UcLEwJ
529527
github.com/libp2p/go-libp2p v0.25.1/go.mod h1:xnK9/1d9+jeQCVvi/f1g12KqtVi/jP/SijtKV1hML3g=
530528
github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw=
531529
github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI=
532-
github.com/libp2p/go-libp2p-pubsub v0.9.2 h1:CoWrvqtIbk+8iTLk1yCN8zODMgBSCqRgyVCvHaGJx8Y=
533-
github.com/libp2p/go-libp2p-pubsub v0.9.2/go.mod h1:RYA7aM9jIic5VV47WXu4GkcRxRhrdElWf8xtyli+Dzc=
534530
github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA=
535531
github.com/libp2p/go-msgio v0.3.0 h1:mf3Z8B1xcFN314sWX+2vOTShIE0Mmn2TXn3YCUQGNj0=
536532
github.com/libp2p/go-msgio v0.3.0/go.mod h1:nyRM819GmVaF9LX3l03RMh10QdOroF++NBbxAb0mmDM=

libp2p/docs/CHANGELOG.md

-47
This file was deleted.

0 commit comments

Comments
 (0)