From 3b3a50e419731d119e6158bb2634b86906292c0b Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 29 Jan 2024 20:52:17 -0500 Subject: [PATCH] refactoring: more refactoring to break import cycles --- service/http.go | 4 ++++ service/p2p.go | 23 +++++++++++++++++++++++ service/registry.go | 11 ++++++++++- service/service.go | 9 +++++---- service/storage.go | 16 ++++++++++++++-- storage/provider/provider.go | 4 ++-- storage/storage.go | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 9 deletions(-) diff --git a/service/http.go b/service/http.go index bc154fb..d84be17 100644 --- a/service/http.go +++ b/service/http.go @@ -21,6 +21,10 @@ type P2PNodeResponse struct { Uris []string `json:"uris"` } +type HTTPServiceInterface interface { + GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router +} + type HTTPService struct { ServiceBase } diff --git a/service/p2p.go b/service/p2p.go index 0ebd879..2125e37 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -11,6 +11,7 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol/base" "git.lumeweb.com/LumeWeb/libs5-go/protocol/signed" + "git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/structs" "git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/utils" @@ -24,6 +25,8 @@ import ( ) var _ Service = (*P2PService)(nil) +var _ storage.StorageLocationProviderP2PService = (*P2PService)(nil) +var _ P2PServiceInterface = (*P2PService)(nil) var ( errUnsupportedProtocol = errors.New("unsupported protocol") @@ -32,6 +35,26 @@ var ( const nodeBucketName = "nodes" +type P2PServiceInterface interface { + SelfConnectionUris() []*url.URL + Peers() structs.Map + ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error + OnNewPeer(peer net.Peer, verifyId bool) error + GetNodeScore(nodeId *encoding.NodeId) (float64, error) + SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error) + SignMessageSimple(message []byte) ([]byte, error) + AddPeer(peer net.Peer) error + SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) error + SendHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) error + UpVote(nodeId *encoding.NodeId) error + DownVote(nodeId *encoding.NodeId) error + NodeId() *encoding.NodeId + WaitOnConnectedPeers() + ConnectionTracker() *sync.WaitGroup + NetworkId() string + HashQueryRoutingTable() structs.Map +} + type P2PService struct { nodeKeyPair *ed25519.KeyPairEd25519 localNodeID *encoding.NodeId diff --git a/service/registry.go b/service/registry.go index 4d51c82..f9cc8ea 100644 --- a/service/registry.go +++ b/service/registry.go @@ -18,9 +18,18 @@ import ( const registryBucketName = "registry" var ( - _ Service = (*RegistryService)(nil) + _ Service = (*RegistryService)(nil) + _ RegistryServiceInterface = (*RegistryService)(nil) ) +type RegistryServiceInterface interface { + Set(sre protocol.SignedRegistryEntry, trusted bool, receivedFrom net.Peer) error + BroadcastEntry(sre protocol.SignedRegistryEntry, receivedFrom net.Peer) error + SendRegistryRequest(pk []byte) error + Get(pk []byte) (protocol.SignedRegistryEntry, error) + Listen(pk []byte, cb func(sre protocol.SignedRegistryEntry)) (func(), error) +} + type RegistryService struct { streams structs.Map subs structs.Map diff --git a/service/service.go b/service/service.go index 6ce301d..2d6e86d 100644 --- a/service/service.go +++ b/service/service.go @@ -13,15 +13,16 @@ type Service interface { SetServices(services Services) } type Services interface { - P2P() *P2PService - Registry() *RegistryService - HTTP() *HTTPService - Storage() *StorageService + P2P() P2PServiceInterface + Registry() RegistryServiceInterface + HTTP() HTTPServiceInterface + Storage() StorageServiceInterface All() []Service IsStarted() bool Start() error Stop() error } + type ServiceParams struct { Logger *zap.Logger Config *config.NodeConfig diff --git a/service/storage.go b/service/storage.go index 49d44c7..48df0c3 100644 --- a/service/storage.go +++ b/service/storage.go @@ -20,9 +20,21 @@ import ( const cacheBucketName = "object-cache" var ( - _ Service = (*StorageService)(nil) + _ Service = (*StorageService)(nil) + _ storage.StorageLocationProviderStorageService = (*StorageService)(nil) + _ StorageServiceInterface = (*StorageService)(nil) ) +type StorageServiceInterface interface { + SetProviderStore(store storage.ProviderStore) + ProviderStore() storage.ProviderStore + GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]storage.StorageLocation, error) + AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location storage.StorageLocation, message []byte) error + DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error) + DownloadBytesByCID(cid *encoding.CID) ([]byte, error) + GetMetadataByCID(cid *encoding.CID) (metadata.Metadata, error) +} + type StorageService struct { httpClient *resty.Client metadataCache structs.Map @@ -198,7 +210,7 @@ func (s *StorageService) AddStorageLocation(hash *encoding.Multihash, nodeId *en func (s *StorageService) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error) { // Initialize the download URI provider dlUriProvider := provider.NewStorageLocationProvider(provider.StorageLocationProviderParams{ - Services: s.services, + Services: storage.NewStorageLocationProviderServices(s.services.P2P(), s.services.Storage()), Hash: hash, LocationTypes: []types.StorageLocationType{ types.StorageLocationTypeFull, diff --git a/storage/provider/provider.go b/storage/provider/provider.go index c39841e..e27e11c 100644 --- a/storage/provider/provider.go +++ b/storage/provider/provider.go @@ -15,7 +15,7 @@ import ( var _ storage.StorageLocationProvider = (*StorageLocationProviderImpl)(nil) type StorageLocationProviderImpl struct { - services service.Services + services storage.StorageLocationProviderServices hash *encoding.Multihash types []types.StorageLocationType timeoutDuration time.Duration @@ -192,7 +192,7 @@ func containsNode(slice []*encoding.NodeId, item *encoding.NodeId) bool { } type StorageLocationProviderParams struct { - Services service.Services + Services storage.StorageLocationProviderServices Hash *encoding.Multihash LocationTypes []types.StorageLocationType service.ServiceParams diff --git a/storage/storage.go b/storage/storage.go index 8c96454..0ddf8d8 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "git.lumeweb.com/LumeWeb/libs5-go/encoding" + "git.lumeweb.com/LumeWeb/libs5-go/types" "github.com/vmihailenco/msgpack/v5" "strconv" "time" @@ -127,3 +128,34 @@ type StorageLocationProvider interface { Upvote(uri SignedStorageLocation) error Downvote(uri SignedStorageLocation) error } + +type StorageLocationProviderServices interface { + P2P() StorageLocationProviderP2PService + Storage() StorageLocationProviderStorageService +} +type StorageLocationProviderP2PService interface { + SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error) + SendHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) error + UpVote(nodeId *encoding.NodeId) error + DownVote(nodeId *encoding.NodeId) error +} +type StorageLocationProviderStorageService interface { + GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]StorageLocation, error) +} + +type StorageLocationProviderServicesImpl struct { + p2p StorageLocationProviderP2PService + storage StorageLocationProviderStorageService +} + +func NewStorageLocationProviderServices(p2p StorageLocationProviderP2PService, storage StorageLocationProviderStorageService) *StorageLocationProviderServicesImpl { + return &StorageLocationProviderServicesImpl{p2p: p2p, storage: storage} +} + +func (s *StorageLocationProviderServicesImpl) P2P() StorageLocationProviderP2PService { + return s.p2p +} + +func (s *StorageLocationProviderServicesImpl) Storage() StorageLocationProviderStorageService { + return s.storage +}