taceo-nodes-common 0.7.3

Collection of common functions used by nodes in our MPC networks
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
//! HTTP RPC provider utilities for interacting with Ethereum nodes.
//!
//! This module provides configurable HTTP RPC providers built on top of
//! [`alloy`] transports. It supports:
//!
//! - HTTP RPC with automatic retry and exponential backoff
//! - Multiple HTTP endpoints with automatic failover
//! - Optional wallet integration for transaction signing
//!
//! Use [`HttpRpcProviderBuilder`] to build an HTTP RPC provider.
//! HTTP transports are wrapped with retry and fallback layers to improve
//! reliability when interacting with RPC endpoints.
use core::fmt;
use std::{
    num::NonZeroUsize,
    ops::Deref,
    task::{Context, Poll},
    time::Duration,
};

use alloy::{
    network::EthereumWallet,
    primitives::ChainId,
    providers::{
        DynProvider, Provider, ProviderBuilder,
        fillers::{BlobGasFiller, ChainIdFiller, NonceManager, SimpleNonceManager},
    },
    rpc::{
        client::RpcClient,
        json_rpc::{RequestPacket, ResponsePacket},
    },
    transports::{
        RpcError, Transport, TransportError, TransportErrorKind, TransportFut,
        http::{
            Http,
            reqwest::{self, IntoUrl, Url},
        },
        layers::{FallbackLayer, OrRetryPolicyFn, RateLimitRetryPolicy, RetryPolicy},
    },
};
use backon::{ExponentialBuilder, Retryable as _};
use serde::Deserialize;
use tower::{Layer, Service};

use crate::Environment;

pub mod erc165;
pub mod event_stream;

/// A dedicated HTTP RPC provider.
///
/// This provider should be used for regular RPC calls, transaction
/// submission, and helpers such as ERC-165 queries.
#[derive(Clone)]
pub struct HttpRpcProvider(DynProvider);

/// Helper struct to redact the URLs when debug printing the config.
///
/// We don't use secret-string, because we want URL validation during config deserialization to fail early.
#[derive(Clone, Deserialize)]
#[serde(transparent)]
pub struct UrlRedacted(Url);

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

/// Configuration for building an [`HttpRpcProvider`].
///
/// Multiple HTTP endpoints can be provided to enable automatic failover.
/// Retry behavior can be tuned via [`RetryPolicyConfig`].
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct HttpRpcProviderConfig {
    /// List of HTTP RPC endpoints used for requests.
    ///
    /// Uses alloy's [`FallbackService`](https://docs.rs/alloy/latest/alloy/providers/transport/layers/struct.FallbackLayer.html) and configures each endpoint as one potential transport.
    pub http_urls: Vec<UrlRedacted>,
    /// Optional chain ID used by the provider.
    ///
    /// If provided, the [`ChainIdFiller`] will automatically populate
    /// transactions with this value.
    #[serde(default)]
    pub chain_id: Option<ChainId>,
    /// The timeout for HTTP requests to the RPC.
    ///
    /// Defaults to **10 seconds**.
    #[serde(default = "HttpRpcProviderConfig::default_timeout")]
    #[serde(with = "humantime_serde")]
    pub timeout: Duration,
    /// The poll interval for the confirmation heartbeat for alloy.
    ///
    /// Uses alloy's default setting if omitted. For `dev` environment 250ms
    /// and for all other environments 7s.
    #[serde(default)]
    #[serde(with = "humantime_serde")]
    pub confirmations_poll_interval: Option<Duration>,
    /// Retry configuration applied to RPC requests.
    #[serde(default)]
    pub retry_policy_config: RetryPolicyConfig,
}

/// Configuration for RPC retry behavior.
///
/// Requests that fail with retryable errors will be retried using
/// exponential backoff.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct RetryPolicyConfig {
    /// Minimum delay between retries.
    ///
    /// Defaults to **1 second**.
    #[serde(default = "RetryPolicyConfig::default_min_delay")]
    #[serde(with = "humantime_serde")]
    pub min_delay: Duration,

    /// Maximum delay between retries.
    ///
    /// Defaults to **8 seconds**.
    #[serde(default = "RetryPolicyConfig::default_max_delay")]
    #[serde(with = "humantime_serde")]
    pub max_delay: Duration,

    /// Maximum number of retry attempts.
    ///
    /// Defaults to **5 retries**.
    #[serde(default = "RetryPolicyConfig::default_max_times")]
    pub max_times: usize,
}

