2024-01-06 18:21:09 +00:00
|
|
|
package node
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-09 13:18:41 +00:00
|
|
|
"errors"
|
2024-01-06 18:26:03 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
2024-01-06 14:46:01 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-07 08:13:35 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-09 13:18:41 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/metadata"
|
2024-01-07 09:16:18 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
2024-01-07 09:30:03 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/storage"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/structs"
|
2024-01-09 11:53:09 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-06 14:46:01 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
2024-01-09 13:18:41 +00:00
|
|
|
"github.com/go-resty/resty/v2"
|
2024-01-06 14:46:01 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-01-06 11:33:46 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
2024-01-09 14:11:36 +00:00
|
|
|
"sync"
|
2024-01-06 14:46:01 +00:00
|
|
|
"time"
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
var _ interfaces.Node = (*NodeImpl)(nil)
|
2024-01-06 14:46:01 +00:00
|
|
|
|
|
|
|
const cacheBucketName = "object-cache"
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
type NodeImpl struct {
|
2024-01-06 18:26:03 +00:00
|
|
|
nodeConfig *config.NodeConfig
|
2024-01-07 08:56:50 +00:00
|
|
|
metadataCache structs.Map
|
2024-01-06 11:33:46 +00:00
|
|
|
started bool
|
2024-01-07 08:56:50 +00:00
|
|
|
hashQueryRoutingTable structs.Map
|
2024-01-07 08:13:35 +00:00
|
|
|
services interfaces.Services
|
2024-01-06 14:46:01 +00:00
|
|
|
cacheBucket *bolt.Bucket
|
2024-01-09 13:18:41 +00:00
|
|
|
httpClient *resty.Client
|
2024-01-09 14:11:36 +00:00
|
|
|
connections sync.WaitGroup
|
2024-01-06 14:46:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 14:02:39 +00:00
|
|
|
func (n *NodeImpl) NetworkId() string {
|
|
|
|
return n.nodeConfig.P2P.Network
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:56:50 +00:00
|
|
|
func (n *NodeImpl) Services() interfaces.Services {
|
|
|
|
return n.services
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 09:16:18 +00:00
|
|
|
func NewNode(config *config.NodeConfig) interfaces.Node {
|
|
|
|
n := &NodeImpl{
|
2024-01-06 11:33:46 +00:00
|
|
|
nodeConfig: config,
|
|
|
|
metadataCache: structs.NewMap(),
|
|
|
|
started: false,
|
|
|
|
hashQueryRoutingTable: structs.NewMap(),
|
2024-01-09 13:18:41 +00:00
|
|
|
httpClient: resty.New(),
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
2024-01-07 09:16:18 +00:00
|
|
|
n.services = NewServices(service.NewP2P(n))
|
|
|
|
|
|
|
|
return n
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
2024-01-07 08:56:50 +00:00
|
|
|
func (n *NodeImpl) HashQueryRoutingTable() structs.Map {
|
2024-01-06 11:33:46 +00:00
|
|
|
return n.hashQueryRoutingTable
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (n *NodeImpl) IsStarted() bool {
|
2024-01-06 11:33:46 +00:00
|
|
|
return n.started
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (n *NodeImpl) Config() *config.NodeConfig {
|
2024-01-06 11:33:46 +00:00
|
|
|
return n.nodeConfig
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (n *NodeImpl) Logger() *zap.Logger {
|
2024-01-06 11:33:46 +00:00
|
|
|
if n.nodeConfig != nil {
|
|
|
|
return n.nodeConfig.Logger
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (n *NodeImpl) Db() *bolt.DB {
|
2024-01-06 11:33:46 +00:00
|
|
|
if n.nodeConfig != nil {
|
2024-01-06 18:15:45 +00:00
|
|
|
return n.nodeConfig.DB
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:13:35 +00:00
|
|
|
func (n *NodeImpl) Start() error {
|
2024-01-06 14:46:01 +00:00
|
|
|
err :=
|
|
|
|
utils.CreateBucket(cacheBucketName, n.Db(), func(bucket *bolt.Bucket) {
|
|
|
|
n.cacheBucket = bucket
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-07 10:25:45 +00:00
|
|
|
n.started = true
|
|
|
|
|
2024-01-07 09:17:19 +00:00
|
|
|
err = n.Services().P2P().Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.Services().P2P().Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:46:01 +00:00
|
|
|
n.started = true
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-09 11:53:09 +00:00
|
|
|
func (n *NodeImpl) GetCachedStorageLocations(hash *encoding.Multihash, kinds []types.StorageLocationType) (map[string]interfaces.StorageLocation, error) {
|
2024-01-07 08:56:50 +00:00
|
|
|
locations := make(map[string]interfaces.StorageLocation)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
locationMap, err := n.readStorageLocationsFromDB(hash)
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-06 14:45:00 +00:00
|
|
|
if len(locationMap) == 0 {
|
2024-01-07 08:56:50 +00:00
|
|
|
return make(map[string]interfaces.StorageLocation), nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := time.Now().Unix()
|
|
|
|
|
2024-01-09 11:53:09 +00:00
|
|
|
for _, t := range kinds {
|
2024-01-06 14:45:00 +00:00
|
|
|
|
2024-01-09 11:53:09 +00:00
|
|
|
nodeMap, ok := (locationMap)[int(t)]
|
2024-01-06 11:33:46 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range nodeMap {
|
|
|
|
if len(value) < 4 {
|
2024-01-06 14:45:00 +00:00
|
|
|
continue
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expiry, ok := value[3].(int64)
|
|
|
|
if !ok || expiry < ts {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses, ok := value[1].([]string)
|
|
|
|
if !ok {
|
2024-01-06 14:45:00 +00:00
|
|
|
continue
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 11:53:09 +00:00
|
|
|
storageLocation := storage.NewStorageLocation(int(t), addresses, expiry)
|
2024-01-06 11:33:46 +00:00
|
|
|
if len(value) > 4 {
|
2024-01-06 14:45:00 +00:00
|
|
|
if providerMessage, ok := value[4].([]byte); ok {
|
2024-01-07 08:56:50 +00:00
|
|
|
(storageLocation).SetProviderMessage(providerMessage)
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:46:01 +00:00
|
|
|
locations[key] = storageLocation
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return locations, nil
|
|
|
|
}
|
2024-01-07 09:33:40 +00:00
|
|
|
func (n *NodeImpl) readStorageLocationsFromDB(hash *encoding.Multihash) (storage.StorageLocationMap, error) {
|
|
|
|
locationMap := storage.NewStorageLocationMap()
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
bytes := n.cacheBucket.Get(hash.FullBytes())
|
2024-01-06 11:33:46 +00:00
|
|
|
if bytes == nil {
|
2024-01-06 14:45:00 +00:00
|
|
|
return locationMap, nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
err := msgpack.Unmarshal(bytes, locationMap)
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
return locationMap, nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
2024-01-07 08:56:50 +00:00
|
|
|
func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location interfaces.StorageLocation, message []byte, config *config.NodeConfig) error {
|
2024-01-06 11:33:46 +00:00
|
|
|
// Read existing storage locations
|
2024-01-06 14:45:00 +00:00
|
|
|
locationDb, err := n.readStorageLocationsFromDB(hash)
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 15:53:20 +00:00
|
|
|
nodeIdStr, err := nodeId.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
// Get or create the inner map for the specific type
|
2024-01-07 08:56:50 +00:00
|
|
|
innerMap, exists := locationDb[location.Type()]
|
2024-01-06 11:33:46 +00:00
|
|
|
if !exists {
|
2024-01-07 09:33:40 +00:00
|
|
|
innerMap = make(storage.NodeStorage, 1)
|
|
|
|
innerMap[nodeIdStr] = make(storage.NodeDetailsStorage, 1)
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create location map with new data
|
2024-01-06 14:45:00 +00:00
|
|
|
locationMap := make(map[int]interface{}, 3)
|
2024-01-07 08:56:50 +00:00
|
|
|
locationMap[1] = location.Parts()
|
|
|
|
locationMap[3] = location.Expiry()
|
2024-01-06 11:33:46 +00:00
|
|
|
locationMap[4] = message
|
|
|
|
|
|
|
|
// Update the inner map with the new location
|
2024-01-06 15:53:20 +00:00
|
|
|
innerMap[nodeIdStr] = locationMap
|
2024-01-07 08:56:50 +00:00
|
|
|
locationDb[location.Type()] = innerMap
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
// Serialize the updated map and store it in the database
|
2024-01-06 14:45:00 +00:00
|
|
|
packedBytes, err := msgpack.Marshal(locationDb)
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-06 14:45:00 +00:00
|
|
|
err = n.cacheBucket.Put(hash.FullBytes(), packedBytes)
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-01-09 13:18:41 +00:00
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 13:18:41 +00:00
|
|
|
func (n *NodeImpl) DownloadBytesByHash(hash *encoding.Multihash) ([]byte, error) {
|
|
|
|
// Initialize the download URI provider
|
2024-01-09 16:43:33 +00:00
|
|
|
dlUriProvider := storage.NewStorageLocationProvider(n, hash, types.StorageLocationTypeFull, types.StorageLocationTypeFile)
|
2024-01-09 13:18:41 +00:00
|
|
|
err := dlUriProvider.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
retryCount := 0
|
|
|
|
for {
|
|
|
|
dlUri, err := dlUriProvider.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:01:25 +00:00
|
|
|
n.Logger().Debug("Trying to download from", zap.String("url", dlUri.Location().BytesURL()))
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 13:18:41 +00:00
|
|
|
res, err := n.httpClient.R().Get(dlUri.Location().BytesURL())
|
2024-01-06 11:33:46 +00:00
|
|
|
if err != nil {
|
2024-01-09 13:18:41 +00:00
|
|
|
err := dlUriProvider.Downvote(dlUri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
retryCount++
|
|
|
|
if retryCount > 32 {
|
|
|
|
return nil, errors.New("too many retries")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:18:41 +00:00
|
|
|
bodyBytes := res.Body()
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 13:18:41 +00:00
|
|
|
return bodyBytes, nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:01:57 +00:00
|
|
|
func (n *NodeImpl) GetMetadataByCID(cid *encoding.CID) (md metadata.Metadata, err error) {
|
2024-01-09 13:18:41 +00:00
|
|
|
hashStr, err := cid.Hash.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 13:18:41 +00:00
|
|
|
if n.metadataCache.Contains(hashStr) {
|
2024-01-09 16:01:57 +00:00
|
|
|
md, _ := n.metadataCache.Get(hashStr)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 16:01:57 +00:00
|
|
|
return md.(metadata.Metadata), nil
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 16:01:57 +00:00
|
|
|
bytes, err := n.DownloadBytesByHash(&cid.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-09 13:18:41 +00:00
|
|
|
|
2024-01-09 16:01:57 +00:00
|
|
|
switch cid.Type {
|
|
|
|
case types.CIDTypeMetadataMedia, types.CIDTypeBridge: // Both cases use the same deserialization method
|
|
|
|
md = metadata.NewEmptyMediaMetadata()
|
|
|
|
|
|
|
|
err = msgpack.Unmarshal(bytes, md)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
2024-01-09 16:01:57 +00:00
|
|
|
case types.CIDTypeMetadataWebapp:
|
|
|
|
md = metadata.NewEmptyWebAppMetadata()
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-09 16:01:57 +00:00
|
|
|
err = msgpack.Unmarshal(bytes, md)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unsupported metadata format")
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:01:57 +00:00
|
|
|
n.metadataCache.Put(hashStr, md)
|
|
|
|
|
2024-01-09 13:18:41 +00:00
|
|
|
return md, nil
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
2024-01-09 14:11:36 +00:00
|
|
|
func (n *NodeImpl) WaitOnConnectedPeers() {
|
|
|
|
n.connections.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NodeImpl) ConnectionTracker() *sync.WaitGroup {
|
|
|
|
return &n.connections
|
|
|
|
}
|