Class PeerStore

java.lang.Object
com.privatechain.network.peer.PeerStore

public final class PeerStore extends Object
Thread-safe in-memory store for known Peer instances.

PeerStore acts as the single source of truth for the peer list maintained by PeerManager. It provides atomic put/get/remove operations and supports snapshot queries. In a production deployment this class would back its map with a persistent store (e.g., a small embedded JSON file or LevelDB entry) so that the node can reconnect to previously known peers after a restart without relying solely on seed peers.

Thread safety

All methods are safe for concurrent use. Reads and writes are serialized via ConcurrentHashMap which provides lock-free reads and striped-lock writes.

Key design decision

Peers are keyed by nodeId (not by host:port) because a peer's IP address can change on reconnect while its cryptographic identity stays constant. This ensures that a reconnected peer replaces its stale entry rather than creating a duplicate.

Since:
1.0.0
See Also:
  • Constructor Details

    • PeerStore

      public PeerStore()
  • Method Details

    • put

      public void put(Peer peer)
      Inserts or replaces a peer entry keyed by Peer.getNodeId().

      If a peer with the same node ID already exists (e.g., reconnecting on a new IP address), the old entry is overwritten atomically.

      Parameters:
      peer - the peer to store (non-null)
      Throws:
      NullPointerException - if peer is null
    • remove

      public void remove(String nodeId)
      Removes a peer from the store by node ID.

      No-op if the node ID is not present.

      Parameters:
      nodeId - the node ID to remove (non-null, non-blank)
      Throws:
      NullPointerException - if nodeId is null
      IllegalArgumentException - if nodeId is blank
    • clear

      public void clear()
      Removes all peers from the store.
    • get

      public Optional<Peer> get(String nodeId)
      Returns the peer with the given node ID, or Optional.empty() if not found.
      Parameters:
      nodeId - the node ID to look up (non-null, non-blank)
      Returns:
      optional peer
      Throws:
      NullPointerException - if nodeId is null
      IllegalArgumentException - if nodeId is blank
    • contains

      public boolean contains(String nodeId)
      Returns true if the store contains a peer with the given node ID.
      Parameters:
      nodeId - the node ID to check (non-null, non-blank)
      Returns:
      true if present
    • getAll

      public List<Peer> getAll()
      Returns a point-in-time snapshot of all known peers.

      Modifications to the returned list do not affect the store. Peers are returned in an unspecified order; callers that need a deterministic order should sort the list themselves.

      Returns:
      unmodifiable list of all current peers
    • getAll

      public List<Peer> getAll(Collection<String> nodeIds)
      Returns a snapshot of all peers whose node ID is in the given collection.
      Parameters:
      nodeIds - the set of node IDs to include (non-null)
      Returns:
      unmodifiable list of matched peers
    • size

      public int size()
      Returns the total number of peers currently stored.
      Returns:
      peer count (≥ 0)
    • isEmpty

      public boolean isEmpty()
      Returns true if the store contains no peers.
      Returns:
      true if empty
    • toString

      public String toString()
      Returns a human-readable summary of the peer store (for logging).
      Overrides:
      toString in class Object
      Returns:
      string representation