Skip to content

Commit

Permalink
use structured log if appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Oct 29, 2024
1 parent dae1f21 commit 694f4b4
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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))
Expand All @@ -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)
Expand Down

0 comments on commit 694f4b4

Please sign in to comment.