refactor: remove dedicated interfaces and minimize interfaces

This commit is contained in:
Derrick Hammer 2024-01-28 23:59:43 -05:00
parent 31ccfb8c0b
commit a0dcc52d63
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
20 changed files with 206 additions and 323 deletions

View File

@ -1,13 +0,0 @@
package interfaces
import (
"github.com/julienschmidt/httprouter"
"go.sia.tech/jape"
)
//go:generate mockgen -source=http.go -destination=../mocks/interfaces/http.go -package=interfaces
type HTTPService interface {
Service
GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router
}

View File

@ -1,7 +0,0 @@
package interfaces
//go:generate mockgen -source=meta.go -destination=../mocks/interfaces/meta.go -package=interfaces
type Metadata interface {
ToJson() map[string]interface{}
}

View File

@ -1,34 +0,0 @@
package interfaces
import (
"git.lumeweb.com/LumeWeb/libs5-go/config"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/metadata"
"git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/types"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
"sync"
)
//go:generate mockgen -source=node.go -destination=../mocks/interfaces/node.go -package=interfaces
type Node interface {
Services() Services
HashQueryRoutingTable() structs.Map
IsStarted() bool
Config() *config.NodeConfig
Logger() *zap.Logger
Db() *bolt.DB
Start() error
GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]StorageLocation, error)
AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location StorageLocation, message []byte) error
NetworkId() string
DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error)
DownloadBytesByCID(cid *encoding.CID) (bytes []byte, err error)
GetMetadataByCID(cid *encoding.CID) (metadata.Metadata, error)
WaitOnConnectedPeers()
ConnectionTracker() *sync.WaitGroup
SetProviderStore(store ProviderStore)
ProviderStore() ProviderStore
}

View File

@ -1,30 +0,0 @@
package interfaces
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"net/url"
)
//go:generate mockgen -source=p2p.go -destination=../mocks/interfaces/p2p.go -package=interfaces
type P2PService interface {
Peers() structs.Map
ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error
OnNewPeer(peer net.Peer, verifyId bool) error
OnNewPeerListen(peer net.Peer, verifyId bool)
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
SelfConnectionUris() []*url.URL
PrepareProvideMessage(hash *encoding.Multihash, location StorageLocation) []byte
Service
}

View File

@ -1,11 +0,0 @@
package interfaces
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/types"
)
type ProviderStore interface {
CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool
Provide(hash *encoding.Multihash, kind []types.StorageLocationType) (StorageLocation, error)
}

View File

@ -1,30 +0,0 @@
package interfaces
import "git.lumeweb.com/LumeWeb/libs5-go/net"
//go:generate mockgen -source=registry.go -destination=../mocks/interfaces/registry.go -package=interfaces
type SignedRegistryEntry interface {
PK() []byte
Revision() uint64
Data() []byte
Signature() []byte
SetPK(pk []byte)
SetRevision(revision uint64)
SetData(data []byte)
SetSignature(signature []byte)
Verify() bool
}
type RegistryEntry interface {
Sign() SignedRegistryEntry
}
type RegistryService interface {
Set(sre SignedRegistryEntry, trusted bool, receivedFrom net.Peer) error
Get(pk []byte) (SignedRegistryEntry, error)
BroadcastEntry(sre SignedRegistryEntry, receivedFrom net.Peer) error
SendRegistryRequest(pk []byte) error
Listen(pk []byte, cb func(sre SignedRegistryEntry)) (func(), error)
Service
}

View File

@ -1,16 +0,0 @@
package interfaces
//go:generate mockgen -source=service.go -destination=../mocks/interfaces/service.go -package=interfaces
type Service interface {
Node() Node
Start() error
Stop() error
Init() error
}
type Services interface {
P2P() P2PService
Registry() RegistryService
HTTP() HTTPService
All() []Service
}

View File

@ -1,33 +0,0 @@
package interfaces
import "git.lumeweb.com/LumeWeb/libs5-go/encoding"
//go:generate mockgen -source=storage.go -destination=../mocks/interfaces/storage.go -package=interfaces
type StorageLocationProvider interface {
Start() error
Next() (SignedStorageLocation, error)
Upvote(uri SignedStorageLocation) error
Downvote(uri SignedStorageLocation) error
}
type StorageLocation interface {
BytesURL() string
OutboardBytesURL() string
String() string
ProviderMessage() []byte
Type() int
Parts() []string
BinaryParts() [][]byte
Expiry() int64
SetProviderMessage(msg []byte)
SetType(t int)
SetParts(p []string)
SetBinaryParts(bp [][]byte)
SetExpiry(e int64)
}
type SignedStorageLocation interface {
String() string
NodeId() *encoding.NodeId
Location() StorageLocation
}

View File

