celestia-client 1.3.0

Celestia client combining RPC and gRPC functionality.
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
use std::error::Error as StdError;
use std::fmt::{self, Debug};
use std::sync::Arc;
use std::time::Duration;

use blockstore::cond_send::CondSend;
pub use celestia_grpc::Endpoint;
use celestia_grpc::{GrpcClient, GrpcClientBuilder};
pub use celestia_rpc::RpcEndpoint;
use celestia_rpc::{FailoverClient, HeaderClient};
use http::Request;
use tonic::body::Body as TonicBody;
use tonic::codegen::{Bytes, Service};

use crate::blob::BlobApi;
use crate::blobstream::BlobstreamApi;
use crate::fraud::FraudApi;
use crate::header::HeaderApi;
use crate::share::ShareApi;
use crate::state::StateApi;
use crate::tx::{DocSigner, Keypair, VerifyingKey};
use crate::types::ExtendedHeader;
use crate::types::state::AccAddress;
use crate::{Error, Result};

/// A high-level client for interacting with a Celestia node.
///
/// There are two modes: read-only mode and submit mode. Read-only mode requires
/// RPC and optionally gRPC endpoint, while submit mode requires both, plus a signer.
///
/// # Examples
///
/// Read-only mode:
///
/// ```no_run
/// # use celestia_client::{Client, Result};
/// # async fn docs() -> Result<()> {
/// let client = Client::builder()
///     .rpc_url("ws://localhost:26658")
///     .grpc_url("http://localhost:9090") // optional in read-only mode
///     .build()
///     .await?;
///
/// client.header().head().await?;
/// # Ok(())
/// # }
/// ```
///
/// Submit mode:
///
/// ```no_run
/// # use celestia_client::{Client, Endpoint, Result};
/// # use celestia_client::tx::TxConfig;
/// # use std::time::Duration;
/// # async fn docs() -> Result<()> {
/// let endpoint = Endpoint::from("http://localhost:9090")
///     .metadata("x-token", "auth-token")
///     .timeout(Duration::from_secs(30));
///
/// let client = Client::builder()
///     .rpc_url("ws://localhost:26658")
///     .grpc_endpoint(endpoint)
///     .private_key_hex("393fdb5def075819de55756b45c9e2c8531a8c78dd6eede483d3440e9457d839")
///     .build()
///     .await?;
///
/// let to_address = "celestia169s50psyj2f4la9a2235329xz7rk6c53zhw9mm".parse().unwrap();
/// client.state().transfer(&to_address, 12345, TxConfig::default()).await?;
/// # Ok(())
/// # }
/// ```
///
/// [`celestia-rpc`]: celestia_rpc
/// [`celestia-grpc`]: celestia_grpc
pub struct Client {
    inner: Arc<ClientInner>,
    state: StateApi,
    blob: BlobApi,
    header: HeaderApi,
    share: ShareApi,
    fraud: FraudApi,
    blobstream: BlobstreamApi,
    fibre: Option<crate::fibre::FibreApi>,
}

pub(crate) struct ClientInner {
    pub(crate) rpc: FailoverClient,
    grpc: Option<GrpcClient>,
    pubkey: Option<VerifyingKey>,
    chain_id: tendermint::chain::Id,
}

/// A builder for [`Client`].
#[derive(Default)]
pub struct ClientBuilder {
    rpc_url: Option<String>,
    rpc_auth_token: Option<String>,
    rpc_endpoints: Vec<RpcEndpoint>,
    rpc_health_check_interval: Option<Duration>,
    rpc_max_head_age: Option<Duration>,
    timeout: Option<Duration>,
    connect_timeout: Option<Duration>,
    grpc_builder: Option<GrpcClientBuilder>,
    fibre_client: Option<celestia_fibre::FibreClient>,
}

