moonproof-node-client 0.2.1

Rust client for the Midnight node's Substrate submission RPC
Documentation
//! Configuration for the Midnight node client.
//!
//! Mirrors the TS `Config` from `PolkadotNodeClient.ts`: a node URL plus
//! reconnection timing. Defaults match the TS reference so local-dev behavior
//! stays aligned across runtimes.

use std::time::Duration;
use url::Url;

/// Configuration for constructing a [`crate::NodeClient`].
///
/// The only required field is the node URL; everything else has sensible
/// defaults via [`Config::new`].
#[derive(Debug, Clone)]
pub struct Config {
    /// Node WebSocket URL, e.g. `ws://127.0.0.1:9944`. Must use the `ws://`
    /// or `wss://` scheme — plain HTTP does not support subscriptions.
    pub node_url: Url,

    /// Total time the client will keep retrying to reconnect before giving
    /// up. TS default is `Duration.infinity`; Rust default is a very large
    /// but finite value (~293 years) so tests can enforce a ceiling.
    pub reconnection_timeout: Duration,

    /// Delay between consecutive reconnection attempts. TS default is 1s.
    pub reconnection_delay: Duration,
}

impl Config {
    /// Build a config from just a node URL, using the default reconnection
    /// schedule (matches the TS `DEFAULT_CONFIG`: 1s delay, unbounded timeout).
    #[must_use]
    pub fn new(node_url: Url) -> Self {
        Self {
            node_url,
            reconnection_timeout: Duration::MAX,
            reconnection_delay: Duration::from_secs(1),
        }
    }

    /// Override the reconnection timeout.
    #[must_use]
    pub fn with_reconnection_timeout(mut self, timeout: Duration) -> Self {
        self.reconnection_timeout = timeout;
        self
    }

    /// Override the reconnection delay.
    #[must_use]
    pub fn with_reconnection_delay(mut self, delay: Duration) -> Self {
        self.reconnection_delay = delay;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_populates_url_and_defaults_match_ts_reference() {
        let url = Url::parse("ws://127.0.0.1:9944").unwrap();
        let cfg = Config::new(url.clone());
        assert_eq!(cfg.node_url, url);
        assert_eq!(cfg.reconnection_delay, Duration::from_secs(1));
        // TS uses `Duration.infinity`; we use Duration::MAX as the closest finite analog.
        assert_eq!(cfg.reconnection_timeout, Duration::MAX);
    }

    #[test]
    fn builder_overrides_apply_in_isolation() {
        let url = Url::parse("wss://node.example:443").unwrap();
        let cfg = Config::new(url)
            .with_reconnection_delay(Duration::from_millis(250))
            .with_reconnection_timeout(Duration::from_secs(30));
        assert_eq!(cfg.reconnection_delay, Duration::from_millis(250));
        assert_eq!(cfg.reconnection_timeout, Duration::from_secs(30));
    }
}