From 3f0dd9125a7e20bfe0895cd2be795c3d5ae6b45c Mon Sep 17 00:00:00 2001 From: Bryan White Date: Wed, 12 Jul 2023 09:52:08 +0200 Subject: [PATCH] refactor: P2P module gets `PeerstoreProvider` from the bus --- p2p/module.go | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/p2p/module.go b/p2p/module.go index de50320cb..2729d9a06 100644 --- a/p2p/module.go +++ b/p2p/module.go @@ -46,12 +46,8 @@ type p2pModule struct { identity libp2p.Option listenAddrs libp2p.Option - // TECHDEBT(#810): register the providers to the module registry instead of - // holding a reference in the module struct and passing via router config. - // // Assigned during creation via `#setupDependencies()`. - pstoreProvider providers.PeerstoreProvider - nonceDeduper *mempool.GenericFIFOSet[uint64, uint64] + nonceDeduper *mempool.GenericFIFOSet[uint64, uint64] // TECHDEBT(#810): register the routers to the module registry instead of // holding a reference in the module struct. This will improve testability. @@ -300,28 +296,20 @@ func (m *p2pModule) setupPeerstoreProvider() error { GetModulesRegistry(). GetModule(peerstore_provider.PeerstoreProviderSubmoduleName) if err != nil { + // TECHDEBT: compare against `runtime.ErrModuleNotRegistered(...)`. m.logger.Debug().Msg("creating new persistence peerstore...") - pstoreProvider, err := persPSP.Create(m.GetBus()) + // Ensure a peerstore provider exists by creating a `persistencePeerstoreProvider`. + _, err := persPSP.Create(m.GetBus()) if err != nil { return err } - - // TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider - // is retrievable as a proper submodule - m.pstoreProvider = pstoreProvider return nil } - m.logger.Debug().Msg("loaded persistence peerstore...") - pstoreProvider, ok := pstoreProviderModule.(providers.PeerstoreProvider) - if !ok { + m.logger.Debug().Msg("loaded peerstore provider...") + if _, ok := pstoreProviderModule.(providers.PeerstoreProvider); !ok { return fmt.Errorf("unknown peerstore provider type: %T", pstoreProviderModule) } - - // TECHDEBT(#810): register the provider to the module registry instead of - // holding a reference in the module struct and passing via router config. - m.pstoreProvider = pstoreProvider - return nil } @@ -521,11 +509,31 @@ func (m *p2pModule) getMultiaddr() (multiaddr.Multiaddr, error) { } func (m *p2pModule) getStakedPeerstore() (typesP2P.Peerstore, error) { - return m.pstoreProvider.GetStakedPeerstoreAtHeight( - m.currentHeightProvider.CurrentHeight(), + pstoreProvider, err := m.getPeerstoreProvider() + if err != nil { + return nil, err + } + + return pstoreProvider.GetStakedPeerstoreAtHeight( + m.GetBus().GetCurrentHeightProvider().CurrentHeight(), ) } +// TECHDEBT(#810, #811): replace with `bus.GetPeerstoreProvider()` once available. +func (m *p2pModule) getPeerstoreProvider() (peerstore_provider.PeerstoreProvider, error) { + pstoreProviderModule, err := m.GetBus(). + GetModulesRegistry(). + GetModule(peerstore_provider.PeerstoreProviderSubmoduleName) + if err != nil { + return nil, err + } + pstoreProvider, ok := pstoreProviderModule.(peerstore_provider.PeerstoreProvider) + if !ok { + return nil, fmt.Errorf("peerstore provider not available") + } + return pstoreProvider, nil +} + // isStakedActor returns whether the current node is a staked actor at the current height. // Return an error if a peerstore can't be provided. func (m *p2pModule) isStakedActor() (bool, error) {