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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! # Retry Logic with Exponential Backoff
//!
//! Production-ready retry mechanisms for Bittensor operations with configurable
//! exponential backoff, jitter, and error-specific retry strategies.

use crate::error::{BittensorError, RetryConfig};
use std::future::Future;
use std::time::Duration;
use tokio::time::sleep;
use tracing::{debug, info, warn};

/// Exponential backoff calculator with jitter support
#[derive(Debug, Clone)]
pub struct ExponentialBackoff {
    config: RetryConfig,
    current_attempt: u32,
}

impl ExponentialBackoff {
    /// Creates a new exponential backoff instance
    pub fn new(config: RetryConfig) -> Self {
        Self {
            config,
            current_attempt: 0,
        }
    }

    /// Calculates the next delay duration
    pub fn next_delay(&mut self) -> Option<Duration> {
        if self.current_attempt >= self.config.max_attempts {
            return None;
        }

        let base_delay = self.config.initial_delay.as_millis() as f64;
        let multiplier = self
            .config
            .backoff_multiplier
            .powi(self.current_attempt as i32);
        let calculated_delay = Duration::from_millis((base_delay * multiplier) as u64);

        // Cap at max_delay
        let mut delay = if calculated_delay > self.config.max_delay {
            self.config.max_delay
        } else {
            calculated_delay
        };

        // Add jitter if enabled
        if self.config.jitter {
            delay = Self::add_jitter(delay);
        }

        self.current_attempt += 1;
        Some(delay)
    }

    /// Adds random jitter to prevent thundering herd
    fn add_jitter(delay: Duration) -> Duration {
        use rand::Rng;
        let jitter_ms = rand::thread_rng().gen_range(0..=delay.as_millis() as u64 / 4);
        delay + Duration::from_millis(jitter_ms)
    }

    /// Resets the backoff state
    pub fn reset(&mut self) {
        self.current_attempt = 0;
    }

    /// Gets the current attempt number
    pub fn attempts(&self) -> u32 {
        self.current_attempt
    }
}

/// Retry node with comprehensive error handling
pub struct RetryNode {
    total_timeout: Option<Duration>,
}

impl RetryNode {
    /// Creates a new retry node
    pub fn new() -> Self {
        Self {
            total_timeout: None,
        }
    }

