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
|
||||
}
|
||||
|
||||
func NewHashQuery() *HashQuery {
|
||||
return &HashQuery{}
|
||||
}
|
||||
|
||||
func (h HashQuery) Hash() *encoding.Multihash {
|
||||
return h.hash
|
||||
}
|
||||
|
@ -87,17 +91,25 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer net.Peer, verifyId
|
|||
}
|
||||
|
||||
var peers *hashset.Set
|
||||
peersVal, ok := node.HashQueryRoutingTable().Get(h.hash.HashCode()) // Implement HashQueryRoutingTable method
|
||||
if !ok {
|
||||
peers = hashset.New()
|
||||
hashString, err := h.hash.ToString()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
peersVal, ok := node.HashQueryRoutingTable().Get(hashString) // Implement HashQueryRoutingTable method
|
||||
if ok {
|
||||
peers = peersVal.(*hashset.Set)
|
||||
|
||||
if exists := peers.Contains(peer.Id()); !exists {
|
||||
if !peers.Contains(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() {
|
||||
peerVal := val.(net.Peer)
|
||||
if !peerVal.Id().Equals(peer.Id()) {
|
||||
|
|
Loading…
Reference in New Issue