bittensor-rs 0.1.1

Standalone Rust SDK for Bittensor blockchain interactions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Integration tests for connection reliability improvements

use bittensor_rs::{
    config::BittensorConfig, BittensorError, ConnectionManager, ConnectionPool,
    ConnectionPoolBuilder, ConnectionState, HealthChecker, RetryConfig,
};
use std::sync::Arc;
use std::time::Duration;
use tokio::time::{sleep, timeout};

/// Helper function to create test config
fn test_config() -> BittensorConfig {
    BittensorConfig {
        wallet_name: "test".to_string(),
        hotkey_name: "test".to_string(),
        network: "test".to_string(),
        netuid: 1,
        chain_endpoint: Some("wss://test.invalid:443".to_string()),
        fallback_endpoints: vec![
            "wss://test2.invalid:443".to_string(),
            "wss://test3.invalid:443".to_string(),
        ],
        weight_interval_secs: 300,
        connection_pool_size: Some(3),
        health_check_interval: Some(Duration::from_millis(100)),
        circuit_breaker_threshold: Some(3),
        circuit_breaker_recovery: Some(Duration::from_secs(1)),
        read_only: false,
    }
}

#[tokio::test]
async fn test_connection_pool_initialization_failure_handling() {
    let config = test_config();
    let pool = ConnectionPoolBuilder::new(config.get_chain_endpoints())
        .max_connections(2)
        .retry_config(RetryConfig {
            max_attempts: 2,
            initial_delay: Duration::from_millis(10),
            max_delay: Duration::from_millis(50),
            backoff_multiplier: 2.0,
            jitter: false,
        })
        .build();

    // Initialization should fail with invalid endpoints
    let result = pool.initialize().await;
    assert!(result.is_err());

    match result {
        Err(BittensorError::NetworkError { .. }) => {
            // Expected error type
        }
        _ => panic!("Expected NetworkError"),
    }
}

#[tokio::test]
async fn test_connection_pool_fallback_behavior() {
    let endpoints = vec![
        "wss://invalid1.test:443".to_string(),
        "wss://invalid2.test:443".to_string(),
        "wss://invalid3.test:443".to_string(),
    ];

    let pool = ConnectionPool::new(endpoints, 3);

    // Should try to reconnect with backoff
    let result = timeout(Duration::from_secs(2), pool.get_healthy_client()).await;

    assert!(result.is_err() || result.unwrap().is_err());
}

#[tokio::test]
async fn test_health_checker_monitoring() {
    let pool = Arc::new(ConnectionPool::new(
        vec!["wss://test.invalid:443".to_string()],
        1,
    ));

    let checker = Arc::new(
        HealthChecker::new()
            .with_interval(Duration::from_millis(50))
            .with_timeout(Duration::from_millis(10))
            .with_failure_threshold(2),
    );

    // Start monitoring
    let handle = checker.clone().start_monitoring(pool.clone());

    // Let it run for a bit
    sleep(Duration::from_millis(200)).await;

    // Check metrics
    let metrics = checker.metrics();
    assert!(metrics.total_checks > 0);

    // Stop monitoring
    handle.abort();
}

#[tokio::test]
async fn test_connection_state_transitions() {
    let manager = ConnectionManager::new(test_config());

    // Initial state should be uninitialized
    let state = manager.get_state().await;
    assert!(matches!(state, ConnectionState::Uninitialized));

    // Attempting to get client should trigger connection attempt
    let _ = manager.get_client().await;

    // State should have changed
    let state = manager.get_state().await;
    assert!(!matches!(state, ConnectionState::Uninitialized));
}

#[tokio::test]
async fn test_connection_manager_metrics() {
    let manager = ConnectionManager::new(test_config());

    // Initial metrics
    let metrics = manager.metrics();
    assert_eq!(metrics.success_count, 0);
    assert_eq!(metrics.failure_count, 0);

    // Try to connect (will fail with invalid endpoint)
    let _ = manager.connect().await;

    // Metrics should be updated
    let metrics = manager.metrics();
    assert!(metrics.failure_count > 0 || metrics.success_count > 0);
}

