Skip to content

Commit

Permalink
refactor: backgroundRouter as submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jul 11, 2023
1 parent b216a36 commit a1e8cc1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 39 deletions.
45 changes: 30 additions & 15 deletions p2p/background/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pokt-network/pocket/p2p/config"
"github.com/pokt-network/pocket/p2p/protocol"
"github.com/pokt-network/pocket/p2p/providers"
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/p2p/unicast"
"github.com/pokt-network/pocket/p2p/utils"
Expand All @@ -27,9 +28,8 @@ import (
)

var (
_ typesP2P.Router = &backgroundRouter{}
_ modules.IntegrableModule = &backgroundRouter{}
_ backgroundRouterFactory = &backgroundRouter{}
_ typesP2P.Router = &backgroundRouter{}
_ backgroundRouterFactory = &backgroundRouter{}
)

type backgroundRouterFactory = modules.FactoryWithConfig[typesP2P.Router, *config.BackgroundConfig]
Expand Down Expand Up @@ -93,7 +93,7 @@ func (*backgroundRouter) Create(bus modules.Bus, cfg *config.BackgroundConfig) (
host: cfg.Host,
cancelReadSubscription: cancel,
}
rtr.SetBus(bus)
bus.RegisterModule(rtr)

bgRouterLogger.Info().Fields(map[string]any{
"host_id": cfg.Host.ID(),
Expand Down Expand Up @@ -127,6 +127,11 @@ func (rtr *backgroundRouter) Close() error {
)
}

// GetModuleName implements the respective `modules.Integrable` interface method.
func (rtr *backgroundRouter) GetModuleName() string {
return typesP2P.UnstakedActorRouterSubmoduleName
}

// Broadcast implements the respective `typesP2P.Router` interface method.
func (rtr *backgroundRouter) Broadcast(pocketEnvelopeBz []byte) error {
backgroundMsg := &typesP2P.BackgroundMessage{
Expand Down Expand Up @@ -215,7 +220,8 @@ func (rtr *backgroundRouter) setupUnicastRouter() error {
return nil
}

func (rtr *backgroundRouter) setupDependencies(ctx context.Context, cfg *config.BackgroundConfig) error {
// TECHBEDT(#810,#811): remove unused `BackgroundConfig`
func (rtr *backgroundRouter) setupDependencies(ctx context.Context, _ *config.BackgroundConfig) error {
// NB: The order in which the internal components are setup below is important
if err := rtr.setupUnicastRouter(); err != nil {
return err
Expand All @@ -237,21 +243,30 @@ func (rtr *backgroundRouter) setupDependencies(ctx context.Context, cfg *config.
return fmt.Errorf("setting up subscription: %w", err)
}

if err := rtr.setupPeerstore(
ctx,
cfg.PeerstoreProvider,
cfg.CurrentHeightProvider,
); err != nil {
if err := rtr.setupPeerstore(ctx); err != nil {
return fmt.Errorf("setting up peerstore: %w", err)
}
return nil
}

func (rtr *backgroundRouter) setupPeerstore(
ctx context.Context,
pstoreProvider providers.PeerstoreProvider,
currentHeightProvider providers.CurrentHeightProvider,
) (err error) {
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().
GetModule(peerstore_provider.PeerstoreProviderSubmoduleName)
if err != nil {
return fmt.Errorf("retrieving peerstore provider: %w", err)
}
pstoreProvider, ok := pstoreProviderModule.(providers.PeerstoreProvider)
if !ok {
return fmt.Errorf("unexpected peerstore provider type: %T", pstoreProviderModule)
}

rtr.logger.Debug().Msg("setupCurrentHeightProvider")
currentHeightProvider := rtr.GetBus().GetCurrentHeightProvider()

// seed initial peerstore with current on-chain peer info (i.e. staked actors)
rtr.pstore, err = pstoreProvider.GetStakedPeerstoreAtHeight(
currentHeightProvider.CurrentHeight(),
Expand Down
32 changes: 23 additions & 9 deletions p2p/background/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ import (
"github.com/pokt-network/pocket/internal/testutil"
"github.com/pokt-network/pocket/p2p/config"
"github.com/pokt-network/pocket/p2p/protocol"
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
typesP2P "github.com/pokt-network/pocket/p2p/types"
mock_types "github.com/pokt-network/pocket/p2p/types/mocks"
"github.com/pokt-network/pocket/p2p/utils"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/runtime/configs"
"github.com/pokt-network/pocket/runtime/defaults"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"github.com/pokt-network/pocket/shared/messaging"
"github.com/pokt-network/pocket/shared/modules"
mockModules "github.com/pokt-network/pocket/shared/modules/mocks"
)

Expand Down Expand Up @@ -399,6 +402,7 @@ func newTestRouter(
return newRouterWithSelfPeerAndHost(t, selfPeer, host, handler)
}

// TECHDEBT(#796): de-dup & refactor
func newRouterWithSelfPeerAndHost(
t *testing.T,
selfPeer typesP2P.Peer,
Expand All @@ -415,16 +419,28 @@ func newRouterWithSelfPeerAndHost(
},
}).AnyTimes()

modulesRegistry := runtime.NewModulesRegistry()
busMock := mockModules.NewMockBus(ctrl)
busMock.EXPECT().GetRuntimeMgr().Return(runtimeMgrMock).AnyTimes()
busMock.EXPECT().GetModulesRegistry().Return(modulesRegistry).AnyTimes()
busMock.EXPECT().RegisterModule(gomock.Any()).DoAndReturn(func(arg modules.Submodule) {
module := arg.(modules.Submodule)
modulesRegistry.RegisterModule(module)
module.SetBus(busMock)
}).AnyTimes()

consensusMock := mockModules.NewMockConsensusModule(ctrl)
consensusMock.EXPECT().GetModuleName().Return(modules.ConsensusModuleName).AnyTimes()
consensusMock.EXPECT().GetBus().Return(busMock).AnyTimes()
consensusMock.EXPECT().SetBus(gomock.Any()).AnyTimes()
consensusMock.EXPECT().CurrentHeight().Return(uint64(1)).AnyTimes()
busMock.EXPECT().GetCurrentHeightProvider().Return(consensusMock).AnyTimes()

pstore := make(typesP2P.PeerAddrMap)
pstoreProviderMock := mock_types.NewMockPeerstoreProvider(ctrl)
pstoreProviderMock.EXPECT().GetModuleName().Return(peerstore_provider.PeerstoreProviderSubmoduleName)
pstoreProviderMock.EXPECT().GetStakedPeerstoreAtHeight(gomock.Any()).Return(pstore, nil).AnyTimes()

busMock := mockModules.NewMockBus(ctrl)
busMock.EXPECT().GetConsensusModule().Return(consensusMock).AnyTimes()
busMock.EXPECT().GetRuntimeMgr().Return(runtimeMgrMock).AnyTimes()
modulesRegistry.RegisterModule(pstoreProviderMock)

err := pstore.AddPeer(selfPeer)
require.NoError(t, err)
Expand All @@ -434,11 +450,9 @@ func newRouterWithSelfPeerAndHost(
}

router, err := Create(busMock, &config.BackgroundConfig{
Addr: selfPeer.GetAddress(),
PeerstoreProvider: pstoreProviderMock,
CurrentHeightProvider: consensusMock,
Host: host,
Handler: handler,
Addr: selfPeer.GetAddress(),
Host: host,
Handler: handler,
})
require.NoError(t, err)

Expand Down
16 changes: 6 additions & 10 deletions p2p/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ type UnicastRouterConfig struct {

// BackgroundConfig implements `RouterConfig` for use with `BackgroundRouter`.
type BackgroundConfig struct {
Host host.Host
Addr crypto.Address
CurrentHeightProvider providers.CurrentHeightProvider
PeerstoreProvider providers.PeerstoreProvider
Handler func(data []byte) error
Host host.Host
Addr crypto.Address
Handler func(data []byte) error
}

// RainTreeConfig implements `RouterConfig` for use with `RainTreeRouter`.
Expand Down Expand Up @@ -111,11 +109,9 @@ func (cfg *UnicastRouterConfig) IsValid() (err error) {
// IsValid implements the respective member of the `RouterConfig` interface.
func (cfg *BackgroundConfig) IsValid() error {
baseCfg := baseConfig{
Host: cfg.Host,
Addr: cfg.Addr,
CurrentHeightProvider: cfg.CurrentHeightProvider,
PeerstoreProvider: cfg.PeerstoreProvider,
Handler: cfg.Handler,
Host: cfg.Host,
Addr: cfg.Addr,
Handler: cfg.Handler,
}
return baseCfg.IsValid()
}
Expand Down
8 changes: 3 additions & 5 deletions p2p/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,9 @@ func (m *p2pModule) setupUnstakedRouter() (err error) {
m.unstakedActorRouter, err = background.Create(
m.GetBus(),
&config.BackgroundConfig{
Addr: m.address,
CurrentHeightProvider: m.currentHeightProvider,
PeerstoreProvider: m.pstoreProvider,
Host: m.host,
Handler: m.handlePocketEnvelope,
Addr: m.address,
Host: m.host,
Handler: m.handlePocketEnvelope,
},
)
if err != nil {
Expand Down

0 comments on commit a1e8cc1

Please sign in to comment.