From 8a531e48ace6206862c07b14461891e5c411374b Mon Sep 17 00:00:00 2001 From: Bryan White Date: Mon, 5 Jun 2023 10:00:42 +0200 Subject: [PATCH] chore: implement `PeerstoreProvider#GetUnstakedPeerstore()` --- .../persistence/provider.go | 15 +++++++- .../peerstore_provider/rpc/provider.go | 13 ++++++- p2p/providers/peerstore_provider/unstaked.go | 34 ------------------- 3 files changed, 26 insertions(+), 36 deletions(-) delete mode 100644 p2p/providers/peerstore_provider/unstaked.go diff --git a/p2p/providers/peerstore_provider/persistence/provider.go b/p2p/providers/peerstore_provider/persistence/provider.go index cbd1ec82c..300c49a38 100644 --- a/p2p/providers/peerstore_provider/persistence/provider.go +++ b/p2p/providers/peerstore_provider/persistence/provider.go @@ -1,6 +1,8 @@ package persistence import ( + "fmt" + "github.com/pokt-network/pocket/p2p/providers/peerstore_provider" typesP2P "github.com/pokt-network/pocket/p2p/types" "github.com/pokt-network/pocket/shared/modules" @@ -58,5 +60,16 @@ func (persistencePSP *persistencePeerstoreProvider) GetStakedPeerstoreAtHeight(h // GetStakedPeerstoreAtHeight implements the respective `PeerstoreProvider` interface method. func (persistencePSP *persistencePeerstoreProvider) GetUnstakedPeerstore() (typesP2P.Peerstore, error) { - return peerstore_provider.GetUnstakedPeerstore(persistencePSP.GetBus()) + // TECHDEBT(#810, #811): use `bus.GetUnstakedActorRouter()` once it's available. + unstakedActorRouterMod, err := persistencePSP.GetBus().GetModulesRegistry().GetModule(typesP2P.UnstakedActorRouterSubmoduleName) + if err != nil { + return nil, err + } + + unstakedActorRouter, ok := unstakedActorRouterMod.(typesP2P.Router) + if !ok { + return nil, fmt.Errorf("unexpected unstaked actor router submodule type: %T", unstakedActorRouterMod) + } + + return unstakedActorRouter.GetPeerstore(), nil } diff --git a/p2p/providers/peerstore_provider/rpc/provider.go b/p2p/providers/peerstore_provider/rpc/provider.go index b0da88f69..c36963604 100644 --- a/p2p/providers/peerstore_provider/rpc/provider.go +++ b/p2p/providers/peerstore_provider/rpc/provider.go @@ -92,7 +92,18 @@ func (rpcPSP *rpcPeerstoreProvider) GetStakedPeerstoreAtHeight(height uint64) (t } func (rpcPSP *rpcPeerstoreProvider) GetUnstakedPeerstore() (typesP2P.Peerstore, error) { - return peerstore_provider.GetUnstakedPeerstore(rpcPSP.GetBus()) + // TECHDEBT(#810, #811): use `bus.GetUnstakedActorRouter()` once it's available. + unstakedActorRouterMod, err := rpcPSP.GetBus().GetModulesRegistry().GetModule(typesP2P.UnstakedActorRouterSubmoduleName) + if err != nil { + return nil, err + } + + unstakedActorRouter, ok := unstakedActorRouterMod.(typesP2P.Router) + if !ok { + return nil, fmt.Errorf("unexpected unstaked actor router submodule type: %T", unstakedActorRouterMod) + } + + return unstakedActorRouter.GetPeerstore(), nil } func (rpcPSP *rpcPeerstoreProvider) initRPCClient() { diff --git a/p2p/providers/peerstore_provider/unstaked.go b/p2p/providers/peerstore_provider/unstaked.go deleted file mode 100644 index b107922fe..000000000 --- a/p2p/providers/peerstore_provider/unstaked.go +++ /dev/null @@ -1,34 +0,0 @@ -package peerstore_provider - -import ( - "fmt" - - typesP2P "github.com/pokt-network/pocket/p2p/types" - "github.com/pokt-network/pocket/shared/modules" -) - -// unstakedPeerstoreProvider is an interface which the p2p module supports in -// order to allow access to the unstaked-actor-router's peerstore. -// -// NB: this peerstore includes all actors which participate in P2P (e.g. full -// and light clients but also validators, servicers, etc.). -// -// TECHDEBT(#811): will become unnecessary after `modules.P2PModule#GetUnstakedPeerstore` is added.` -// CONSIDERATION: split `PeerstoreProvider` into `StakedPeerstoreProvider` and `UnstakedPeerstoreProvider`. -// (see: https://github.com/pokt-network/pocket/pull/804#issuecomment-1576531916) -type unstakedPeerstoreProvider interface { - GetUnstakedPeerstore() (typesP2P.Peerstore, error) -} - -func GetUnstakedPeerstore(bus modules.Bus) (typesP2P.Peerstore, error) { - p2pModule := bus.GetP2PModule() - if p2pModule == nil { - return nil, fmt.Errorf("p2p module is not registered to bus and is required") - } - - unstakedPSP, ok := p2pModule.(unstakedPeerstoreProvider) - if !ok { - return nil, fmt.Errorf("p2p module does not implement unstakedPeerstoreProvider") - } - return unstakedPSP.GetUnstakedPeerstore() -}