impl HttpRpcProviderConfig {
    /// Creates a new configuration using default retry settings.
    ///
    /// # Errors
    ///
    /// Returns an error if any of the provided URLs cannot be parsed.
    pub fn with_default_values<I, U>(http_urls: I) -> reqwest::Result<Self>
    where
        I: IntoIterator<Item = U>,
        U: IntoUrl,
    {
        let http_urls = http_urls
            .into_iter()
            .map(|x| x.into_url().map(UrlRedacted))
            .collect::<reqwest::Result<Vec<_>>>()?;
        Ok(Self {
            http_urls,
            timeout: Self::default_timeout(),
            confirmations_poll_interval: None,
            chain_id: None,
            retry_policy_config: RetryPolicyConfig::default(),
        })
    }

    /// Default timeout for HTTP requests to the RPC: 10 seconds
    fn default_timeout() -> Duration {
        Duration::from_secs(10)
    }
}

impl RetryPolicyConfig {
    /// Default minimum delay between retries: 1 second
    fn default_min_delay() -> Duration {
        Duration::from_secs(1)
    }

    /// Default maximum delay between retries: 8 seconds
    fn default_max_delay() -> Duration {
        Duration::from_secs(8)
    }

    /// Default maximum number of retry attempts: 5
    fn default_max_times() -> usize {
        5
    }

    /// Initialize a `RetryPolicyConfig` with default values
    fn with_default_values() -> Self {
        Self {
            min_delay: Self::default_min_delay(),
            max_delay: Self::default_max_delay(),
            max_times: Self::default_max_times(),
        }
    }
}

impl Default for RetryPolicyConfig {
    fn default() -> Self {
        Self::with_default_values()
    }
}

fn build_transport_stack<S>(
    transports: Vec<S>,
    retry_policy_config: &RetryPolicyConfig,
) -> impl Transport + Clone
where
    S: Service<RequestPacket, Response = ResponsePacket, Error = TransportError>
        + Clone
        + Send
        + Sync
        + 'static,
    S::Future: Send,
{
    let retry_layer = RetryLayer::new(http_retry_policy(), retry_policy_config);
    let retrying_transports = transports
        .into_iter()
        .map(|transport| retry_layer.layer(transport))
        .collect::<Vec<_>>();
    let transport_count =
        NonZeroUsize::new(retrying_transports.len()).expect("transport stack must not be empty");

    // Retry each transport before fallback so JSON-RPC error responses cannot
    // win the fallback race against a slower healthy endpoint.
    FallbackLayer::default()
        .with_active_transport_count(transport_count)
        .layer(retrying_transports)
}

fn http_retry_policy() -> OrRetryPolicyFn {
    // Configure retry policy.
    //
    // The RateLimitRetryPolicy already handles 503 Service Unavailable and other common RPC errors.
    // We additionally check for other common transient errors:
    //   - 403 Forbidden
    //   - 408 Request Timeout
    //   - 502 Bad Gateway
    //   - 504 Gateway Timeout
    RateLimitRetryPolicy::default().or(|error: &TransportError| match error {
        RpcError::Transport(TransportErrorKind::HttpError(e)) => {
            matches!(e.status, 403 | 408 | 502 | 504)
        }
        RpcError::Transport(kind) => kind
            .as_custom()
            .and_then(|error| error.downcast_ref::<reqwest::Error>())
            .is_some_and(reqwest::Error::is_timeout),
        _ => false,
    })
}

/// Builder for constructing an [`HttpRpcProvider`].
///
/// The builder configures retry behavior, fallback transports, optional
/// wallet integration, and provider fillers before creating the provider.
pub struct HttpRpcProviderBuilder {
    http_urls: Vec<UrlRedacted>,
    retry_policy_config: RetryPolicyConfig,
    chain_id: Option<ChainId>,
    timeout: Duration,
    confirmations_poll_interval: Option<Duration>,
    is_local: bool,
    wallet: Option<EthereumWallet>,
}

impl From<HttpRpcProviderConfig> for HttpRpcProviderBuilder {
    fn from(value: HttpRpcProviderConfig) -> Self {
        Self::from(&value)
    }
}

impl From<&HttpRpcProviderConfig> for HttpRpcProviderBuilder {
    fn from(value: &HttpRpcProviderConfig) -> Self {
        Self::with_config(value)
    }
}