impl fmt::Debug for ClientBuilder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ClientBuilder")
            .field("rpc_url", &self.rpc_url)
            .field(
                "rpc_auth_token",
                &self.rpc_auth_token.as_ref().map(|_| "***"),
            )
            .field(
                "rpc_endpoints",
                &self
                    .rpc_endpoints
                    .iter()
                    .map(|e| &e.url)
                    .collect::<Vec<_>>(),
            )
            .field("rpc_health_check_interval", &self.rpc_health_check_interval)
            .field("rpc_max_head_age", &self.rpc_max_head_age)
            .field("timeout", &self.timeout)
            .field("connect_timeout", &self.connect_timeout)
            .field("grpc_builder", &self.grpc_builder)
            .field(
                "fibre_client",
                &self.fibre_client.as_ref().map(|_| "FibreClient { .. }"),
            )
            .finish()
    }
}

impl ClientInner {
    pub(crate) fn grpc(&self) -> Result<&GrpcClient> {
        self.grpc.as_ref().ok_or(Error::GrpcEndpointNotSet)
    }

    pub(crate) fn pubkey(&self) -> Result<&VerifyingKey> {
        self.pubkey.as_ref().ok_or(Error::NoAssociatedAddress)
    }

    pub(crate) fn address(&self) -> Result<AccAddress> {
        let pubkey = self.pubkey()?.to_owned();
        Ok(AccAddress::new(pubkey.into()))
    }

    pub(crate) async fn get_header_validated(&self, height: u64) -> Result<ExtendedHeader> {
        let header = self.rpc.header_get_by_height(height).await?;
        header.validate()?;
        Ok(header)
    }
}

impl Client {
    /// Returns `ClientBuilder`.
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }

    /// Returns chain id of the network.
    pub fn chain_id(&self) -> &tendermint::chain::Id {
        &self.inner.chain_id
    }

    /// Returns the public key of the signer.
    pub fn pubkey(&self) -> Result<VerifyingKey> {
        self.inner.pubkey().cloned()
    }

    /// Returns the address of signer.
    pub fn address(&self) -> Result<AccAddress> {
        self.inner.address()
    }

    /// Returns state API accessor.
    pub fn state(&self) -> &StateApi {
        &self.state
    }

    /// Returns blob API accessor.
    pub fn blob(&self) -> &BlobApi {
        &self.blob
    }

    /// Returns blobstream API accessor.
    pub fn blobstream(&self) -> &BlobstreamApi {
        &self.blobstream
    }

    /// Returns header API accessor.
    pub fn header(&self) -> &HeaderApi {
        &self.header
    }

    /// Returns share API accessor.
    pub fn share(&self) -> &ShareApi {
        &self.share
    }

    /// Returns fraud API accessor.
    pub fn fraud(&self) -> &FraudApi {
        &self.fraud
    }

    /// Returns fibre API accessor.
    ///
    /// Returns `None` if the client was not configured with a fibre client.
    /// Use [`ClientBuilder::fibre_client()`] to provide one.
    pub fn fibre(&self) -> Option<&crate::fibre::FibreApi> {
        self.fibre.as_ref()
    }
}

impl ClientBuilder {
    /// Returns a new builder.
    pub fn new() -> ClientBuilder {
        ClientBuilder::default()
    }

