Skip to content

Commit

Permalink
chore: introduce Submodule interface (#855)
Browse files Browse the repository at this point in the history
## Description

Introduces the `Submodule` interface and applies it to the P2P module's
peerstore provider.

## Issue

Deliverables 1, 2, 3, & 5:
- #810

## Type of change

Please mark the relevant option(s):

- [ ] New feature, functionality or library
- [ ] Bug fix
- [x] Code health or cleanup
- [ ] Major breaking change
- [x] Documentation
- [ ] Other <!-- add details here if it a different type of change -->

## List of changes

* fixed typo; renamee `IntegratableModule` to `IntegrableModule`
* renamed `InitializableModule` to `InjectableModule`
* removed `InjectableModule#Create()` as `Moudle` also embeds
`ModuleFactoryWithConfig`
* added `Submodule` interface type
* refactored peerstore providers as submodules
* updated module README

## Testing

- [x] `make develop_test`; if any code changes were made
- [x] `make test_e2e` on [k8s
LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md);
if any code changes were made
- [x] `e2e-devnet-test` passes tests on
[DevNet](https://pocketnetwork.notion.site/How-to-DevNet-ff1598f27efe44c09f34e2aa0051f0dd);
if any code was changed
- [ ] [Docker Compose
LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md);
if any major functionality was changed or introduced
- [x] [k8s
LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md);
if any infrastructure or configuration changes were made

## Required Checklist

- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added, or updated, [`godoc` format
comments](https://go.dev/blog/godoc) on touched members (see:
[tip.golang.org/doc/comment](https://tip.golang.org/doc/comment))
- [ ] I have tested my changes using the available tooling
- [ ] I have updated the corresponding CHANGELOG

### If Applicable Checklist

- [x] I have updated the corresponding README(s); local and/or global
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] I have added, or updated,
[mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding
README(s)
- [ ] I have added, or updated, documentation and
[mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*`
if I updated `shared/*`README(s)

---------

Co-authored-by: d7t <[email protected]>
Co-authored-by: @olshansky <[email protected]>
Co-authored-by: Daniel Olshansky <[email protected]>
Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
5 people authored Jul 11, 2023
1 parent 6b875ee commit c7412b6
Show file tree
Hide file tree
Showing 46 changed files with 488 additions and 159 deletions.
4 changes: 3 additions & 1 deletion app/client/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ func fetchPeerstore(cmd *cobra.Command) (typesP2P.Peerstore, error) {
return nil, errors.New("retrieving bus from CLI context")
}
modulesRegistry := bus.GetModulesRegistry()
pstoreProvider, err := modulesRegistry.GetModule(peerstore_provider.ModuleName)
// TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider
// is retrievable as a proper submodule
pstoreProvider, err := modulesRegistry.GetModule(peerstore_provider.PeerstoreProviderSubmoduleName)
if err != nil {
return nil, errors.New("retrieving peerstore provider")
}
Expand Down
20 changes: 11 additions & 9 deletions app/client/cli/helpers/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ func P2PDependenciesPreRunE(cmd *cobra.Command, _ []string) error {
bus := runtimeMgr.GetBus()
SetValueInCLIContext(cmd, BusCLICtxKey, bus)

setupPeerstoreProvider(*runtimeMgr, flags.RemoteCLIURL)
if err := setupPeerstoreProvider(*runtimeMgr, flags.RemoteCLIURL); err != nil {
return err
}
setupCurrentHeightProvider(*runtimeMgr, flags.RemoteCLIURL)
setupAndStartP2PModule(*runtimeMgr)

return nil
}

func setupPeerstoreProvider(rm runtime.Manager, rpcURL string) {
bus := rm.GetBus()
modulesRegistry := bus.GetModulesRegistry()
pstoreProvider := rpcPSP.Create(
rpcPSP.WithP2PConfig(rm.GetConfig().P2P),
rpcPSP.WithCustomRPCURL(rpcURL),
)
modulesRegistry.RegisterModule(pstoreProvider)
func setupPeerstoreProvider(rm runtime.Manager, rpcURL string) error {
// Ensure `PeerstoreProvider` exists in the modules registry.
if _, err := rpcPSP.Create(rm.GetBus(), rpcPSP.WithCustomRPCURL(rpcURL)); err != nil {
return err
}
return nil
}

func setupCurrentHeightProvider(rm runtime.Manager, rpcURL string) {
// TECHDEBT(#810): simplify after current height provider is refactored as
// a submodule.
bus := rm.GetBus()
modulesRegistry := bus.GetModulesRegistry()
currentHeightProvider := rpcCHP.NewRPCCurrentHeightProvider(
Expand Down
2 changes: 1 addition & 1 deletion consensus/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/pokt-network/pocket/shared/messaging"
)

// publishNewHeightEvent publishes a new height event to the bus so that other interested IntegratableModules can react to it if necessary
// publishNewHeightEvent publishes a new height event to the bus so that other interested IntegrableModules can react to it if necessary
func (m *consensusModule) publishNewHeightEvent(height uint64) {
newHeightEvent, err := messaging.PackMessage(&messaging.ConsensusNewHeightEvent{Height: height})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion consensus/leader_election/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type LeaderElectionModule interface {
var _ LeaderElectionModule = &leaderElectionModule{}

type leaderElectionModule struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
base_modules.InterruptableModule
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
)

type consensusModule struct {
base_modules.IntegratableModule
base_modules.IntegrableModule

privateKey cryptoPocket.Ed25519PrivateKey

Expand Down Expand Up @@ -209,7 +209,7 @@ func (m *consensusModule) GetModuleName() string {
}

func (m *consensusModule) SetBus(pocketBus modules.Bus) {
m.IntegratableModule.SetBus(pocketBus)
m.IntegrableModule.SetBus(pocketBus)
if m.paceMaker != nil {
m.paceMaker.SetBus(pocketBus)
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/pacemaker/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Pacemaker interface {
}

type pacemaker struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
base_modules.InterruptableModule

pacemakerCfg *configs.PacemakerConfig
Expand Down
2 changes: 1 addition & 1 deletion ibc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var _ modules.IBCModule = &ibcModule{}

type ibcModule struct {
base_modules.IntegratableModule
base_modules.IntegrableModule

cfg *configs.IBCConfig
logger *modules.Logger
Expand Down
2 changes: 1 addition & 1 deletion logger/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var _ modules.Module = &loggerModule{}

type loggerModule struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
base_modules.InterruptableModule

zerolog.Logger
Expand Down
10 changes: 6 additions & 4 deletions p2p/background/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ import (
)

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

type backgroundRouterFactory = modules.FactoryWithConfig[typesP2P.Router, *config.BackgroundConfig]

// backgroundRouter implements `typesP2P.Router` for use with all P2P participants.
type backgroundRouter struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
unicast.UnicastRouter

logger *modules.Logger
Expand Down Expand Up @@ -260,6 +260,7 @@ func (rtr *backgroundRouter) setupPeerstore(
return err
}

// TECHDEBT(#859): integrate with `p2pModule#bootstrap()`.
if err := rtr.bootstrap(ctx); err != nil {
return fmt.Errorf("bootstrapping peerstore: %w", err)
}
Expand Down Expand Up @@ -318,6 +319,7 @@ func (rtr *backgroundRouter) setupSubscription() (err error) {
return err
}

// TECHDEBT(#859): integrate with `p2pModule#bootstrap()`.
func (rtr *backgroundRouter) bootstrap(ctx context.Context) error {
// CONSIDERATION: add `GetPeers` method, which returns a map,
// to the `PeerstoreProvider` interface to simplify this loop.
Expand Down
9 changes: 5 additions & 4 deletions p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ func (m *p2pModule) bootstrap() error {
continue
}

pstoreProvider := rpcABP.Create(
rpcABP.WithP2PConfig(
m.GetBus().GetRuntimeMgr().GetConfig().P2P,
),
pstoreProvider, err := rpcABP.Create(
m.GetBus(),
rpcABP.WithCustomRPCURL(bootstrapNode),
)
if err != nil {
return fmt.Errorf("creating RPC peerstore provider: %w", err)
}

currentHeightProvider := rpcCHP.NewRPCCurrentHeightProvider(rpcCHP.WithCustomRPCURL(bootstrapNode))

Expand Down
1 change: 0 additions & 1 deletion p2p/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (cfg *baseConfig) IsValid() (err error) {
if cfg.Handler == nil {
err = multierr.Append(err, fmt.Errorf("handler not configured"))
}

return err
}

Expand Down
9 changes: 6 additions & 3 deletions p2p/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
var _ modules.P2PModule = &p2pModule{}

type p2pModule struct {
base_modules.IntegratableModule
base_modules.IntegrableModule

started atomic.Bool
address cryptoPocket.Address
Expand Down Expand Up @@ -300,8 +300,11 @@ func (m *p2pModule) setupDependencies() error {
func (m *p2pModule) setupPeerstoreProvider() error {
m.logger.Debug().Msg("setupPeerstoreProvider")

// TECHDEBT(#810): simplify once submodules are more convenient to retrieve.
pstoreProviderModule, err := m.GetBus().GetModulesRegistry().GetModule(peerstore_provider.ModuleName)
// TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider
// is retrievable as a proper submodule
pstoreProviderModule, err := m.GetBus().
GetModulesRegistry().
GetModule(peerstore_provider.PeerstoreProviderSubmoduleName)
if err != nil {
m.logger.Debug().Msg("creating new persistence peerstore...")
pstoreProvider, err := persPSP.Create(m.GetBus())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "github.com/pokt-network/pocket/shared/modules"
const ModuleName = "current_height_provider"

type CurrentHeightProvider interface {
modules.IntegratableModule
modules.IntegrableModule
modules.InterruptableModule

CurrentHeight() uint64
Expand Down
4 changes: 2 additions & 2 deletions p2p/providers/current_height_provider/rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var _ current_height_provider.CurrentHeightProvider = &rpcCurrentHeightProvider{}

type rpcCurrentHeightProvider struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
base_modules.InterruptableModule

rpcURL string
Expand Down Expand Up @@ -80,7 +80,7 @@ func (rchp *rpcCurrentHeightProvider) initRPCClient() {

// WithCustomRPCURL allows to specify a custom RPC URL
func WithCustomRPCURL(rpcURL string) modules.ModuleOption {
return func(rabp modules.InitializableModule) {
return func(rabp modules.InjectableModule) {
rabp.(*rpcCurrentHeightProvider).rpcURL = rpcURL
}
}
4 changes: 2 additions & 2 deletions p2p/providers/peerstore_provider/peerstore_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"go.uber.org/multierr"
)

const ModuleName = "peerstore_provider"
const PeerstoreProviderSubmoduleName = "peerstore_provider"

// PeerstoreProvider is an interface that provides Peerstore accessors
type PeerstoreProvider interface {
modules.IntegratableModule
modules.Submodule

// GetStakedPeerstoreAtHeight returns a peerstore containing all staked peers
// at a given height. These peers communicate via the p2p module's staked actor
Expand Down
8 changes: 4 additions & 4 deletions p2p/providers/peerstore_provider/persistence/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ var (
type persistencePStoreProviderOption func(*persistencePeerstoreProvider)
type persistencePStoreProviderFactory = modules.FactoryWithOptions[peerstore_provider.PeerstoreProvider, persistencePStoreProviderOption]

// TECHDEBT(#810): refactor to implement `Submodule` interface.
type persistencePeerstoreProvider struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
}

func Create(bus modules.Bus, options ...persistencePStoreProviderOption) (peerstore_provider.PeerstoreProvider, error) {
Expand All @@ -26,8 +25,9 @@ func Create(bus modules.Bus, options ...persistencePStoreProviderOption) (peerst

func (*persistencePeerstoreProvider) Create(bus modules.Bus, options ...persistencePStoreProviderOption) (peerstore_provider.PeerstoreProvider, error) {
persistencePSP := &persistencePeerstoreProvider{
IntegratableModule: *base_modules.NewIntegratableModule(bus),
IntegrableModule: *base_modules.NewIntegrableModule(bus),
}
bus.RegisterModule(persistencePSP)

for _, o := range options {
o(persistencePSP)
Expand All @@ -37,7 +37,7 @@ func (*persistencePeerstoreProvider) Create(bus modules.Bus, options ...persiste
}

func (*persistencePeerstoreProvider) GetModuleName() string {
return peerstore_provider.ModuleName
return peerstore_provider.PeerstoreProviderSubmoduleName
}

// GetStakedPeerstoreAtHeight implements the respective `PeerstoreProvider` interface method.
Expand Down
56 changes: 28 additions & 28 deletions p2p/providers/peerstore_provider/rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,53 @@ import (
"github.com/pokt-network/pocket/p2p/providers/peerstore_provider"
typesP2P "github.com/pokt-network/pocket/p2p/types"
"github.com/pokt-network/pocket/rpc"
"github.com/pokt-network/pocket/runtime/configs"
"github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/modules/base_modules"
)

var _ peerstore_provider.PeerstoreProvider = &rpcPeerstoreProvider{}
var (
_ peerstore_provider.PeerstoreProvider = &rpcPeerstoreProvider{}
_ rpcPeerstoreProviderFactory = &rpcPeerstoreProvider{}
)

type rpcPeerstoreProviderOption func(*rpcPeerstoreProvider)
type rpcPeerstoreProviderFactory = modules.FactoryWithOptions[peerstore_provider.PeerstoreProvider, rpcPeerstoreProviderOption]

// TECHDEBT(#810): refactor to implement `Submodule` interface.
type rpcPeerstoreProvider struct {
// TECHDEBT(#810): simplify once submodules are more convenient to retrieve.
base_modules.IntegratableModule
base_modules.InterruptableModule
base_modules.IntegrableModule

rpcURL string
p2pCfg *configs.P2PConfig
rpcClient *rpc.ClientWithResponses
}

func Create(options ...modules.ModuleOption) *rpcPeerstoreProvider {
rabp := &rpcPeerstoreProvider{
func Create(
bus modules.Bus,
options ...rpcPeerstoreProviderOption,
) (peerstore_provider.PeerstoreProvider, error) {
return new(rpcPeerstoreProvider).Create(bus, options...)
}

func (*rpcPeerstoreProvider) Create(
bus modules.Bus,
options ...rpcPeerstoreProviderOption,
) (peerstore_provider.PeerstoreProvider, error) {
rpcPSP := &rpcPeerstoreProvider{
rpcURL: flags.RemoteCLIURL,
}
bus.RegisterModule(rpcPSP)

for _, o := range options {
o(rabp)
o(rpcPSP)
}

rabp.initRPCClient()
rpcPSP.initRPCClient()

return rabp
}

// TECHDEBT(#810): refactor to implement `Submodule` interface.
func (*rpcPeerstoreProvider) Create(bus modules.Bus, options ...modules.ModuleOption) (modules.Module, error) {
return Create(options...), nil
return rpcPSP, nil
}

func (*rpcPeerstoreProvider) GetModuleName() string {
return peerstore_provider.ModuleName
return peerstore_provider.PeerstoreProviderSubmoduleName
}

func (rpcPSP *rpcPeerstoreProvider) GetStakedPeerstoreAtHeight(height uint64) (typesP2P.Peerstore, error) {
Expand Down Expand Up @@ -98,16 +105,9 @@ func (rpcPSP *rpcPeerstoreProvider) initRPCClient() {

// options

// WithP2PConfig allows to specify a custom P2P config
func WithP2PConfig(p2pCfg *configs.P2PConfig) modules.ModuleOption {
return func(rabp modules.InitializableModule) {
rabp.(*rpcPeerstoreProvider).p2pCfg = p2pCfg
}
}

// WithCustomRPCURL allows to specify a custom RPC URL
func WithCustomRPCURL(rpcURL string) modules.ModuleOption {
return func(rabp modules.InitializableModule) {
rabp.(*rpcPeerstoreProvider).rpcURL = rpcURL
func WithCustomRPCURL(rpcURL string) rpcPeerstoreProviderOption {
return func(rpcPSP *rpcPeerstoreProvider) {
rpcPSP.rpcURL = rpcURL
}
}
10 changes: 5 additions & 5 deletions p2p/raintree/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

libp2pHost "github.com/libp2p/go-libp2p/core/host"
"github.com/pokt-network/pocket/p2p/unicast"
"google.golang.org/protobuf/proto"

"github.com/pokt-network/pocket/logger"
Expand All @@ -13,6 +12,7 @@ import (
"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"
"github.com/pokt-network/pocket/shared/codec"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
Expand All @@ -23,15 +23,15 @@ import (
)

var (
_ typesP2P.Router = &rainTreeRouter{}
_ modules.IntegratableModule = &rainTreeRouter{}
_ rainTreeFactory = &rainTreeRouter{}
_ typesP2P.Router = &rainTreeRouter{}
_ modules.IntegrableModule = &rainTreeRouter{}
_ rainTreeFactory = &rainTreeRouter{}
)

type rainTreeFactory = modules.FactoryWithConfig[typesP2P.Router, *config.RainTreeConfig]

type rainTreeRouter struct {
base_modules.IntegratableModule
base_modules.IntegrableModule
unicast.UnicastRouter

logger *modules.Logger
Expand Down
Loading

0 comments on commit c7412b6

Please sign in to comment.