impl HttpRpcProviderBuilder {
    /// Creates a new builder from the given configuration.
    ///
    /// # Panics
    ///
    /// Panics if `config.http_urls` is empty. At least one HTTP endpoint
    /// must be provided so that a transport stack can be constructed.
    #[must_use]
    pub fn with_config(config: &HttpRpcProviderConfig) -> Self {
        assert!(!config.http_urls.is_empty(), "http URLs must not be empty");
        Self {
            http_urls: config.http_urls.clone(),
            retry_policy_config: config.retry_policy_config.clone(),
            timeout: config.timeout,
            chain_id: config.chain_id,
            is_local: false,
            wallet: None,
            confirmations_poll_interval: config.confirmations_poll_interval,
        }
    }

    /// Creates a new builder using default retry settings.
    ///
    /// # Errors
    ///
    /// Returns an error if any of the provided URLs cannot be parsed.
    ///
    /// # Example
    ///
    /// ```
    /// use taceo_nodes_common::web3::HttpRpcProviderBuilder;
    ///
    /// let builder = HttpRpcProviderBuilder::with_default_values(["http://127.0.0.1:8545"])?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn with_default_values<I, U>(http_urls: I) -> reqwest::Result<Self>
    where
        I: IntoIterator<Item = U>,
        U: IntoUrl,
    {
        Ok(Self::with_config(
            &HttpRpcProviderConfig::with_default_values(http_urls)?,
        ))
    }

    /// Configures the environment used by the provider.
    #[must_use]
    pub fn environment(mut self, environment: Environment) -> Self {
        self.is_local = environment.is_dev();
        self
    }

    /// Sets the timeout for HTTP RPC requests.
    #[must_use]
    pub fn http_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the poll interval in which alloy fetches blocks for transaction confirmations.
    #[must_use]
    pub fn confirmations_poll_interval(mut self, confirmations_poll_interval: Duration) -> Self {
        self.confirmations_poll_interval = Some(confirmations_poll_interval);
        self
    }

    /// Sets the chain ID used by the provider.
    #[must_use]
    pub fn chain_id(mut self, chain_id: ChainId) -> Self {
        self.chain_id = Some(chain_id);
        self
    }

    /// Configures the retry behavior for HTTP RPC requests.
    #[must_use]
    pub fn retry_policy(mut self, retry_policy_config: RetryPolicyConfig) -> Self {
        self.retry_policy_config = retry_policy_config;
        self
    }

    /// Adds a wallet used for signing transactions.
    #[must_use]
    pub fn wallet(mut self, wallet: EthereumWallet) -> Self {
        self.wallet = Some(wallet);
        self
    }

    /// Builds the [`HttpRpcProvider`].
    ///
    /// Uses [`SimpleNonceManager::default()`] for nonce management. Use
    /// [`Self::build_with_nonce_manager`] to provide a custom nonce manager.
    ///
    /// # Errors
    ///
    /// Returns a [`TransportError`] if the HTTP transport stack cannot be
    /// initialized, including failures to create the underlying reqwest client.
    pub fn build(self) -> Result<HttpRpcProvider, TransportError> {
        self.build_with_nonce_manager(SimpleNonceManager::default())
    }

    /// Builds the [`HttpRpcProvider`] using the provided nonce manager.
    ///
    /// This allows callers to customize how transaction nonces are tracked
    /// while keeping the rest of the builder configuration unchanged.
    ///
    /// # Errors
    ///
    /// Returns a [`TransportError`] if the HTTP transport stack cannot be
    /// initialized, including failures to create the underlying reqwest client.
    pub fn build_with_nonce_manager<N: NonceManager + 'static>(
        self,
        nonce_manager: N,
    ) -> Result<HttpRpcProvider, TransportError> {
        let HttpRpcProviderBuilder {
            http_urls,
            retry_policy_config,
            chain_id,
            timeout,
            is_local,
            wallet,
            confirmations_poll_interval,
        } = self;

        let reqwest = reqwest::ClientBuilder::new()
            .timeout(timeout)
            .build()
            .map_err(TransportErrorKind::custom)?;

        let transports = http_urls
            .into_iter()
            .map(|url| Http::with_client(reqwest.clone(), url.0))
            .collect::<Vec<_>>();
        let transport = build_transport_stack(transports, &retry_policy_config);

        let client = RpcClient::builder().transport(transport, is_local);
        let client = if let Some(confirmations_poll_interval) = confirmations_poll_interval {
            client.with_poll_interval(confirmations_poll_interval)
        } else {
            client
        };

        let http_provider_builder = ProviderBuilder::new()
            .filler(ChainIdFiller::new(chain_id))
            .filler(BlobGasFiller::default())
            .with_nonce_management(nonce_manager)
            .with_gas_estimation();

        let provider = if let Some(wallet) = wallet {
            http_provider_builder
                .wallet(wallet)
                .connect_client(client)
                .erased()
        } else {
            http_provider_builder.connect_client(client).erased()
        };

        Ok(HttpRpcProvider(provider))
    }
}

impl HttpRpcProvider {
    /// Returns the HTTP RPC provider.
    #[must_use]
    #[inline]
    pub fn inner(&self) -> DynProvider {
        self.0.clone()
    }
}

impl AsRef<DynProvider> for HttpRpcProvider {
    fn as_ref(&self) -> &DynProvider {
        self
    }
}

impl Deref for HttpRpcProvider {
    type Target = DynProvider;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug, Clone)]
struct RetryLayer {
    policy: OrRetryPolicyFn,
    backoff: ExponentialBuilder,
}

impl RetryLayer {
    /// Creates a new retry layer using the provided retry policy and configuration.
    ///
    /// The retry behavior is implemented using exponential backoff with jitter.
    ///
    /// The following parameters are taken from [`RetryPolicyConfig`]:
    ///
    /// - minimum retry delay
    /// - maximum retry delay
    /// - maximum number of retry attempts
    pub fn new(policy: OrRetryPolicyFn, config: &RetryPolicyConfig) -> Self {
        let backoff = ExponentialBuilder::default()
            .with_min_delay(config.min_delay)
            .with_max_delay(config.max_delay)
            .with_max_times(config.max_times)
            .with_jitter();
        Self { policy, backoff }
    }
}

impl<S> Layer<S> for RetryLayer {
    type Service = RetryService<S>;

    fn layer(&self, inner: S) -> Self::Service {
        RetryService {
            inner,
            policy: self.policy.clone(),
            backoff: self.backoff,
        }
    }
}

/// Tower service that wraps each request in a retry loop with exponential backoff.
#[derive(Debug, Clone)]
struct RetryService<S> {
    inner: S,
    policy: OrRetryPolicyFn,
    backoff: ExponentialBuilder,
}

impl<S> Service<RequestPacket> for RetryService<S>
where
    S: Service<RequestPacket, Response = ResponsePacket, Error = TransportError>
        + Clone
        + Send
        + Sync
        + 'static,
    S::Future: Send,
{
    type Response = ResponsePacket;
    type Error = TransportError;
    type Future = TransportFut<'static>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, request: RequestPacket) -> Self::Future {
        let service = self.clone();
        let backoff = self.backoff;
        let policy = self.policy.clone();

        Box::pin(async move {
            (|| service.clone().call_and_parse_error(request.clone()))
                .retry(backoff)
                .sleep(tokio::time::sleep)
                .when(|e| policy.should_retry(e))
                .notify(|_, duration| tracing::debug!("Retrying RPC request after: {duration:?}"))
                // Adjust the backoff duration based on the policy and the current hint:
                // - If `dur` is `None`, we stop retrying (max attempts reached).
                // - If `dur` is `Some(d)` and the policy provides a backoff hint, use the policy hint.
                // - If `dur` is `Some(d)` and the policy hint is `None`, use the original `d`.
                .adjust(|e, dur| dur.and_then(|d| policy.backoff_hint(e).or(Some(d))))
                .await
        })
    }
}

impl<S> RetryService<S>
where
    S: Service<RequestPacket, Response = ResponsePacket, Error = TransportError>
        + Clone
        + Send
        + Sync
        + 'static,
    S::Future: Send,
{
    async fn call_and_parse_error(
        mut self,
        request: RequestPacket,
    ) -> Result<ResponsePacket, RpcError<TransportErrorKind>> {
        let resp = self.inner.call(request).await?;
        if let Some(e) = resp.as_error() {
            Err(TransportError::ErrorResp(e.to_owned()))
        } else {
            Ok(resp)
        }
    }
}

#[cfg(test)]
pub(crate) mod tests;