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 1f5c27c commit 9862d90
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
17 changes: 10 additions & 7 deletions p2p/background/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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/current_height_provider"
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/p2p/unicast"
Expand All @@ -29,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 @@ -95,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 @@ -129,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 @@ -266,11 +269,11 @@ func (rtr *backgroundRouter) setupPeerstore(ctx context.Context) (err error) {
// provider is retrievable as a proper submodule
rtr.logger.Debug().Msg("setupCurrentHeightProvider")
currentHeightProviderModule, err := rtr.GetBus().GetModulesRegistry().
GetModule(current_height_provider.CurrentHeightProviderSubmoduleName)
GetModule(modules.CurrentHeightProviderSubmoduleName)
if err != nil {
currentHeightProviderModule = rtr.GetBus().GetConsensusModule()
}
currentHeightProvider, ok := currentHeightProviderModule.(providers.CurrentHeightProvider)
currentHeightProvider, ok := currentHeightProviderModule.(modules.CurrentHeightProvider)
if !ok {
return fmt.Errorf("unexpected current height provider type: %T", currentHeightProviderModule)
}
Expand Down
7 changes: 5 additions & 2 deletions p2p/background/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ 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/current_height_provider"
"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"
Expand All @@ -31,6 +30,7 @@ import (
"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 @@ -426,12 +426,15 @@ func newRouterWithSelfPeerAndHost(

modulesRegistryMock := mockModules.NewMockModulesRegistry(ctrl)
modulesRegistryMock.EXPECT().GetModule(gomock.Eq(peerstore_provider.PeerstoreProviderSubmoduleName)).Return(pstoreProviderMock, nil).AnyTimes()
modulesRegistryMock.EXPECT().GetModule(gomock.Eq(current_height_provider.CurrentHeightProviderSubmoduleName)).Return(consensusMock, nil).AnyTimes()
modulesRegistryMock.EXPECT().GetModule(gomock.Eq(modules.CurrentHeightProviderSubmoduleName)).Return(consensusMock, nil).AnyTimes()

busMock := mockModules.NewMockBus(ctrl)
busMock.EXPECT().GetConsensusModule().Return(consensusMock).AnyTimes()
busMock.EXPECT().GetRuntimeMgr().Return(runtimeMgrMock).AnyTimes()
busMock.EXPECT().GetModulesRegistry().Return(modulesRegistryMock).AnyTimes()
busMock.EXPECT().RegisterModule(gomock.Any()).DoAndReturn(func(m modules.Submodule) {
m.SetBus(busMock)
}).AnyTimes()

err := pstore.AddPeer(selfPeer)
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 @@ -113,11 +111,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 9862d90

Please sign in to comment.