mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-07 15:23:08 +00:00
fix slice bounds issues; getCount takes care of the slicing
This commit is contained in:
parent
060a9bba62
commit
1f5959bf54
27
gossipsub.go
27
gossipsub.go
@ -203,10 +203,10 @@ func (gs *GossipSubRouter) Publish(from peer.ID, msg *pb.Message) {
|
|||||||
gmap, ok = gs.fanout[topic]
|
gmap, ok = gs.fanout[topic]
|
||||||
if !ok {
|
if !ok {
|
||||||
// we don't have any, pick some
|
// we don't have any, pick some
|
||||||
peers := gs.getPeers(topic, func(peer.ID) bool { return true })
|
peers := gs.getPeers(topic, GossipSubD, func(peer.ID) bool { return true })
|
||||||
|
|
||||||
if len(peers) > 0 {
|
if len(peers) > 0 {
|
||||||
gmap = peerListToMap(peers[:GossipSubD])
|
gmap = peerListToMap(peers)
|
||||||
gs.fanout[topic] = gmap
|
gs.fanout[topic] = gmap
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -238,8 +238,8 @@ func (gs *GossipSubRouter) Join(topic string) {
|
|||||||
gs.mesh[topic] = gmap
|
gs.mesh[topic] = gmap
|
||||||
delete(gs.fanout, topic)
|
delete(gs.fanout, topic)
|
||||||
} else {
|
} else {
|
||||||
peers := gs.getPeers(topic, func(peer.ID) bool { return true })
|
peers := gs.getPeers(topic, GossipSubD, func(peer.ID) bool { return true })
|
||||||
gmap = peerListToMap(peers[:GossipSubD])
|
gmap = peerListToMap(peers)
|
||||||
gs.mesh[topic] = gmap
|
gs.mesh[topic] = gmap
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,12 +344,12 @@ func (gs *GossipSubRouter) heartbeat() {
|
|||||||
// do we have enough peers?
|
// do we have enough peers?
|
||||||
if len(peers) < GossipSubDlo {
|
if len(peers) < GossipSubDlo {
|
||||||
ineed := GossipSubD - len(peers)
|
ineed := GossipSubD - len(peers)
|
||||||
plst := gs.getPeers(topic, func(p peer.ID) bool {
|
plst := gs.getPeers(topic, ineed, func(p peer.ID) bool {
|
||||||
_, ok := peers[p]
|
_, ok := peers[p]
|
||||||
return !ok
|
return !ok
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, p := range plst[:ineed] {
|
for _, p := range plst {
|
||||||
peers[p] = struct{}{}
|
peers[p] = struct{}{}
|
||||||
topics := tograft[p]
|
topics := tograft[p]
|
||||||
tograft[p] = append(topics, topic)
|
tograft[p] = append(topics, topic)
|
||||||
@ -375,8 +375,8 @@ func (gs *GossipSubRouter) heartbeat() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
gpeers := gs.getPeers(topic, func(peer.ID) bool { return true })
|
gpeers := gs.getPeers(topic, GossipSubD, func(peer.ID) bool { return true })
|
||||||
for _, p := range gpeers[:GossipSubD] {
|
for _, p := range gpeers {
|
||||||
// skip mesh peers
|
// skip mesh peers
|
||||||
_, ok := peers[p]
|
_, ok := peers[p]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -429,12 +429,12 @@ func (gs *GossipSubRouter) heartbeat() {
|
|||||||
// do we need more peers?
|
// do we need more peers?
|
||||||
if len(peers) < GossipSubD {
|
if len(peers) < GossipSubD {
|
||||||
ineed := GossipSubD - len(peers)
|
ineed := GossipSubD - len(peers)
|
||||||
plst := gs.getPeers(topic, func(p peer.ID) bool {
|
plst := gs.getPeers(topic, ineed, func(p peer.ID) bool {
|
||||||
_, ok := peers[p]
|
_, ok := peers[p]
|
||||||
return !ok
|
return !ok
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, p := range plst[:ineed] {
|
for _, p := range plst {
|
||||||
peers[p] = struct{}{}
|
peers[p] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -525,7 +525,7 @@ func (gs *GossipSubRouter) piggybackControl(p peer.ID, out *RPC, ctl *pb.Control
|
|||||||
xctl.Prune = append(xctl.Prune, toprune...)
|
xctl.Prune = append(xctl.Prune, toprune...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gs *GossipSubRouter) getPeers(topic string, filter func(peer.ID) bool) []peer.ID {
|
func (gs *GossipSubRouter) getPeers(topic string, count int, filter func(peer.ID) bool) []peer.ID {
|
||||||
tmap, ok := gs.p.topics[topic]
|
tmap, ok := gs.p.topics[topic]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
@ -539,6 +539,11 @@ func (gs *GossipSubRouter) getPeers(topic string, filter func(peer.ID) bool) []p
|
|||||||
}
|
}
|
||||||
|
|
||||||
shufflePeers(peers)
|
shufflePeers(peers)
|
||||||
|
|
||||||
|
if count > 0 && len(peers) > count {
|
||||||
|
peers = peers[:count]
|
||||||
|
}
|
||||||
|
|
||||||
return peers
|
return peers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user