#[tokio::test]
async fn test_retry_configuration() {
    let config = RetryConfig {
        max_attempts: 3,
        initial_delay: Duration::from_millis(10),
        max_delay: Duration::from_millis(100),
        backoff_multiplier: 2.0,
        jitter: false,
    };

    let pool = ConnectionPoolBuilder::new(vec!["wss://test.invalid:443".to_string()])
        .retry_config(config.clone())
        .max_connections(1)
        .build();

    let start = tokio::time::Instant::now();
    let _ = pool.reconnect_with_backoff().await;
    let elapsed = start.elapsed();

    // Should have attempted retries with backoff
    // 10ms + 20ms + 40ms = 70ms minimum
    assert!(elapsed >= Duration::from_millis(70));
}

#[tokio::test]
async fn test_concurrent_health_checks() {
    let pool = Arc::new(ConnectionPool::new(
        vec![
            "wss://test1.invalid:443".to_string(),
            "wss://test2.invalid:443".to_string(),
        ],
        2,
    ));

    let checker = HealthChecker::new().with_timeout(Duration::from_millis(10));

    // Run multiple health checks concurrently
    let handles: Vec<_> = (0..5)
        .map(|_| {
            let pool_clone = pool.clone();
            let _checker_clone = checker.clone();
            tokio::spawn(async move { pool_clone.healthy_connection_count().await })
        })
        .collect();

    let results = futures::future::join_all(handles).await;

    // All should complete without panic
    for result in results {
        assert!(result.is_ok());
    }
}

#[tokio::test]
async fn test_connection_state_status_messages() {
    let states = vec![
        ConnectionState::Uninitialized,
        ConnectionState::Connected {
            since: tokio::time::Instant::now(),
            endpoint: "wss://test:443".to_string(),
        },
        ConnectionState::Reconnecting {
            attempts: 3,
            since: tokio::time::Instant::now(),
            last_error: Some("timeout".to_string()),
        },
        ConnectionState::Failed {
            error: "connection refused".to_string(),
            at: tokio::time::Instant::now(),
            consecutive_failures: 5,
        },
    ];

    for state in states {
        let msg = state.status_message();
        assert!(!msg.is_empty());

        match &state {
            ConnectionState::Connected { .. } => assert!(msg.contains("Connected")),
            ConnectionState::Reconnecting { .. } => assert!(msg.contains("Reconnecting")),
            ConnectionState::Failed { .. } => assert!(msg.contains("Failed")),
            ConnectionState::Uninitialized => assert!(msg.contains("Not initialized")),
        }
    }
}

#[tokio::test]
async fn test_connection_pool_refresh() {
    let pool = ConnectionPool::new(vec!["wss://test.invalid:443".to_string()], 1);

    // Refresh should attempt re-initialization
    let result = pool.refresh_connections().await;
    assert!(result.is_err()); // Will fail with invalid endpoint

    // Total connections should still be 0
    assert_eq!(pool.total_connections().await, 0);
}

#[tokio::test]
async fn test_health_checker_failure_threshold() {
    let pool = Arc::new(ConnectionPool::new(
        vec!["wss://test.invalid:443".to_string()],
        1,
    ));

    let checker = Arc::new(
        HealthChecker::new()
            .with_interval(Duration::from_millis(50))
            .with_failure_threshold(2),
    );

    let handle = checker.clone().start_monitoring(pool);

    // Wait for multiple check cycles
    sleep(Duration::from_millis(200)).await;

    handle.abort();

    // Should have accumulated failures
    let metrics = checker.metrics();
    assert!(metrics.failed_checks > 0 || metrics.total_checks > 0);
}

#[tokio::test]
async fn test_connection_manager_force_reconnect() {
    let manager = ConnectionManager::new(test_config());

    // Force reconnect
    let result = manager.force_reconnect().await;
    assert!(result.is_err()); // Will fail with invalid endpoints

    // State should reflect attempt
    let state = manager.get_state().await;
    assert!(!matches!(state, ConnectionState::Uninitialized));
}

