diff --git a/p2p/discovery.go b/p2p/discovery.go index 8c31d72..01e0793 100644 --- a/p2p/discovery.go +++ b/p2p/discovery.go @@ -47,11 +47,14 @@ func NewPeerDiscovery(h host.Host, bootstrapPeers []peer.AddrInfo) *PeerDiscover // Start begins the discovery process func (pd *PeerDiscovery) Start(ctx context.Context) { - pd.logger.Info().Msgf("Starting peer discovery with bootstrap peers: %v", pd.bootstrapPeers) + pd.logger.Info().Msg("Starting peer discovery with bootstrap peers") // Connect to bootstrap peers first for _, pinfo := range pd.bootstrapPeers { if err := pd.host.Connect(ctx, pinfo); err != nil { - pd.logger.Error().Err(err).Msgf("Failed to connect to bootstrap peer %s", pinfo.ID) + pd.logger.Error().Err(err). + Stringer("bootstrap_peer_id", pinfo.ID). + Stringer("bootstrap_peer_info", pinfo). + Msgf("Failed to connect to bootstrap peer") continue } pd.addPeer(pinfo) @@ -101,7 +104,9 @@ func (pd *PeerDiscovery) GetPeers() []peer.AddrInfo { // handleDiscovery handles incoming discovery streams func (pd *PeerDiscovery) handleDiscovery(s network.Stream) { - pd.logger.Debug().Msgf("Received discovery stream from %s", s.Conn().RemotePeer()) + pd.logger.Debug(). + Stringer("from_peer", s.Conn().RemotePeer()). + Msgf("Received discovery stream") defer s.Close() ma := s.Conn().RemoteMultiaddr() @@ -137,7 +142,7 @@ func (pd *PeerDiscovery) startGossip(ctx context.Context) { pd.logger.Info().Msg("Peer discovery stopped") return } - pd.logger.Warn().Msgf("Should not receive from closed channel!") + pd.logger.Warn().Msg("Should not receive from closed channel!") case <-ctx.Done(): return case <-ticker.C: @@ -149,7 +154,9 @@ func (pd *PeerDiscovery) startGossip(ctx context.Context) { func (pd *PeerDiscovery) gossipPeers(ctx context.Context) { pd.logger.Debug().Msgf("Gossiping known peers") peers := pd.GetPeers() - pd.logger.Debug().Msgf("current peers: %v", peers) + pd.logger.Debug(). + Array("peers", zerolog.Arr().Interface(peers)). + Msgf("current peers") ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() @@ -169,26 +176,36 @@ func (pd *PeerDiscovery) gossipPeers(ctx context.Context) { err := pd.host.Connect(ctx, p) if err != nil { - pd.logger.Error().Err(err).Msgf("Failed to connect to peer %s", p) + pd.logger.Error().Err(err). + Stringer("to", p.ID). + Msg("Failed to connect to peer") return } - pd.logger.Debug().Msgf("Connected to peer %s", p) + pd.logger.Debug(). + Stringer("to", p). + Msg("Connected to peer") // Open discovery stream s, err := pd.host.NewStream(ctx, p.ID, DiscoveryProtocol) if err != nil { - pd.logger.Error().Err(err).Msgf("Failed to open discovery stream to %s", p) + pd.logger.Error().Err(err). + Stringer("to", p). + Msg("Failed to open discovery stream to peer") return } defer s.Close() - pd.logger.Debug().Msgf("Opened discovery stream to %s", p) + pd.logger.Debug(). + Stringer("to", p). + Msg("Opened discovery stream to peer") // Read peer info from stream // This is a simplified example - implement proper serialization limitedReader := io.LimitReader(s, 1<<20) // Limit to 1MB buf, err := io.ReadAll(limitedReader) if err != nil { - pd.logger.Error().Err(err).Msgf("Failed to read from stream") + pd.logger.Error().Err(err). + Stringer("from", p). + Msg("Failed to read from stream") return } pd.logger.Debug().Msgf("Received peer data: %s", string(buf)) @@ -197,11 +214,15 @@ func (pd *PeerDiscovery) gossipPeers(ctx context.Context) { var recvPeers []peer.AddrInfo err = json.Unmarshal(buf, &recvPeers) if err != nil { - pd.logger.Error().Err(err).Msgf("Failed to unmarshal peer data") + pd.logger.Error().Err(err). + Stringer("from", p). + Msg("Failed to unmarshal peer data received") return } for _, p := range recvPeers { - pd.logger.Debug().Msgf("Adding peer %s", p) + pd.logger.Debug(). + Stringer("peer", p). + Msg("Adding peer") pd.addPeer(p) } }(p)