Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[P2P] chore: concurrent bootstrapping #694

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions p2p/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.44] - 2023-04-19

- Refactored P2P bootstrapping to its own go routine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀


## [0.0.0.43] - 2023-04-17

- Add test to exercise `sortedPeersView#Add()` and `#Remove()`
Expand Down
75 changes: 45 additions & 30 deletions p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"regexp"
"strconv"
"strings"
"sync"

rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc"
rpcABP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/rpc"
"github.com/pokt-network/pocket/runtime/defaults"
)
Expand Down Expand Up @@ -43,37 +43,57 @@ func (m *p2pModule) configureBootstrapNodes() error {

// bootstrap attempts to bootstrap from a bootstrap node
func (m *p2pModule) bootstrap() error {
var pstore typesP2P.Peerstore
var wg sync.WaitGroup

for _, bootstrapNode := range m.bootstrapNodes {
m.logger.Info().Str("endpoint", bootstrapNode).Msg("Attempting to bootstrap from bootstrap node")
for _, serviceURL := range m.bootstrapNodes {
wg.Add(1)
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved

client, err := rpc.NewClientWithResponses(bootstrapNode)
if err != nil {
continue
}
healthCheck, err := client.GetV1Health(context.TODO())
if err != nil || healthCheck == nil || healthCheck.StatusCode != http.StatusOK {
m.logger.Warn().Str("bootstrapNode", bootstrapNode).Msg("Error getting a green health check from bootstrap node")
continue
}
// concurrent bootstrapping
// TECHDEBT: add maximum bootstrapping concurrency to P2P config
go m.bootstrapFromServiceURL(serviceURL)
}
wg.Wait()

pstoreProvider := rpcABP.NewRPCPeerstoreProvider(
rpcABP.WithP2PConfig(
m.GetBus().GetRuntimeMgr().GetConfig().P2P,
),
rpcABP.WithCustomRPCURL(bootstrapNode),
)
if m.network.GetPeerstore().Size() == 0 {
return fmt.Errorf("bootstrap failed")
}
return nil
}

currentHeightProvider := rpcCHP.NewRPCCurrentHeightProvider(rpcCHP.WithCustomRPCURL(bootstrapNode))
// boostrapFromServiceURL fetches the peerstore of the peer at `serviceURL and
// adds it to this host's peerstore after performing a health check.
// TECHDEBT(SOLID): refactor; this method has more than one reason to change
func (m *p2pModule) bootstrapFromServiceURL(serviceURL string) {
// NB: no need closure `serviceURL` as long as it remains a value type
m.logger.Info().Str("endpoint", serviceURL).Msg("Attempting to bootstrap from bootstrap node")

pstore, err = pstoreProvider.GetStakedPeerstoreAtHeight(currentHeightProvider.CurrentHeight())
if err != nil {
m.logger.Warn().Err(err).Str("endpoint", bootstrapNode).Msg("Error getting address book from bootstrap node")
continue
}
client, err := rpc.NewClientWithResponses(serviceURL)
if err != nil {
return
}
healthCheck, err := client.GetV1Health(context.TODO())
if err != nil || healthCheck == nil || healthCheck.StatusCode != http.StatusOK {
m.logger.Warn().Str("serviceURL", serviceURL).Msg("Error getting a green health check from bootstrap node")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
m.logger.Warn().Str("serviceURL", serviceURL).Msg("Error getting a green health check from bootstrap node")
m.logger.Warn().Str("serviceURL", serviceURL).Int("code", healthCheck.StatusCode).Msg("Error getting a green health check from bootstrap node")

return
}

// fetch `serviceURL`'s peerstore
pstoreProvider := rpcABP.NewRPCPeerstoreProvider(
rpcABP.WithP2PConfig(
m.GetBus().GetRuntimeMgr().GetConfig().P2P,
),
rpcABP.WithCustomRPCURL(serviceURL),
)

currentHeightProvider := rpcCHP.NewRPCCurrentHeightProvider(rpcCHP.WithCustomRPCURL(serviceURL))

pstore, err := pstoreProvider.GetStakedPeerstoreAtHeight(currentHeightProvider.CurrentHeight())
if err != nil {
m.logger.Warn().Err(err).Str("endpoint", serviceURL).Msg("Error getting address book from bootstrap node")
return
}

// add `serviceURL`'s peers to this node's peerstore
for _, peer := range pstore.GetPeerList() {
m.logger.Debug().Str("address", peer.GetAddress().String()).Msg("Adding peer to network")
if err := m.network.AddPeer(peer); err != nil {
Expand All @@ -82,11 +102,6 @@ func (m *p2pModule) bootstrap() error {
Msg("adding peer")
}
}

if m.network.GetPeerstore().Size() == 0 {
return fmt.Errorf("bootstrap failed")
}
return nil
}

func isValidHostnamePort(str string) bool {
Expand Down