refactoring: more refactoring to break import cycles

This commit is contained in:
Derrick Hammer 2024-01-29 20:52:17 -05:00
parent b2c06590b1
commit 3b3a50e419
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
7 changed files with 90 additions and 9 deletions

View File

@ -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
}

View File

@ -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

View File

@ -19,8 +19,17 @@ const registryBucketName = "registry"
var (
_ 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

View File

@ -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

View File

@ -21,8 +21,20 @@ const cacheBucketName = "object-cache"
var (
_ 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,

View File

@ -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

View File

@ -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
}