refactor: move node and storage to its own package

This commit is contained in:
Derrick Hammer 2024-01-06 13:21:09 -05:00
parent 348b20ba4a
commit 2a21ca4d60
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
11 changed files with 33 additions and 30 deletions

View File

@ -1,6 +1,7 @@
package libs5_go package node
import ( import (
"git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/service" "git.lumeweb.com/LumeWeb/libs5-go/service"
"git.lumeweb.com/LumeWeb/libs5-go/structs" "git.lumeweb.com/LumeWeb/libs5-go/structs"
@ -26,7 +27,7 @@ func (s *Services) P2P() *service.P2P {
const cacheBucketName = "object-cache" const cacheBucketName = "object-cache"
type Node struct { type Node struct {
nodeConfig *NodeConfig nodeConfig *libs5_go.NodeConfig
metadataCache *structs.Map metadataCache *structs.Map
started bool started bool
hashQueryRoutingTable *structs.Map hashQueryRoutingTable *structs.Map
@ -38,7 +39,7 @@ func (n *Node) Services() *Services {
return &n.services return &n.services
} }
func NewNode(config *NodeConfig) *Node { func NewNode(config *libs5_go.NodeConfig) *Node {
return &Node{ return &Node{
nodeConfig: config, nodeConfig: config,
metadataCache: structs.NewMap(), metadataCache: structs.NewMap(),
@ -54,7 +55,7 @@ func (n *Node) IsStarted() bool {
return n.started return n.started
} }
func (n *Node) Config() *NodeConfig { func (n *Node) Config() *libs5_go.NodeConfig {
return n.nodeConfig return n.nodeConfig
} }
@ -151,8 +152,8 @@ func (n *Node) GetCachedStorageLocations(hash *encoding.Multihash, types []int)
} }
return locations, nil return locations, nil
} }
func (n *Node) readStorageLocationsFromDB(hash *encoding.Multihash) (storageLocationMap, error) { func (n *Node) readStorageLocationsFromDB(hash *encoding.Multihash) (libs5_go.storageLocationMap, error) {
locationMap := newStorageLocationMap() locationMap := libs5_go.newStorageLocationMap()
bytes := n.cacheBucket.Get(hash.FullBytes()) bytes := n.cacheBucket.Get(hash.FullBytes())
if bytes == nil { if bytes == nil {
@ -166,7 +167,7 @@ func (n *Node) readStorageLocationsFromDB(hash *encoding.Multihash) (storageLoca
return locationMap, nil return locationMap, nil
} }
func (n *Node) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location *StorageLocation, message []byte, config *NodeConfig) error { func (n *Node) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location *StorageLocation, message []byte, config *libs5_go.NodeConfig) 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 {
@ -181,8 +182,8 @@ func (n *Node) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.Nod
// Get or create the inner map for the specific type // Get or create the inner map for the specific type
innerMap, exists := locationDb[location.Type] innerMap, exists := locationDb[location.Type]
if !exists { if !exists {
innerMap = make(nodeStorage, 1) innerMap = make(libs5_go.nodeStorage, 1)
innerMap[nodeIdStr] = make(nodeDetailsStorage, 1) innerMap[nodeIdStr] = make(libs5_go.nodeDetailsStorage, 1)
} }
// Create location map with new data // Create location map with new data

View File

@ -1,4 +1,4 @@
package libs5_go package node
import ( import (
"fmt" "fmt"

View File

@ -2,8 +2,8 @@ package protocol
import ( import (
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
) )
@ -55,7 +55,7 @@ func (m HandshakeOpen) EncodeMsgpack(enc *msgpack.Encoder) error {
return nil return nil
} }
func (m *HandshakeOpen) HandleMessage(node *libs5_go.Node, peer *net.Peer, verifyId bool) error { func (m *HandshakeOpen) HandleMessage(node *node.Node, peer *net.Peer, verifyId bool) error {
return nil return nil
} }

View File

@ -1,9 +1,9 @@
package protocol package protocol
import ( import (
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"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"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"github.com/emirpasic/gods/sets/hashset" "github.com/emirpasic/gods/sets/hashset"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"go.uber.org/zap" "go.uber.org/zap"

View File

@ -2,8 +2,8 @@ package protocol
import ( import (
"fmt" "fmt"
"git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"net/url" "net/url"
@ -14,7 +14,7 @@ var (
) )
type IncomingMessage interface { type IncomingMessage interface {
HandleMessage(node *libs5_go.Node, peer *net.Peer, verifyId bool) error HandleMessage(node *node.Node, peer *net.Peer, verifyId bool) error
SetIncomingMessage(msg IncomingMessage) SetIncomingMessage(msg IncomingMessage)
msgpack.CustomDecoder msgpack.CustomDecoder
} }
@ -54,7 +54,7 @@ func (i *IncomingMessageImpl) ToMessage() (message []byte, err error) {
return msgpack.Marshal(i) return msgpack.Marshal(i)
} }
func (i *IncomingMessageImpl) HandleMessage(node *libs5_go.Node, peer *net.Peer, verifyId bool) error { func (i *IncomingMessageImpl) HandleMessage(node *node.Node, peer *net.Peer, verifyId bool) error {
panic("child class should implement this method") panic("child class should implement this method")
} }
@ -89,7 +89,7 @@ func NewIncomingMessageTyped(kind types.ProtocolMethod, data msgpack.RawMessage)
return &IncomingMessageTypedImpl{*known} return &IncomingMessageTypedImpl{*known}
} }
type IncomingMessageHandler func(node *libs5_go.Node, peer *net.Peer, u *url.URL, verifyId bool) error type IncomingMessageHandler func(node *node.Node, peer *net.Peer, u *url.URL, verifyId bool) error
func (i *IncomingMessageImpl) DecodeMsgpack(dec *msgpack.Decoder) error { func (i *IncomingMessageImpl) DecodeMsgpack(dec *msgpack.Decoder) error {
if i.known { if i.known {

View File

@ -1,9 +1,9 @@
package signed package signed
import ( import (
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"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"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"net/url" "net/url"

View File

@ -3,8 +3,8 @@ package signed
import ( import (
"bytes" "bytes"
"errors" "errors"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
) )

View File

@ -3,9 +3,9 @@ package protocol
import ( import (
"crypto/ed25519" "crypto/ed25519"
"errors" "errors"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"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"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed" "git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"

View File

@ -3,9 +3,9 @@ package protocol
import ( import (
"crypto/ed25519" "crypto/ed25519"
"fmt" "fmt"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"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/node"
"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"
"github.com/emirpasic/gods/sets/hashset" "github.com/emirpasic/gods/sets/hashset"
@ -39,7 +39,7 @@ func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
return nil return nil
} }
func (s *StorageLocation) HandleMessage(node *libs5_go.Node, peer *net.Peer, verifyId bool) error { func (s *StorageLocation) HandleMessage(node *node.Node, peer *net.Peer, verifyId bool) error {
hash := encoding.NewMultihash(s.raw[1:34]) // Replace NewMultihash with appropriate function hash := encoding.NewMultihash(s.raw[1:34]) // Replace NewMultihash with appropriate function
fmt.Println("Hash:", hash) fmt.Println("Hash:", hash)
@ -73,7 +73,7 @@ func (s *StorageLocation) HandleMessage(node *libs5_go.Node, peer *net.Peer, ver
nodeId := encoding.NewNodeId(publicKey) nodeId := encoding.NewNodeId(publicKey)
// Assuming `node` is an instance of your Node structure // Assuming `node` is an instance of your Node structure
err := node.AddStorageLocation(hash, nodeId, libs5_go.NewStorageLocation(int(typeOfData), parts, int64(expiry)), s.raw, node.Config()) // Implement AddStorageLocation err := node.AddStorageLocation(hash, nodeId, node.NewStorageLocation(int(typeOfData), parts, int64(expiry)), s.raw, node.Config()) // Implement AddStorageLocation
if err != nil { if err != nil {
return fmt.Errorf("Failed to add storage location: %s", err) return fmt.Errorf("Failed to add storage location: %s", err)

View File

@ -2,10 +2,10 @@ package service
import ( import (
"errors" "errors"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"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/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
"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/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
@ -30,11 +30,11 @@ const nodeBucketName = "nodes"
type P2P struct { type P2P 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 *libs5_go.Node node *node.Node
inited bool inited bool
reconnectDelay *structs.Map reconnectDelay *structs.Map
peers *structs.Map peers *structs.Map
@ -76,7 +76,7 @@ func (n *nodeVotes) DecodeMsgpack(dec *msgpack.Decoder) error {
return nil return nil
} }
func NewP2P(node *libs5_go.Node) *P2P { func NewP2P(node *node.Node) *P2P {
service := &P2P{ service := &P2P{
logger: node.Logger(), logger: node.Logger(),
nodeKeyPair: node.Config().KeyPair, nodeKeyPair: node.Config().KeyPair,
@ -90,7 +90,7 @@ func NewP2P(node *libs5_go.Node) *P2P {
return service return service
} }
func (p *P2P) Node() *libs5_go.Node { func (p *P2P) Node() *node.Node {
return p.node return p.node
} }

View File

@ -1,9 +1,11 @@
package service package service
import libs5_go "git.lumeweb.com/LumeWeb/libs5-go" import (
"git.lumeweb.com/LumeWeb/libs5-go/node"
)
type Service interface { type Service interface {
Node() *libs5_go.Node Node() *node.Node
Start() error Start() error
Stop() error Stop() error
Init() error Init() error