refactor: update HandleMessage and add hash to routing table
This commit is contained in:
parent
350d9c8244
commit
d51f5e4590
|
@ -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,16 +91,24 @@ 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)
|
||||||
|
if !peers.Contains(peer.Id()) {
|
||||||
|
peers.Add(peer.Id())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
peers = peersVal.(*hashset.Set)
|
peerList := hashset.New()
|
||||||
|
peerList.Add(peer.Id())
|
||||||
|
|
||||||
if exists := peers.Contains(peer.Id()); !exists {
|
node.HashQueryRoutingTable().Put(hashString, peerList)
|
||||||
peers.Add(peer.Id())
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, val := range node.Services().P2P().Peers().Values() {
|
for _, val := range node.Services().P2P().Peers().Values() {
|
||||||
peerVal := val.(net.Peer)
|
peerVal := val.(net.Peer)
|
||||||
|
|
Loading…
Reference in New Issue