diff --git a/gossipsub.go b/gossipsub.go index e2db462..c829f81 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -107,6 +107,9 @@ func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er iasked: make(map[peer.ID]int), connect: make(chan connectInfo, GossipSubMaxPendingConnections), mcache: NewMessageCache(GossipSubHistoryGossip, GossipSubHistoryLength), + // these must be pulled in to resolve races in tests... sigh. + directConnectTicks: GossipSubDirectConnectTicks, + opportunisticGraftTicks: GossipSubOpportunisticGraftTicks, } return NewPubSub(ctx, h, rt, opts...) } @@ -258,6 +261,10 @@ type GossipSubRouter struct { // number of heartbeats since the beginning of time; this allows us to amortize some resource // clean up -- eg backoff clean up. heartbeatTicks uint64 + + // tick "constants" for triggering direct connect and opportunistic grafting + // these are pulled from their global value or else the race detector is angry on travis + directConnectTicks, opportunisticGraftTicks uint64 } type connectInfo struct { @@ -1001,7 +1008,7 @@ func (gs *GossipSubRouter) heartbeat() { } // should we try to improve the mesh with opportunistic grafting? - if gs.heartbeatTicks%GossipSubOpportunisticGraftTicks == 0 && len(peers) > 1 { + if gs.heartbeatTicks%gs.opportunisticGraftTicks == 0 && len(peers) > 1 { // Opportunistic grafting works as follows: we check the median score of peers in the // mesh; if this score is below the opportunisticGraftThreshold, we select a few peers at // random with score over the median. @@ -1131,7 +1138,7 @@ func (gs *GossipSubRouter) clearBackoff() { func (gs *GossipSubRouter) directConnect() { // we donly do this every some ticks to allow pending connections to complete and account // for restarts/downtime - if gs.heartbeatTicks%GossipSubDirectConnectTicks != 0 { + if gs.heartbeatTicks%gs.directConnectTicks != 0 { return } diff --git a/gossipsub_spam_test.go b/gossipsub_spam_test.go index 76c610e..11dd7ef 100644 --- a/gossipsub_spam_test.go +++ b/gossipsub_spam_test.go @@ -321,7 +321,6 @@ func TestGossipsubAttackGRAFTDuringBackoff(t *testing.T) { originalGossipSubPruneBackoffPenalty := GossipSubPruneBackoffPenalty GossipSubPruneBackoffPenalty = 500 * time.Millisecond defer func() { - time.Sleep(time.Second + 100*time.Millisecond) GossipSubPruneBackoff = originalGossipSubPruneBackoff GossipSubPruneBackoffPenalty = originalGossipSubPruneBackoffPenalty GossipSubGraftFloodThreshold = originalGossipSubGraftFloodThreshold diff --git a/gossipsub_test.go b/gossipsub_test.go index b7aad9b..9b2ba37 100644 --- a/gossipsub_test.go +++ b/gossipsub_test.go @@ -272,7 +272,6 @@ func TestGossipsubFanoutMaintenance(t *testing.T) { func TestGossipsubFanoutExpiry(t *testing.T) { GossipSubFanoutTTL = 1 * time.Second defer func() { - time.Sleep(time.Second + 100*time.Millisecond) GossipSubFanoutTTL = 60 * time.Second }() @@ -1000,7 +999,6 @@ func TestGossipsubDirectPeers(t *testing.T) { originalGossipSubDirectConnectTicks := GossipSubDirectConnectTicks GossipSubDirectConnectTicks = 2 defer func() { - time.Sleep(time.Second + 100*time.Millisecond) GossipSubDirectConnectTicks = originalGossipSubDirectConnectTicks }() @@ -1286,7 +1284,6 @@ func TestGossipsubOpportunisticGrafting(t *testing.T) { originalGossipSubOpportunisticGraftTicks := GossipSubOpportunisticGraftTicks GossipSubOpportunisticGraftTicks = 2 defer func() { - time.Sleep(time.Second + 100*time.Millisecond) GossipSubPruneBackoff = originalGossipSubPruneBackoff GossipSubGraftFloodThreshold = originalGossipSubGraftFloodThreshold GossipSubOpportunisticGraftTicks = originalGossipSubOpportunisticGraftTicks