Skip to content

Commit

Permalink
refactor: staked actor router peer discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jun 22, 2023
1 parent 0156f5c commit 4174cb2
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions p2p/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package p2p
import (
"fmt"

"google.golang.org/protobuf/types/known/anypb"

"github.com/pokt-network/pocket/shared/codec"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/messaging"
"google.golang.org/protobuf/types/known/anypb"
)

// CONSIDERATION(#576): making this part of some new `ConnManager`.
Expand All @@ -23,21 +24,25 @@ func (m *p2pModule) HandleEvent(event *anypb.Any) error {
return fmt.Errorf("failed to cast event to ConsensusNewHeightEvent")
}

oldPeerList := m.router.GetPeerstore().GetPeerList()
updatedPeerstore, err := m.pstoreProvider.GetStakedPeerstoreAtHeight(consensusNewHeightEvent.Height)
if err != nil {
if isStaked, err := m.isStakedActor(); err != nil {
return err
}

added, removed := oldPeerList.Delta(updatedPeerstore.GetPeerList())
for _, add := range added {
if err := m.router.AddPeer(add); err != nil {
} else if isStaked {
oldPeerList := m.stakedActorRouter.GetPeerstore().GetPeerList()
updatedPeerstore, err := m.pstoreProvider.GetStakedPeerstoreAtHeight(consensusNewHeightEvent.Height)
if err != nil {
return err
}
}
for _, rm := range removed {
if err := m.router.RemovePeer(rm); err != nil {
return err

added, removed := oldPeerList.Delta(updatedPeerstore.GetPeerList())
for _, add := range added {
if err := m.stakedActorRouter.AddPeer(add); err != nil {
return err
}
}
for _, rm := range removed {
if err := m.stakedActorRouter.RemovePeer(rm); err != nil {
return err
}
}
}

Expand All @@ -50,7 +55,7 @@ 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) {
if m.router.GetPeerstore().Size() == 0 {
if m.stakedActorRouter.GetPeerstore().Size() == 0 {
m.logger.Warn().Msg("No peers in addrbook, bootstrapping")

if err := m.bootstrap(); err != nil {
Expand Down

0 comments on commit 4174cb2

Please sign in to comment.