Skip to content

Commit dd1293d

Browse files
committed
provide gossipSubParams D values
1 parent e935a44 commit dd1293d

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

network/p2p/pubsub.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,24 @@ const TXTopicName = "algotx01"
5858

5959
const incomingThreads = 20 // matches to number wsNetwork workers
6060

61-
func makePubSub(ctx context.Context, cfg config.Local, host host.Host, opts ...pubsub.Option) (*pubsub.PubSub, error) {
62-
//defaultParams := pubsub.DefaultGossipSubParams()
61+
// deriveGossipSubParams derives the gossip sub parameters from the cfg.GossipFanout value
62+
// by using the same proportions as pubsub defaults - see GossipSubD, GossipSubDlo, etc.
63+
func deriveGossipSubParams(cfg config.Local) pubsub.GossipSubParams {
64+
params := pubsub.DefaultGossipSubParams()
65+
params.D = cfg.GossipFanout
66+
params.Dlo = params.D - 1
67+
if params.Dlo <= 0 {
68+
params.Dlo = params.D
69+
}
70+
params.Dscore = params.D * 2 / 3
71+
params.Dout = params.D * 1 / 3
72+
return params
73+
}
6374

75+
func makePubSub(ctx context.Context, cfg config.Local, host host.Host, opts ...pubsub.Option) (*pubsub.PubSub, error) {
76+
gossipSubParams := deriveGossipSubParams(cfg)
6477
options := []pubsub.Option{
78+
pubsub.WithGossipSubParams(gossipSubParams),
6579
pubsub.WithPeerScore(&pubsub.PeerScoreParams{
6680
DecayInterval: pubsub.DefaultDecayInterval,
6781
DecayToZero: pubsub.DefaultDecayToZero,

network/p2p/pubsub_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)