    /// Sets a total timeout for all retry attempts
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.total_timeout = Some(timeout);
        self
    }

    /// Executes an operation with retry logic based on error types
    pub async fn execute<F, Fut, T>(&self, operation: F) -> Result<T, BittensorError>
    where
        F: Fn() -> Fut,
        Fut: Future<Output = Result<T, BittensorError>>,
    {
        let start_time = tokio::time::Instant::now();

        // First attempt without delay
        match operation().await {
            Ok(result) => Ok(result),
            Err(error) => {
                if !error.is_retryable() {
                    debug!("Error is not retryable: {:?}", error);
                    return Err(error);
                }

                let config = match error.retry_config() {
                    Some(config) => config,
                    None => {
                        debug!("No retry config for error: {:?}", error);
                        return Err(error);
                    }
                };

                info!(
                    "Starting retry for error category: {:?}, max_attempts: {}",
                    error.category(),
                    config.max_attempts
                );

                let mut backoff = ExponentialBackoff::new(config);
                let mut _last_error = error;

                // Retry loop
                while let Some(delay) = backoff.next_delay() {
                    // Check total timeout
                    if let Some(total_timeout) = self.total_timeout {
                        if start_time.elapsed() + delay >= total_timeout {
                            warn!(
                                "Total timeout reached after {} attempts",
                                backoff.attempts()
                            );
                            return Err(BittensorError::backoff_timeout(start_time.elapsed()));
                        }
                    }

                    debug!(
                        "Retry attempt {} after delay {:?}",
                        backoff.attempts(),
                        delay
                    );
                    sleep(delay).await;

                    match operation().await {
                        Ok(result) => {
                            info!("Operation succeeded after {} attempts", backoff.attempts());
                            return Ok(result);
                        }
                        Err(error) => {
                            _last_error = error;

                            // If error category changed, stop retrying
                            if !_last_error.is_retryable() {
                                debug!("Error became non-retryable: {:?}", _last_error);
                                return Err(_last_error);
                            }

                            warn!(
                                "Retry attempt {} failed: {}",
                                backoff.attempts(),
                                _last_error
                            );
                        }
                    }
                }

                warn!(
                    "All {} retry attempts exhausted, last error: {}",
                    backoff.config.max_attempts, _last_error
                );
                Err(BittensorError::max_retries_exceeded(
                    backoff.config.max_attempts,
                ))
            }
        }
    }

    /// Executes an operation with custom retry configuration
    pub async fn execute_with_config<F, Fut, T>(
        &self,
        operation: F,
        config: RetryConfig,
    ) -> Result<T, BittensorError>
    where
        F: Fn() -> Fut,
        Fut: Future<Output = Result<T, BittensorError>>,
    {
        let start_time = tokio::time::Instant::now();
        let mut backoff = ExponentialBackoff::new(config);

        // First attempt
        match operation().await {
            Ok(result) => Ok(result),
            Err(mut _last_error) => {
                info!(
                    "Starting custom retry, max_attempts: {}",
                    backoff.config.max_attempts
                );

                // Retry loop
                while let Some(delay) = backoff.next_delay() {
                    // Check total timeout
                    if let Some(total_timeout) = self.total_timeout {
                        if start_time.elapsed() + delay >= total_timeout {
                            warn!(
                                "Total timeout reached after {} attempts",
                                backoff.attempts()
                            );
                            return Err(BittensorError::backoff_timeout(start_time.elapsed()));
                        }
                    }

                    debug!(
                        "Custom retry attempt {} after delay {:?}",
                        backoff.attempts(),
                        delay
                    );
                    sleep(delay).await;

                    match operation().await {
                        Ok(result) => {
                            info!(
                                "Custom retry succeeded after {} attempts",
                                backoff.attempts()
                            );
                            return Ok(result);
                        }
                        Err(error) => {
                            _last_error = error;
                            warn!(
                                "Custom retry attempt {} failed: {}",
                                backoff.attempts(),
                                _last_error
                            );
                        }
                    }
                }

                Err(BittensorError::max_retries_exceeded(
                    backoff.config.max_attempts,
                ))
            }
        }
    }
}

impl Default for RetryNode {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience function for retrying operations with default settings
pub async fn retry_operation<F, Fut, T>(operation: F) -> Result<T, BittensorError>
where
    F: Fn() -> Fut,
    Fut: Future<Output = Result<T, BittensorError>>,
{
    RetryNode::new().execute(operation).await
}

/// Convenience function for retrying operations with timeout
pub async fn retry_operation_with_timeout<F, Fut, T>(
    operation: F,
    timeout: Duration,
) -> Result<T, BittensorError>
where
    F: Fn() -> Fut,
    Fut: Future<Output = Result<T, BittensorError>>,
{
    RetryNode::new()
        .with_timeout(timeout)
        .execute(operation)
        .await
}

/// Circuit breaker for preventing cascade failures
#[derive(Debug, Clone)]
pub struct CircuitBreaker {
    failure_threshold: u32,
    recovery_timeout: Duration,
    current_failures: u32,
    state: CircuitState,
    last_failure_time: Option<tokio::time::Instant>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CircuitState {
    Closed,   // Normal operation
    Open,     // Failing fast
    HalfOpen, // Testing recovery
}

impl CircuitBreaker {
    /// Creates a new circuit breaker
    pub fn new(failure_threshold: u32, recovery_timeout: Duration) -> Self {
        Self {
            failure_threshold,
            recovery_timeout,
            current_failures: 0,
            state: CircuitState::Closed,
            last_failure_time: None,
        }
    }

    /// Executes an operation through the circuit breaker
    pub async fn execute<F, Fut, T>(&mut self, operation: F) -> Result<T, BittensorError>
    where
        F: Fn() -> Fut,
        Fut: Future<Output = Result<T, BittensorError>>,
    {
        match self.state {
            CircuitState::Open => {
                if let Some(last_failure) = self.last_failure_time {
                    if last_failure.elapsed() >= self.recovery_timeout {
                        debug!("Circuit breaker transitioning to half-open");
                        self.state = CircuitState::HalfOpen;
                    } else {
                        return Err(BittensorError::ServiceUnavailable {
                            message: "Circuit breaker is open".to_string(),
                        });
                    }
                } else {
                    return Err(BittensorError::ServiceUnavailable {
                        message: "Circuit breaker is open".to_string(),
                    });
                }
            }
            CircuitState::Closed | CircuitState::HalfOpen => {}
        }

        match operation().await {
            Ok(result) => {
                // Success - reset circuit breaker
                if self.state == CircuitState::HalfOpen {
                    debug!("Circuit breaker recovering - closing circuit");
                    self.state = CircuitState::Closed;
                }
                self.current_failures = 0;
                self.last_failure_time = None;
                Ok(result)
            }
            Err(error) => {
                // Failure - update circuit breaker state
                self.current_failures += 1;
                self.last_failure_time = Some(tokio::time::Instant::now());

                if self.current_failures >= self.failure_threshold {
                    warn!(
                        "Circuit breaker opening after {} failures",
                        self.current_failures
                    );
                    self.state = CircuitState::Open;
                }

                Err(error)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU32, Ordering};
    use std::sync::Arc;

    #[test]
    fn test_exponential_backoff() {
        let config = RetryConfig {
            max_attempts: 3,
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(5),
            backoff_multiplier: 2.0,
            jitter: false,
        };

        let mut backoff = ExponentialBackoff::new(config);

        // First delay
        let delay1 = backoff.next_delay().unwrap();
        assert_eq!(delay1, Duration::from_millis(100));
        assert_eq!(backoff.attempts(), 1);

        // Second delay
        let delay2 = backoff.next_delay().unwrap();
        assert_eq!(delay2, Duration::from_millis(200));
        assert_eq!(backoff.attempts(), 2);

        // Third delay
        let delay3 = backoff.next_delay().unwrap();
        assert_eq!(delay3, Duration::from_millis(400));
        assert_eq!(backoff.attempts(), 3);

        // Should return None after max attempts
        assert!(backoff.next_delay().is_none());
    }

    #[tokio::test]
    async fn test_retry_node_success_after_failure() {
        let counter = Arc::new(AtomicU32::new(0));
        let counter_clone = counter.clone();

        let operation = move || {
            let counter = counter_clone.clone();
            async move {
                let count = counter.fetch_add(1, Ordering::SeqCst);
                if count < 2 {
                    Err(BittensorError::RpcConnectionError {
                        message: "Connection failed".to_string(),
                    })
                } else {
                    Ok("success")
                }
            }
        };

        let node = RetryNode::new();
        let result: Result<&str, BittensorError> = node.execute(operation).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_node_non_retryable_error() {
        let operation = || async {
            Err(BittensorError::InvalidHotkey {
                hotkey: "invalid".to_string(),
            })
        };

        let node = RetryNode::new();
        let result: Result<&str, BittensorError> = node.execute(operation).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BittensorError::InvalidHotkey { .. } => {}
            other => panic!("Expected InvalidHotkey, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn test_circuit_breaker() {
        let mut circuit_breaker = CircuitBreaker::new(2, Duration::from_millis(100));
        let counter = Arc::new(AtomicU32::new(0));

        // First failure
        let counter_clone = counter.clone();
        let result: Result<(), BittensorError> = circuit_breaker
            .execute(|| {
                let counter = counter_clone.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Err(BittensorError::RpcConnectionError {
                        message: "Connection failed".to_string(),
                    })
                }
            })
            .await;
        assert!(result.is_err());

        // Second failure - should open circuit
        let counter_clone = counter.clone();
        let result: Result<(), BittensorError> = circuit_breaker
            .execute(|| {
                let counter = counter_clone.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Err(BittensorError::RpcConnectionError {
                        message: "Connection failed".to_string(),
                    })
                }
            })
            .await;
        assert!(result.is_err());

        // Third call should fail fast without calling operation
        let counter_before = counter.load(Ordering::SeqCst);
        let result: Result<&str, BittensorError> = circuit_breaker
            .execute(|| {
                let counter = counter.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Ok("should not reach here")
                }
            })
            .await;
        assert!(result.is_err());
        assert_eq!(counter.load(Ordering::SeqCst), counter_before); // No increment

        match result.unwrap_err() {
            BittensorError::ServiceUnavailable { .. } => {}
            other => panic!("Expected ServiceUnavailable, got {other:?}"),
        }
    }
}