2024-01-06 11:33:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-07 08:58:22 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
2024-01-09 11:56:44 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-06 15:54:38 +00:00
|
|
|
"github.com/emirpasic/gods/sets/hashset"
|
2024-01-06 11:33:46 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-01-06 15:54:38 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"log"
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
var _ base.IncomingMessageTyped = (*HashQuery)(nil)
|
2024-01-09 12:07:00 +00:00
|
|
|
var _ base.EncodeableMessage = (*HashQuery)(nil)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
type HashQuery struct {
|
|
|
|
hash *encoding.Multihash
|
2024-01-09 11:56:44 +00:00
|
|
|
kinds []types.StorageLocationType
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-07 10:12:43 +00:00
|
|
|
base.IncomingMessageTypedImpl
|
|
|
|
base.IncomingMessageHandler
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 17:41:38 +00:00
|
|
|
func NewHashQuery() *HashQuery {
|
|
|
|
return &HashQuery{}
|
|
|
|
}
|
|
|
|
|
2024-01-09 11:57:06 +00:00
|
|
|
func NewHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) *HashQuery {
|
|
|
|
if len(kinds) == 0 {
|
|
|
|
kinds = []types.StorageLocationType{types.StorageLocationTypeFile}
|
|
|
|
}
|
|
|
|
return &HashQuery{
|
|
|
|
hash: hash,
|
|
|
|
kinds: kinds,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
func (h HashQuery) Hash() *encoding.Multihash {
|
|
|
|
return h.hash
|
|
|
|
}
|
|
|
|
|
2024-01-09 11:56:44 +00:00
|
|
|
func (h HashQuery) Kinds() []types.StorageLocationType {
|
2024-01-06 11:33:46 +00:00
|
|
|
return h.kinds
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HashQuery) DecodeMessage(dec *msgpack.Decoder) error {
|
|
|
|
hash, err := dec.DecodeBytes()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.hash = encoding.NewMultihash(hash)
|
|
|
|
|
2024-01-09 11:56:44 +00:00
|
|
|
var kinds []types.StorageLocationType
|
2024-01-06 11:33:46 +00:00
|
|
|
err = dec.Decode(&kinds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.kinds = kinds
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-09 12:07:00 +00:00
|
|
|
|
|
|
|
func (h HashQuery) EncodeMsgpack(enc *msgpack.Encoder) error {
|
|
|
|
err := enc.EncodeInt(int64(types.ProtocolMethodHashQuery))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = enc.EncodeBytes(h.hash.FullBytes())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = enc.Encode(h.kinds)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-07 11:35:41 +00:00
|
|
|
func (h *HashQuery) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
2024-01-06 15:54:38 +00:00
|
|
|
mapLocations, err := node.GetCachedStorageLocations(h.hash, h.kinds)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting cached storage locations: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mapLocations) > 0 {
|
|
|
|
availableNodes := make([]*encoding.NodeId, 0, len(mapLocations))
|
|
|
|
for key := range mapLocations {
|
|
|
|
nodeId, err := encoding.DecodeNodeId(key)
|
|
|
|
if err != nil {
|
|
|
|
node.Logger().Error("Error decoding node id", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
availableNodes = append(availableNodes, nodeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
score, err := node.Services().P2P().SortNodesByScore(availableNodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-07 08:58:22 +00:00
|
|
|
sortedNodeId, err := (*score[0]).ToString()
|
2024-01-06 15:54:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, exists := mapLocations[sortedNodeId]
|
|
|
|
if exists {
|
2024-01-07 11:35:41 +00:00
|
|
|
err := peer.SendMessage(entry.ProviderMessage())
|
2024-01-06 15:54:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var peers *hashset.Set
|
2024-01-08 17:41:38 +00:00
|
|
|
hashString, err := h.hash.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-06 15:54:38 +00:00
|
|
|
}
|
2024-01-08 17:41:38 +00:00
|
|
|
peersVal, ok := node.HashQueryRoutingTable().Get(hashString) // Implement HashQueryRoutingTable method
|
|
|
|
if ok {
|
|
|
|
peers = peersVal.(*hashset.Set)
|
|
|
|
if !peers.Contains(peer.Id()) {
|
|
|
|
peers.Add(peer.Id())
|
|
|
|
}
|
2024-01-06 15:54:38 +00:00
|
|
|
|
2024-01-08 17:41:38 +00:00
|
|
|
return nil
|
2024-01-06 15:54:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 17:41:38 +00:00
|
|
|
peerList := hashset.New()
|
|
|
|
peerList.Add(peer.Id())
|
|
|
|
|
|
|
|
node.HashQueryRoutingTable().Put(hashString, peerList)
|
|
|
|
|
2024-01-06 15:54:38 +00:00
|
|
|
for _, val := range node.Services().P2P().Peers().Values() {
|
|
|
|
peerVal := val.(net.Peer)
|
2024-01-07 11:35:41 +00:00
|
|
|
if !peerVal.Id().Equals(peer.Id()) {
|
2024-01-06 15:54:38 +00:00
|
|
|
err := peerVal.SendMessage(h.IncomingMessageImpl.Original())
|
|
|
|
if err != nil {
|
|
|
|
node.Logger().Error("Failed to send message", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|