    /// Set signer and its public key.
    pub fn signer<S>(mut self, pubkey: VerifyingKey, signer: S) -> ClientBuilder
    where
        S: DocSigner + Sync + Send + 'static,
    {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.pubkey_and_signer(pubkey, signer));
        self
    }

    /// Set signer from a keypair.
    pub fn keypair<S>(mut self, keypair: S) -> ClientBuilder
    where
        S: DocSigner + Keypair<VerifyingKey = VerifyingKey> + Sync + Send + 'static,
    {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.signer_keypair(keypair));
        self
    }

    /// Set signer from a raw private key.
    pub fn private_key(mut self, bytes: &[u8]) -> ClientBuilder {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.private_key(bytes));
        self
    }

    /// Set signer from a hex formatted private key.
    pub fn private_key_hex(mut self, s: &str) -> ClientBuilder {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.private_key_hex(s));
        self
    }

    /// Set the RPC endpoint.
    pub fn rpc_url(mut self, url: &str) -> ClientBuilder {
        self.rpc_url = Some(url.to_owned());
        self
    }

    /// Set the authentication token of RPC endpoint.
    pub fn rpc_auth_token(mut self, auth_token: &str) -> ClientBuilder {
        self.rpc_auth_token = Some(auth_token.to_owned());
        self
    }

    /// Add an RPC endpoint for failover.
    ///
    /// Accepts [`RpcEndpoint`], `&str`, or `String`. Use [`RpcEndpoint`] when an
    /// endpoint needs its own authentication token (e.g. a fallback provider
    /// such as QuickNode that uses a different token than your own node).
    ///
    /// Endpoints are tried in priority order: the first endpoint added (or the
    /// one set via [`rpc_url`](ClientBuilder::rpc_url)) is the most preferred.
    /// When the preferred endpoint fails, the client automatically falls back to
    /// the next one, and a background health-check switches back to the preferred
    /// endpoint once it recovers.
    pub fn rpc_endpoint(mut self, endpoint: impl Into<RpcEndpoint>) -> ClientBuilder {
        self.rpc_endpoints.push(endpoint.into());
        self
    }

    /// Add multiple RPC endpoints at once for failover.
    ///
    /// Accepts [`RpcEndpoint`], `&str`, or `String` items. See
    /// [`rpc_endpoint`](ClientBuilder::rpc_endpoint) for the failover semantics.
    pub fn rpc_endpoints<I, E>(mut self, endpoints: I) -> ClientBuilder
    where
        I: IntoIterator<Item = E>,
        E: Into<RpcEndpoint>,
    {
        self.rpc_endpoints
            .extend(endpoints.into_iter().map(Into::into));
        self
    }

    /// Set the interval at which the preferred RPC endpoint is probed for
    /// recovery while a fallback is in use.
    ///
    /// Only has an effect when more than one RPC endpoint is configured.
    /// Defaults to [`celestia_rpc::DEFAULT_HEALTH_CHECK_INTERVAL`].
    pub fn rpc_health_check_interval(mut self, interval: Duration) -> ClientBuilder {
        self.rpc_health_check_interval = Some(interval);
        self
    }

    /// Set the maximum age of the preferred RPC endpoint's head for it to be
    /// considered healthy enough to switch back to.
    ///
    /// While a fallback is in use, the background health-check only switches back
    /// to the preferred endpoint once its head is no older than this (i.e. it has
    /// caught up to the chain tip). Defaults to
    /// [`celestia_rpc::DEFAULT_MAX_HEAD_AGE`].
    pub fn rpc_max_head_age(mut self, max_head_age: Duration) -> ClientBuilder {
        self.rpc_max_head_age = Some(max_head_age);
        self
    }

    /// Set the request timeout for both RPC and gRPC endpoints.
    ///
    /// This bounds the duration of individual requests only. To bound how long
    /// connection establishment may take, use [`ClientBuilder::connect_timeout`].
    ///
    /// # Note
    ///
    /// Unlike previous versions, this no longer doubles as the WebSocket RPC
    /// connection timeout; that is now controlled solely by
    /// [`ClientBuilder::connect_timeout`].
    pub fn timeout(mut self, timeout: Duration) -> ClientBuilder {
        self.timeout = Some(timeout);
        self
    }

    /// Set the connection establishment timeout for RPC and gRPC endpoints.
    ///
    /// This is independent of [`ClientBuilder::timeout`] (the request timeout): a low
    /// connect timeout lets a down/unreachable host be detected faster than a merely
    /// slow one.
    ///
    /// # Note
    ///
    /// For HTTP(S) RPC this bounds only the TCP connection establishment, not DNS
    /// resolution or the TLS handshake.
    ///
    /// Not supported for WASM RPC or WASM (gRPC-Web) gRPC transports, where it is
    /// ignored.
    pub fn connect_timeout(mut self, connect_timeout: Duration) -> ClientBuilder {
        self.connect_timeout = Some(connect_timeout);
        self
    }

    /// Set the gRPC endpoint.
    ///
    /// Alias of [`ClientBuilder::grpc_endpoint`].
    ///
    /// Accepts `Endpoint`, `&str`, or `String`.
    ///
    /// # Note
    ///
    /// In WASM the endpoint needs to support gRPC-Web.
    pub fn grpc_url(self, url: impl Into<Endpoint>) -> ClientBuilder {
        self.grpc_endpoint(url)
    }

    /// Set the gRPC endpoint.
    pub fn grpc_endpoint(mut self, endpoint: impl Into<Endpoint>) -> ClientBuilder {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.endpoint(endpoint));
        self
    }

    /// Add multiple gRPC endpoints at once for fallback support.
    ///
    /// Accepts `Endpoint`, `&str`, or `String` items.
    ///
    /// When multiple endpoints are configured, the client will automatically
    /// fall back to the next endpoint if a network-related error occurs.
    ///
    /// # Note
    ///
    /// In WASM the endpoints need to support gRPC-Web.
    pub fn grpc_endpoints<I, E>(mut self, endpoints: I) -> ClientBuilder
    where
        I: IntoIterator<Item = E>,
        E: Into<Endpoint>,
    {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.endpoints(endpoints));
        self
    }

    /// Add multiple gRPC endpoints. Alias of [`ClientBuilder::grpc_endpoints`].
    pub fn grpc_urls<I, E>(self, urls: I) -> ClientBuilder
    where
        I: IntoIterator<Item = E>,
        E: Into<Endpoint>,
    {
        self.grpc_endpoints(urls)
    }

    /// Set manually configured gRPC transport
    pub fn grpc_transport<B, T>(mut self, transport: T) -> Self
    where
        B: http_body::Body<Data = Bytes> + Send + Unpin + 'static,
        <B as http_body::Body>::Error: StdError + Send + Sync,
        T: Service<Request<TonicBody>, Response = http::Response<B>>
            + Send
            + Sync
            + Clone
            + 'static,
        <T as Service<Request<TonicBody>>>::Error: StdError + Send + Sync + 'static,
        <T as Service<Request<TonicBody>>>::Future: CondSend + 'static,
    {
        let grpc_builder = self.grpc_builder.unwrap_or_default();
        self.grpc_builder = Some(grpc_builder.transport(transport));
        self
    }

    /// Set a pre-built [`FibreClient`](celestia_fibre::FibreClient) for the Fibre
    /// data availability protocol.
    ///
    /// When set, the client exposes a [`FibreApi`](crate::fibre::FibreApi) via
    /// [`Client::fibre()`].
    ///
    /// Use [`FibreClient::from_endpoint()`](celestia_fibre::FibreClient::from_endpoint) for
    /// convenient construction from a gRPC endpoint URL.
    pub fn fibre_client(mut self, fibre_client: celestia_fibre::FibreClient) -> Self {
        self.fibre_client = Some(fibre_client);
        self
    }

    /// Build [`Client`].
    pub async fn build(self) -> Result<Client> {
        // The endpoint set via `rpc_url` (with its optional `rpc_auth_token`) is
        // the most preferred one; any endpoints added via `rpc_endpoint(s)`
        // follow it as fallbacks.
        let mut rpc_endpoints = Vec::with_capacity(self.rpc_endpoints.len() + 1);
        if let Some(url) = self.rpc_url {
            let mut endpoint = RpcEndpoint::new(url);
            if let Some(token) = self.rpc_auth_token {
                endpoint = endpoint.auth_token(token);
            }
            rpc_endpoints.push(endpoint);
        }
        rpc_endpoints.extend(self.rpc_endpoints);

        if rpc_endpoints.is_empty() {
            return Err(Error::RpcEndpointNotSet);
        }

        let (grpc, pubkey) = if let Some(mut grpc_builder) = self.grpc_builder {
            if let Some(timeout) = self.timeout {
                grpc_builder = grpc_builder.timeout(timeout)
            };
            if let Some(connect_timeout) = self.connect_timeout {
                grpc_builder = grpc_builder.connect_timeout(connect_timeout)
            };
            let client = grpc_builder.build()?;
            let pubkey = client.get_account_pubkey();
            (Some(client), pubkey)
        } else {
            (None, None)
        };

        // Only run the background health-check when there is a preferred endpoint
        // to switch back to.
        let health_check_interval = (rpc_endpoints.len() > 1).then(|| {
            self.rpc_health_check_interval
                .unwrap_or(celestia_rpc::DEFAULT_HEALTH_CHECK_INTERVAL)
        });

        let max_head_age = self
            .rpc_max_head_age
            .unwrap_or(celestia_rpc::DEFAULT_MAX_HEAD_AGE);

        let rpc = FailoverClient::new(
            rpc_endpoints,
            self.connect_timeout,
            self.timeout,
            health_check_interval,
            max_head_age,
        )?;

        let head = rpc.header_network_head().await?;
        head.validate()?;

        if let Some(grpc) = &grpc
            && &grpc.chain_id().await? != head.chain_id()
        {
            return Err(Error::ChainIdMissmatch);
        }

        let inner = Arc::new(ClientInner {
            rpc,
            grpc,
            pubkey,
            chain_id: head.chain_id().to_owned(),
        });

        let fibre = self
            .fibre_client
            .map(|fc| crate::fibre::FibreApi::new(inner.clone(), fc));

        Ok(Client {
            inner: inner.clone(),
            blob: BlobApi::new(inner.clone()),
            header: HeaderApi::new(inner.clone()),
            share: ShareApi::new(inner.clone()),
            fraud: FraudApi::new(inner.clone()),
            blobstream: BlobstreamApi::new(inner.clone()),
            state: StateApi::new(inner.clone()),
            fibre,
        })
    }
}

