refactoring: more refactoring to break import cycles
This commit is contained in:
parent
ca41aee245
commit
2e8c335b7e
|
@ -2,6 +2,7 @@ package base
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
@ -26,6 +27,7 @@ type IncomingMessageData struct {
|
||||||
Services service.Services
|
Services service.Services
|
||||||
Logger *zap.Logger
|
Logger *zap.Logger
|
||||||
Peer net.Peer
|
Peer net.Peer
|
||||||
|
Config *config.NodeConfig
|
||||||
VerifyId bool
|
VerifyId bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/storage"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"github.com/emirpasic/gods/sets/hashset"
|
"github.com/emirpasic/gods/sets/hashset"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
@ -91,6 +92,7 @@ func (h *HashQuery) HandleMessage(message base.IncomingMessageData) error {
|
||||||
peer := message.Peer
|
peer := message.Peer
|
||||||
services := message.Services
|
services := message.Services
|
||||||
logger := message.Logger
|
logger := message.Logger
|
||||||
|
config := message.Config
|
||||||
|
|
||||||
mapLocations, err := services.Storage().GetCachedStorageLocations(h.hash, h.kinds)
|
mapLocations, err := services.Storage().GetCachedStorageLocations(h.hash, h.kinds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -136,7 +138,7 @@ func (h *HashQuery) HandleMessage(message base.IncomingMessageData) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
message := services.P2P().PrepareProvideMessage(h.hash, location)
|
message := storage.PrepareProvideMessage(config.KeyPair, h.hash, location)
|
||||||
|
|
||||||
err = services.Storage().AddStorageLocation(h.hash, services.P2P().NodeId(), location, message)
|
err = services.Storage().AddStorageLocation(h.hash, services.P2P().NodeId(), location, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package service
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
ed25519p "crypto/ed25519"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.lumeweb.com/LumeWeb/libs5-go/ed25519"
|
"git.lumeweb.com/LumeWeb/libs5-go/ed25519"
|
||||||
|
@ -12,7 +11,6 @@ import (
|
||||||
"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"
|
||||||
|
@ -445,6 +443,7 @@ func (p *P2PService) OnNewPeerListen(peer net.Peer, verifyId bool) {
|
||||||
Ctx: context.Background(),
|
Ctx: context.Background(),
|
||||||
Peer: peer,
|
Peer: peer,
|
||||||
VerifyId: verifyId,
|
VerifyId: verifyId,
|
||||||
|
Config: p.config,
|
||||||
}
|
}
|
||||||
|
|
||||||
dec := msgpack.NewDecoder(bytes.NewReader(reader.Data))
|
dec := msgpack.NewDecoder(bytes.NewReader(reader.Data))
|
||||||
|
@ -664,48 +663,6 @@ func (p *P2PService) NodeId() *encoding.NodeId {
|
||||||
return p.localNodeID
|
return p.localNodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *P2PService) PrepareProvideMessage(hash *encoding.Multihash, location storage.StorageLocation) []byte {
|
|
||||||
// Initialize the list with the record type.
|
|
||||||
list := []byte{byte(types.RecordTypeStorageLocation)}
|
|
||||||
|
|
||||||
// Append the full bytes of the hash.
|
|
||||||
list = append(list, hash.FullBytes()...)
|
|
||||||
|
|
||||||
// Append the location type.
|
|
||||||
list = append(list, byte(location.Type()))
|
|
||||||
|
|
||||||
// Append the expiry time of the location, encoded as 4 bytes.
|
|
||||||
list = append(list, utils.EncodeEndian(uint64(location.Expiry()), 4)...)
|
|
||||||
|
|
||||||
// Append the number of parts in the location.
|
|
||||||
list = append(list, byte(len(location.Parts())))
|
|
||||||
|
|
||||||
// Iterate over each part in the location.
|
|
||||||
for _, part := range location.Parts() {
|
|
||||||
// Convert part to bytes.
|
|
||||||
bytes := []byte(part)
|
|
||||||
|
|
||||||
// Encode the length of the part as 4 bytes and append.
|
|
||||||
list = append(list, utils.EncodeEndian(uint64(len(bytes)), 2)...)
|
|
||||||
|
|
||||||
// Append the actual part bytes.
|
|
||||||
list = append(list, bytes...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append a null byte at the end of the list.
|
|
||||||
list = append(list, 0)
|
|
||||||
|
|
||||||
// Sign the list using the node's private key.
|
|
||||||
signature := ed25519p.Sign(p.nodeKeyPair.ExtractBytes(), list)
|
|
||||||
|
|
||||||
// Append the public key and signature to the list.
|
|
||||||
finalList := append(list, p.nodeKeyPair.PublicKey()...)
|
|
||||||
finalList = append(finalList, signature...)
|
|
||||||
|
|
||||||
// Return the final byte slice.
|
|
||||||
return finalList
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *P2PService) WaitOnConnectedPeers() {
|
func (p *P2PService) WaitOnConnectedPeers() {
|
||||||
p.connections.Wait()
|
p.connections.Wait()
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) Type() int {
|
||||||
|
return s.kind
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) Parts() []string {
|
||||||
|
return s.parts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) BinaryParts() [][]byte {
|
||||||
|
return s.binaryParts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) Expiry() int64 {
|
||||||
|
return s.expiry
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) SetType(t int) {
|
||||||
|
s.kind = t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) SetParts(p []string) {
|
||||||
|
s.parts = p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) SetBinaryParts(bp [][]byte) {
|
||||||
|
s.binaryParts = bp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) SetExpiry(e int64) {
|
||||||
|
s.expiry = e
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) SetProviderMessage(msg []byte) {
|
||||||
|
s.providerMessage = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StorageLocationImpl) ProviderMessage() []byte {
|
||||||
|
return s.providerMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStorageLocation(Type int, Parts []string, Expiry int64) StorageLocation {
|
||||||
|
return &StorageLocationImpl{
|
||||||
|
kind: Type,
|
||||||
|
parts: Parts,
|
||||||
|
expiry: Expiry,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type StorageLocationImpl struct {
|
||||||
|
kind int
|
||||||
|
parts []string
|
||||||
|
binaryParts [][]byte
|
||||||
|
expiry int64
|
||||||
|
providerMessage []byte
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
ed25519p "crypto/ed25519"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/ed25519"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PrepareProvideMessage(identity *ed25519.KeyPairEd25519, hash *encoding.Multihash, location StorageLocation) []byte {
|
||||||
|
// Initialize the list with the record type.
|
||||||
|
list := []byte{byte(types.RecordTypeStorageLocation)}
|
||||||
|
|
||||||
|
// Append the full bytes of the hash.
|
||||||
|
list = append(list, hash.FullBytes()...)
|
||||||
|
|
||||||
|
// Append the location type.
|
||||||
|
list = append(list, byte(location.Type()))
|
||||||
|
|
||||||
|
// Append the expiry time of the location, encoded as 4 bytes.
|
||||||
|
list = append(list, utils.EncodeEndian(uint64(location.Expiry()), 4)...)
|
||||||
|
|
||||||
|
// Append the number of parts in the location.
|
||||||
|
list = append(list, byte(len(location.Parts())))
|
||||||
|
|
||||||
|
// Iterate over each part in the location.
|
||||||
|
for _, part := range location.Parts() {
|
||||||
|
// Convert part to bytes.
|
||||||
|
bytes := []byte(part)
|
||||||
|
|
||||||
|
// Encode the length of the part as 4 bytes and append.
|
||||||
|
list = append(list, utils.EncodeEndian(uint64(len(bytes)), 2)...)
|
||||||
|
|
||||||
|
// Append the actual part bytes.
|
||||||
|
list = append(list, bytes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append a null byte at the end of the list.
|
||||||
|
list = append(list, 0)
|
||||||
|
|
||||||
|
// Sign the list using the node's private key.
|
||||||
|
signature := ed25519p.Sign(identity.ExtractBytes(), list)
|
||||||
|
|
||||||
|
// Append the public key and signature to the list.
|
||||||
|
finalList := append(list, identity.PublicKey()...)
|
||||||
|
finalList = append(finalList, signature...)
|
||||||
|
|
||||||
|
// Return the final byte slice.
|
||||||
|
return finalList
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import "git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||||
|
|
||||||
|
type SignedStorageLocation interface {
|
||||||
|
String() string
|
||||||
|
NodeId() *encoding.NodeId
|
||||||
|
Location() StorageLocation
|
||||||
|
}
|
|
@ -25,14 +25,6 @@ type StorageLocationMap map[int]NodeStorage
|
||||||
type NodeStorage map[string]NodeDetailsStorage
|
type NodeStorage map[string]NodeDetailsStorage
|
||||||
type NodeDetailsStorage map[int]interface{}
|
type NodeDetailsStorage map[int]interface{}
|
||||||
|
|
||||||
type StorageLocationImpl struct {
|
|
||||||
kind int
|
|
||||||
parts []string
|
|
||||||
binaryParts [][]byte
|
|
||||||
expiry int64
|
|
||||||
providerMessage []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type StorageLocationProviderParams struct {
|
type StorageLocationProviderParams struct {
|
||||||
Services service.Services
|
Services service.Services
|
||||||
Hash *encoding.Multihash
|
Hash *encoding.Multihash
|
||||||
|
@ -40,54 +32,6 @@ type StorageLocationProviderParams struct {
|
||||||
service.ServiceParams
|
service.ServiceParams
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageLocationImpl) Type() int {
|
|
||||||
return s.kind
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) Parts() []string {
|
|
||||||
return s.parts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) BinaryParts() [][]byte {
|
|
||||||
return s.binaryParts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) Expiry() int64 {
|
|
||||||
return s.expiry
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) SetType(t int) {
|
|
||||||
s.kind = t
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) SetParts(p []string) {
|
|
||||||
s.parts = p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) SetBinaryParts(bp [][]byte) {
|
|
||||||
s.binaryParts = bp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) SetExpiry(e int64) {
|
|
||||||
s.expiry = e
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) SetProviderMessage(msg []byte) {
|
|
||||||
s.providerMessage = msg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) ProviderMessage() []byte {
|
|
||||||
return s.providerMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStorageLocation(Type int, Parts []string, Expiry int64) StorageLocation {
|
|
||||||
return &StorageLocationImpl{
|
|
||||||
kind: Type,
|
|
||||||
parts: Parts,
|
|
||||||
expiry: Expiry,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *StorageLocationImpl) BytesURL() string {
|
func (s *StorageLocationImpl) BytesURL() string {
|
||||||
return s.parts[0]
|
return s.parts[0]
|
||||||
}
|
}
|
||||||
|
@ -373,29 +317,3 @@ type StorageLocationProvider interface {
|
||||||
Upvote(uri SignedStorageLocation) error
|
Upvote(uri SignedStorageLocation) error
|
||||||
Downvote(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)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue