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
//! An extensible blocking/async Esplora client
//!
//! This library provides an extensible blocking and
//! async Esplora client to query Esplora's backend.
//!
//! The library provides the possibility to build a blocking
//! client using [`ureq`] and an async client using [`reqwest`].
//! The library supports communicating to Esplora via a proxy
//! and also using TLS (SSL) for secure communication.
//!
//!
//! ## Usage
//!
//! You can create a blocking client as follows:
//!
//! ```no_run
//! use esplora_client::Builder;
//! let builder = Builder::new("https://blockstream.info/testnet/api");
//! let blocking_client = builder.build_blocking();
//! # Ok::<(), esplora_client::Error>(());
//! ```
//!
//! Here is an example of how to create an asynchronous client.
//!
//! ```no_run
//! use esplora_client::Builder;
//! let builder = Builder::new("https://blockstream.info/testnet/api");
//! let async_client = builder.build_async();
//! # Ok::<(), esplora_client::Error>(());
//! ```
//!
//! ## Features
//!
//! By default the library enables all features. To specify
//! specific features, set `default-features` to `false` in your `Cargo.toml`
//! and specify the features you want. This will look like this:
//!
//! `esplora_client = { version = "*", default-features = false, features = ["blocking"] }`
//!
//! * `blocking` enables [`ureq`], the blocking client with proxy and TLS (SSL) capabilities.
//! * `async` enables [`reqwest`], the async client with proxy capabilities.
//! * `async-https` enables [`reqwest`], the async client with support for proxying and TLS (SSL).
//!
//!

#![allow(clippy::result_large_err)]

use std::collections::HashMap;
use std::fmt;
use std::io;

use bitcoin::consensus;
use bitcoin::{BlockHash, Txid};

pub mod api;

#[cfg(any(feature = "async", feature = "async-https"))]
pub mod r#async;
#[cfg(feature = "blocking")]
pub mod blocking;

pub use api::*;
#[cfg(feature = "blocking")]
pub use blocking::BlockingClient;
#[cfg(any(feature = "async", feature = "async-https"))]
pub use r#async::AsyncClient;

/// Get a fee value in sats/vbytes from the estimates
/// that matches the confirmation target set as parameter.
pub fn convert_fee_rate(target: usize, estimates: HashMap<String, f64>) -> Result<f32, Error> {
    let fee_val = {
        let mut pairs = estimates
            .into_iter()
            .filter_map(|(k, v)| Some((k.parse::<usize>().ok()?, v)))
            .collect::<Vec<_>>();
        pairs.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(*k));
        pairs
            .into_iter()
            .find(|(k, _)| k <= &target)
            .map(|(_, v)| v)
            .unwrap_or(1.0)
    };
    Ok(fee_val as f32)
}

#[derive(Debug, Clone)]
pub struct Builder {
    pub base_url: String,
    /// Optional URL of the proxy to use to make requests to the Esplora server
    ///
    /// The string should be formatted as: `<protocol>://<user>:<password>@host:<port>`.
    ///
    /// Note that the format of this value and the supported protocols change slightly between the
    /// blocking version of the client (using `ureq`) and the async version (using `reqwest`). For more
    /// details check with the documentation of the two crates. Both of them are compiled with
    /// the `socks` feature enabled.
    ///
    /// The proxy is ignored when targeting `wasm32`.
    pub proxy: Option<String>,
    /// Socket timeout.
    pub timeout: Option<u64>,
}

impl Builder {
    /// Instantiate a new builder
    pub fn new(base_url: &str) -> Self {
        Builder {
            base_url: base_url.to_string(),
            proxy: None,
            timeout: None,
        }
    }

    /// Set the proxy of the builder
    pub fn proxy(mut self, proxy: &str) -> Self {
        self.proxy = Some(proxy.to_string());
        self
    }

    /// Set the timeout of the builder
    pub fn timeout(mut self, timeout: u64) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// build a blocking client from builder
    #[cfg(feature = "blocking")]
    pub fn build_blocking(self) -> Result<BlockingClient, Error> {
        BlockingClient::from_builder(self)
    }

    // build an asynchronous client from builder
    #[cfg(feature = "async")]
    pub fn build_async(self) -> Result<AsyncClient, Error> {
        AsyncClient::from_builder(self)
    }
}

/// Errors that can happen during a sync with `Esplora`
#[derive(Debug)]
pub enum Error {
    /// Error during ureq HTTP request
    #[cfg(feature = "blocking")]
    Ureq(::ureq::Error),
    /// Transport error during the ureq HTTP call
    #[cfg(feature = "blocking")]
    UreqTransport(::ureq::Transport),
    /// Error during reqwest HTTP request
    #[cfg(any(feature = "async", feature = "async-https"))]
    Reqwest(::reqwest::Error),
    /// HTTP response error
    HttpResponse(u16),
    /// IO error during ureq response read
    Io(io::Error),
    /// No header found in ureq response
    NoHeader,
    /// Invalid number returned
    Parsing(std::num::ParseIntError),
    /// Invalid Bitcoin data returned
    BitcoinEncoding(bitcoin::consensus::encode::Error),
    /// Invalid Hex data returned
    Hex(bitcoin::hashes::hex::Error),

    /// Transaction not found
    TransactionNotFound(Txid),
    /// Header height not found
    HeaderHeightNotFound(u32),
    /// Header hash not found
    HeaderHashNotFound(BlockHash),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

macro_rules! impl_error {
    ( $from:ty, $to:ident ) => {
        impl_error!($from, $to, Error);
    };
    ( $from:ty, $to:ident, $impl_for:ty ) => {
        impl std::convert::From<$from> for $impl_for {
            fn from(err: $from) -> Self {
                <$impl_for>::$to(err)
            }
        }
    };
}

impl std::error::Error for Error {}
#[cfg(feature = "blocking")]
impl_error!(::ureq::Transport, UreqTransport, Error);
#[cfg(any(feature = "async", feature = "async-https"))]
impl_error!(::reqwest::Error, Reqwest, Error);
impl_error!(io::Error, Io, Error);
impl_error!(std::num::ParseIntError, Parsing, Error);
impl_error!(consensus::encode::Error, BitcoinEncoding, Error);
impl_error!(bitcoin::hashes::hex::Error, Hex, Error);

#[cfg(test)]
mod test {
    use super::*;
    use electrsd::{bitcoind, bitcoind::BitcoinD, ElectrsD};
    use lazy_static::lazy_static;
    use std::env;
    use tokio::sync::Mutex;
    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    use {
        bitcoin::hashes::Hash,
        bitcoin::Amount,
        electrsd::{
            bitcoind::bitcoincore_rpc::bitcoincore_rpc_json::AddressType,
            bitcoind::bitcoincore_rpc::RpcApi,
        },
        electrum_client::ElectrumApi,
        std::time::Duration,
        tokio::sync::OnceCell,
    };

    lazy_static! {
        static ref BITCOIND: BitcoinD = {
            let bitcoind_exe = env::var("BITCOIND_EXE")
                .ok()
                .or_else(|| bitcoind::downloaded_exe_path().ok())
                .expect(
                    "you need to provide an env var BITCOIND_EXE or specify a bitcoind version feature",
                );
            let conf = bitcoind::Conf::default();
            BitcoinD::with_conf(bitcoind_exe, &conf).unwrap()
        };
        static ref ELECTRSD: ElectrsD = {
            let electrs_exe = env::var("ELECTRS_EXE")
                .ok()
                .or_else(electrsd::downloaded_exe_path)
                .expect(
                    "you need to provide env var ELECTRS_EXE or specify an electrsd version feature",
                );
            let mut conf = electrsd::Conf::default();
            conf.http_enabled = true;
            ElectrsD::with_conf(electrs_exe, &BITCOIND, &conf).unwrap()
        };
        static ref MINER: Mutex<()> = Mutex::new(());
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    static PREMINE: OnceCell<()> = OnceCell::const_new();

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    async fn setup_clients() -> (BlockingClient, AsyncClient) {
        PREMINE
            .get_or_init(|| async {
                let _miner = MINER.lock().await;
                generate_blocks_and_wait(101);
            })
            .await;

        let esplora_url = ELECTRSD.esplora_url.as_ref().unwrap();

        let builder = Builder::new(&format!("http://{}", esplora_url));
        let blocking_client = builder.build_blocking().unwrap();

        let builder_async = Builder::new(&format!("http://{}", esplora_url));
        let async_client = builder_async.build_async().unwrap();

        (blocking_client, async_client)
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    fn generate_blocks_and_wait(num: usize) {
        let cur_height = BITCOIND.client.get_block_count().unwrap();
        generate_blocks(num);
        wait_for_block(cur_height as usize + num);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    fn generate_blocks(num: usize) {
        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let _block_hashes = BITCOIND
            .client
            .generate_to_address(num as u64, &address)
            .unwrap();
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    fn wait_for_block(min_height: usize) {
        let mut header = ELECTRSD.client.block_headers_subscribe().unwrap();
        loop {
            if header.height >= min_height {
                break;
            }
            header = exponential_backoff_poll(|| {
                ELECTRSD.trigger().unwrap();
                ELECTRSD.client.ping().unwrap();
                ELECTRSD.client.block_headers_pop().unwrap()
            });
        }
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    fn exponential_backoff_poll<T, F>(mut poll: F) -> T
    where
        F: FnMut() -> Option<T>,
    {
        let mut delay = Duration::from_millis(64);
        loop {
            match poll() {
                Some(data) => break data,
                None if delay.as_millis() < 512 => delay = delay.mul_f32(2.0),
                None => {}
            }

            std::thread::sleep(delay);
        }
    }

    #[test]
    fn feerate_parsing() {
        let esplora_fees = serde_json::from_str::<HashMap<String, f64>>(
            r#"{
  "25": 1.015,
  "5": 2.3280000000000003,
  "12": 2.0109999999999997,
  "15": 1.018,
  "17": 1.018,
  "11": 2.0109999999999997,
  "3": 3.01,
  "2": 4.9830000000000005,
  "6": 2.2359999999999998,
  "21": 1.018,
  "13": 1.081,
  "7": 2.2359999999999998,
  "8": 2.2359999999999998,
  "16": 1.018,
  "20": 1.018,
  "22": 1.017,
  "23": 1.017,
  "504": 1,
  "9": 2.2359999999999998,
  "14": 1.018,
  "10": 2.0109999999999997,
  "24": 1.017,
  "1008": 1,
  "1": 4.9830000000000005,
  "4": 2.3280000000000003,
  "19": 1.018,
  "144": 1,
  "18": 1.018
}
"#,
        )
        .unwrap();
        assert_eq!(convert_fee_rate(6, esplora_fees.clone()).unwrap(), 2.236);
        assert_eq!(
            convert_fee_rate(26, esplora_fees).unwrap(),
            1.015,
            "should inherit from value for 25"
        );
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_tx() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let tx = blocking_client.get_tx(&txid).unwrap();
        let tx_async = async_client.get_tx(&txid).await.unwrap();
        assert_eq!(tx, tx_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_tx_no_opt() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let tx_no_opt = blocking_client.get_tx_no_opt(&txid).unwrap();
        let tx_no_opt_async = async_client.get_tx_no_opt(&txid).await.unwrap();
        assert_eq!(tx_no_opt, tx_no_opt_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_tx_status() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let tx_status = blocking_client.get_tx_status(&txid).unwrap().unwrap();
        let tx_status_async = async_client.get_tx_status(&txid).await.unwrap().unwrap();
        assert_eq!(tx_status, tx_status_async);
        assert!(tx_status.confirmed);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_header_by_hash() {
        let (blocking_client, async_client) = setup_clients().await;

        let block_hash = BITCOIND.client.get_block_hash(23).unwrap();

        let block_header = blocking_client.get_header_by_hash(&block_hash).unwrap();
        let block_header_async = async_client.get_header_by_hash(&block_hash).await.unwrap();
        assert_eq!(block_header, block_header_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_block_status() {
        let (blocking_client, async_client) = setup_clients().await;

        let block_hash = BITCOIND.client.get_block_hash(21).unwrap();
        let next_block_hash = BITCOIND.client.get_block_hash(22).unwrap();

        let expected = BlockStatus {
            in_best_chain: true,
            height: Some(21),
            next_best: Some(next_block_hash),
        };

        let block_status = blocking_client.get_block_status(&block_hash).unwrap();
        let block_status_async = async_client.get_block_status(&block_hash).await.unwrap();
        assert_eq!(expected, block_status);
        assert_eq!(expected, block_status_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_non_existing_block_status() {
        // Esplora returns the same status for orphaned blocks as for non-existing blocks:
        // non-existing: https://blockstream.info/api/block/0000000000000000000000000000000000000000000000000000000000000000/status
        // orphaned: https://blockstream.info/api/block/000000000000000000181b1a2354620f66868a723c0c4d5b24e4be8bdfc35a7f/status
        // (Here the block is cited as orphaned: https://bitcoinchain.com/block_explorer/block/000000000000000000181b1a2354620f66868a723c0c4d5b24e4be8bdfc35a7f/ )
        // For this reason, we only test for the non-existing case here.

        let (blocking_client, async_client) = setup_clients().await;

        let block_hash = BlockHash::all_zeros();

        let expected = BlockStatus {
            in_best_chain: false,
            height: None,
            next_best: None,
        };

        let block_status = blocking_client.get_block_status(&block_hash).unwrap();
        let block_status_async = async_client.get_block_status(&block_hash).await.unwrap();
        assert_eq!(expected, block_status);
        assert_eq!(expected, block_status_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_block_by_hash() {
        let (blocking_client, async_client) = setup_clients().await;

        let block_hash = BITCOIND.client.get_block_hash(21).unwrap();

        let expected = Some(BITCOIND.client.get_block(&block_hash).unwrap());

        let block = blocking_client.get_block_by_hash(&block_hash).unwrap();
        let block_async = async_client.get_block_by_hash(&block_hash).await.unwrap();
        assert_eq!(expected, block);
        assert_eq!(expected, block_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_block_by_hash_not_existing() {
        let (blocking_client, async_client) = setup_clients().await;

        let block = blocking_client
            .get_block_by_hash(&BlockHash::all_zeros())
            .unwrap();
        let block_async = async_client
            .get_block_by_hash(&BlockHash::all_zeros())
            .await
            .unwrap();
        assert!(block.is_none());
        assert!(block_async.is_none());
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_merkle_proof() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let merkle_proof = blocking_client.get_merkle_proof(&txid).unwrap().unwrap();
        let merkle_proof_async = async_client.get_merkle_proof(&txid).await.unwrap().unwrap();
        assert_eq!(merkle_proof, merkle_proof_async);
        assert!(merkle_proof.pos > 0);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_merkle_block() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let merkle_block = blocking_client.get_merkle_block(&txid).unwrap().unwrap();
        let merkle_block_async = async_client.get_merkle_block(&txid).await.unwrap().unwrap();
        assert_eq!(merkle_block, merkle_block_async);

        let mut matches = vec![txid];
        let mut indexes = vec![];
        let root = merkle_block
            .txn
            .extract_matches(&mut matches, &mut indexes)
            .unwrap();
        assert_eq!(root, merkle_block.header.merkle_root);
        assert_eq!(indexes.len(), 1);
        assert!(indexes[0] > 0);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_output_status() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let output_status = blocking_client
            .get_output_status(&txid, 1)
            .unwrap()
            .unwrap();
        let output_status_async = async_client
            .get_output_status(&txid, 1)
            .await
            .unwrap()
            .unwrap();

        assert_eq!(output_status, output_status_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_height() {
        let (blocking_client, async_client) = setup_clients().await;
        let block_height = blocking_client.get_height().unwrap();
        let block_height_async = async_client.get_height().await.unwrap();
        assert!(block_height > 0);
        assert_eq!(block_height, block_height_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_tip_hash() {
        let (blocking_client, async_client) = setup_clients().await;
        let tip_hash = blocking_client.get_tip_hash().unwrap();
        let tip_hash_async = async_client.get_tip_hash().await.unwrap();
        assert_eq!(tip_hash, tip_hash_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_block_hash() {
        let (blocking_client, async_client) = setup_clients().await;

        let block_hash = BITCOIND.client.get_block_hash(21).unwrap();

        let block_hash_blocking = blocking_client.get_block_hash(21).unwrap();
        let block_hash_async = async_client.get_block_hash(21).await.unwrap();
        assert_eq!(block_hash, block_hash_blocking);
        assert_eq!(block_hash, block_hash_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_txid_at_block_index() {
        let (blocking_client, async_client) = setup_clients().await;

        let block_hash = BITCOIND.client.get_block_hash(23).unwrap();

        let txid_at_block_index = blocking_client
            .get_txid_at_block_index(&block_hash, 0)
            .unwrap()
            .unwrap();
        let txid_at_block_index_async = async_client
            .get_txid_at_block_index(&block_hash, 0)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(txid_at_block_index, txid_at_block_index_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_fee_estimates() {
        let (blocking_client, async_client) = setup_clients().await;
        let fee_estimates = blocking_client.get_fee_estimates().unwrap();
        let fee_estimates_async = async_client.get_fee_estimates().await.unwrap();
        assert_eq!(fee_estimates.len(), fee_estimates_async.len());
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_scripthash_txs() {
        let (blocking_client, async_client) = setup_clients().await;

        let address = BITCOIND
            .client
            .get_new_address(Some("test"), Some(AddressType::Legacy))
            .unwrap();
        let txid = BITCOIND
            .client
            .send_to_address(
                &address,
                Amount::from_sat(1000),
                None,
                None,
                None,
                None,
                None,
                None,
            )
            .unwrap();
        let _miner = MINER.lock().await;
        generate_blocks_and_wait(1);

        let expected_tx = BITCOIND
            .client
            .get_transaction(&txid, None)
            .unwrap()
            .transaction()
            .unwrap();
        let script = &expected_tx.output[0].script_pubkey;
        let scripthash_txs_txids: Vec<Txid> = blocking_client
            .scripthash_txs(script, None)
            .unwrap()
            .iter()
            .map(|tx| tx.txid)
            .collect();
        let scripthash_txs_txids_async: Vec<Txid> = async_client
            .scripthash_txs(script, None)
            .await
            .unwrap()
            .iter()
            .map(|tx| tx.txid)
            .collect();
        assert_eq!(scripthash_txs_txids, scripthash_txs_txids_async);
    }

    #[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
    #[tokio::test]
    async fn test_get_blocks() {
        let (blocking_client, async_client) = setup_clients().await;
        let start_height = BITCOIND.client.get_block_count().unwrap();
        let blocks1 = blocking_client.get_blocks(None).unwrap();
        let blocks_async1 = async_client.get_blocks(None).await.unwrap();
        assert_eq!(blocks1[0].time.height, start_height as u32);
        assert_eq!(blocks1, blocks_async1);
        generate_blocks_and_wait(10);
        let blocks2 = blocking_client.get_blocks(None).unwrap();
        let blocks_async2 = async_client.get_blocks(None).await.unwrap();
        assert_eq!(blocks2, blocks_async2);
        assert_ne!(blocks2, blocks1);
        let blocks3 = blocking_client
            .get_blocks(Some(start_height as u32))
            .unwrap();
        let blocks_async3 = async_client
            .get_blocks(Some(start_height as u32))
            .await
            .unwrap();
        assert_eq!(blocks3, blocks_async3);
        assert_eq!(blocks3[0].time.height, start_height as u32);
        assert_eq!(blocks3, blocks1);
        let blocks_genesis = blocking_client.get_blocks(Some(0)).unwrap();
        let blocks_genesis_async = async_client.get_blocks(Some(0)).await.unwrap();
        assert_eq!(blocks_genesis, blocks_genesis_async);
    }
}