refactor: update HandleMessage and add hash to routing table

This commit is contained in:
Derrick Hammer 2024-01-08 12:41:38 -05:00
parent 350d9c8244
commit d51f5e4590
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 19 additions and 7 deletions

View File

@ -21,6 +21,10 @@ type HashQuery struct {
base.IncomingMessageHandler base.IncomingMessageHandler
} }
func NewHashQuery() *HashQuery {
return &HashQuery{}
}
func (h HashQuery) Hash() *encoding.Multihash { func (h HashQuery) Hash() *encoding.Multihash {
return h.hash return h.hash
} }
@ -87,17 +91,25 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer net.Peer, verifyId
} }
var peers *hashset.Set var peers *hashset.Set
peersVal, ok := node.HashQueryRoutingTable().Get(h.hash.HashCode()) // Implement HashQueryRoutingTable method hashString, err := h.hash.ToString()
if !ok { if err != nil {
peers = hashset.New() return err
} }
peersVal, ok := node.HashQueryRoutingTable().Get(hashString) // Implement HashQueryRoutingTable method
if ok {
peers = peersVal.(*hashset.Set) peers = peersVal.(*hashset.Set)
if !peers.Contains(peer.Id()) {
if exists := peers.Contains(peer.Id()); !exists {
peers.Add(peer.Id()) peers.Add(peer.Id())
} }
return nil
}
peerList := hashset.New()
peerList.Add(peer.Id())
node.HashQueryRoutingTable().Put(hashString, peerList)
for _, val := range node.Services().P2P().Peers().Values() { for _, val := range node.Services().P2P().Peers().Values() {
peerVal := val.(net.Peer) peerVal := val.(net.Peer)
if !peerVal.Id().Equals(peer.Id()) { if !peerVal.Id().Equals(peer.Id()) {