Kademlia: Use information requests from updated DHT library. More testing & fixes upcoming.

This commit is contained in:
Kleissner
2021-04-09 16:36:35 +02:00
parent c44e57a0a0
commit e69589c5c8
10 changed files with 185 additions and 64 deletions

View File

@@ -6,26 +6,65 @@ Author: Peter Kleissner
package core
import "github.com/PeernetOfficial/core/dht"
import (
"bytes"
"github.com/PeernetOfficial/core/dht"
)
var nodesDHT *dht.DHT
func initKademlia() {
nodesDHT = dht.NewDHT(&dht.Node{ID: nodeID}, 256, 20)
nodesDHT = dht.NewDHT(&dht.Node{ID: nodeID}, 256, 20, 5)
// ShouldEvict determines whether the given node shall be evicted
// TODO For now always return true.
nodesDHT.ShouldEvict = func(node *dht.Node) bool {
// TODO: logic
return true
}
// SendStore sends a store message to the remote node. I.e. asking it to store the given key-value
nodesDHT.SendStore = func(node *dht.Node, key []byte, value []byte) {
// TODO
// SendRequestStore sends a store message to the remote node. I.e. asking it to store the given key-value
nodesDHT.SendRequestStore = func(node *dht.Node, key []byte, value []byte) {
node.Info.(*PeerInfo).sendAnnouncementStore(key, value)
}
// SendRequest sends an information request to the remote node. I.e. requesting information.
nodesDHT.SendRequest = func(request *dht.InformationRequest, nodes []*dht.Node) {
// TODO
// SendRequestFindNode sends an information request to find a particular node. nodes are the nodes to send the request to.
nodesDHT.SendRequestFindNode = func(request *dht.InformationRequest) {
for _, node := range request.Nodes {
node.Info.(*PeerInfo).sendAnnouncementFindNode(request)
}
}
// SendRequestFindValue sends an information request to find data. nodes are the nodes to send the request to.
nodesDHT.SendRequestFindValue = func(request *dht.InformationRequest) {
for _, node := range request.Nodes {
node.Info.(*PeerInfo).sendAnnouncementFindValue(request)
}
}
}
// Future sendAnnouncementX: If it detects that announcements are sent out to the same peer within 50ms it should activate a wait-and-group scheme.
func (peer *PeerInfo) sendAnnouncementFindNode(request *dht.InformationRequest) {
// If the key is self, send it as FIND_SELF
if bytes.Equal(request.Key, nodeID) {
peer.sendAnnouncement(false, true, nil, nil, nil)
} else {
peer.sendAnnouncement(false, false, []KeyHash{{Hash: request.Key}}, nil, nil)
}
}
func (peer *PeerInfo) sendAnnouncementFindValue(request *dht.InformationRequest) {
findSelf := false
var findPeer []KeyHash
var findValue []KeyHash
findValue = append(findValue, KeyHash{Hash: request.Key})
peer.sendAnnouncement(false, findSelf, findPeer, findValue, nil)
}
func (peer *PeerInfo) sendAnnouncementStore(key []byte, value []byte) {
peer.sendAnnouncement(false, false, nil, nil, []InfoStore{{ID: KeyHash{Hash: key}, Size: uint64(len(value)), Type: 0}})
}