Skip to content

Commit

Permalink
chore: interim bootstrapping changes (pre-#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jul 13, 2023
1 parent 8a531e4 commit 8869df4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
32 changes: 15 additions & 17 deletions p2p/background/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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().
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions p2p/background/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
36 changes: 23 additions & 13 deletions p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand Down
14 changes: 1 addition & 13 deletions p2p/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8869df4

Please sign in to comment.