Skip to content

fix(discovery): replace panics in config validation with typed errors #1529

@viktorcrypt

Description

@viktorcrypt

Summary

Two locations in the discovery crate currently use panic! to signal invalid configuration, which causes the entire process to crash on misconfiguration instead of returning a recoverable error.

Locations

1. code/crates/discovery/src/config.rs:107
DiscoveryConfig::set_peers_bounds panics if num_inbound_peers < num_outbound_peers.

2. code/crates/discovery/src/handlers/selection/selector.rs:28
make_selector panics if Selector::Kademlia is used with a non-Kademlia
bootstrap protocol.

Proposed Fix

  • Introduce a DiscoveryConfigError enum (or extend an existing error type)
    with variants for each invalid case
  • Change set_peers_bounds to return Result<(), DiscoveryConfigError>
  • Change make_selector (or its caller) to return Result and propagate
    the error up to the initialization boundary

This follows the pattern already used elsewhere in the codebase and aligns with the library's goal of being embeddable — callers should be able to
handle configuration errors gracefully without crashing.

Impact

Any application embedding Malachite that passes invalid discovery config will crash unconditionally. Returning a typed error allows callers to
validate config early and handle mistakes gracefully.

Note

I'd be happy to work on this if you're open to a contribution. Please assign this to me if so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    need-triageThis issue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions