Skip to content
Open
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion go-peer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func main() {
nickFlag := flag.String("nick", "", "nickname to use in chat. will be generated if empty")
idPath := flag.String("identity", "identity.key", "path to the private key (PeerID) file")
headless := flag.Bool("headless", false, "run without chat UI")
bootstrapper := flag.Bool("bootstrapper", false, "run as a bootstrap peer")

var directPeers stringSlice
flag.Var(&directPeers, "directpeer", "reciprocal gossipsub bootstrap peers (can be used multiple times)")

var addrsToConnectTo stringSlice
flag.Var(&addrsToConnectTo, "connect", "address to connect to (can be used multiple times)")
Expand Down Expand Up @@ -198,8 +202,28 @@ func main() {
panic(err)
}

gossipSubOpts := []pubsub.Option{}

if *bootstrapper {
gossipSubOpts = append(gossipSubOpts, pubsub.WithPeerExchange(true))

if len(directPeers) > 0 {
dp := peerStrSliceToAddrInfoSlice(directPeers)
gossipSubOpts = append(gossipSubOpts, pubsub.WithDirectPeers(dp))
pubsub.WithDirectConnectTicks(60) // attempt to reconnect to direct peers every 60 ticks (seconds)
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pubsub.WithDirectConnectTicks(60) option is not being appended to gossipSubOpts. It should be added to the slice like the other options.

Suggested change
pubsub.WithDirectConnectTicks(60) // attempt to reconnect to direct peers every 60 ticks (seconds)
gossipSubOpts = append(gossipSubOpts, pubsub.WithDirectConnectTicks(60)) // attempt to reconnect to direct peers every 60 ticks (seconds)

Copilot uses AI. Check for mistakes.
}

pubsub.WithFloodPublish(true)

// See https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#recommendations-for-network-operators
pubsub.GossipSubD = 0
pubsub.GossipSubDlo = 0
pubsub.GossipSubDhi = 0
pubsub.GossipSubDout = 0
}

// create a new PubSub service using the GossipSub router
ps, err := pubsub.NewGossipSub(ctx, h)
ps, err := pubsub.NewGossipSub(ctx, h, gossipSubOpts...)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -304,6 +328,24 @@ func main() {
}
}

func peerStrSliceToAddrInfoSlice(peerStrs []string) []peer.AddrInfo {
var addrInfos []peer.AddrInfo

if len(peerStrs) > 0 {
for _, addr := range peerStrs {
peerInfo, err := peer.AddrInfoFromString(addr)
if err != nil {
LogMsgf("Failed to parse multiaddr: %s", err.Error())
continue
}

addrInfos = append(addrInfos, *peerInfo)
}
}

return addrInfos
}

// printErr is like fmt.Printf, but writes to stderr.
func printErr(m string, args ...interface{}) {
fmt.Fprintf(os.Stderr, m, args...)
Expand Down
Loading