#[tokio::test]
async fn test_configuration_endpoint_deduplication() {
    let mut config = test_config();
    config.chain_endpoint = Some("wss://duplicate.test:443".to_string());
    config.fallback_endpoints = vec![
        "wss://duplicate.test:443".to_string(), // Duplicate
        "wss://unique.test:443".to_string(),
    ];

    let endpoints = config.get_chain_endpoints();

    // Should deduplicate
    assert_eq!(endpoints.len(), 2);
    assert!(endpoints.contains(&"wss://duplicate.test:443".to_string()));
    assert!(endpoints.contains(&"wss://unique.test:443".to_string()));
}

#[tokio::test]
async fn test_health_checker_reset_metrics() {
    let checker = HealthChecker::new();

    // Simulate some metrics
    for _ in 0..5 {
        checker
            .metrics
            .total_checks
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        checker
            .metrics
            .successful_checks
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }

    let metrics = checker.metrics();
    assert_eq!(metrics.total_checks, 5);
    assert_eq!(metrics.successful_checks, 5);

    // Reset
    checker.reset_metrics();

    let metrics = checker.metrics();
    assert_eq!(metrics.total_checks, 0);
    assert_eq!(metrics.successful_checks, 0);
}

#[tokio::test]
async fn test_connection_pool_builder_configuration() {
    let custom_checker = HealthChecker::new()
        .with_interval(Duration::from_secs(30))
        .with_timeout(Duration::from_secs(10));

    let custom_retry = RetryConfig {
        max_attempts: 10,
        initial_delay: Duration::from_secs(1),
        max_delay: Duration::from_secs(60),
        backoff_multiplier: 3.0,
        jitter: true,
    };

    let pool = ConnectionPoolBuilder::new(vec!["wss://test:443".to_string()])
        .max_connections(5)
        .health_checker(custom_checker)
        .retry_config(custom_retry.clone())
        .build();

    assert_eq!(pool.max_connections, 5);
    assert_eq!(pool.retry_config.max_attempts, 10);
}

#[tokio::test]
async fn test_edge_case_empty_endpoints() {
    let pool = ConnectionPool::new(vec![], 3);
    let result = pool.initialize().await;

    assert!(result.is_err());
    if let Err(BittensorError::ConfigError { field, .. }) = result {
        assert_eq!(field, "endpoints");
    } else {
        panic!("Expected ConfigError for empty endpoints");
    }
}

#[tokio::test]
async fn test_edge_case_zero_max_connections() {
    let pool = ConnectionPool::new(vec!["wss://test:443".to_string()], 0);

    // Should handle gracefully
    let count = pool.healthy_connection_count().await;
    assert_eq!(count, 0);
}

#[tokio::test]
async fn test_concurrent_connection_attempts() {
    let manager = Arc::new(ConnectionManager::new(test_config()));

    // Multiple threads trying to connect simultaneously
    let handles: Vec<_> = (0..5)
        .map(|_| {
            let manager_clone = manager.clone();
            tokio::spawn(async move { manager_clone.get_client().await })
        })
        .collect();

    let results = futures::future::join_all(handles).await;

    // All should complete without panic
    for result in results {
        assert!(result.is_ok()); // Task completed
                                 // The actual connection will fail due to invalid endpoints
    }
}

#[tokio::test]
async fn test_connection_manager_consecutive_failures() {
    let mut manager = ConnectionManager::new(test_config());
    manager.max_consecutive_failures = 2;

    // Simulate failures
    manager
        .update_state(ConnectionState::Failed {
            error: "test".to_string(),
            at: tokio::time::Instant::now(),
            consecutive_failures: 3,
        })
        .await;

    // Should fail due to exceeding max consecutive failures
    let result = manager.reconnect_with_backoff().await;
    assert!(result.is_err());
}

// Performance test - ensure connection pool doesn't leak memory
#[tokio::test]
#[ignore] // Run with --ignored for performance tests
async fn test_connection_pool_memory_stability() {
    let pool = Arc::new(ConnectionPool::new(
        vec!["wss://test.invalid:443".to_string()],
        1,
    ));

    // Run many health checks
    for _ in 0..1000 {
        let _ = pool.healthy_connection_count().await;
        // Small delay to avoid overwhelming
        sleep(Duration::from_micros(100)).await;
    }

    // Memory should be stable (manual verification needed)
}