From 8869df48433b26b08c1b7443f42bdb9c4446086e Mon Sep 17 00:00:00 2001 From: Bryan White Date: Tue, 11 Jul 2023 09:28:54 +0200 Subject: [PATCH] chore: interim bootstrapping changes (pre-#859) --- p2p/background/router.go | 32 +++++++++++++++---------------- p2p/background/router_test.go | 11 +++-------- p2p/bootstrap.go | 36 ++++++++++++++++++++++------------- p2p/event_handler.go | 14 +------------- 4 files changed, 42 insertions(+), 51 deletions(-) diff --git a/p2p/background/router.go b/p2p/background/router.go index 5e6254d20..f32c629f9 100644 --- a/p2p/background/router.go +++ b/p2p/background/router.go @@ -65,7 +65,7 @@ type backgroundRouter struct { subscription *pubsub.Subscription // kadDHT is a kademlia distributed hash table used for routing and peer discovery. kadDHT *dht.IpfsDHT - // TECHDEBT: `pstore` will likely be removed in future refactoring / simplification + // TECHDEBT(#747, #749): `pstore` will likely be removed in future refactoring / simplification // of the `Router` interface. // pstore is the background router's peerstore. Assigned in `backgroundRouter#setupPeerstore()`. pstore typesP2P.Peerstore @@ -250,8 +250,6 @@ func (rtr *backgroundRouter) setupDependencies(ctx context.Context, _ *config.Ba } func (rtr *backgroundRouter) setupPeerstore(ctx context.Context) (err error) { - rtr.logger.Warn().Msg("setting up peerstore...") - // TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider // is retrievable as a proper submodule pstoreProviderModule, err := rtr.GetBus().GetModulesRegistry(). @@ -276,10 +274,7 @@ func (rtr *backgroundRouter) setupPeerstore(ctx context.Context) (err error) { } // TECHDEBT(#859): integrate with `p2pModule#bootstrap()`. - if err := rtr.bootstrap(ctx); err != nil { - return fmt.Errorf("bootstrapping peerstore: %w", err) - } - + rtr.bootstrap(ctx) return nil } @@ -335,33 +330,36 @@ func (rtr *backgroundRouter) setupSubscription() (err error) { } // TECHDEBT(#859): integrate with `p2pModule#bootstrap()`. -func (rtr *backgroundRouter) bootstrap(ctx context.Context) error { +func (rtr *backgroundRouter) bootstrap(ctx context.Context) { // CONSIDERATION: add `GetPeers` method, which returns a map, // to the `PeerstoreProvider` interface to simplify this loop. for _, peer := range rtr.pstore.GetPeerList() { if err := utils.AddPeerToLibp2pHost(rtr.host, peer); err != nil { - return err + rtr.logger.Error().Err(err).Msg("adding peer to libp2p host") + continue } libp2pAddrInfo, err := utils.Libp2pAddrInfoFromPeer(peer) if err != nil { - return fmt.Errorf( - "converting peer info, pokt address: %s: %w", - peer.GetAddress(), - err, - ) + rtr.logger.Error().Err(err).Msg("converting peer info") + continue } // don't attempt to connect to self if rtr.host.ID() == libp2pAddrInfo.ID { - return nil + rtr.logger.Debug().Msg("not bootstrapping against self") + continue } + rtr.logger.Debug().Fields(map[string]any{ + "peer_id": libp2pAddrInfo.ID.String(), + "peer_addr": libp2pAddrInfo.Addrs[0].String(), + }).Msg("connecting to peer") if err := rtr.host.Connect(ctx, libp2pAddrInfo); err != nil { - return fmt.Errorf("connecting to peer: %w", err) + rtr.logger.Error().Err(err).Msg("connecting to bootstrap peer") + continue } } - return nil } // topicValidator is used in conjunction with libp2p-pubsub's notion of "topic diff --git a/p2p/background/router_test.go b/p2p/background/router_test.go index 3c93cf758..14c7143fa 100644 --- a/p2p/background/router_test.go +++ b/p2p/background/router_test.go @@ -363,18 +363,13 @@ func bootstrap(t *testing.T, ctx context.Context, testHosts []libp2pHost.Host) { continue } - p2pAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", bootstrapHost.ID())) - require.NoError(t, err) - addrInfo := libp2pPeer.AddrInfo{ - ID: bootstrapHost.ID(), - Addrs: []multiaddr.Multiaddr{ - bootstrapAddr.Encapsulate(p2pAddr), - }, + ID: bootstrapHost.ID(), + Addrs: []multiaddr.Multiaddr{bootstrapAddr}, } t.Logf("connecting to %s...", addrInfo.ID.String()) - err = h.Connect(ctx, addrInfo) + err := h.Connect(ctx, addrInfo) require.NoError(t, err) } } diff --git a/p2p/bootstrap.go b/p2p/bootstrap.go index 258fb19a2..c74d34ad2 100644 --- a/p2p/bootstrap.go +++ b/p2p/bootstrap.go @@ -10,7 +10,7 @@ import ( "strings" rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc" - rpcABP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc" + rpcPSP "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" @@ -59,9 +59,9 @@ func (m *p2pModule) bootstrap() error { continue } - pstoreProvider, err := rpcABP.Create( + pstoreProvider, err := rpcPSP.Create( m.GetBus(), - rpcABP.WithCustomRPCURL(bootstrapNode), + rpcPSP.WithCustomRPCURL(bootstrapNode), ) if err != nil { return fmt.Errorf("creating RPC peerstore provider: %w", err) @@ -81,20 +81,30 @@ func (m *p2pModule) bootstrap() error { m.logger.Warn().Err(err).Str("endpoint", bootstrapNode).Msg("Error getting address book from bootstrap node") continue } - } - for _, peer := range pstore.GetPeerList() { - m.logger.Debug().Str("address", peer.GetAddress().String()).Msg("Adding peer to router") - if err := m.stakedActorRouter.AddPeer(peer); err != nil { - m.logger.Error().Err(err). - Str("pokt_address", peer.GetAddress().String()). - Msg("adding peer") + for _, peer := range pstore.GetPeerList() { + m.logger.Debug().Str("address", peer.GetAddress().String()).Msg("Adding peer to router") + isStaked, err := m.isStakedActor() + if err != nil { + m.logger.Error().Err(err).Msg("checking if node is staked") + } + if isStaked { + if err := m.stakedActorRouter.AddPeer(peer); err != nil { + m.logger.Error().Err(err). + Str("pokt_address", peer.GetAddress().String()). + Msg("adding peer to staked actor router") + } + } + + if err := m.unstakedActorRouter.AddPeer(peer); err != nil { + m.logger.Error().Err(err). + Str("pokt_address", peer.GetAddress().String()). + Msg("adding peer to unstaked actor router") + } } } - if m.stakedActorRouter.GetPeerstore().Size() == 0 { - return fmt.Errorf("bootstrap failed") - } + // TECHDEBT(#859): determine bootstrapping success/error conditions. return nil } diff --git a/p2p/event_handler.go b/p2p/event_handler.go index 2c612ab27..5dde1c5e1 100644 --- a/p2p/event_handler.go +++ b/p2p/event_handler.go @@ -61,21 +61,9 @@ func (m *p2pModule) HandleEvent(event *anypb.Any) error { m.logger.Debug().Fields(messaging.TransitionEventToMap(stateMachineTransitionEvent)).Msg("Received state machine transition event") if stateMachineTransitionEvent.NewState == string(coreTypes.StateMachineState_P2P_Bootstrapping) { - staked, err := m.isStakedActor() - if err != nil { + if err := m.bootstrap(); err != nil { return err } - if staked { - // TECHDEBT(#859): this will never happen as the peerstore is - // seeded from consensus during P2P module construction. - if m.stakedActorRouter.GetPeerstore().Size() == 0 { - m.logger.Warn().Msg("No peers in peerstore, bootstrapping") - - if err := m.bootstrap(); err != nil { - return err - } - } - } // TECHDEBT(#859): for unstaked actors, unstaked actor (background) // router bootstrapping SHOULD complete before the event below is sent.