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

View File

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

View File

@ -2,8 +2,8 @@ package protocol
import (
"errors"
"git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
)
@ -55,7 +55,7 @@ func (m HandshakeOpen) EncodeMsgpack(enc *msgpack.Encoder) error {
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
}

View File

@ -1,9 +1,9 @@
package protocol
import (
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"github.com/emirpasic/gods/sets/hashset"
"github.com/vmihailenco/msgpack/v5"
"go.uber.org/zap"

View File

@ -2,8 +2,8 @@ package protocol
import (
"fmt"
"git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
"net/url"
@ -14,7 +14,7 @@ var (
)
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)
msgpack.CustomDecoder
}
@ -54,7 +54,7 @@ func (i *IncomingMessageImpl) ToMessage() (message []byte, err error) {
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")
}
@ -89,7 +89,7 @@ func NewIncomingMessageTyped(kind types.ProtocolMethod, data msgpack.RawMessage)
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 {
if i.known {

View File

@ -1,9 +1,9 @@
package signed
import (
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
"github.com/vmihailenco/msgpack/v5"
"net/url"

View File

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

View File

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

View File

@ -3,9 +3,9 @@ package protocol
import (
"crypto/ed25519"
"fmt"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"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/utils"
"github.com/emirpasic/gods/sets/hashset"
@ -39,7 +39,7 @@ func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
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
fmt.Println("Hash:", hash)
@ -73,7 +73,7 @@ func (s *StorageLocation) HandleMessage(node *libs5_go.Node, peer *net.Peer, ver
nodeId := encoding.NewNodeId(publicKey)
// 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 {
return fmt.Errorf("Failed to add storage location: %s", err)

View File

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

View File

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