Skip to content

Commit 8869df4

Browse files
chore: interim bootstrapping changes (pre-#859)
1 parent 8a531e4 commit 8869df4

File tree

4 files changed

+42
-51
lines changed

4 files changed

+42
-51
lines changed

p2p/background/router.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type backgroundRouter struct {
6565
subscription *pubsub.Subscription
6666
// kadDHT is a kademlia distributed hash table used for routing and peer discovery.
6767
kadDHT *dht.IpfsDHT
68-
// TECHDEBT: `pstore` will likely be removed in future refactoring / simplification
68+
// TECHDEBT(#747, #749): `pstore` will likely be removed in future refactoring / simplification
6969
// of the `Router` interface.
7070
// pstore is the background router's peerstore. Assigned in `backgroundRouter#setupPeerstore()`.
7171
pstore typesP2P.Peerstore
@@ -250,8 +250,6 @@ func (rtr *backgroundRouter) setupDependencies(ctx context.Context, _ *config.Ba
250250
}
251251

252252
func (rtr *backgroundRouter) setupPeerstore(ctx context.Context) (err error) {
253-
rtr.logger.Warn().Msg("setting up peerstore...")
254-
255253
// TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider
256254
// is retrievable as a proper submodule
257255
pstoreProviderModule, err := rtr.GetBus().GetModulesRegistry().
@@ -276,10 +274,7 @@ func (rtr *backgroundRouter) setupPeerstore(ctx context.Context) (err error) {
276274
}
277275

278276
// TECHDEBT(#859): integrate with `p2pModule#bootstrap()`.
279-
if err := rtr.bootstrap(ctx); err != nil {
280-
return fmt.Errorf("bootstrapping peerstore: %w", err)
281-
}
282-
277+
rtr.bootstrap(ctx)
283278
return nil
284279
}
285280

@@ -335,33 +330,36 @@ func (rtr *backgroundRouter) setupSubscription() (err error) {
335330
}
336331

337332
// TECHDEBT(#859): integrate with `p2pModule#bootstrap()`.
338-
func (rtr *backgroundRouter) bootstrap(ctx context.Context) error {
333+
func (rtr *backgroundRouter) bootstrap(ctx context.Context) {
339334
// CONSIDERATION: add `GetPeers` method, which returns a map,
340335
// to the `PeerstoreProvider` interface to simplify this loop.
341336
for _, peer := range rtr.pstore.GetPeerList() {
342337
if err := utils.AddPeerToLibp2pHost(rtr.host, peer); err != nil {
343-
return err
338+
rtr.logger.Error().Err(err).Msg("adding peer to libp2p host")
339+
continue
344340
}
345341

346342
libp2pAddrInfo, err := utils.Libp2pAddrInfoFromPeer(peer)
347343
if err != nil {
348-
return fmt.Errorf(
349-
"converting peer info, pokt address: %s: %w",
350-
peer.GetAddress(),
351-
err,
352-
)
344+
rtr.logger.Error().Err(err).Msg("converting peer info")
345+
continue
353346
}
354347

355348
// don't attempt to connect to self
356349
if rtr.host.ID() == libp2pAddrInfo.ID {
357-
return nil
350+
rtr.logger.Debug().Msg("not bootstrapping against self")
351+
continue
358352
}
359353

354+
rtr.logger.Debug().Fields(map[string]any{
355+
"peer_id": libp2pAddrInfo.ID.String(),
356+
"peer_addr": libp2pAddrInfo.Addrs[0].String(),
357+
}).Msg("connecting to peer")
360358
if err := rtr.host.Connect(ctx, libp2pAddrInfo); err != nil {
361-
return fmt.Errorf("connecting to peer: %w", err)
359+
rtr.logger.Error().Err(err).Msg("connecting to bootstrap peer")
360+
continue
362361
}
363362
}
364-
return nil
365363
}
366364

367365
// topicValidator is used in conjunction with libp2p-pubsub's notion of "topic

p2p/background/router_test.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,13 @@ func bootstrap(t *testing.T, ctx context.Context, testHosts []libp2pHost.Host) {
363363
continue
364364
}
365365

366-
p2pAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", bootstrapHost.ID()))
367-
require.NoError(t, err)
368-
369366
addrInfo := libp2pPeer.AddrInfo{
370-
ID: bootstrapHost.ID(),
371-
Addrs: []multiaddr.Multiaddr{
372-
bootstrapAddr.Encapsulate(p2pAddr),
373-
},
367+
ID: bootstrapHost.ID(),
368+
Addrs: []multiaddr.Multiaddr{bootstrapAddr},
374369
}
375370

376371
t.Logf("connecting to %s...", addrInfo.ID.String())
377-
err = h.Connect(ctx, addrInfo)
372+
err := h.Connect(ctx, addrInfo)
378373
require.NoError(t, err)
379374
}
380375
}

p2p/bootstrap.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111

1212
rpcCHP "github.com/pokt-network/pocket/p2p/providers/current_height_provider/rpc"
13-
rpcABP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc"
13+
rpcPSP "github.com/pokt-network/pocket/p2p/providers/peerstore_provider/rpc"
1414
typesP2P "github.com/pokt-network/pocket/p2p/types"
1515
"github.com/pokt-network/pocket/rpc"
1616
"github.com/pokt-network/pocket/runtime/defaults"
@@ -59,9 +59,9 @@ func (m *p2pModule) bootstrap() error {
5959
continue
6060
}
6161

62-
pstoreProvider, err := rpcABP.Create(
62+
pstoreProvider, err := rpcPSP.Create(
6363
m.GetBus(),
64-
rpcABP.WithCustomRPCURL(bootstrapNode),
64+
rpcPSP.WithCustomRPCURL(bootstrapNode),
6565
)
6666
if err != nil {
6767
return fmt.Errorf("creating RPC peerstore provider: %w", err)
@@ -81,20 +81,30 @@ func (m *p2pModule) bootstrap() error {
8181
m.logger.Warn().Err(err).Str("endpoint", bootstrapNode).Msg("Error getting address book from bootstrap node")
8282
continue
8383
}
84-
}
8584

86-
for _, peer := range pstore.GetPeerList() {
87-
m.logger.Debug().Str("address", peer.GetAddress().String()).Msg("Adding peer to router")
88-
if err := m.stakedActorRouter.AddPeer(peer); err != nil {
89-
m.logger.Error().Err(err).
90-
Str("pokt_address", peer.GetAddress().String()).
91-
Msg("adding peer")
85+
for _, peer := range pstore.GetPeerList() {
86+
m.logger.Debug().Str("address", peer.GetAddress().String()).Msg("Adding peer to router")
87+
isStaked, err := m.isStakedActor()
88+
if err != nil {
89+
m.logger.Error().Err(err).Msg("checking if node is staked")
90+
}
91+
if isStaked {
92+
if err := m.stakedActorRouter.AddPeer(peer); err != nil {
93+
m.logger.Error().Err(err).
94+
Str("pokt_address", peer.GetAddress().String()).
95+
Msg("adding peer to staked actor router")
96+
}
97+
}
98+
99+
if err := m.unstakedActorRouter.AddPeer(peer); err != nil {
100+
m.logger.Error().Err(err).
101+
Str("pokt_address", peer.GetAddress().String()).
102+
Msg("adding peer to unstaked actor router")
103+
}
92104
}
93105
}
94106

95-
if m.stakedActorRouter.GetPeerstore().Size() == 0 {
96-
return fmt.Errorf("bootstrap failed")
97-
}
107+
// TECHDEBT(#859): determine bootstrapping success/error conditions.
98108
return nil
99109
}
100110

p2p/event_handler.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,9 @@ func (m *p2pModule) HandleEvent(event *anypb.Any) error {
6161
m.logger.Debug().Fields(messaging.TransitionEventToMap(stateMachineTransitionEvent)).Msg("Received state machine transition event")
6262

6363
if stateMachineTransitionEvent.NewState == string(coreTypes.StateMachineState_P2P_Bootstrapping) {
64-
staked, err := m.isStakedActor()
65-
if err != nil {
64+
if err := m.bootstrap(); err != nil {
6665
return err
6766
}
68-
if staked {
69-
// TECHDEBT(#859): this will never happen as the peerstore is
70-
// seeded from consensus during P2P module construction.
71-
if m.stakedActorRouter.GetPeerstore().Size() == 0 {
72-
m.logger.Warn().Msg("No peers in peerstore, bootstrapping")
73-
74-
if err := m.bootstrap(); err != nil {
75-
return err
76-
}
77-
}
78-
}
7967

8068
// TECHDEBT(#859): for unstaked actors, unstaked actor (background)
8169
// router bootstrapping SHOULD complete before the event below is sent.

0 commit comments

Comments
 (0)