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,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()) {
|
||||||
|
|
Loading…
Reference in New Issue