@ -1,14 +0,0 @@
package interfaces
import "github.com/vmihailenco/msgpack/v5"
//go:generate mockgen -source=vote.go -destination=../mocks/interfaces/vote.go -package=interfaces
type NodeVotes interface {
msgpack.CustomEncoder
msgpack.CustomDecoder
Good() int
Bad() int
Upvote()
Downvote()
}

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"git.lumeweb.com/LumeWeb/libs5-go/config" "git.lumeweb.com/LumeWeb/libs5-go/config"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/metadata" "git.lumeweb.com/LumeWeb/libs5-go/metadata"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed" "git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
@ -22,31 +21,29 @@ import (
"time" "time"
) )
var _ interfaces.Node = (*NodeImpl)(nil)
const cacheBucketName = "object-cache" const cacheBucketName = "object-cache"
type NodeImpl struct { type Node struct {
nodeConfig *config.NodeConfig nodeConfig *config.NodeConfig
metadataCache structs.Map metadataCache structs.Map
started bool started bool
hashQueryRoutingTable structs.Map hashQueryRoutingTable structs.Map
services interfaces.Services services service.Services
httpClient *resty.Client httpClient *resty.Client
connections sync.WaitGroup connections sync.WaitGroup
providerStore interfaces.ProviderStore providerStore storage.ProviderStore
} }
func (n *NodeImpl) NetworkId() string { func (n *Node) NetworkId() string {
return n.nodeConfig.P2P.Network return n.nodeConfig.P2P.Network
} }
func (n *NodeImpl) Services() interfaces.Services { func (n *Node) Services() service.Services {
return n.services return n.services
} }
func NewNode(config *config.NodeConfig) interfaces.Node { func NewNode(config *config.NodeConfig) *Node {
n := &NodeImpl{ n := &Node{
nodeConfig: config, nodeConfig: config,
metadataCache: structs.NewMap(), metadataCache: structs.NewMap(),
started: false, started: false,
@ -57,33 +54,33 @@ func NewNode(config *config.NodeConfig) interfaces.Node {
return n return n
} }
func (n *NodeImpl) HashQueryRoutingTable() structs.Map { func (n *Node) HashQueryRoutingTable() structs.Map {
return n.hashQueryRoutingTable return n.hashQueryRoutingTable
} }
func (n *NodeImpl) IsStarted() bool { func (n *Node) IsStarted() bool {
return n.started return n.started
} }
func (n *NodeImpl) Config() *config.NodeConfig { func (n *Node) Config() *config.NodeConfig {
return n.nodeConfig return n.nodeConfig
} }
func (n *NodeImpl) Logger() *zap.Logger { func (n *Node) Logger() *zap.Logger {
if n.nodeConfig != nil { if n.nodeConfig != nil {
return n.nodeConfig.Logger return n.nodeConfig.Logger
} }
return nil return nil
} }
func (n *NodeImpl) Db() *bolt.DB { func (n *Node) Db() *bolt.DB {
if n.nodeConfig != nil { if n.nodeConfig != nil {
return n.nodeConfig.DB return n.nodeConfig.DB
} }
return nil return nil
} }
func (n *NodeImpl) Start() error { func (n *Node) Start() error {
protocol.Init() protocol.Init()
signed.Init() signed.Init()
err := err :=
@ -109,15 +106,15 @@ func (n *NodeImpl) Start() error {
n.started = true n.started = true
return nil return nil
} }
func (n *NodeImpl) GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]interfaces.StorageLocation, error) { func (n *Node) GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]storage.StorageLocation, error) {
locations := make(map[string]interfaces.StorageLocation) locations := make(map[string]storage.StorageLocation)
locationMap, err := n.readStorageLocationsFromDB(hash) locationMap, err := n.readStorageLocationsFromDB(hash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(locationMap) == 0 { if len(locationMap) == 0 {
return make(map[string]interfaces.StorageLocation), nil return make(map[string]storage.StorageLocation), nil
} }
ts := time.Now().Unix() ts := time.Now().Unix()
@ -163,7 +160,7 @@ func (n *NodeImpl) GetCachedStorageLocations(hash *encoding.Multihash, kinds []t
} }
return locations, nil return locations, nil
} }
func (n *NodeImpl) readStorageLocationsFromDB(hash *encoding.Multihash) (storage.StorageLocationMap, error) { func (n *Node) readStorageLocationsFromDB(hash *encoding.Multihash) (storage.StorageLocationMap, error) {
var locationMap storage.StorageLocationMap var locationMap storage.StorageLocationMap
err := n.Db().View(func(tx *bolt.Tx) error { err := n.Db().View(func(tx *bolt.Tx) error {
@ -189,7 +186,7 @@ func (n *NodeImpl) readStorageLocationsFromDB(hash *encoding.Multihash) (storage
return locationMap, nil return locationMap, nil
} }
func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location interfaces.StorageLocation, message []byte) error { func (n *Node) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location storage.StorageLocation, message []byte) error {
// Read existing storage locations // Read existing storage locations
locationDb, err := n.readStorageLocationsFromDB(hash) locationDb, err := n.readStorageLocationsFromDB(hash)
if err != nil { if err != nil {
@ -235,7 +232,7 @@ func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding
return nil return nil
} }
func (n *NodeImpl) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error) { func (n *Node) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error) {
// Initialize the download URI provider // Initialize the download URI provider
dlUriProvider := storage.NewStorageLocationProvider(n, hash, types.StorageLocationTypeFull, types.StorageLocationTypeFile) dlUriProvider := storage.NewStorageLocationProvider(n, hash, types.StorageLocationTypeFull, types.StorageLocationTypeFile)
err := dlUriProvider.Start() err := dlUriProvider.Start()
@ -271,7 +268,7 @@ func (n *NodeImpl) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error)
} }
} }
func (n *NodeImpl) DownloadBytesByCID(cid *encoding.CID) (bytes []byte, err error) { func (n *Node) DownloadBytesByCID(cid *encoding.CID) (bytes []byte, err error) {
bytes, err = n.DownloadBytesByHash(&cid.Hash) bytes, err = n.DownloadBytesByHash(&cid.Hash)
if err != nil { if err != nil {
return nil, err return nil, err
@ -280,7 +277,7 @@ func (n *NodeImpl) DownloadBytesByCID(cid *encoding.CID) (bytes []byte, err erro
return bytes, nil return bytes, nil
} }
func (n *NodeImpl) GetMetadataByCID(cid *encoding.CID) (md metadata.Metadata, err error) { func (n *Node) GetMetadataByCID(cid *encoding.CID) (md metadata.Metadata, err error) {
hashStr, err := cid.Hash.ToString() hashStr, err := cid.Hash.ToString()
if err != nil { if err != nil {
return nil, err return nil, err
@ -327,18 +324,18 @@ func (n *NodeImpl) GetMetadataByCID(cid *encoding.CID) (md metadata.Metadata, er
return md, nil return md, nil
} }
func (n *NodeImpl) WaitOnConnectedPeers() { func (n *Node) WaitOnConnectedPeers() {
n.connections.Wait() n.connections.Wait()
} }
func (n *NodeImpl) ConnectionTracker() *sync.WaitGroup { func (n *Node) ConnectionTracker() *sync.WaitGroup {
return &n.connections return &n.connections
} }
func (n *NodeImpl) SetProviderStore(store interfaces.ProviderStore) { func (n *Node) SetProviderStore(store storage.ProviderStore) {
n.providerStore = store n.providerStore = store
} }
func (n *NodeImpl) ProviderStore() interfaces.ProviderStore { func (n *Node) ProviderStore() storage.ProviderStore {
return n.providerStore return n.providerStore
} }

View File

@ -1,23 +1,25 @@
package node package node
import "git.lumeweb.com/LumeWeb/libs5-go/interfaces" import (
"git.lumeweb.com/LumeWeb/libs5-go/service"
)
var ( var (
_ interfaces.Services = (*ServicesImpl)(nil) _ service.Services = (*ServicesImpl)(nil)
) )
type ServicesImpl struct { type ServicesImpl struct {
p2p interfaces.P2PService p2p *service.P2PService
registry interfaces.RegistryService registry *service.RegistryService
http interfaces.HTTPService http *service.HTTPService
} }
func (s *ServicesImpl) HTTP() interfaces.HTTPService { func (s *ServicesImpl) HTTP() *service.HTTPService {
return s.http return s.http
} }
func (s *ServicesImpl) All() []interfaces.Service { func (s *ServicesImpl) All() []service.Service {
services := make([]interfaces.Service, 0) services := make([]service.Service, 0)
services = append(services, s.p2p) services = append(services, s.p2p)
services = append(services, s.registry) services = append(services, s.registry)
services = append(services, s.http) services = append(services, s.http)
@ -25,11 +27,11 @@ func (s *ServicesImpl) All() []interfaces.Service {
return services return services
} }
func (s *ServicesImpl) Registry() interfaces.RegistryService { func (s *ServicesImpl) Registry() *service.RegistryService {
return s.registry return s.registry
} }
func NewServices(p2p interfaces.P2PService, registry interfaces.RegistryService, http interfaces.HTTPService) interfaces.Services { func NewServices(p2p *service.P2PService, registry *service.RegistryService, http *service.HTTPService) service.Services {
return &ServicesImpl{ return &ServicesImpl{
p2p: p2p, p2p: p2p,
registry: registry, registry: registry,
@ -37,6 +39,6 @@ func NewServices(p2p interfaces.P2PService, registry interfaces.RegistryService,
} }
} }
func (s *ServicesImpl) P2P() interfaces.P2PService { func (s *ServicesImpl) P2P() *service.P2PService {
return s.p2p return s.p2p
} }

View File

@ -24,7 +24,7 @@ type IncomingMessageData struct {
Original []byte Original []byte
Data []byte Data []byte
Ctx context.Context Ctx context.Context
Node *node.NodeImpl Node *node.Node
Peer net.Peer Peer net.Peer
VerifyId bool VerifyId bool
} }

View File

@ -4,16 +4,31 @@ import (
ed25519p "crypto/ed25519" ed25519p "crypto/ed25519"
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/ed25519" "git.lumeweb.com/LumeWeb/libs5-go/ed25519"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
) )
var ( var (
_ interfaces.SignedRegistryEntry = (*SignedRegistryEntryImpl)(nil) _ SignedRegistryEntry = (*SignedRegistryEntryImpl)(nil)
_ interfaces.SignedRegistryEntry = (*SignedRegistryEntryImpl)(nil) _ SignedRegistryEntry = (*SignedRegistryEntryImpl)(nil)
) )
type SignedRegistryEntry interface {
PK() []byte
Revision() uint64
Data() []byte
Signature() []byte
SetPK(pk []byte)
SetRevision(revision uint64)
SetData(data []byte)
SetSignature(signature []byte)
Verify() bool
}
type RegistryEntry interface {
Sign() SignedRegistryEntry
}
type SignedRegistryEntryImpl struct { type SignedRegistryEntryImpl struct {
pk []byte pk []byte
revision uint64 revision uint64
@ -57,7 +72,7 @@ func (s *SignedRegistryEntryImpl) SetSignature(signature []byte) {
s.signature = signature s.signature = signature
} }
func NewSignedRegistryEntry(pk []byte, revision uint64, data []byte, signature []byte) interfaces.SignedRegistryEntry { func NewSignedRegistryEntry(pk []byte, revision uint64, data []byte, signature []byte) SignedRegistryEntry {
return &SignedRegistryEntryImpl{ return &SignedRegistryEntryImpl{
pk: pk, pk: pk,
revision: revision, revision: revision,
@ -72,7 +87,7 @@ type RegistryEntryImpl struct {
revision uint64 revision uint64
} }
func NewRegistryEntry(kp ed25519.KeyPairEd25519, data []byte, revision uint64) interfaces.RegistryEntry { func NewRegistryEntry(kp ed25519.KeyPairEd25519, data []byte, revision uint64) RegistryEntry {
return &RegistryEntryImpl{ return &RegistryEntryImpl{
kp: kp, kp: kp,
data: data, data: data,
@ -80,11 +95,11 @@ func NewRegistryEntry(kp ed25519.KeyPairEd25519, data []byte, revision uint64) i
} }
} }
func (r *RegistryEntryImpl) Sign() interfaces.SignedRegistryEntry { func (r *RegistryEntryImpl) Sign() SignedRegistryEntry {
return SignRegistryEntry(r.kp, r.data, r.revision) return SignRegistryEntry(r.kp, r.data, r.revision)
} }
func SignRegistryEntry(kp ed25519.KeyPairEd25519, data []byte, revision uint64) interfaces.SignedRegistryEntry { func SignRegistryEntry(kp ed25519.KeyPairEd25519, data []byte, revision uint64) SignedRegistryEntry {
buffer := MarshalRegistryEntry(data, revision) buffer := MarshalRegistryEntry(data, revision)
privateKey := kp.ExtractBytes() privateKey := kp.ExtractBytes()
@ -92,14 +107,14 @@ func SignRegistryEntry(kp ed25519.KeyPairEd25519, data []byte, revision uint64)
return NewSignedRegistryEntry(kp.PublicKey(), uint64(revision), data, signature) return NewSignedRegistryEntry(kp.PublicKey(), uint64(revision), data, signature)
} }
func VerifyRegistryEntry(sre interfaces.SignedRegistryEntry) bool { func VerifyRegistryEntry(sre SignedRegistryEntry) bool {
buffer := MarshalRegistryEntry(sre.Data(), sre.Revision()) buffer := MarshalRegistryEntry(sre.Data(), sre.Revision())
publicKey := sre.PK()[1:] publicKey := sre.PK()[1:]
return ed25519p.Verify(publicKey, buffer, sre.Signature()) return ed25519p.Verify(publicKey, buffer, sre.Signature())
} }
func MarshalSignedRegistryEntry(sre interfaces.SignedRegistryEntry) []byte { func MarshalSignedRegistryEntry(sre SignedRegistryEntry) []byte {
buffer := MarshalRegistryEntry(sre.Data(), sre.Revision()) buffer := MarshalRegistryEntry(sre.Data(), sre.Revision())
buffer = append(buffer, sre.Signature()...) buffer = append(buffer, sre.Signature()...)
@ -118,7 +133,7 @@ func MarshalRegistryEntry(data []byte, revision uint64) []byte {
return buffer return buffer
} }
func UnmarshalSignedRegistryEntry(event []byte) (sre interfaces.SignedRegistryEntry, err error) { func UnmarshalSignedRegistryEntry(event []byte) (sre SignedRegistryEntry, err error) {
if len(event) < 43 { if len(event) < 43 {
return nil, errors.New("Invalid registry entry") return nil, errors.New("Invalid registry entry")
} }

View File

@ -1,7 +1,6 @@
package protocol package protocol
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base" "git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
@ -11,7 +10,7 @@ var _ base.IncomingMessage = (*RegistryEntryRequest)(nil)
var _ base.EncodeableMessage = (*RegistryEntryRequest)(nil) var _ base.EncodeableMessage = (*RegistryEntryRequest)(nil)
type RegistryEntryRequest struct { type RegistryEntryRequest struct {
sre interfaces.SignedRegistryEntry sre SignedRegistryEntry
base.HandshakeRequirement base.HandshakeRequirement
} }
@ -22,7 +21,7 @@ func NewEmptyRegistryEntryRequest() *RegistryEntryRequest {
return rer return rer
} }
func NewRegistryEntryRequest(sre interfaces.SignedRegistryEntry) *RegistryEntryRequest { func NewRegistryEntryRequest(sre SignedRegistryEntry) *RegistryEntryRequest {
return &RegistryEntryRequest{sre: sre} return &RegistryEntryRequest{sre: sre}
} }

View File

@ -2,8 +2,8 @@ package service
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go/build" "git.lumeweb.com/LumeWeb/libs5-go/build"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
_node "git.lumeweb.com/LumeWeb/libs5-go/node"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"go.sia.tech/jape" "go.sia.tech/jape"
"go.uber.org/zap" "go.uber.org/zap"
@ -11,7 +11,7 @@ import (
"nhooyr.io/websocket" "nhooyr.io/websocket"
) )
var _ interfaces.HTTPService = (*HTTPImpl)(nil) var _ Service = (*HTTPService)(nil)
type P2PNodesResponse struct { type P2PNodesResponse struct {
Nodes []P2PNodeResponse `json:"nodes"` Nodes []P2PNodeResponse `json:"nodes"`
@ -22,17 +22,17 @@ type P2PNodeResponse struct {
Uris []string `json:"uris"` Uris []string `json:"uris"`
} }
type HTTPImpl struct { type HTTPService struct {
node interfaces.Node node *_node.Node
} }
func NewHTTP(node interfaces.Node) interfaces.HTTPService { func NewHTTP(node *_node.Node) *HTTPService {
return &HTTPImpl{ return &HTTPService{
node: node, node: node,
} }
} }
func (h *HTTPImpl) GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router { func (h *HTTPService) GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router {
routes := map[string]jape.Handler{ routes := map[string]jape.Handler{
"GET /s5/version": h.versionHandler, "GET /s5/version": h.versionHandler,
"GET /s5/p2p": h.p2pHandler, "GET /s5/p2p": h.p2pHandler,
@ -46,26 +46,26 @@ func (h *HTTPImpl) GetHttpRouter(inject map[string]jape.Handler) *httprouter.Rou
return jape.Mux(routes) return jape.Mux(routes)
} }
func (h *HTTPImpl) Node() interfaces.Node { func (h *HTTPService) Node() *_node.Node {
return h.node return h.node
} }
func (h *HTTPImpl) Start() error { func (h *HTTPService) Start() error {
return nil return nil
} }
func (h *HTTPImpl) Stop() error { func (h *HTTPService) Stop() error {
return nil return nil
} }
func (h *HTTPImpl) Init() error { func (h *HTTPService) Init() error {
return nil return nil
} }
func (h *HTTPImpl) versionHandler(ctx jape.Context) { func (h *HTTPService) versionHandler(ctx jape.Context) {
_, _ = ctx.ResponseWriter.Write([]byte(build.Version)) _, _ = ctx.ResponseWriter.Write([]byte(build.Version))
} }
func (h *HTTPImpl) p2pHandler(ctx jape.Context) { func (h *HTTPService) p2pHandler(ctx jape.Context) {
c, err := websocket.Accept(ctx.ResponseWriter, ctx.Request, nil) c, err := websocket.Accept(ctx.ResponseWriter, ctx.Request, nil)
if err != nil { if err != nil {
h.node.Logger().Error("error accepting websocket connection", zap.Error(err)) h.node.Logger().Error("error accepting websocket connection", zap.Error(err))
@ -97,7 +97,7 @@ func (h *HTTPImpl) p2pHandler(ctx jape.Context) {
}() }()
} }
func (h *HTTPImpl) p2pNodesHandler(ctx jape.Context) { func (h *HTTPService) p2pNodesHandler(ctx jape.Context) {
localId, err := h.node.Services().P2P().NodeId().ToString() localId, err := h.node.Services().P2P().NodeId().ToString()
if ctx.Check("error getting local node id", err) != nil { if ctx.Check("error getting local node id", err) != nil {

View File

@ -8,12 +8,12 @@ import (
"fmt" "fmt"
"git.lumeweb.com/LumeWeb/libs5-go/ed25519" "git.lumeweb.com/LumeWeb/libs5-go/ed25519"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/node" _node "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base" "git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed" "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/structs"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
@ -26,8 +26,7 @@ import (
"time" "time"
) )
var _ interfaces.P2PService = (*P2PImpl)(nil) var _ Service = (*P2PService)(nil)
var _ interfaces.NodeVotes = (*NodeVotesImpl)(nil)
var ( var (
errUnsupportedProtocol = errors.New("unsupported protocol") errUnsupportedProtocol = errors.New("unsupported protocol")
@ -36,13 +35,13 @@ var (
const nodeBucketName = "nodes" const nodeBucketName = "nodes"
type P2PImpl struct { type P2PService struct {
logger *zap.Logger logger *zap.Logger
nodeKeyPair *ed25519.KeyPairEd25519 nodeKeyPair *ed25519.KeyPairEd25519
localNodeID *encoding.NodeId localNodeID *encoding.NodeId
networkID string networkID string
nodesBucket *bolt.Bucket nodesBucket *bolt.Bucket
node interfaces.Node node *_node.Node
inited bool inited bool
reconnectDelay structs.Map reconnectDelay structs.Map
peers structs.Map peers structs.Map
@ -55,13 +54,13 @@ type P2PImpl struct {
maxOutgoingPeerFailures uint maxOutgoingPeerFailures uint
} }
func NewP2P(node interfaces.Node) *P2PImpl { func NewP2P(node *_node.Node) *P2PService {
uri, err := url.Parse(fmt.Sprintf("wss://%s:%d/s5/p2p", node.Config().HTTP.API.Domain, node.Config().HTTP.API.Port)) uri, err := url.Parse(fmt.Sprintf("wss://%s:%d/s5/p2p", node.Config().HTTP.API.Domain, node.Config().HTTP.API.Port))
if err != nil { if err != nil {
node.Logger().Fatal("failed to HTTP API URL Config", zap.Error(err)) node.Logger().Fatal("failed to HTTP API URL Config", zap.Error(err))
} }
service := &P2PImpl{ service := &P2PService{
logger: node.Logger(), logger: node.Logger(),
nodeKeyPair: node.Config().KeyPair, nodeKeyPair: node.Config().KeyPair,
networkID: node.Config().P2P.Network, networkID: node.Config().P2P.Network,
@ -81,19 +80,19 @@ func NewP2P(node interfaces.Node) *P2PImpl {
return service return service
} }
func (p *P2PImpl) SelfConnectionUris() []*url.URL { func (p *P2PService) SelfConnectionUris() []*url.URL {
return p.selfConnectionUris return p.selfConnectionUris
} }
func (p *P2PImpl) Node() interfaces.Node { func (p *P2PService) Node() *_node.Node {
return p.node return p.node
} }
func (p *P2PImpl) Peers() structs.Map { func (p *P2PService) Peers() structs.Map {
return p.peers return p.peers
} }
func (p *P2PImpl) Start() error { func (p *P2PService) Start() error {
config := p.Node().Config() config := p.Node().Config()
if len(config.P2P.Peers.Initial) > 0 { if len(config.P2P.Peers.Initial) > 0 {
initialPeers := config.P2P.Peers.Initial initialPeers := config.P2P.Peers.Initial
@ -117,11 +116,11 @@ func (p *P2PImpl) Start() error {
return nil return nil
} }
func (p *P2PImpl) Stop() error { func (p *P2PService) Stop() error {
panic("implement me") panic("implement me")
} }
func (p *P2PImpl) Init() error { func (p *P2PService) Init() error {
if p.inited { if p.inited {
return nil return nil
} }
@ -137,7 +136,7 @@ func (p *P2PImpl) Init() error {
return nil return nil
} }
func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error { func (p *P2PService) ConnectToNode(connectionUris []*url.URL, retried bool, fromPeer net.Peer) error {
if !p.Node().IsStarted() { if !p.Node().IsStarted() {
return nil return nil
} }
@ -343,7 +342,7 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool, fromPee
} }
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error { func (p *P2PService) OnNewPeer(peer net.Peer, verifyId bool) error {
var wg sync.WaitGroup var wg sync.WaitGroup
var pid string var pid string
@ -400,7 +399,7 @@ func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
p.logger.Debug("OnNewPeer ended", zap.String("peer", pid)) p.logger.Debug("OnNewPeer ended", zap.String("peer", pid))
return nil return nil
} }
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) { func (p *P2PService) OnNewPeerListen(peer net.Peer, verifyId bool) {
onDone := net.CloseCallback(func() { onDone := net.CloseCallback(func() {
if peer.Id() != nil { if peer.Id() != nil {
pid, err := peer.Id().ToString() pid, err := peer.Id().ToString()
@ -444,7 +443,7 @@ func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
Original: message, Original: message,
Data: reader.Data, Data: reader.Data,
Ctx: context.Background(), Ctx: context.Background(),
Node: p.node.(*node.NodeImpl), Node: p.node,
Peer: peer, Peer: peer,
VerifyId: verifyId, VerifyId: verifyId,
} }
@ -471,7 +470,7 @@ func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
}) })
} }
func (p *P2PImpl) readNodeVotes(nodeId *encoding.NodeId) (interfaces.NodeVotes, error) { func (p *P2PService) readNodeVotes(nodeId *encoding.NodeId) (NodeVotes, error) {
var value []byte var value []byte
var found bool var found bool
err := p.node.Db().View(func(tx *bolt.Tx) error { err := p.node.Db().View(func(tx *bolt.Tx) error {
@ -493,7 +492,7 @@ func (p *P2PImpl) readNodeVotes(nodeId *encoding.NodeId) (interfaces.NodeVotes,
return NewNodeVotes(), nil return NewNodeVotes(), nil
} }
var score interfaces.NodeVotes var score NodeVotes
err = msgpack.Unmarshal(value, &score) err = msgpack.Unmarshal(value, &score)
if err != nil { if err != nil {
return nil, err return nil, err
@ -502,7 +501,7 @@ func (p *P2PImpl) readNodeVotes(nodeId *encoding.NodeId) (interfaces.NodeVotes,
return score, nil return score, nil
} }
func (p *P2PImpl) saveNodeVotes(nodeId *encoding.NodeId, votes interfaces.NodeVotes) error { func (p *P2PService) saveNodeVotes(nodeId *encoding.NodeId, votes NodeVotes) error {
// Marshal the votes into data // Marshal the votes into data
data, err := msgpack.Marshal(votes) data, err := msgpack.Marshal(votes)
if err != nil { if err != nil {
@ -521,7 +520,7 @@ func (p *P2PImpl) saveNodeVotes(nodeId *encoding.NodeId, votes interfaces.NodeVo
return err return err
} }
func (p *P2PImpl) GetNodeScore(nodeId *encoding.NodeId) (float64, error) { func (p *P2PService) GetNodeScore(nodeId *encoding.NodeId) (float64, error) {
if nodeId.Equals(p.localNodeID) { if nodeId.Equals(p.localNodeID) {
return 1, nil return 1, nil
} }
@ -534,7 +533,7 @@ func (p *P2PImpl) GetNodeScore(nodeId *encoding.NodeId) (float64, error) {
return protocol.CalculateNodeScore(score.Good(), score.Bad()), nil return protocol.CalculateNodeScore(score.Good(), score.Bad()), nil
} }
func (p *P2PImpl) SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error) { func (p *P2PService) SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error) {
scores := make(map[encoding.NodeIdCode]float64) scores := make(map[encoding.NodeIdCode]float64)
var errOccurred error var errOccurred error
@ -554,7 +553,7 @@ func (p *P2PImpl) SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId
return nodes, errOccurred return nodes, errOccurred
} }
func (p *P2PImpl) SignMessageSimple(message []byte) ([]byte, error) { func (p *P2PService) SignMessageSimple(message []byte) ([]byte, error) {
signedMessage := signed.NewSignedMessageRequest(message) signedMessage := signed.NewSignedMessageRequest(message)
signedMessage.SetNodeId(p.localNodeID) signedMessage.SetNodeId(p.localNodeID)
@ -573,7 +572,7 @@ func (p *P2PImpl) SignMessageSimple(message []byte) ([]byte, error) {
return result, nil return result, nil
} }
func (p *P2PImpl) AddPeer(peer net.Peer) error { func (p *P2PService) AddPeer(peer net.Peer) error {
peerId, err := peer.Id().ToString() peerId, err := peer.Id().ToString()
if err != nil { if err != nil {
return err return err
@ -587,7 +586,7 @@ func (p *P2PImpl) AddPeer(peer net.Peer) error {
return nil return nil
} }
func (p *P2PImpl) SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) error { func (p *P2PService) SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) error {
announceRequest := signed.NewAnnounceRequest(peer, peersToSend) announceRequest := signed.NewAnnounceRequest(peer, peersToSend)
message, err := msgpack.Marshal(announceRequest) message, err := msgpack.Marshal(announceRequest)
@ -606,7 +605,7 @@ func (p *P2PImpl) SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) e
return nil return nil
} }
func (p *P2PImpl) SendHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) error { func (p *P2PService) SendHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) error {
hashRequest := protocol.NewHashRequest(hash, kinds) hashRequest := protocol.NewHashRequest(hash, kinds)
message, err := msgpack.Marshal(hashRequest) message, err := msgpack.Marshal(hashRequest)
if err != nil { if err != nil {
@ -625,7 +624,7 @@ func (p *P2PImpl) SendHashRequest(hash *encoding.Multihash, kinds []types.Storag
return nil return nil
} }
func (p *P2PImpl) UpVote(nodeId *encoding.NodeId) error { func (p *P2PService) UpVote(nodeId *encoding.NodeId) error {
err := p.vote(nodeId, true) err := p.vote(nodeId, true)
if err != nil { if err != nil {
return err return err
@ -634,7 +633,7 @@ func (p *P2PImpl) UpVote(nodeId *encoding.NodeId) error {
return nil return nil
} }
func (p *P2PImpl) DownVote(nodeId *encoding.NodeId) error { func (p *P2PService) DownVote(nodeId *encoding.NodeId) error {
err := p.vote(nodeId, false) err := p.vote(nodeId, false)
if err != nil { if err != nil {
return err return err
@ -643,7 +642,7 @@ func (p *P2PImpl) DownVote(nodeId *encoding.NodeId) error {
return nil return nil
} }
func (p *P2PImpl) vote(nodeId *encoding.NodeId, upvote bool) error { func (p *P2PService) vote(nodeId *encoding.NodeId, upvote bool) error {
votes, err := p.readNodeVotes(nodeId) votes, err := p.readNodeVotes(nodeId)
if err != nil { if err != nil {
return err return err
@ -662,11 +661,11 @@ func (p *P2PImpl) vote(nodeId *encoding.NodeId, upvote bool) error {
return nil return nil
} }
func (p *P2PImpl) NodeId() *encoding.NodeId { func (p *P2PService) NodeId() *encoding.NodeId {
return p.localNodeID return p.localNodeID
} }
func (p *P2PImpl) PrepareProvideMessage(hash *encoding.Multihash, location interfaces.StorageLocation) []byte { func (p *P2PService) PrepareProvideMessage(hash *encoding.Multihash, location storage.StorageLocation) []byte {
// Initialize the list with the record type. // Initialize the list with the record type.
list := []byte{byte(types.RecordTypeStorageLocation)} list := []byte{byte(types.RecordTypeStorageLocation)}

View File

@ -3,8 +3,8 @@ package service
import ( import (
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
_node "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/structs" "git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
@ -16,42 +16,44 @@ import (
"time" "time"
) )
var _ interfaces.RegistryService = (*RegistryImpl)(nil)
const registryBucketName = "registry" const registryBucketName = "registry"
type RegistryImpl struct { var (
node interfaces.Node _ Service = (*RegistryService)(nil)
)
type RegistryService struct {
node *_node.Node
logger *zap.Logger logger *zap.Logger
streams structs.Map streams structs.Map
subs structs.Map subs structs.Map
} }
func (r *RegistryImpl) Node() interfaces.Node { func (r *RegistryService) Node() *_node.Node {
return r.node return r.node
} }
func (r *RegistryImpl) Start() error { func (r *RegistryService) Start() error {
return nil return nil
} }
func (r *RegistryImpl) Stop() error { func (r *RegistryService) Stop() error {
return nil return nil
} }
func (r *RegistryImpl) Init() error { func (r *RegistryService) Init() error {
return utils.CreateBucket(registryBucketName, r.node.Db()) return utils.CreateBucket(registryBucketName, r.node.Db())
} }
func NewRegistry(node interfaces.Node) *RegistryImpl { func NewRegistry(node *_node.Node) *RegistryService {
return &RegistryImpl{ return &RegistryService{
node: node, node: node,
logger: node.Logger(), logger: node.Logger(),
streams: structs.NewMap(), streams: structs.NewMap(),
subs: structs.NewMap(), subs: structs.NewMap(),
} }
} }
func (r *RegistryImpl) Set(sre interfaces.SignedRegistryEntry, trusted bool, receivedFrom net.Peer) error { func (r *RegistryService) Set(sre protocol.SignedRegistryEntry, trusted bool, receivedFrom net.Peer) error {
hash := encoding.NewMultihash(sre.PK()) hash := encoding.NewMultihash(sre.PK())
hashString, err := hash.ToString() hashString, err := hash.ToString()
if err != nil { if err != nil {
@ -138,7 +140,7 @@ func (r *RegistryImpl) Set(sre interfaces.SignedRegistryEntry, trusted bool, rec
return nil return nil
} }
func (r *RegistryImpl) BroadcastEntry(sre interfaces.SignedRegistryEntry, receivedFrom net.Peer) error { func (r *RegistryService) BroadcastEntry(sre protocol.SignedRegistryEntry, receivedFrom net.Peer) error {
hash := encoding.NewMultihash(sre.PK()) hash := encoding.NewMultihash(sre.PK())
hashString, err := hash.ToString() hashString, err := hash.ToString()
if err != nil { if err != nil {
@ -171,7 +173,7 @@ func (r *RegistryImpl) BroadcastEntry(sre interfaces.SignedRegistryEntry, receiv
return nil return nil
} }
func (r *RegistryImpl) SendRegistryRequest(pk []byte) error { func (r *RegistryService) SendRegistryRequest(pk []byte) error {
query := protocol.NewRegistryQuery(pk) query := protocol.NewRegistryQuery(pk)
request, err := msgpack.Marshal(query) request, err := msgpack.Marshal(query)
@ -198,7 +200,7 @@ func (r *RegistryImpl) SendRegistryRequest(pk []byte) error {
return nil return nil
} }
func (r *RegistryImpl) Get(pk []byte) (interfaces.SignedRegistryEntry, error) { func (r *RegistryService) Get(pk []byte) (protocol.SignedRegistryEntry, error) {
key := encoding.NewMultihash(pk) key := encoding.NewMultihash(pk)
keyString, err := key.ToString() keyString, err := key.ToString()
if err != nil { if err != nil {
@ -259,14 +261,14 @@ func (r *RegistryImpl) Get(pk []byte) (interfaces.SignedRegistryEntry, error) {
return nil, nil return nil, nil
} }
func (r *RegistryImpl) Listen(pk []byte, cb func(sre interfaces.SignedRegistryEntry)) (func(), error) { func (r *RegistryService) Listen(pk []byte, cb func(sre protocol.SignedRegistryEntry)) (func(), error) {
key, err := encoding.NewMultihash(pk).ToString() key, err := encoding.NewMultihash(pk).ToString()
if err != nil { if err != nil {
return nil, err return nil, err
} }
cbProxy := func(event *emitter.Event) { cbProxy := func(event *emitter.Event) {
sre, ok := event.Args[0].(interfaces.SignedRegistryEntry) sre, ok := event.Args[0].(protocol.SignedRegistryEntry)
if !ok { if !ok {
r.logger.Error("Failed to cast event to SignedRegistryEntry") r.logger.Error("Failed to cast event to SignedRegistryEntry")
return return
@ -292,7 +294,7 @@ func (r *RegistryImpl) Listen(pk []byte, cb func(sre interfaces.SignedRegistryEn
}, nil }, nil
} }
func (r *RegistryImpl) getFromDB(pk []byte) (sre interfaces.SignedRegistryEntry, err error) { func (r *RegistryService) getFromDB(pk []byte) (sre protocol.SignedRegistryEntry, err error) {
err = r.node.Db().View(func(txn *bbolt.Tx) error { err = r.node.Db().View(func(txn *bbolt.Tx) error {
bucket := txn.Bucket([]byte(registryBucketName)) bucket := txn.Bucket([]byte(registryBucketName))
val := bucket.Get(pk) val := bucket.Get(pk)

16
service/service.go Normal file
View File

@ -0,0 +1,16 @@
package service
import "git.lumeweb.com/LumeWeb/libs5-go/node"
type Service interface {
Node() *node.Node
Start() error
Stop() error
Init() error
}
type Services interface {
P2P() *P2PService
Registry() *RegistryService
HTTP() *HTTPService
All() []Service
}

View File

@ -1,12 +1,11 @@
package service package service
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
) )
var ( var (
_ interfaces.NodeVotes = (*NodeVotesImpl)(nil) _ NodeVotes = (*NodeVotesImpl)(nil)
_ msgpack.CustomDecoder = (*NodeVotesImpl)(nil) _ msgpack.CustomDecoder = (*NodeVotesImpl)(nil)
_ msgpack.CustomEncoder = (*NodeVotesImpl)(nil) _ msgpack.CustomEncoder = (*NodeVotesImpl)(nil)
) )
@ -16,7 +15,7 @@ type NodeVotesImpl struct {
bad int bad int
} }
func NewNodeVotes() interfaces.NodeVotes { func NewNodeVotes() NodeVotes {
return &NodeVotesImpl{ return &NodeVotesImpl{
good: 0, good: 0,
bad: 0, bad: 0,
@ -69,3 +68,12 @@ func (n *NodeVotesImpl) Upvote() {
func (n *NodeVotesImpl) Downvote() { func (n *NodeVotesImpl) Downvote() {
n.bad++ n.bad++
} }
type NodeVotes interface {
msgpack.CustomEncoder
msgpack.CustomDecoder
Good() int
Bad() int
Upvote()
Downvote()
}

View File

@ -16,9 +16,9 @@ import (
var ( var (
_ msgpack.CustomDecoder = (*StorageLocationMap)(nil) _ msgpack.CustomDecoder = (*StorageLocationMap)(nil)
_ msgpack.CustomEncoder = (*StorageLocationMap)(nil) _ msgpack.CustomEncoder = (*StorageLocationMap)(nil)
_ interfaces.StorageLocation = (*StorageLocationImpl)(nil) _ StorageLocation = (*StorageLocationImpl)(nil)
_ interfaces.StorageLocationProvider = (*StorageLocationProviderImpl)(nil) _ StorageLocationProvider = (*StorageLocationProviderImpl)(nil)
_ interfaces.SignedStorageLocation = (*SignedStorageLocationImpl)(nil) _ SignedStorageLocation = (*SignedStorageLocationImpl)(nil)
) )
type StorageLocationMap map[int]NodeStorage type StorageLocationMap map[int]NodeStorage
@ -73,7 +73,7 @@ func (s *StorageLocationImpl) ProviderMessage() []byte {
return s.providerMessage return s.providerMessage
} }
func NewStorageLocation(Type int, Parts []string, Expiry int64) interfaces.StorageLocation { func NewStorageLocation(Type int, Parts []string, Expiry int64) StorageLocation {
return &StorageLocationImpl{ return &StorageLocationImpl{
kind: Type, kind: Type,
parts: Parts, parts: Parts,
@ -99,10 +99,10 @@ func (s *StorageLocationImpl) String() string {
type SignedStorageLocationImpl struct { type SignedStorageLocationImpl struct {
nodeID *encoding.NodeId nodeID *encoding.NodeId
location interfaces.StorageLocation location StorageLocation
} }
func NewSignedStorageLocation(NodeID *encoding.NodeId, Location interfaces.StorageLocation) interfaces.SignedStorageLocation { func NewSignedStorageLocation(NodeID *encoding.NodeId, Location StorageLocation) SignedStorageLocation {
return &SignedStorageLocationImpl{ return &SignedStorageLocationImpl{
nodeID: NodeID, nodeID: NodeID,
location: Location, location: Location,
@ -122,7 +122,7 @@ func (ssl *SignedStorageLocationImpl) String() string {
func (ssl *SignedStorageLocationImpl) NodeId() *encoding.NodeId { func (ssl *SignedStorageLocationImpl) NodeId() *encoding.NodeId {
return ssl.nodeID return ssl.nodeID
} }
func (ssl *SignedStorageLocationImpl) Location() interfaces.StorageLocation { func (ssl *SignedStorageLocationImpl) Location() StorageLocation {
return ssl.location return ssl.location
} }
@ -189,7 +189,7 @@ type StorageLocationProviderImpl struct {
types []types.StorageLocationType types []types.StorageLocationType
timeoutDuration time.Duration timeoutDuration time.Duration
availableNodes []*encoding.NodeId availableNodes []*encoding.NodeId
uris map[string]interfaces.StorageLocation uris map[string]StorageLocation
timeout time.Time timeout time.Time
isTimedOut bool isTimedOut bool
isWaitingForUri bool isWaitingForUri bool
@ -283,7 +283,7 @@ func (s *StorageLocationProviderImpl) Start() error {
}() }()
return nil return nil
} }
func (s *StorageLocationProviderImpl) Next() (interfaces.SignedStorageLocation, error) { func (s *StorageLocationProviderImpl) Next() (SignedStorageLocation, error) {
s.timeout = time.Now().Add(s.timeoutDuration) s.timeout = time.Now().Add(s.timeoutDuration)
for { for {
@ -319,7 +319,7 @@ func (s *StorageLocationProviderImpl) Next() (interfaces.SignedStorageLocation,
} }
} }
func (s *StorageLocationProviderImpl) Upvote(uri interfaces.SignedStorageLocation) error { func (s *StorageLocationProviderImpl) Upvote(uri SignedStorageLocation) error {
err := s.node.Services().P2P().UpVote(uri.NodeId()) err := s.node.Services().P2P().UpVote(uri.NodeId())
if err != nil { if err != nil {
return err return err
@ -328,7 +328,7 @@ func (s *StorageLocationProviderImpl) Upvote(uri interfaces.SignedStorageLocatio
return nil return nil
} }
func (s *StorageLocationProviderImpl) Downvote(uri interfaces.SignedStorageLocation) error { func (s *StorageLocationProviderImpl) Downvote(uri SignedStorageLocation) error {
err := s.node.Services().P2P().DownVote(uri.NodeId()) err := s.node.Services().P2P().DownVote(uri.NodeId())
if err != nil { if err != nil {
return err return err
@ -336,7 +336,7 @@ func (s *StorageLocationProviderImpl) Downvote(uri interfaces.SignedStorageLocat
return nil return nil
} }
func NewStorageLocationProvider(node interfaces.Node, hash *encoding.Multihash, locationTypes ...types.StorageLocationType) interfaces.StorageLocationProvider { func NewStorageLocationProvider(node interfaces.Node, hash *encoding.Multihash, locationTypes ...types.StorageLocationType) StorageLocationProvider {
if locationTypes == nil { if locationTypes == nil {
locationTypes = []types.StorageLocationType{ locationTypes = []types.StorageLocationType{
types.StorageLocationTypeFull, types.StorageLocationTypeFull,
@ -348,7 +348,7 @@ func NewStorageLocationProvider(node interfaces.Node, hash *encoding.Multihash,
hash: hash, hash: hash,
types: locationTypes, types: locationTypes,
timeoutDuration: 60 * time.Second, timeoutDuration: 60 * time.Second,
uris: make(map[string]interfaces.StorageLocation), uris: make(map[string]StorageLocation),
} }
} }
func containsNode(slice []*encoding.NodeId, item *encoding.NodeId) bool { func containsNode(slice []*encoding.NodeId, item *encoding.NodeId) bool {
@ -359,3 +359,36 @@ func containsNode(slice []*encoding.NodeId, item *encoding.NodeId) bool {
} }
return false return false
} }
type StorageLocationProvider interface {
Start() error
Next() (SignedStorageLocation, error)
Upvote(uri SignedStorageLocation) error
Downvote(uri SignedStorageLocation) error
}
type StorageLocation interface {
BytesURL() string
OutboardBytesURL() string
String() string
ProviderMessage() []byte
Type() int
Parts() []string
BinaryParts() [][]byte
Expiry() int64
SetProviderMessage(msg []byte)
SetType(t int)
SetParts(p []string)
SetBinaryParts(bp [][]byte)
SetExpiry(e int64)
}
type SignedStorageLocation interface {
String() string
NodeId() *encoding.NodeId
Location() StorageLocation
}
type ProviderStore interface {
CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool
Provide(hash *encoding.Multihash, kind []types.StorageLocationType) (StorageLocation, error)
}