|
| 1 | +// Copyright (C) 2019-2025 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package p2p |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + pubsub "github.com/libp2p/go-libp2p-pubsub" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "github.com/algorand/go-algorand/config" |
| 26 | + "github.com/algorand/go-algorand/test/partitiontest" |
| 27 | +) |
| 28 | + |
| 29 | +func TestPubsub_GossipSubParamsBasic(t *testing.T) { |
| 30 | + partitiontest.PartitionTest(t) |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + cfg := config.GetDefaultLocal() |
| 34 | + |
| 35 | + for _, fanout := range []int{4, 8} { |
| 36 | + cfg.GossipFanout = fanout |
| 37 | + |
| 38 | + params := deriveGossipSubParams(cfg) |
| 39 | + |
| 40 | + require.Equal(t, fanout, params.D) |
| 41 | + require.Equal(t, fanout-1, params.Dlo) |
| 42 | + require.Equal(t, fanout*2/3, params.Dscore) |
| 43 | + require.Equal(t, fanout*1/3, params.Dout) |
| 44 | + |
| 45 | + // Sanity: other defaults are preserved (not zeroed). Avoid asserting exact values to reduce brittleness. |
| 46 | + def := pubsub.DefaultGossipSubParams() |
| 47 | + require.Equal(t, def.HeartbeatInitialDelay, params.HeartbeatInitialDelay) |
| 48 | + require.Equal(t, def.HistoryLength, params.HistoryLength) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestPubsub_GossipSubParamsEdgeCases(t *testing.T) { |
| 53 | + partitiontest.PartitionTest(t) |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + // D = 1 => Dlo must not drop below 1 |
| 57 | + cfg := config.GetDefaultLocal() |
| 58 | + cfg.GossipFanout = 1 |
| 59 | + p := deriveGossipSubParams(cfg) |
| 60 | + require.Equal(t, 1, p.D) |
| 61 | + require.Equal(t, 1, p.Dlo) |
| 62 | + require.Equal(t, 0, p.Dscore) |
| 63 | + require.Equal(t, 0, p.Dout) |
| 64 | + |
| 65 | + // D = 0 => keep Dlo = D (0) instead of negative |
| 66 | + cfg = config.GetDefaultLocal() |
| 67 | + cfg.GossipFanout = 0 |
| 68 | + p = deriveGossipSubParams(cfg) |
| 69 | + require.Equal(t, 0, p.D) |
| 70 | + require.Equal(t, 0, p.Dlo) |
| 71 | + require.Equal(t, 0, p.Dscore) |
| 72 | + require.Equal(t, 0, p.Dout) |
| 73 | +} |
0 commit comments