Skip to content

Conversation

GheisMohammadi
Copy link
Collaborator

Summary

This PR addresses the critical issue where stream sync takes ~15 minutes to find enough peers on node startup, while restarting the service allows much faster peer discovery. The solution implements a comprehensive peer discovery optimization strategy with startup mode, network-aware limits, and improved timing algorithms.

Problem Statement

Issue: Stream sync on node startup takes approximately 15 minutes to find minimum streams and begin syncing, while restarting the service enables much faster peer discovery.

Root Causes:

  • 30-second discovery intervals combined with cooldowns create long delays
  • Hardcoded peer discovery limits not optimized for different networks
  • No early exit mechanism when sufficient peers are found
  • Inefficient timing algorithms with aggressive backoff

Solution Overview

1. Startup Mode Implementation

  • Faster Initial Discovery: Reduced timeouts and intervals during first 10 minutes
  • Early Exit: Automatically exits startup mode when enough streams are found
  • Adaptive Timing: Different timing strategies for startup vs normal operation

2. Network-Aware Peer Discovery

  • Configuration-Aligned: Target valid peers match actual stream sync requirements
  • Network-Specific Limits: Different DHT request limits per network type
  • Realistic Expectations: Success rates based on actual DHT behavior

3. Improved Timing Algorithms

  • Centralized Constants: All timing values moved to const.go
  • Adaptive Sleep: Increase sleep when peers found, decrease when none found
  • Better Backoff: 30% decrease instead of aggressive /= 2

Configuration Alignment

Network InitStreams Target Valid DHT Request Success Rate
Mainnet 8 8 20 40%
Testnet 3 3 10 30%
Pangaea 3 3 10 30%
Partner 3 3 10 30%
Stressnet 4 4 12 33%
Devnet 4 4 12 33%
Localnet 4 4 12 33%

@GheisMohammadi GheisMohammadi self-assigned this Aug 8, 2025
@GheisMohammadi GheisMohammadi changed the title Optimize Stream Sync Peer Discovery: Reduce Startup Time from 15 Minutes to Seconds Optimize Stream Sync Peer Discovery: Reduce Startup Time to Seconds Aug 8, 2025
@GheisMohammadi GheisMohammadi marked this pull request as draft August 8, 2025 02:13
- Add SetEnoughStreamsCallback method to Operator interface
- Implement SetEnoughStreamsCallback in streamManager struct
- Add enoughStreamsCallback field to streamManager
- Call callback when softHaveEnoughStreams() is true
- Enables early exit from startup mode when enough streams are found
- Add startup mode with faster timings for initial peer discovery
- Centralize all timing constants in const.go
- Add network-specific DHT request limits and target valid peer counts
- Align target valid peers with stream sync configuration requirements
- Improve adaptive sleep logic with SleepDecreaseRatio
- Add comprehensive timing constants for startup vs normal modes
- Add startup mode with faster timings for initial peer discovery
- Implement early exit from startup mode when enough streams found
- Add network-specific peer discovery limits and validation
- Improve advertise() function with better error handling and logging
- Add isValidPeer() stub for future peer validation logic
- Use configurable DHT request limits instead of hardcoded values
- Add target valid peer counting and filtering
- Improve advertiseLoop() with adaptive timing based on discovery success
- Add TestProtocol_Advertise with mock discovery and proper setup
- Add TestProtocol_AdvertiseLoop with timing validation
- Add TestProtocol_ExitStartupMode for startup mode functionality
- Add TestProtocol_GetDHTRequestLimit for network-specific limits
- Add TestProtocol_GetTargetValidPeers for target peer validation
- Fix interface compilation errors by adding SetEnoughStreamsCallback stubs
- Add mockDiscovery struct to properly mock discovery interface
- Update tests to handle nil pointer scenarios and proper initialization
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libp2p Peer to Peer networking p2p_stream stream-sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant