Skip to content

Conversation

@s-h-ubham
Copy link
Contributor

Title

network/server: persist only active peers to last_peers.json; server: load custom bootnodes from genesis params and merge with external sources

Context

Two related issues were observed in production-like setups:

  • Some nodes occasionally persisted inactive peers to last_peers.json, causing them to try reconnecting to dead addresses after restart.
  • For custom networks, even if params.bootnodes were present in genesis.json, the node required an adjacent bootnode.json or --bootnodes file to start. Operators expected params.bootnodes in custom genesis to be honored, similar to mainnet/testnet.

Changes

  1. Persist only active peers to last_peers.json
  • Problem: Peers() returned all entries in the in-memory s.peers map without checking whether they’re still connected. During shutdown or disconnect races, stale entries could be written to last_peers.json.
  • Fix: Peers() now filters by live connectedness (network.Connected) before returning the list used by Close() for persistence.
  • Impact: last_peers.json now contains only peers confirmed to be connected at persistence time, preventing invalid reconnect attempts after restart.
  1. Load bootnodes from custom genesis params and merge
  • Previous behavior:
    • mainnet/testnet: bootnodes loaded from the embedded chain config (works).
    • custom: ignored params.bootnodes in genesis.json; only looked for adjacent bootnode.json (node.p2p.staticNodes) or --bootnodes <file>.
  • New behavior for custom networks:
    • Also read genesis.params.bootnodes (if present).
    • Merge order (deduped):
      • genesis.params.bootnodes
      • adjacent bootnode.json (node.p2p.staticNodes)
      • --bootnodes <file> with schema { "bootnodes": ["..."] }
    • If, after merging, no bootnodes are found, the error message now clearly suggests all accepted sources.
  • Impact: Operators can embed bootnodes directly in custom genesis.json without needing a separate file or flag. External sources still work and are merged.

Files Touched

  • network/server.go
    • Filter peers by live connectedness in Peers() before persisting.
  • command/server/init.go
    • In getBootnodeConfig(), for custom networks, include genesis.Params.Bootnodes and merge with adjacent bootnode.json and --bootnodes file. Improved error message.

Why This Is Safe

  • The connectedness check is a fast state lookup; locking semantics unchanged.
  • Bootnode merging is additive and deduplicated. Existing mainnet/testnet behavior is preserved.
  • Error messaging now guides operators to all valid sources.

Testing

  • Peers persistence:
    • Start 5 nodes; establish connections.
    • Stop a peer; quickly stop another node to induce a tight window.
    • Inspect libp2p/last_peers.json on the stopped node:
      • Before: could include the disconnected peer.
      • After: contains only peers with Connected state at the time of persistence.
    • Restart using last_peers.json and verify successful reconnects.
  • Bootnodes loading (custom):
    • Case A: only params.bootnodes in genesis.json → node starts and dials bootnodes.
    • Case B: only adjacent bootnode.json (staticNodes) → node starts and dials bootnodes.
    • Case C: both → merged set (no duplicates) is used.
    • Case D: --bootnodes file provided → merged with above (no duplicates).
    • Case E: none available → clear error instructs accepted sources.

Backward Compatibility

  • No breaking flags or config changes.
  • mainnet/testnet flows unchanged.
  • Custom networks gain support for params.bootnodes while keeping existing external file/flag paths.

Operational Guidance

  • For custom networks, you can now:
    • Embed bootnodes in genesis.json under params.bootnodes, and/or
    • Keep using adjacent bootnode.json and/or --bootnodes <file> at runtime.
  • For built-in networks (mainnet/testnet), continue using the embedded list, optionally extending via --bootnodes <file> if needed.

@s-h-ubham s-h-ubham requested a review from R-Santev October 6, 2025 13:26
@s-h-ubham s-h-ubham force-pushed the fix/bootnode-discovery branch from ed7011e to 3c35a01 Compare October 7, 2025 07:10
@s-h-ubham s-h-ubham merged commit 18da7d5 into develop Oct 7, 2025
4 checks passed
@s-h-ubham s-h-ubham deleted the fix/bootnode-discovery branch October 9, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants