feat: implement SendHashRequest, UpVote, DownVote
This commit is contained in:
parent
e11f3065d3
commit
bb68bf3be1
|
@ -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/structs"
|
"git.lumeweb.com/LumeWeb/libs5-go/structs"
|
||||||
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,4 +24,7 @@ type P2PService interface {
|
||||||
SignMessageSimple(message []byte) ([]byte, error)
|
SignMessageSimple(message []byte) ([]byte, error)
|
||||||
AddPeer(peer net.Peer) error
|
AddPeer(peer net.Peer) error
|
||||||
SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) error
|
SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) error
|
||||||
|
SendHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) error
|
||||||
|
UpVote(nodeId *encoding.NodeId) error
|
||||||
|
DownVote(nodeId *encoding.NodeId) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -401,3 +401,54 @@ func (p *P2PImpl) SendPublicPeersToPeer(peer net.Peer, peersToSend []net.Peer) e
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (p *P2PImpl) SendHashRequest(hash *encoding.Multihash, kinds []types.StorageLocationType) error {
|
||||||
|
hashRequest := protocol.NewHashRequest(hash, kinds)
|
||||||
|
message, err := msgpack.Marshal(hashRequest)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, peer := range p.peers.Values() {
|
||||||
|
peerValue, ok := peer.(net.Peer)
|
||||||
|
if !ok {
|
||||||
|
p.node.Logger().Error("failed to cast peer to net.Peer")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = peerValue.SendMessage(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *P2PImpl) UpVote(nodeId *encoding.NodeId) error {
|
||||||
|
err := p.vote(nodeId, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *P2PImpl) DownVote(nodeId *encoding.NodeId) error {
|
||||||
|
err := p.vote(nodeId, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *P2PImpl) vote(nodeId *encoding.NodeId, upvote bool) error {
|
||||||
|
votes, err := p.readNodeVotes(nodeId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if upvote {
|
||||||
|
votes.Upvote()
|
||||||
|
} else {
|
||||||
|
votes.Downvote()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue