bsv-wallet-toolbox 0.2.23

Pure Rust BSV wallet-toolbox implementation
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
//! WebSocket-based Live Header Ingestor
//!
//! Connects to WhatsOnChain WebSocket for real-time block header updates.
//! Based on TypeScript: `wallet-toolbox/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainWs.ts`

use async_trait::async_trait;
use serde::Deserialize;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::{broadcast, Mutex, RwLock};
use tracing::{debug, error, info, warn};

use super::{
    ws_header_to_block_header, WocWsBlockHeader, WOC_API_URL_MAIN, WOC_API_URL_TEST,
    WOC_WS_URL_MAIN, WOC_WS_URL_TEST,
};
use crate::chaintracks::{BlockHeader, ChaintracksStorageIngest, LiveBlockHeader, LiveIngestor};
use crate::error::{WalletError, WalletResult};
use crate::types::Chain;

// ---------------------------------------------------------------------------
// History URL constants (not in mod.rs)
// ---------------------------------------------------------------------------

/// WebSocket URL for historical headers (mainnet)
#[allow(dead_code)]
pub const WOC_WS_HISTORY_URL_MAIN: &str =
    "wss://socket-v2.whatsonchain.com/websocket/blockheaders/history";

/// WebSocket URL for historical headers (testnet)
#[allow(dead_code)]
pub const WOC_WS_HISTORY_URL_TEST: &str =
    "wss://socket-v2-testnet.whatsonchain.com/websocket/blockheaders/history";

// ---------------------------------------------------------------------------
// WsError
// ---------------------------------------------------------------------------

/// Error types specific to WebSocket operations
#[derive(Debug)]
#[allow(dead_code)]
pub enum WsError {
    ConnectionFailed(String),
    MessageParseFailed(String),
    IdleTimeout,
    Stopped,
}

impl std::fmt::Display for WsError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WsError::ConnectionFailed(msg) => write!(f, "WebSocket connection failed: {}", msg),
            WsError::MessageParseFailed(msg) => write!(f, "Failed to parse message: {}", msg),
            WsError::IdleTimeout => write!(f, "WebSocket idle timeout"),
            WsError::Stopped => write!(f, "WebSocket stopped"),
        }
    }
}

// ---------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------

/// Options for WebSocket live ingestor
#[derive(Debug, Clone)]
pub struct LiveWebSocketOptions {
    /// Chain to monitor
    pub chain: Chain,
    /// API key (optional)
    pub api_key: Option<String>,
    /// Idle timeout in milliseconds before reconnecting
    pub idle_timeout_ms: u64,
    /// Ping interval in milliseconds
    pub ping_interval_ms: u64,
    /// Maximum reconnection attempts
    pub max_reconnect_attempts: u32,
    /// Reconnect delay in milliseconds
    pub reconnect_delay_ms: u64,
    /// User agent for HTTP requests (fallback)
    pub user_agent: String,
    /// HTTP timeout for fallback requests
    pub http_timeout_secs: u64,
}

impl Default for LiveWebSocketOptions {
    fn default() -> Self {
        LiveWebSocketOptions {
            chain: Chain::Main,
            api_key: None,
            idle_timeout_ms: 100_000,
            ping_interval_ms: 10_000,
            max_reconnect_attempts: 10,
            reconnect_delay_ms: 5000,
            user_agent: "BsvWalletToolbox/1.0".to_string(),
            http_timeout_secs: 30,
        }
    }
}

impl LiveWebSocketOptions {
    /// Create options for mainnet
    pub fn mainnet() -> Self {
        Self::default()
    }

    /// Create options for testnet
    pub fn testnet() -> Self {
        LiveWebSocketOptions {
            chain: Chain::Test,
            ..Default::default()
        }
    }

    /// Set API key
    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// Set idle timeout
    pub fn with_idle_timeout(mut self, ms: u64) -> Self {
        self.idle_timeout_ms = ms;
        self
    }
}

// ---------------------------------------------------------------------------
// Ingestor struct
// ---------------------------------------------------------------------------

/// WebSocket-based live header ingestor
///
/// Connects to WhatsOnChain WebSocket for real-time block notifications.
/// Provides lower latency than polling but requires persistent connection.
pub struct LiveWebSocketIngestor {
    options: LiveWebSocketOptions,
    http_client: reqwest::Client,
    storage: Arc<RwLock<Option<Arc<dyn ChaintracksStorageIngest>>>>,

    /// Whether the ingestor should be running
    running: Arc<AtomicBool>,

    /// Broadcast channel for new headers
    sender: broadcast::Sender<LiveBlockHeader>,

    /// Error count for monitoring
    error_count: Arc<RwLock<Vec<(i32, String)>>>,

    /// Stop signal sender
    stop_signal: Arc<Mutex<Option<tokio::sync::oneshot::Sender<()>>>>,
}

impl LiveWebSocketIngestor {
    /// Create a new WebSocket live ingestor
    pub fn new(options: LiveWebSocketOptions) -> WalletResult<Self> {
        let http_client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(options.http_timeout_secs))
            .user_agent(&options.user_agent)
            .build()
            .map_err(|e| WalletError::Internal(e.to_string()))?;

        let (sender, _) = broadcast::channel(100);

        Ok(LiveWebSocketIngestor {
            options,
            http_client,
            storage: Arc::new(RwLock::new(None)),
            running: Arc::new(AtomicBool::new(false)),
            sender,
            error_count: Arc::new(RwLock::new(Vec::new())),
            stop_signal: Arc::new(Mutex::new(None)),
        })
    }

    /// Create a default mainnet ingestor
    pub fn mainnet() -> WalletResult<Self> {
        Self::new(LiveWebSocketOptions::mainnet())
    }

    /// Create a default testnet ingestor
    pub fn testnet() -> WalletResult<Self> {
        Self::new(LiveWebSocketOptions::testnet())
    }

    /// Get WebSocket URL for the configured chain
    fn ws_url(&self) -> &'static str {
        match self.options.chain {
            Chain::Main => WOC_WS_URL_MAIN,
            Chain::Test => WOC_WS_URL_TEST,
        }
    }

    /// Get REST API URL for the configured chain
    fn api_url(&self) -> &'static str {
        match self.options.chain {
            Chain::Main => WOC_API_URL_MAIN,
            Chain::Test => WOC_API_URL_TEST,
        }
    }

    /// Fetch a header by hash via HTTP (fallback)
    async fn fetch_header_by_hash_http(&self, hash: &str) -> WalletResult<Option<BlockHeader>> {
        let url = format!("{}/block/{}/header", self.api_url(), hash);
        debug!("Fetching header by hash via HTTP: {}", hash);

        let response = self
            .http_client
            .get(&url)
            .send()
            .await
            .map_err(|e| WalletError::NetworkChain(e.to_string()))?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        }

        if !response.status().is_success() {
            return Err(WalletError::NetworkChain(format!(
                "WOC header lookup returned status {}",
                response.status()
            )));
        }

        #[derive(Deserialize)]
        struct WocHeaderResp {
            hash: String,
            height: u32,
            version: u32,
            merkleroot: String,
            time: u32,
            bits: String,
            nonce: u32,
            previousblockhash: Option<String>,
        }

        let woc: WocHeaderResp = response
            .json()
            .await
            .map_err(|e| WalletError::Internal(e.to_string()))?;
        let bits = u32::from_str_radix(&woc.bits, 16).unwrap_or(0);

        Ok(Some(BlockHeader {
            version: woc.version,
            previous_hash: woc.previousblockhash.unwrap_or_else(|| "0".repeat(64)),
            merkle_root: woc.merkleroot,
            time: woc.time,
            bits,
            nonce: woc.nonce,
            height: woc.height,
            hash: woc.hash,
        }))
    }

    /// Run the WebSocket listener loop with reconnection
    async fn websocket_loop(self: Arc<Self>, live_headers: Arc<RwLock<Vec<BlockHeader>>>) {
        info!("Starting WebSocket listener for {:?}", self.options.chain);

        let mut reconnect_attempts = 0;

        while self.running.load(Ordering::SeqCst) {
            match self.connect_and_listen(&live_headers).await {
                Ok(true) => {
                    // Normal shutdown requested
                    info!("WebSocket listener stopped normally");
                    break;
                }
                Ok(false) => {
                    // Connection lost, try to reconnect
                    reconnect_attempts += 1;
                    if reconnect_attempts > self.options.max_reconnect_attempts {
                        error!("Max reconnection attempts reached");
                        break;
                    }

                    warn!(
                        "WebSocket connection lost, reconnecting (attempt {})",
                        reconnect_attempts
                    );
                    tokio::time::sleep(tokio::time::Duration::from_millis(
                        self.options.reconnect_delay_ms,
                    ))
                    .await;
                }
                Err(e) => {
                    reconnect_attempts += 1;
                    error!(
                        "WebSocket error: {}, reconnecting (attempt {})",
                        e, reconnect_attempts
                    );

                    if reconnect_attempts > self.options.max_reconnect_attempts {
                        error!("Max reconnection attempts reached after error");
                        break;
                    }

                    tokio::time::sleep(tokio::time::Duration::from_millis(
                        self.options.reconnect_delay_ms,
                    ))
                    .await;
                }
            }
        }

        self.running.store(false, Ordering::SeqCst);
        info!("WebSocket loop terminated");
    }

    /// Connect to WebSocket and listen for messages
    ///
    /// Returns Ok(true) if stopped normally, Ok(false) if connection lost
    async fn connect_and_listen(
        &self,
        live_headers: &Arc<RwLock<Vec<BlockHeader>>>,
    ) -> WalletResult<bool> {
        use futures::{SinkExt, StreamExt};
        use tokio_tungstenite::connect_async;
        use tokio_tungstenite::tungstenite::Message;

        let url = self.ws_url();
        info!("Connecting to WebSocket: {}", url);

        let (ws_stream, _) = connect_async(url)
            .await
            .map_err(|e| WalletError::NetworkChain(format!("WebSocket connect failed: {}", e)))?;

        info!("WebSocket connected");

        let (mut write, mut read) = ws_stream.split();

        // Send initial empty object to trigger connection on server
        write
            .send(Message::Text("{}".to_string()))
            .await
            .map_err(|e| WalletError::NetworkChain(format!("WebSocket send failed: {}", e)))?;

        let mut last_message_time = std::time::Instant::now();
        let idle_timeout = std::time::Duration::from_millis(self.options.idle_timeout_ms);
        let ping_interval = std::time::Duration::from_millis(self.options.ping_interval_ms);
        let mut last_ping_time = std::time::Instant::now();

        loop {
            // Check if we should stop
            if !self.running.load(Ordering::SeqCst) {
                info!("Stop signal received, closing WebSocket");
                let _ = write.close().await;
                return Ok(true);
            }

            // Send ping if needed
            if last_ping_time.elapsed() > ping_interval {
                debug!("Sending ping");
                if write.send(Message::Text("ping".to_string())).await.is_err() {
                    warn!("Failed to send ping");
                    return Ok(false);
                }
                last_ping_time = std::time::Instant::now();
            }

            // Check for idle timeout
            if last_message_time.elapsed() > idle_timeout {
                warn!("WebSocket idle timeout");
                return Ok(false);
            }

            // Try to receive a message with timeout
            let receive_timeout =
                tokio::time::timeout(std::time::Duration::from_secs(1), read.next()).await;

            match receive_timeout {
                Ok(Some(Ok(message))) => {
                    last_message_time = std::time::Instant::now();

                    match message {
                        Message::Text(text) => {
                            if text.is_empty() {
                                // Ping response
                                continue;
                            }

                            if let Err(e) = self.process_message(&text, live_headers).await {
                                warn!("Error processing message: {}", e);
                            }
                        }
                        Message::Binary(data) => {
                            if let Ok(text) = String::from_utf8(data.to_vec()) {
                                if let Err(e) = self.process_message(&text, live_headers).await {
                                    warn!("Error processing binary message: {}", e);
                                }
                            }
                        }
                        Message::Close(_) => {
                            info!("WebSocket closed by server");
                            return Ok(false);
                        }
                        Message::Ping(data) => {
                            let _ = write.send(Message::Pong(data)).await;
                        }
                        Message::Pong(_) => {
                            // Pong received
                        }
                        _ => {}
                    }
                }
                Ok(Some(Err(e))) => {
                    warn!("WebSocket receive error: {}", e);
                    return Ok(false);
                }
                Ok(None) => {
                    info!("WebSocket stream ended");
                    return Ok(false);
                }
                Err(_) => {
                    // Timeout, continue loop
                    continue;
                }
            }
        }
    }

    /// Process a WebSocket message
    async fn process_message(
        &self,
        text: &str,
        live_headers: &Arc<RwLock<Vec<BlockHeader>>>,
    ) -> WalletResult<()> {
        // Handle empty or ping
        if text.is_empty() || text == "{}" {
            return Ok(());
        }

        // Try to parse as JSON
        let msg: serde_json::Value = serde_json::from_str(text)?;

        // Check for typed message
        if let Some(msg_type) = msg.get("type").and_then(|v| v.as_u64()) {
            match msg_type {
                3 => debug!("Unsubscribe message received"),
                5 => debug!("Subscribed to channel"),
                6 => debug!("Subscribe confirmation received"),
                7 => {
                    // Data delivered or error
                    if let Some(data) = msg.get("data") {
                        if let Some(code) = data.get("code").and_then(|v| v.as_i64()) {
                            if code != 200 {
                                let reason = data
                                    .get("reason")
                                    .and_then(|v| v.as_str())
                                    .unwrap_or("unknown");
                                warn!("WOC message type 7: code={}, reason={}", code, reason);
                            }
                        }
                    }
                }
                _ => debug!("Unknown message type: {}", msg_type),
            }
            return Ok(());
        }

        // Try to extract header data from nested structures:
        // pub.data.data  or  data.data  or  message.data
        let header_data = msg
            .get("pub")
            .and_then(|p| p.get("data"))
            .or_else(|| msg.get("data").and_then(|d| d.get("data")))
            .or_else(|| msg.get("message").and_then(|m| m.get("data")));

        if let Some(data) = header_data {
            let woc_header: WocWsBlockHeader = serde_json::from_value(data.clone())
                .map_err(|e| WalletError::Internal(format!("Invalid header data: {}", e)))?;

            let header = ws_header_to_block_header(&woc_header);
            info!(
                "New block from WebSocket: height={}, hash={}",
                header.height,
                &header.hash[..16]
            );

            // Add to live headers
            {
                let mut live = live_headers.write().await;
                live.push(header.clone());
            }

            // Broadcast to subscribers
            let live_header = block_header_to_live_header(header);
            let _ = self.sender.send(live_header);
        }

        Ok(())
    }

    /// Subscribe to new header notifications
    pub fn subscribe(&self) -> broadcast::Receiver<LiveBlockHeader> {
        self.sender.subscribe()
    }

    /// Check if currently running
    pub fn is_running(&self) -> bool {
        self.running.load(Ordering::SeqCst)
    }

    /// Get recent errors
    pub async fn get_errors(&self) -> Vec<(i32, String)> {
        self.error_count.read().await.clone()
    }
}

// ---------------------------------------------------------------------------
// LiveIngestor trait implementation
// ---------------------------------------------------------------------------

#[async_trait]
impl LiveIngestor for LiveWebSocketIngestor {
    async fn get_header_by_hash(&self, hash: &str) -> WalletResult<Option<BlockHeader>> {
        self.fetch_header_by_hash_http(hash).await
    }

    async fn start_listening(&self, live_headers: &mut Vec<BlockHeader>) -> WalletResult<()> {
        if self.running.load(Ordering::SeqCst) {
            warn!("WebSocket ingestor already running");
            return Ok(());
        }

        self.running.store(true, Ordering::SeqCst);

        // Wrap headers in Arc for the WebSocket loop
        let headers_arc = Arc::new(RwLock::new(live_headers.clone()));

        // Clone self into Arc for the spawned task
        let self_arc = Arc::new(LiveWebSocketIngestor {
            options: self.options.clone(),
            http_client: self.http_client.clone(),
            storage: self.storage.clone(),
            running: self.running.clone(),
            sender: self.sender.clone(),
            error_count: self.error_count.clone(),
            stop_signal: self.stop_signal.clone(),
        });

        let headers_clone = headers_arc.clone();

        // Spawn the WebSocket loop
        tokio::spawn(async move {
            self_arc.websocket_loop(headers_clone).await;
        });

        // Wait for initial connection
        tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

        // Copy any new headers back
        let updated = headers_arc.read().await;
        live_headers.clear();
        live_headers.extend(updated.iter().cloned());

        Ok(())
    }

    async fn stop_listening(&self) -> WalletResult<()> {
        info!("Stopping WebSocket ingestor");
        self.running.store(false, Ordering::SeqCst);
        Ok(())
    }

    async fn set_storage(&self, storage: Arc<dyn ChaintracksStorageIngest>) -> WalletResult<()> {
        *self.storage.write().await = Some(storage);
        Ok(())
    }

    async fn shutdown(&self) -> WalletResult<()> {
        self.stop_listening().await?;
        // Give the WebSocket loop time to close gracefully
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------

/// Convert BlockHeader to LiveBlockHeader
fn block_header_to_live_header(header: BlockHeader) -> LiveBlockHeader {
    LiveBlockHeader {
        version: header.version,
        previous_hash: header.previous_hash,
        merkle_root: header.merkle_root,
        time: header.time,
        bits: header.bits,
        nonce: header.nonce,
        height: header.height,
        hash: header.hash,
        chain_work: "0".repeat(64),
        is_chain_tip: true,
        is_active: true,
        header_id: None,
        previous_header_id: None,
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_options_creation() {
        let mainnet = LiveWebSocketOptions::mainnet();
        assert_eq!(mainnet.chain, Chain::Main);

        let testnet = LiveWebSocketOptions::testnet();
        assert_eq!(testnet.chain, Chain::Test);

        let custom = LiveWebSocketOptions::mainnet()
            .with_api_key("test-key")
            .with_idle_timeout(50000);
        assert_eq!(custom.api_key, Some("test-key".to_string()));
        assert_eq!(custom.idle_timeout_ms, 50000);
    }

    #[test]
    fn test_ws_url() {
        let mainnet = LiveWebSocketIngestor::new(LiveWebSocketOptions::mainnet()).unwrap();
        assert!(mainnet.ws_url().contains("socket-v2.whatsonchain"));

        let testnet = LiveWebSocketIngestor::new(LiveWebSocketOptions::testnet()).unwrap();
        assert!(testnet.ws_url().contains("testnet"));
    }

    #[test]
    fn test_ws_header_conversion() {
        let woc = WocWsBlockHeader {
            hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".to_string(),
            height: 0,
            version: 1,
            previous_block_hash: None,
            merkleroot: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
                .to_string(),
            time: 1231006505,
            bits: 486604799,
            nonce: 2083236893,
            confirmations: 800000,
            size: 285,
        };

        let header = ws_header_to_block_header(&woc);
        assert_eq!(header.height, 0);
        assert_eq!(header.nonce, 2083236893);
        assert_eq!(header.bits, 486604799);
        assert_eq!(header.previous_hash, "0".repeat(64));
    }

    #[tokio::test]
    async fn test_ingestor_lifecycle() {
        let ingestor = LiveWebSocketIngestor::new(LiveWebSocketOptions::mainnet()).unwrap();

        assert!(!ingestor.is_running());

        // Don't actually start listening in unit tests
        ingestor.stop_listening().await.unwrap();
        assert!(!ingestor.is_running());
    }

    #[test]
    fn test_options_defaults() {
        let opts = LiveWebSocketOptions::default();
        assert_eq!(opts.chain, Chain::Main);
        assert_eq!(opts.idle_timeout_ms, 100_000);
        assert_eq!(opts.ping_interval_ms, 10_000);
        assert_eq!(opts.max_reconnect_attempts, 10);
        assert_eq!(opts.reconnect_delay_ms, 5000);
        assert!(opts.api_key.is_none());
    }

    #[test]
    fn test_ws_url_constants() {
        assert!(WOC_WS_URL_MAIN.starts_with("wss://"));
        assert!(WOC_WS_URL_MAIN.contains("whatsonchain"));
        assert!(!WOC_WS_URL_MAIN.contains("testnet"));

        assert!(WOC_WS_URL_TEST.starts_with("wss://"));
        assert!(WOC_WS_URL_TEST.contains("whatsonchain"));
        assert!(WOC_WS_URL_TEST.contains("testnet"));

        assert!(WOC_WS_HISTORY_URL_MAIN.contains("history"));
        assert!(WOC_WS_HISTORY_URL_TEST.contains("history"));
    }

    #[test]
    fn test_api_url_constants() {
        assert!(WOC_API_URL_MAIN.starts_with("https://"));
        assert!(WOC_API_URL_MAIN.contains("main"));

        assert!(WOC_API_URL_TEST.starts_with("https://"));
        assert!(WOC_API_URL_TEST.contains("test"));
    }

    #[test]
    fn test_ws_header_with_previous() {
        let woc = WocWsBlockHeader {
            hash: "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048".to_string(),
            height: 1,
            version: 1,
            previous_block_hash: Some(
                "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".to_string(),
            ),
            merkleroot: "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
                .to_string(),
            time: 1231469665,
            bits: 486604799,
            nonce: 2573394689,
            confirmations: 799999,
            size: 215,
        };

        let header = ws_header_to_block_header(&woc);
        assert_eq!(header.height, 1);
        assert_eq!(
            header.previous_hash,
            "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
        );
    }

    #[test]
    fn test_ws_block_header_deserialization() {
        let json = r#"{
            "hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
            "height": 0,
            "version": 1,
            "merkleroot": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
            "time": 1231006505,
            "bits": 486604799,
            "nonce": 2083236893
        }"#;

        let header: WocWsBlockHeader = serde_json::from_str(json).unwrap();
        assert_eq!(header.height, 0);
        assert_eq!(header.nonce, 2083236893);
        assert!(header.previous_block_hash.is_none());
    }

    #[test]
    fn test_ws_error_display() {
        let err1 = WsError::ConnectionFailed("test".to_string());
        assert!(err1.to_string().contains("connection failed"));

        let err2 = WsError::MessageParseFailed("parse error".to_string());
        assert!(err2.to_string().contains("parse"));

        let err3 = WsError::IdleTimeout;
        assert!(err3.to_string().contains("idle"));

        let err4 = WsError::Stopped;
        assert!(err4.to_string().contains("stopped"));
    }

    #[test]
    fn test_block_header_to_live_header() {
        let header = BlockHeader {
            version: 1,
            previous_hash: "0".repeat(64),
            merkle_root: "abc".repeat(21) + "a",
            time: 1231006505,
            bits: 0x1d00ffff,
            nonce: 2083236893,
            height: 0,
            hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f".to_string(),
        };

        let live = block_header_to_live_header(header.clone());
        assert_eq!(live.height, header.height);
        assert_eq!(live.hash, header.hash);
        assert!(live.is_chain_tip);
        assert!(live.is_active);
        assert!(live.header_id.is_none());
        assert!(live.previous_header_id.is_none());
    }

    #[tokio::test]
    async fn test_ingestor_subscribe() {
        let ingestor = LiveWebSocketIngestor::new(LiveWebSocketOptions::mainnet()).unwrap();
        let _receiver = ingestor.subscribe();
        // Verify subscription works without panicking
    }

    #[tokio::test]
    async fn test_get_errors() {
        let ingestor = LiveWebSocketIngestor::new(LiveWebSocketOptions::mainnet()).unwrap();
        let errors = ingestor.get_errors().await;
        assert!(errors.is_empty());
    }

    #[test]
    fn test_mainnet_testnet_creation() {
        let mainnet = LiveWebSocketIngestor::mainnet();
        assert!(mainnet.is_ok());

        let testnet = LiveWebSocketIngestor::testnet();
        assert!(testnet.is_ok());
    }

    #[test]
    fn test_api_url_method() {
        let mainnet = LiveWebSocketIngestor::new(LiveWebSocketOptions::mainnet()).unwrap();
        assert!(mainnet.api_url().contains("main"));

        let testnet = LiveWebSocketIngestor::new(LiveWebSocketOptions::testnet()).unwrap();
        assert!(testnet.api_url().contains("test"));
    }
}