impl Debug for Client {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Client { .. }")
    }
}

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

    use lumina_utils::test_utils::async_test;

    use crate::test_utils::{TEST_PRIV_KEY, TEST_RPC_URL};

    #[async_test]
    async fn reject_fibre_blob_submission() {
        use crate::tx::TxConfig;
        use crate::types::Blob;
        use crate::types::blob::RawBlob;
        use crate::types::nmt::Namespace;

        let grpc = GrpcClient::builder()
            .endpoint("http://127.0.0.1:1")
            .timeout(Duration::from_millis(100))
            .build()
            .unwrap();
        let rpc = FailoverClient::new(
            vec![RpcEndpoint::new("http://127.0.0.1:1")],
            None,
            Some(Duration::from_millis(100)),
            None,
            celestia_rpc::DEFAULT_MAX_HEAD_AGE,
        )
        .unwrap();
        let inner = Arc::new(ClientInner {
            rpc,
            grpc: Some(grpc),
            pubkey: None,
            chain_id: "private".parse().unwrap(),
        });
        let blob_api = BlobApi::new(inner.clone());
        let state_api = StateApi::new(inner);
        let namespace = Namespace::new_v0(b"fibre").unwrap();
        let fibre = Blob::from_raw(RawBlob {
            namespace_id: namespace.id().to_vec(),
            namespace_version: 0,
            share_version: 2,
            data: vec![0; 36],
            signer: vec![0xAA; 20],
        })
        .unwrap();
        let unsigned = Blob::new(fibre.namespace, vec![1], None).unwrap();
        let signed = Blob::new(fibre.namespace, vec![2], fibre.signer).unwrap();

        let blobs = [unsigned, signed, fibre];
        let blob_error = blob_api
            .submit(&blobs, TxConfig::default())
            .await
            .unwrap_err();
        let state_error = state_api
            .submit_pay_for_blob(&blobs, TxConfig::default())
            .await
            .unwrap_err();
        for error in [blob_error, state_error] {
            assert!(matches!(
                error,
                Error::Grpc(celestia_grpc::Error::CelestiaTypesError(
                    celestia_types::Error::FibreBlobSubmission
                ))
            ));
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn read_blobs_after_blob_and_pay_for_fibre_in_same_namespace() {
        use celestia_fibre::{FibreClient, FibreClientConfig};

        use crate::test_utils::{new_client, node0_client};
        use crate::tx::{SigningKey, TxConfig};
        use crate::types::Blob;
        use crate::types::nmt::Namespace;

        let blob_client = new_client().await;
        let (_node0_lock, node0) = node0_client().await;
        let namespace = Namespace::new_v0(b"same-ns").unwrap();
        let blob = Blob::new(
            namespace,
            b"normal blob".to_vec(),
            Some(blob_client.address().unwrap()),
        )
        .unwrap();

        let signer_key =
            SigningKey::from_slice(&hex::decode(TEST_PRIV_KEY.trim()).unwrap()).unwrap();
        let signer_address = node0.address().unwrap();
        let blob_grpc = blob_client.inner.grpc().unwrap();
        let fibre_grpc = node0.inner.grpc().unwrap();
        let fibre_client = FibreClient::from_grpc_client(
            fibre_grpc.clone(),
            FibreClientConfig::new(node0.chain_id().as_str()).unwrap(),
        )
        .unwrap();
        let mut common_block = None;

        for _ in 0..5 {
            // a fresh upload per attempt: the promise embeds the height
            let msg = fibre_client
                .upload_and_prepare(&signer_key, namespace, b"fibre blob", &signer_address)
                .await
                .unwrap();
            let promise = msg.payment_promise.clone().unwrap();
            let (blob_tx, fibre_tx) = tokio::join!(
                blob_grpc.broadcast_blobs(std::slice::from_ref(&blob), TxConfig::default()),
                fibre_grpc.broadcast_message(msg, TxConfig::default()),
            );
            let (blob_info, fibre_info) =
                tokio::join!(blob_tx.unwrap().confirm(), fibre_tx.unwrap().confirm());
            let (blob_height, fibre_height) =
                (blob_info.unwrap().height, fibre_info.unwrap().height);

            if blob_height == fibre_height {
                common_block = Some((blob_height, promise));
                break;
            }
        }

        let (height, promise) =
            common_block.expect("transactions were not included in the same block");
        let blobs = blob_client
            .blob()
            .get_all(height, &[namespace])
            .await
            .unwrap()
            .unwrap();
        assert_eq!(blobs.len(), 2);

        let ordinary = blobs
            .iter()
            .find(|returned| returned.share_version == blob.share_version)
            .expect("ordinary blob missing");
        assert_eq!(ordinary.namespace, blob.namespace);
        assert_eq!(ordinary.data, blob.data);
        assert_eq!(ordinary.signer, blob.signer);
        assert_eq!(ordinary.commitment, blob.commitment);

        let fibre = blobs
            .iter()
            .find(|returned| returned.share_version == 2)
            .expect("Fibre blob missing");
        assert_eq!(fibre.namespace, namespace);
        assert_eq!(fibre.signer, Some(signer_address));
        assert_eq!(fibre.fibre_blob_version(), Some(promise.blob_version));
        assert_eq!(
            fibre.fibre_commitment().map(|c| c.to_vec()),
            Some(promise.commitment)
        );
    }

    #[async_test]
    async fn builder() {
        let e = Client::builder()
            .rpc_url(TEST_RPC_URL)
            .private_key_hex(TEST_PRIV_KEY)
            .build()
            .await
            .unwrap_err();
        assert!(matches!(e, Error::GrpcEndpointNotSet))
    }

    #[test]
    fn connect_timeout_is_independent_of_timeout() {
        // opt-in: defaults to None
        assert!(ClientBuilder::default().connect_timeout.is_none());

        // request timeout does not set the connect timeout
        let builder = ClientBuilder::default().timeout(Duration::from_secs(30));
        assert_eq!(builder.timeout, Some(Duration::from_secs(30)));
        assert!(builder.connect_timeout.is_none());

        // connect timeout is set independently
        let builder = ClientBuilder::default().connect_timeout(Duration::from_secs(2));
        assert_eq!(builder.connect_timeout, Some(Duration::from_secs(2)));
        assert!(builder.timeout.is_none());
    }
}