hopr_lib/
lib.rs

1//! HOPR library creating a unified [`Hopr`] object that can be used on its own,
2//! as well as integrated into other systems and libraries.
3//!
4//! The [`Hopr`] object is standalone, meaning that once it is constructed and run,
5//! it will perform its functionality autonomously. The API it offers serves as a
6//! high-level integration point for other applications and utilities, but offers
7//! a complete and fully featured HOPR node stripped from top level functionality
8//! such as the REST API, key management...
9//!
10//! The intended way to use hopr_lib is for a specific tool to be built on top of it;
11//! should the default `hoprd` implementation not be acceptable.
12//!
13//! For most of the practical use cases, the `hoprd` application should be a preferable
14//! choice.
15
16/// Helper functions.
17mod helpers;
18
19/// Configuration-related public types
20pub mod config;
21/// Various public constants.
22pub mod constants;
23/// Lists all errors thrown from this library.
24pub mod errors;
25
26/// Utility module with helper types and functionality over hopr-lib behavior.
27pub mod utils;
28
29/// Public traits for interactions with this library.
30pub mod traits;
31
32/// Functionality related to the HOPR node state.
33pub mod state;
34
35#[cfg(any(feature = "testing", test))]
36pub mod testing;
37
38pub use hopr_api as api;
39
40/// Exports of libraries necessary for API and interface operations.
41#[doc(hidden)]
42pub mod exports {
43    pub mod types {
44        pub use hopr_chain_types as chain;
45        pub use hopr_internal_types as internal;
46        pub use hopr_primitive_types as primitive;
47    }
48
49    pub mod crypto {
50        pub use hopr_crypto_keypair as keypair;
51        pub use hopr_crypto_types as types;
52    }
53
54    pub mod network {
55        pub use hopr_network_types as types;
56    }
57
58    pub use hopr_transport as transport;
59}
60
61/// Export of relevant types for easier integration.
62#[doc(hidden)]
63pub mod prelude {
64    pub use super::exports::{
65        crypto::{
66            keypair::key_pair::HoprKeys,
67            types::prelude::{ChainKeypair, Hash, OffchainKeypair},
68        },
69        network::types::{
70            prelude::ForeignDataMode,
71            udp::{ConnectedUdpStream, UdpStreamParallelism},
72        },
73        transport::{OffchainPublicKey, socket::HoprSocket},
74        types::primitive::prelude::Address,
75    };
76}
77
78use std::{
79    convert::identity,
80    future::Future,
81    sync::{Arc, OnceLock, atomic::Ordering},
82    time::Duration,
83};
84
85use futures::{FutureExt, SinkExt, Stream, StreamExt, TryFutureExt, channel::mpsc::channel, pin_mut};
86use futures_time::future::FutureExt as FuturesTimeFutureExt;
87use hopr_api::{
88    chain::{AccountSelector, AnnouncementError, ChannelSelector, *},
89    ct::TrafficGeneration,
90    db::{HoprNodeDbApi, TicketMarker, TicketSelector},
91};
92pub use hopr_api::{db::ChannelTicketStatistics, network::Observable};
93use hopr_async_runtime::prelude::spawn;
94pub use hopr_async_runtime::{Abortable, AbortableList};
95pub use hopr_crypto_keypair::key_pair::{HoprKeys, IdentityRetrievalModes};
96pub use hopr_crypto_types::prelude::*;
97pub use hopr_internal_types::prelude::*;
98pub use hopr_network_types::prelude::*;
99#[cfg(all(feature = "prometheus", not(test)))]
100use hopr_platform::time::native::current_time;
101pub use hopr_primitive_types::prelude::*;
102use hopr_transport::errors::HoprTransportError;
103#[cfg(feature = "runtime-tokio")]
104pub use hopr_transport::transfer_session;
105pub use hopr_transport::*;
106use tracing::{debug, error, info, warn};
107use validator::Validate;
108
109pub use crate::{
110    config::SafeModule,
111    constants::{MIN_NATIVE_BALANCE, SUGGESTED_NATIVE_BALANCE},
112    errors::{HoprLibError, HoprStatusError},
113    state::{HoprLibProcess, HoprState},
114    traits::chain::{CloseChannelResult, OpenChannelResult},
115};
116
117#[cfg(all(feature = "prometheus", not(test)))]
118lazy_static::lazy_static! {
119    static ref METRIC_PROCESS_START_TIME:  hopr_metrics::SimpleGauge =  hopr_metrics::SimpleGauge::new(
120        "hopr_start_time",
121        "The unix timestamp in seconds at which the process was started"
122    ).unwrap();
123    static ref METRIC_HOPR_LIB_VERSION:  hopr_metrics::MultiGauge =  hopr_metrics::MultiGauge::new(
124        "hopr_lib_version",
125        "Executed version of hopr-lib",
126        &["version"]
127    ).unwrap();
128    static ref METRIC_HOPR_NODE_INFO:  hopr_metrics::MultiGauge =  hopr_metrics::MultiGauge::new(
129        "hopr_node_addresses",
130        "Node on-chain and off-chain addresses",
131        &["peerid", "address", "safe_address", "module_address"]
132    ).unwrap();
133}
134
135/// Prepare an optimized version of the tokio runtime setup for hopr-lib specifically.
136///
137/// Divide the available CPU parallelism by 2, since half of the available threads are
138/// to be used for IO-bound and half for CPU-bound tasks.
139#[cfg(feature = "runtime-tokio")]
140pub fn prepare_tokio_runtime(
141    num_cpu_threads: Option<std::num::NonZeroUsize>,
142    num_io_threads: Option<std::num::NonZeroUsize>,
143) -> anyhow::Result<tokio::runtime::Runtime> {
144    use std::str::FromStr;
145    let avail_parallelism = std::thread::available_parallelism().ok().map(|v| v.get() / 2);
146
147    hopr_parallelize::cpu::init_thread_pool(
148        num_cpu_threads
149            .map(|v| v.get())
150            .or(avail_parallelism)
151            .ok_or(anyhow::anyhow!(
152                "Could not determine the number of CPU threads to use. Please set the HOPRD_NUM_CPU_THREADS \
153                 environment variable."
154            ))?
155            .max(1),
156    )?;
157
158    Ok(tokio::runtime::Builder::new_multi_thread()
159        .enable_all()
160        .worker_threads(
161            num_io_threads
162                .map(|v| v.get())
163                .or(avail_parallelism)
164                .ok_or(anyhow::anyhow!(
165                    "Could not determine the number of IO threads to use. Please set the HOPRD_NUM_IO_THREADS \
166                     environment variable."
167                ))?
168                .max(1),
169        )
170        .thread_name("hoprd")
171        .thread_stack_size(
172            std::env::var("HOPRD_THREAD_STACK_SIZE")
173                .ok()
174                .and_then(|v| usize::from_str(&v).ok())
175                .unwrap_or(10 * 1024 * 1024)
176                .max(2 * 1024 * 1024),
177        )
178        .build()?)
179}
180
181/// Type alias used to send and receive transport data via a running HOPR node.
182pub type HoprTransportIO = socket::HoprSocket<
183    futures::channel::mpsc::Receiver<ApplicationDataIn>,
184    futures::channel::mpsc::Sender<(DestinationRouting, ApplicationDataOut)>,
185>;
186
187type NewTicketEvents = (
188    async_broadcast::Sender<VerifiedTicket>,
189    async_broadcast::InactiveReceiver<VerifiedTicket>,
190);
191
192/// Time to wait until the node's keybinding appears on-chain
193const NODE_READY_TIMEOUT: Duration = Duration::from_secs(120);
194
195/// Timeout to wait until an on-chain event is received in response to a successful on-chain operation resolution.
196// TODO: use the value from ChainInfo instead (once available via https://github.com/hoprnet/blokli/issues/200)
197const ON_CHAIN_RESOLUTION_EVENT_TIMEOUT: Duration = Duration::from_secs(90);
198
199/// HOPR main object providing the entire HOPR node functionality
200///
201/// Instantiating this object creates all processes and objects necessary for
202/// running the HOPR node. Once created, the node can be started using the
203/// `run()` method.
204///
205/// Externally offered API should be enough to perform all necessary tasks
206/// with the HOPR node manually, but it is advised to create such a configuration
207/// that manual interaction is unnecessary.
208///
209/// As such, the `hopr_lib` serves mainly as an integration point into Rust programs.
210pub struct Hopr<Chain, Db> {
211    me: OffchainKeypair,
212    cfg: config::HoprLibConfig,
213    state: Arc<state::AtomicHoprState>,
214    transport_api: HoprTransport<Chain, Db>,
215    redeem_requests: OnceLock<futures::channel::mpsc::Sender<TicketSelector>>,
216    node_db: Db,
217    chain_api: Chain,
218    winning_ticket_subscribers: NewTicketEvents,
219    processes: OnceLock<AbortableList<HoprLibProcess>>,
220}
221
222impl<Chain, Db> Hopr<Chain, Db>
223where
224    Chain: HoprChainApi + Clone + Send + Sync + 'static,
225    Db: HoprNodeDbApi + Clone + Send + Sync + 'static,
226{
227    pub async fn new(
228        identity: (&ChainKeypair, &OffchainKeypair),
229        hopr_chain_api: Chain,
230        hopr_node_db: Db,
231        cfg: config::HoprLibConfig,
232    ) -> errors::Result<Self> {
233        if hopr_crypto_random::is_rng_fixed() {
234            warn!("!! FOR TESTING ONLY !! THIS BUILD IS USING AN INSECURE FIXED RNG !!")
235        }
236
237        cfg.validate()?;
238
239        let hopr_transport_api = HoprTransport::new(
240            identity,
241            hopr_chain_api.clone(),
242            hopr_node_db.clone(),
243            vec![(&cfg.host).try_into().map_err(HoprLibError::TransportError)?],
244            cfg.protocol,
245        );
246
247        #[cfg(all(feature = "prometheus", not(test)))]
248        {
249            METRIC_PROCESS_START_TIME.set(current_time().as_unix_timestamp().as_secs_f64());
250            METRIC_HOPR_LIB_VERSION.set(
251                &[const_format::formatcp!("{}", constants::APP_VERSION)],
252                const_format::formatcp!(
253                    "{}.{}",
254                    env!("CARGO_PKG_VERSION_MAJOR"),
255                    env!("CARGO_PKG_VERSION_MINOR")
256                )
257                .parse()
258                .unwrap_or(0.0),
259            );
260
261            // Calling get_ticket_statistics will initialize the respective metrics on tickets
262            if let Err(error) = hopr_node_db.get_ticket_statistics(None).await {
263                error!(%error, "failed to initialize ticket statistics metrics");
264            }
265        }
266
267        let (mut new_tickets_tx, new_tickets_rx) = async_broadcast::broadcast(2048);
268        new_tickets_tx.set_await_active(false);
269        new_tickets_tx.set_overflow(true);
270
271        Ok(Self {
272            me: identity.1.clone(),
273            cfg,
274            state: Arc::new(state::AtomicHoprState::new(HoprState::Uninitialized)),
275            transport_api: hopr_transport_api,
276            chain_api: hopr_chain_api,
277            node_db: hopr_node_db,
278            redeem_requests: OnceLock::new(),
279            processes: OnceLock::new(),
280            winning_ticket_subscribers: (new_tickets_tx, new_tickets_rx.deactivate()),
281        })
282    }
283
284    fn error_if_not_in_state(&self, state: HoprState, error: String) -> errors::Result<()> {
285        if self.status() == state {
286            Ok(())
287        } else {
288            Err(HoprLibError::StatusError(HoprStatusError::NotThereYet(state, error)))
289        }
290    }
291
292    pub fn status(&self) -> HoprState {
293        self.state.load(Ordering::Relaxed)
294    }
295
296    pub async fn get_balance<C: Currency + Send>(&self) -> errors::Result<Balance<C>> {
297        self.chain_api
298            .balance(self.me_onchain())
299            .await
300            .map_err(HoprLibError::chain)
301    }
302
303    pub async fn get_safe_balance<C: Currency + Send>(&self) -> errors::Result<Balance<C>> {
304        self.chain_api
305            .balance(self.cfg.safe_module.safe_address)
306            .await
307            .map_err(HoprLibError::chain)
308    }
309
310    pub async fn chain_info(&self) -> errors::Result<ChainInfo> {
311        self.chain_api.chain_info().await.map_err(HoprLibError::chain)
312    }
313
314    pub fn get_safe_config(&self) -> SafeModule {
315        self.cfg.safe_module.clone()
316    }
317
318    pub fn config(&self) -> &config::HoprLibConfig {
319        &self.cfg
320    }
321
322    #[inline]
323    fn is_public(&self) -> bool {
324        self.cfg.publish
325    }
326
327    pub async fn run<
328        Ct,
329        #[cfg(feature = "session-server")] T: traits::session::HoprSessionServer + Clone + Send + 'static,
330    >(
331        &self,
332        cover_traffic: Ct,
333        #[cfg(feature = "session-server")] serve_handler: T,
334    ) -> errors::Result<HoprTransportIO>
335    where
336        Ct: TrafficGeneration + Send + Sync + 'static,
337    {
338        self.error_if_not_in_state(
339            HoprState::Uninitialized,
340            "cannot start the hopr node multiple times".into(),
341        )?;
342
343        #[cfg(feature = "testing")]
344        warn!("!! FOR TESTING ONLY !! Node is running with some safety checks disabled!");
345
346        info!(
347            address = %self.me_onchain(), minimum_balance = %*SUGGESTED_NATIVE_BALANCE,
348            "node is not started, please fund this node",
349        );
350
351        self.state.store(HoprState::WaitingForFunds, Ordering::Relaxed);
352        helpers::wait_for_funds(
353            *MIN_NATIVE_BALANCE,
354            *SUGGESTED_NATIVE_BALANCE,
355            Duration::from_secs(200),
356            self.me_onchain(),
357            &self.chain_api,
358        )
359        .await?;
360
361        let mut processes = AbortableList::<HoprLibProcess>::default();
362
363        info!("starting HOPR node...");
364        self.state.store(HoprState::CheckingBalance, Ordering::Relaxed);
365
366        let balance: XDaiBalance = self.get_balance().await?;
367        let minimum_balance = *constants::MIN_NATIVE_BALANCE;
368
369        info!(
370            address = %self.me_onchain(),
371            %balance,
372            %minimum_balance,
373            "node information"
374        );
375
376        if balance.le(&minimum_balance) {
377            return Err(HoprLibError::GeneralError(
378                "cannot start the node without a sufficiently funded wallet".into(),
379            ));
380        }
381
382        self.state.store(HoprState::ValidatingNetworkConfig, Ordering::Relaxed);
383
384        // Once we are able to query the chain,
385        // check if the ticket price is configured correctly.
386        let network_min_ticket_price = self
387            .chain_api
388            .minimum_ticket_price()
389            .await
390            .map_err(HoprLibError::chain)?;
391        let configured_ticket_price = self.cfg.protocol.packet.codec.outgoing_ticket_price;
392        if configured_ticket_price.is_some_and(|c| c < network_min_ticket_price) {
393            return Err(HoprLibError::GeneralError(format!(
394                "configured outgoing ticket price is lower than the network minimum ticket price: \
395                 {configured_ticket_price:?} < {network_min_ticket_price}"
396            )));
397        }
398        // Once we are able to query the chain,
399        // check if the winning probability is configured correctly.
400        let network_min_win_prob = self
401            .chain_api
402            .minimum_incoming_ticket_win_prob()
403            .await
404            .map_err(HoprLibError::chain)?;
405        let configured_win_prob = self.cfg.protocol.packet.codec.outgoing_win_prob;
406        if !std::env::var("HOPR_TEST_DISABLE_CHECKS").is_ok_and(|v| v.to_lowercase() == "true")
407            && configured_win_prob.is_some_and(|c| c.approx_cmp(&network_min_win_prob).is_lt())
408        {
409            return Err(HoprLibError::GeneralError(format!(
410                "configured outgoing ticket winning probability is lower than the network minimum winning \
411                 probability: {configured_win_prob:?} < {network_min_win_prob}"
412            )));
413        }
414
415        // Calculate the minimum capacity based on accounts (each account can generate 2 messages),
416        // plus 100 as an additional buffer
417        let minimum_capacity = self
418            .chain_api
419            .count_accounts(AccountSelector {
420                public_only: true,
421                ..Default::default()
422            })
423            .await
424            .map_err(HoprLibError::chain)?
425            .saturating_mul(2)
426            .saturating_add(100);
427
428        let chain_discovery_events_capacity = std::env::var("HOPR_INTERNAL_CHAIN_DISCOVERY_CHANNEL_CAPACITY")
429            .ok()
430            .and_then(|s| s.trim().parse::<usize>().ok())
431            .filter(|&c| c > 0)
432            .unwrap_or(2048)
433            .max(minimum_capacity);
434
435        debug!(
436            capacity = chain_discovery_events_capacity,
437            minimum_required = minimum_capacity,
438            "creating chain discovery events channel"
439        );
440        let (indexer_peer_update_tx, indexer_peer_update_rx) =
441            channel::<PeerDiscovery>(chain_discovery_events_capacity);
442
443        self.state
444            .store(HoprState::SubscribingToAnnouncements, Ordering::Relaxed);
445
446        // Stream all the existing announcements and also subscribe to all future on-chain
447        // announcements
448        let (announcements_stream, announcements_handle) = futures::stream::abortable(
449            self.chain_api
450                .subscribe_with_state_sync([StateSyncOptions::PublicAccounts])
451                .map_err(HoprLibError::chain)?,
452        );
453        processes.insert(HoprLibProcess::AccountAnnouncements, announcements_handle);
454
455        spawn(
456            announcements_stream
457                .filter_map(|event| {
458                    futures::future::ready(event.try_as_announcement().map(|account| {
459                        PeerDiscovery::Announce(account.public_key.into(), account.get_multiaddrs().to_vec())
460                    }))
461                })
462                .map(Ok)
463                .forward(indexer_peer_update_tx)
464                .inspect(
465                    |_| warn!(task = %HoprLibProcess::AccountAnnouncements,"long-running background task finished"),
466                ),
467        );
468
469        info!(peer_id = %self.me_peer_id(), address = %self.me_onchain(), version = constants::APP_VERSION, "Node information");
470
471        let safe_addr = self.cfg.safe_module.safe_address;
472
473        if self.me_onchain() == safe_addr {
474            return Err(HoprLibError::GeneralError("cannot self as staking safe address".into()));
475        }
476
477        self.state.store(HoprState::RegisteringSafe, Ordering::Relaxed);
478        info!(%safe_addr, "registering safe with this node");
479        match self.chain_api.register_safe(&safe_addr).await {
480            Ok(awaiter) => {
481                // Wait until the registration is confirmed on-chain, otherwise we cannot proceed.
482                awaiter.await.map_err(|error| {
483                    error!(%safe_addr, %error, "safe registration failed with error");
484                    HoprLibError::chain(error)
485                })?;
486                info!(%safe_addr, "safe successfully registered with this node");
487            }
488            Err(SafeRegistrationError::AlreadyRegistered(registered_safe)) if registered_safe == safe_addr => {
489                info!(%safe_addr, "this safe is already registered with this node");
490            }
491            Err(SafeRegistrationError::AlreadyRegistered(registered_safe)) if registered_safe != safe_addr => {
492                // TODO: support safe deregistration flow
493                error!(%safe_addr, %registered_safe, "this node is currently registered with different safe");
494                return Err(HoprLibError::GeneralError("node registered with different safe".into()));
495            }
496            Err(error) => {
497                error!(%safe_addr, %error, "safe registration failed");
498                return Err(HoprLibError::chain(error));
499            }
500        }
501
502        // Only public nodes announce multiaddresses
503        let multiaddresses_to_announce = if self.is_public() {
504            // The multiaddresses are filtered for the non-private ones,
505            // unless `announce_local_addresses` is set to `true`.
506            self.transport_api.announceable_multiaddresses()
507        } else {
508            Vec::with_capacity(0)
509        };
510
511        // Warn when announcing a private multiaddress, which is acceptable in certain scenarios
512        multiaddresses_to_announce
513            .iter()
514            .filter(|a| !is_public_address(a))
515            .for_each(|multi_addr| warn!(?multi_addr, "announcing private multiaddress"));
516
517        self.state.store(HoprState::AnnouncingNode, Ordering::Relaxed);
518
519        let chain_api = self.chain_api.clone();
520        let me_offchain = *self.me.public();
521        let node_ready = spawn(async move { chain_api.await_key_binding(&me_offchain, NODE_READY_TIMEOUT).await });
522
523        // At this point the node is already registered with Safe, so
524        // we can announce via Safe-compliant TX
525        info!(?multiaddresses_to_announce, "announcing node on chain");
526        match self.chain_api.announce(&multiaddresses_to_announce, &self.me).await {
527            Ok(awaiter) => {
528                // Wait until the announcement is confirmed on-chain, otherwise we cannot proceed.
529                awaiter.await.map_err(|error| {
530                    error!(?multiaddresses_to_announce, %error, "node announcement failed");
531                    HoprLibError::chain(error)
532                })?;
533                info!(?multiaddresses_to_announce, "node has been successfully announced");
534            }
535            Err(AnnouncementError::AlreadyAnnounced) => {
536                info!(multiaddresses_announced = ?multiaddresses_to_announce, "node already announced on chain")
537            }
538            Err(error) => {
539                error!(%error, ?multiaddresses_to_announce, "failed to transmit node announcement");
540                return Err(HoprLibError::chain(error));
541            }
542        }
543
544        self.state.store(HoprState::AwaitingKeyBinding, Ordering::Relaxed);
545
546        // Wait for the node key-binding readiness to return
547        let this_node_account = node_ready
548            .await
549            .map_err(HoprLibError::other)?
550            .map_err(HoprLibError::chain)?;
551        if this_node_account.chain_addr != self.me_onchain()
552            || this_node_account.safe_address.is_none_or(|a| a != safe_addr)
553        {
554            error!(%this_node_account, "account bound to offchain key does not match this node");
555            return Err(HoprLibError::GeneralError("account key-binding mismatch".into()));
556        }
557
558        info!(%this_node_account, "node account is ready");
559
560        self.state.store(HoprState::InitializingServices, Ordering::Relaxed);
561
562        info!("initializing session infrastructure");
563        let incoming_session_channel_capacity = std::env::var("HOPR_INTERNAL_SESSION_INCOMING_CAPACITY")
564            .ok()
565            .and_then(|s| s.trim().parse::<usize>().ok())
566            .filter(|&c| c > 0)
567            .unwrap_or(256);
568
569        let (session_tx, _session_rx) = channel::<IncomingSession>(incoming_session_channel_capacity);
570        #[cfg(feature = "session-server")]
571        {
572            debug!(capacity = incoming_session_channel_capacity, "creating session server");
573            processes.insert(
574                HoprLibProcess::SessionServer,
575                hopr_async_runtime::spawn_as_abortable!(
576                    _session_rx
577                        .for_each_concurrent(None, move |session| {
578                            let serve_handler = serve_handler.clone();
579                            async move {
580                                let session_id = *session.session.id();
581                                match serve_handler.process(session).await {
582                                    Ok(_) => debug!(?session_id, "client session processed successfully"),
583                                    Err(error) => error!(
584                                        ?session_id,
585                                        %error,
586                                        "client session processing failed"
587                                    ),
588                                }
589                            }
590                        })
591                        .inspect(|_| tracing::warn!(
592                            task = %HoprLibProcess::SessionServer,
593                            "long-running background task finished"
594                        ))
595                ),
596            );
597        }
598
599        info!("starting ticket events processor");
600        let (tickets_tx, tickets_rx) = channel(8192);
601        let (tickets_rx, tickets_handle) = futures::stream::abortable(tickets_rx);
602        processes.insert(HoprLibProcess::TicketEvents, tickets_handle);
603        let node_db = self.node_db.clone();
604        let new_ticket_tx = self.winning_ticket_subscribers.0.clone();
605        spawn(
606            tickets_rx
607                .filter_map(move |ticket_event| {
608                    let node_db = node_db.clone();
609                    async move {
610                        match ticket_event {
611                            TicketEvent::WinningTicket(winning) => {
612                                if let Err(error) = node_db.insert_ticket(*winning).await {
613                                    tracing::error!(%error, %winning, "failed to insert ticket into database");
614                                } else {
615                                    tracing::debug!(%winning, "inserted ticket into database");
616                                }
617                                Some(winning)
618                            }
619                            TicketEvent::RejectedTicket(rejected, issuer) => {
620                                if let Some(issuer) = &issuer {
621                                    if let Err(error) =
622                                        node_db.mark_unsaved_ticket_rejected(issuer, rejected.as_ref()).await
623                                    {
624                                        tracing::error!(%error, %rejected, "failed to mark ticket as rejected");
625                                    } else {
626                                        tracing::debug!(%rejected, "marked ticket as rejected");
627                                    }
628                                } else {
629                                    tracing::debug!(%rejected, "issuer of the rejected ticket could not be determined");
630                                }
631                                None
632                            }
633                        }
634                    }
635                })
636                .for_each(move |ticket| {
637                    if let Err(error) = new_ticket_tx.try_broadcast(ticket.ticket) {
638                        tracing::error!(%error, "failed to broadcast new winning ticket to subscribers");
639                    }
640                    futures::future::ready(())
641                })
642                .inspect(|_| {
643                    tracing::warn!(
644                        task = %HoprLibProcess::TicketEvents,
645                        "long-running background task finished"
646                    )
647                }),
648        );
649
650        info!("starting transport");
651        let (hopr_socket, transport_processes) = self
652            .transport_api
653            .run(cover_traffic, indexer_peer_update_rx, tickets_tx, session_tx)
654            .await?;
655        processes.flat_map_extend_from(transport_processes, HoprLibProcess::Transport);
656
657        info!("starting ticket redemption service");
658        // Start a queue that takes care of redeeming tickets via given TicketSelectors
659        let (redemption_req_tx, redemption_req_rx) = channel::<TicketSelector>(1024);
660        let _ = self.redeem_requests.set(redemption_req_tx);
661        let (redemption_req_rx, redemption_req_handle) = futures::stream::abortable(redemption_req_rx);
662        processes.insert(HoprLibProcess::TicketRedemptions, redemption_req_handle);
663        let chain = self.chain_api.clone();
664        let node_db = self.node_db.clone();
665        spawn(redemption_req_rx
666            .for_each(move |selector| {
667                let chain = chain.clone();
668                let db = node_db.clone();
669                async move {
670                    match chain.redeem_tickets_via_selectors(&db, [selector]).await {
671                        Ok(res) => debug!(%res, "redemption complete"),
672                        Err(error) => error!(%error, "redemption failed"),
673                    }
674                }
675            })
676            .inspect(|_| tracing::warn!(task = %HoprLibProcess::TicketRedemptions, "long-running background task finished"))
677        );
678
679        info!("subscribing to channel events");
680        let (chain_events_sub_handle, chain_events_sub_reg) = hopr_async_runtime::AbortHandle::new_pair();
681        processes.insert(HoprLibProcess::ChannelEvents, chain_events_sub_handle);
682        let chain = self.chain_api.clone();
683        let node_db = self.node_db.clone();
684        let events = chain.subscribe().map_err(HoprLibError::chain)?;
685        spawn(
686            futures::stream::Abortable::new(
687                events
688                    .filter_map(move |event|
689                        futures::future::ready(event.try_as_channel_closed())
690                    ),
691                chain_events_sub_reg
692            )
693            .for_each(move |closed_channel| {
694                let node_db = node_db.clone();
695                let chain = chain.clone();
696                async move {
697                    match closed_channel.direction(chain.me()) {
698                        Some(ChannelDirection::Incoming) => {
699                            match node_db.mark_tickets_as([&closed_channel], TicketMarker::Neglected).await {
700                                Ok(num_neglected) if num_neglected > 0 => {
701                                    warn!(%num_neglected, %closed_channel, "tickets on incoming closed channel were neglected");
702                                },
703                                Ok(_) => {
704                                    debug!(%closed_channel, "no neglected tickets on incoming closed channel");
705                                },
706                                Err(error) => {
707                                    error!(%error, %closed_channel, "failed to mark tickets on incoming closed channel as neglected");
708                                }
709                            }
710                        },
711                        Some(ChannelDirection::Outgoing) => {
712                            if let Err(error) = node_db.remove_outgoing_ticket_index(closed_channel.get_id(), closed_channel.channel_epoch).await {
713                                error!(%error, %closed_channel, "failed to reset ticket index on closed outgoing channel");
714                            } else {
715                                debug!(%closed_channel, "outgoing ticket index has been resets on outgoing channel closure");
716                            }
717                        }
718                        _ => {} // Event for a channel that is not our own
719                    }
720                }
721            })
722            .inspect(|_| tracing::warn!(task = %HoprLibProcess::ChannelEvents, "long-running background task finished"))
723        );
724
725        info!("synchronizing ticket states");
726        // NOTE: after the chain is synced, we can reset tickets which are considered
727        // redeemed but on-chain state does not align with that. This implies there was a problem
728        // right when the transaction was sent on-chain. In such cases, we simply let it retry and
729        // handle errors appropriately.
730        let mut channels = self
731            .chain_api
732            .stream_channels(ChannelSelector {
733                destination: self.me_onchain().into(),
734                ..Default::default()
735            })
736            .map_err(HoprLibError::chain)
737            .await?;
738
739        while let Some(channel) = channels.next().await {
740            self.node_db
741                .update_ticket_states_and_fetch(
742                    [TicketSelector::from(&channel)
743                        .with_state(AcknowledgedTicketStatus::BeingRedeemed)
744                        .with_index_range(channel.ticket_index..)],
745                    AcknowledgedTicketStatus::Untouched,
746                )
747                .map_err(HoprLibError::db)
748                .await?
749                .for_each(|ticket| {
750                    info!(%ticket, "fixed next out-of-sync ticket");
751                    futures::future::ready(())
752                })
753                .await;
754        }
755
756        self.state.store(HoprState::Running, Ordering::Relaxed);
757
758        info!(
759            id = %self.me_peer_id(),
760            version = constants::APP_VERSION,
761            "NODE STARTED AND RUNNING"
762        );
763
764        #[cfg(all(feature = "prometheus", not(test)))]
765        METRIC_HOPR_NODE_INFO.set(
766            &[
767                &self.me.public().to_peerid_str(),
768                &self.me_onchain().to_string(),
769                &self.cfg.safe_module.safe_address.to_string(),
770                &self.cfg.safe_module.module_address.to_string(),
771            ],
772            1.0,
773        );
774
775        let _ = self.processes.set(processes);
776        Ok(hopr_socket)
777    }
778
779    /// Used to practically shut down all node's processes without dropping the instance.
780    ///
781    /// This means that the instance can be used to retrieve some information, but all
782    /// active operations will stop and new will be impossible to perform.
783    /// Such operations will return [`HoprStatusError::NotThereYet`].
784    ///
785    /// This is the final state and cannot be reversed by calling [`HoprLib::run`] again.
786    pub fn shutdown(&self) -> Result<(), HoprLibError> {
787        self.error_if_not_in_state(HoprState::Running, "node is not running".into())?;
788        if let Some(processes) = self.processes.get() {
789            processes.abort_all();
790        }
791        self.state.store(HoprState::Terminated, Ordering::Relaxed);
792        info!("NODE SHUTDOWN COMPLETE");
793        Ok(())
794    }
795
796    /// Allows external users to receive notifications about new winning tickets.
797    pub fn subscribe_winning_tickets(&self) -> impl Stream<Item = VerifiedTicket> + Send {
798        self.winning_ticket_subscribers.1.activate_cloned()
799    }
800
801    // p2p transport =========
802    /// Own PeerId used in the libp2p transport layer
803    pub fn me_peer_id(&self) -> PeerId {
804        (*self.me.public()).into()
805    }
806
807    /// Get the list of all announced public nodes in the network
808    pub async fn get_public_nodes(&self) -> errors::Result<Vec<(PeerId, Address, Vec<Multiaddr>)>> {
809        Ok(self
810            .chain_api
811            .stream_accounts(AccountSelector {
812                public_only: true,
813                ..Default::default()
814            })
815            .map_err(HoprLibError::chain)
816            .await?
817            .map(|entry| {
818                (
819                    PeerId::from(entry.public_key),
820                    entry.chain_addr,
821                    entry.get_multiaddrs().to_vec(),
822                )
823            })
824            .collect()
825            .await)
826    }
827
828    /// Ping another node in the network based on the PeerId
829    ///
830    /// Returns the RTT (round trip time), i.e. how long it took for the ping to return.
831    pub async fn ping(&self, peer: &PeerId) -> errors::Result<(std::time::Duration, Observations)> {
832        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
833
834        Ok(self.transport_api.ping(peer).await?)
835    }
836
837    /// Create a client session connection returning a session object that implements
838    /// [`futures::io::AsyncRead`] and [`futures::io::AsyncWrite`] and can bu used as a read/write binary session.
839    #[cfg(feature = "session-client")]
840    pub async fn connect_to(
841        &self,
842        destination: Address,
843        target: SessionTarget,
844        cfg: SessionClientConfig,
845    ) -> errors::Result<HoprSession> {
846        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
847
848        let backoff = backon::ConstantBuilder::default()
849            .with_max_times(self.cfg.protocol.session.establish_max_retries as usize)
850            .with_delay(self.cfg.protocol.session.establish_retry_timeout)
851            .with_jitter();
852
853        use backon::Retryable;
854
855        Ok((|| {
856            let cfg = cfg.clone();
857            let target = target.clone();
858            async { self.transport_api.new_session(destination, target, cfg).await }
859        })
860        .retry(backoff)
861        .sleep(backon::FuturesTimerSleeper)
862        .await?)
863    }
864
865    /// Sends keep-alive to the given [`HoprSessionId`], making sure the session is not
866    /// closed due to inactivity.
867    #[cfg(feature = "session-client")]
868    pub async fn keep_alive_session(&self, id: &SessionId) -> errors::Result<()> {
869        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
870        Ok(self.transport_api.probe_session(id).await?)
871    }
872
873    #[cfg(feature = "session-client")]
874    pub async fn get_session_surb_balancer_config(&self, id: &SessionId) -> errors::Result<Option<SurbBalancerConfig>> {
875        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
876        Ok(self.transport_api.session_surb_balancing_cfg(id).await?)
877    }
878
879    #[cfg(feature = "session-client")]
880    pub async fn update_session_surb_balancer_config(
881        &self,
882        id: &SessionId,
883        cfg: SurbBalancerConfig,
884    ) -> errors::Result<()> {
885        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
886        Ok(self.transport_api.update_session_surb_balancing_cfg(id, cfg).await?)
887    }
888
889    /// List all multiaddresses announced by this node
890    pub fn local_multiaddresses(&self) -> Vec<Multiaddr> {
891        self.transport_api.local_multiaddresses()
892    }
893
894    /// List all multiaddresses on which the node is listening
895    pub async fn listening_multiaddresses(&self) -> Vec<Multiaddr> {
896        self.transport_api.listening_multiaddresses().await
897    }
898
899    /// List all multiaddresses observed for a PeerId
900    pub async fn network_observed_multiaddresses(&self, peer: &PeerId) -> Vec<Multiaddr> {
901        self.transport_api.network_observed_multiaddresses(peer).await
902    }
903
904    /// List all multiaddresses announced on-chain for the given node.
905    pub async fn multiaddresses_announced_on_chain(&self, peer: &PeerId) -> errors::Result<Vec<Multiaddr>> {
906        let peer = *peer;
907        // PeerId -> OffchainPublicKey is a CPU-intensive blocking operation
908        let pubkey =
909            hopr_parallelize::cpu::spawn_blocking(move || OffchainPublicKey::from_peerid(&peer), "multiaddr_lookup")
910                .await??;
911
912        match self
913            .chain_api
914            .stream_accounts(AccountSelector {
915                public_only: false,
916                offchain_key: Some(pubkey),
917                ..Default::default()
918            })
919            .map_err(HoprLibError::chain)
920            .await?
921            .next()
922            .await
923        {
924            Some(entry) => Ok(entry.get_multiaddrs().to_vec()),
925            None => {
926                error!(%peer, "no information");
927                Ok(vec![])
928            }
929        }
930    }
931
932    // Network =========
933
934    /// Get measured network health
935    pub async fn network_health(&self) -> Health {
936        self.transport_api.network_health().await
937    }
938
939    /// List all peers connected to this
940    pub async fn network_connected_peers(&self) -> errors::Result<Vec<PeerId>> {
941        Ok(self.transport_api.network_connected_peers().await?)
942    }
943
944    /// Get all data collected from the network relevant for a PeerId
945    pub async fn network_peer_info(&self, peer: &PeerId) -> errors::Result<Option<Observations>> {
946        Ok(self.transport_api.network_peer_observations(peer).await?)
947    }
948
949    /// Get peers connected peers with quality higher than some value
950    pub async fn all_network_peers(
951        &self,
952        minimum_score: f64,
953    ) -> errors::Result<Vec<(Option<Address>, PeerId, Observations)>> {
954        Ok(
955            futures::stream::iter(self.transport_api.network_connected_peers().await?)
956                .filter_map(|peer| async move {
957                    if let Ok(Some(info)) = self.transport_api.network_peer_observations(&peer).await {
958                        if info.score() >= minimum_score {
959                            Some((peer, info))
960                        } else {
961                            None
962                        }
963                    } else {
964                        None
965                    }
966                })
967                .filter_map(|(peer_id, info)| async move {
968                    let address = self.peerid_to_chain_key(&peer_id).await.ok().flatten();
969                    Some((address, peer_id, info))
970                })
971                .collect::<Vec<_>>()
972                .await,
973        )
974    }
975
976    // Ticket ========
977    /// Get all tickets in a channel specified by [`channel_id`](ChannelId).
978    pub async fn tickets_in_channel(&self, channel_id: &ChannelId) -> errors::Result<Option<Vec<RedeemableTicket>>> {
979        if let Some(channel) = self
980            .chain_api
981            .channel_by_id(channel_id)
982            .await
983            .map_err(|e| HoprTransportError::Other(e.into()))?
984        {
985            if &channel.destination == self.chain_api.me() {
986                Ok(Some(
987                    self.node_db
988                        .stream_tickets([&channel])
989                        .await
990                        .map_err(HoprLibError::db)?
991                        .collect()
992                        .await,
993                ))
994            } else {
995                Ok(None)
996            }
997        } else {
998            Ok(None)
999        }
1000    }
1001
1002    /// Get all tickets
1003    pub async fn all_tickets(&self) -> errors::Result<Vec<VerifiedTicket>> {
1004        Ok(self
1005            .node_db
1006            .stream_tickets(None::<TicketSelector>)
1007            .await
1008            .map_err(HoprLibError::db)?
1009            .map(|v| v.ticket)
1010            .collect()
1011            .await)
1012    }
1013
1014    /// Get statistics for all tickets
1015    pub async fn ticket_statistics(&self) -> errors::Result<ChannelTicketStatistics> {
1016        self.node_db.get_ticket_statistics(None).await.map_err(HoprLibError::db)
1017    }
1018
1019    /// Reset the ticket metrics to zero
1020    pub async fn reset_ticket_statistics(&self) -> errors::Result<()> {
1021        self.node_db
1022            .reset_ticket_statistics()
1023            .await
1024            .map_err(HoprLibError::chain)
1025    }
1026
1027    // Chain =========
1028    pub fn me_onchain(&self) -> Address {
1029        *self.chain_api.me()
1030    }
1031
1032    /// Get ticket price
1033    pub async fn get_ticket_price(&self) -> errors::Result<HoprBalance> {
1034        self.chain_api.minimum_ticket_price().await.map_err(HoprLibError::chain)
1035    }
1036
1037    /// Get minimum incoming ticket winning probability
1038    pub async fn get_minimum_incoming_ticket_win_probability(&self) -> errors::Result<WinningProbability> {
1039        self.chain_api
1040            .minimum_incoming_ticket_win_prob()
1041            .await
1042            .map_err(HoprLibError::chain)
1043    }
1044
1045    /// List of all accounts announced on the chain
1046    pub async fn accounts_announced_on_chain(&self) -> errors::Result<Vec<AccountEntry>> {
1047        Ok(self
1048            .chain_api
1049            .stream_accounts(AccountSelector {
1050                public_only: true,
1051                ..Default::default()
1052            })
1053            .map_err(HoprLibError::chain)
1054            .await?
1055            .collect()
1056            .await)
1057    }
1058
1059    /// Get the channel entry from Hash.
1060    /// @returns the channel entry of those two nodes
1061    pub async fn channel_from_hash(&self, channel_id: &Hash) -> errors::Result<Option<ChannelEntry>> {
1062        self.chain_api
1063            .channel_by_id(channel_id)
1064            .await
1065            .map_err(HoprLibError::chain)
1066    }
1067
1068    /// Get the channel entry between source and destination node.
1069    /// @param src Address
1070    /// @param dest Address
1071    /// @returns the channel entry of those two nodes
1072    pub async fn channel(&self, src: &Address, dest: &Address) -> errors::Result<Option<ChannelEntry>> {
1073        self.chain_api
1074            .channel_by_parties(src, dest)
1075            .await
1076            .map_err(HoprLibError::chain)
1077    }
1078
1079    /// List all channels open from a specified Address
1080    pub async fn channels_from(&self, src: &Address) -> errors::Result<Vec<ChannelEntry>> {
1081        Ok(self
1082            .chain_api
1083            .stream_channels(ChannelSelector::default().with_source(*src).with_allowed_states(&[
1084                ChannelStatusDiscriminants::Closed,
1085                ChannelStatusDiscriminants::Open,
1086                ChannelStatusDiscriminants::PendingToClose,
1087            ]))
1088            .map_err(HoprLibError::chain)
1089            .await?
1090            .collect()
1091            .await)
1092    }
1093
1094    /// List all channels open to a specified address
1095    pub async fn channels_to(&self, dest: &Address) -> errors::Result<Vec<ChannelEntry>> {
1096        Ok(self
1097            .chain_api
1098            .stream_channels(
1099                ChannelSelector::default()
1100                    .with_destination(*dest)
1101                    .with_allowed_states(&[
1102                        ChannelStatusDiscriminants::Closed,
1103                        ChannelStatusDiscriminants::Open,
1104                        ChannelStatusDiscriminants::PendingToClose,
1105                    ]),
1106            )
1107            .map_err(HoprLibError::chain)
1108            .await?
1109            .collect()
1110            .await)
1111    }
1112
1113    /// List all channels
1114    pub async fn all_channels(&self) -> errors::Result<Vec<ChannelEntry>> {
1115        Ok(self
1116            .chain_api
1117            .stream_channels(ChannelSelector::default().with_allowed_states(&[
1118                ChannelStatusDiscriminants::Closed,
1119                ChannelStatusDiscriminants::Open,
1120                ChannelStatusDiscriminants::PendingToClose,
1121            ]))
1122            .map_err(HoprLibError::chain)
1123            .await?
1124            .collect()
1125            .await)
1126    }
1127
1128    /// Current safe allowance balance
1129    pub async fn safe_allowance(&self) -> errors::Result<HoprBalance> {
1130        self.chain_api
1131            .safe_allowance(self.cfg.safe_module.safe_address)
1132            .await
1133            .map_err(HoprLibError::chain)
1134    }
1135
1136    /// Withdraw on-chain assets to a given address
1137    /// @param recipient the account where the assets should be transferred to
1138    /// @param amount how many tokens to be transferred
1139    pub async fn withdraw_tokens(&self, recipient: Address, amount: HoprBalance) -> errors::Result<prelude::Hash> {
1140        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1141
1142        self.chain_api
1143            .withdraw(amount, &recipient)
1144            .and_then(identity)
1145            .map_err(HoprLibError::chain)
1146            .await
1147    }
1148
1149    /// Withdraw on-chain native assets to a given address
1150    /// @param recipient the account where the assets should be transferred to
1151    /// @param amount how many tokens to be transferred
1152    pub async fn withdraw_native(&self, recipient: Address, amount: XDaiBalance) -> errors::Result<prelude::Hash> {
1153        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1154
1155        self.chain_api
1156            .withdraw(amount, &recipient)
1157            .and_then(identity)
1158            .map_err(HoprLibError::chain)
1159            .await
1160    }
1161
1162    /// Spawns a one-shot awaiter that hooks up to the [`ChainEvent`] bus and either matching the given `predicate`
1163    /// successfully or timing out after `timeout`.
1164    fn spawn_wait_for_on_chain_event(
1165        &self,
1166        context: impl std::fmt::Display,
1167        predicate: impl Fn(&ChainEvent) -> bool + Send + Sync + 'static,
1168        timeout: Duration,
1169    ) -> errors::Result<(
1170        impl Future<Output = errors::Result<ChainEvent>>,
1171        hopr_async_runtime::AbortHandle,
1172    )> {
1173        debug!(%context, "registering wait for on-chain event");
1174        let (event_stream, handle) = futures::stream::abortable(
1175            self.chain_api
1176                .subscribe()
1177                .map_err(HoprLibError::chain)?
1178                .skip_while(move |event| futures::future::ready(!predicate(event))),
1179        );
1180
1181        let ctx = context.to_string();
1182
1183        Ok((
1184            spawn(async move {
1185                pin_mut!(event_stream);
1186                let res = event_stream
1187                    .next()
1188                    .timeout(futures_time::time::Duration::from(timeout))
1189                    .map_err(|_| HoprLibError::GeneralError(format!("{ctx} timed out after {timeout:?}")))
1190                    .await?
1191                    .ok_or(HoprLibError::GeneralError(format!(
1192                        "failed to yield an on-chain event for {ctx}"
1193                    )));
1194                debug!(%ctx, ?res, "on-chain event waiting done");
1195                res
1196            })
1197            .map_err(move |_| HoprLibError::GeneralError(format!("failed to spawn future for {context}")))
1198            .and_then(futures::future::ready),
1199            handle,
1200        ))
1201    }
1202
1203    pub async fn open_channel(&self, destination: &Address, amount: HoprBalance) -> errors::Result<OpenChannelResult> {
1204        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1205
1206        let channel_id = generate_channel_id(&self.me_onchain(), destination);
1207
1208        let confirm_awaiter = self
1209            .chain_api
1210            .open_channel(destination, amount)
1211            .await
1212            .map_err(HoprLibError::chain)?;
1213
1214        let (event_awaiter, event_abort) = self.spawn_wait_for_on_chain_event(
1215            format!("open channel to {destination} ({channel_id})"),
1216            move |event| matches!(event, ChainEvent::ChannelOpened(c) if c.get_id() == &channel_id),
1217            ON_CHAIN_RESOLUTION_EVENT_TIMEOUT,
1218        )?;
1219
1220        let tx_hash = confirm_awaiter.await.map_err(|e| {
1221            event_abort.abort();
1222            HoprLibError::chain(e)
1223        })?;
1224
1225        let event = event_awaiter.await?;
1226        debug!(%event, "open channel event received");
1227
1228        Ok(OpenChannelResult { tx_hash, channel_id })
1229    }
1230
1231    pub async fn fund_channel(&self, channel_id: &ChannelId, amount: HoprBalance) -> errors::Result<Hash> {
1232        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1233
1234        let channel_id = *channel_id;
1235
1236        let confirm_awaiter = self
1237            .chain_api
1238            .fund_channel(&channel_id, amount)
1239            .await
1240            .map_err(HoprLibError::chain)?;
1241
1242        let (event_awaiter, event_abort) = self.spawn_wait_for_on_chain_event(
1243            format!("fund channel {channel_id}"),
1244            move |event| matches!(event, ChainEvent::ChannelBalanceIncreased(c, a) if c.get_id() == &channel_id && a == &amount),
1245            ON_CHAIN_RESOLUTION_EVENT_TIMEOUT
1246        )?;
1247
1248        let res = confirm_awaiter.await.map_err(|e| {
1249            event_abort.abort();
1250            HoprLibError::chain(e)
1251        })?;
1252
1253        let event = event_awaiter.await?;
1254        debug!(%event, "fund channel event received");
1255
1256        Ok(res)
1257    }
1258
1259    pub async fn close_channel_by_id(&self, channel_id: &ChannelId) -> errors::Result<CloseChannelResult> {
1260        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1261
1262        let channel_id = *channel_id;
1263
1264        let confirm_awaiter = self
1265            .chain_api
1266            .close_channel(&channel_id)
1267            .await
1268            .map_err(HoprLibError::chain)?;
1269
1270        let (event_awaiter, event_abort) = self.spawn_wait_for_on_chain_event(
1271            format!("close channel {channel_id}"),
1272            move |event| {
1273                matches!(event, ChainEvent::ChannelClosed(c) if c.get_id() == &channel_id)
1274                    || matches!(event, ChainEvent::ChannelClosureInitiated(c) if c.get_id() == &channel_id)
1275            },
1276            ON_CHAIN_RESOLUTION_EVENT_TIMEOUT,
1277        )?;
1278
1279        let tx_hash = confirm_awaiter.await.map_err(|e| {
1280            event_abort.abort();
1281            HoprLibError::chain(e)
1282        })?;
1283
1284        let event = event_awaiter.await?;
1285        debug!(%event, "close channel event received");
1286
1287        Ok(CloseChannelResult { tx_hash })
1288    }
1289
1290    pub async fn get_channel_closure_notice_period(&self) -> errors::Result<Duration> {
1291        self.chain_api
1292            .channel_closure_notice_period()
1293            .await
1294            .map_err(HoprLibError::chain)
1295    }
1296
1297    pub fn redemption_requests(
1298        &self,
1299    ) -> errors::Result<impl futures::Sink<TicketSelector, Error = HoprLibError> + Clone> {
1300        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1301
1302        // TODO: add universal timeout sink here
1303        Ok(self
1304            .redeem_requests
1305            .get()
1306            .cloned()
1307            .expect("redeem_requests is not initialized")
1308            .sink_map_err(HoprLibError::other))
1309    }
1310
1311    pub async fn redeem_all_tickets<B: Into<HoprBalance>>(&self, min_value: B) -> errors::Result<()> {
1312        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1313
1314        let min_value = min_value.into();
1315
1316        self.chain_api
1317            .stream_channels(
1318                ChannelSelector::default()
1319                    .with_destination(self.me_onchain())
1320                    .with_allowed_states(&[
1321                        ChannelStatusDiscriminants::Open,
1322                        ChannelStatusDiscriminants::PendingToClose,
1323                    ]),
1324            )
1325            .map_err(HoprLibError::chain)
1326            .await?
1327            .map(|channel| {
1328                Ok(TicketSelector::from(&channel)
1329                    .with_amount(min_value..)
1330                    .with_index_range(channel.ticket_index..)
1331                    .with_state(AcknowledgedTicketStatus::Untouched))
1332            })
1333            .forward(self.redemption_requests()?)
1334            .await?;
1335
1336        Ok(())
1337    }
1338
1339    pub async fn redeem_tickets_with_counterparty<B: Into<HoprBalance>>(
1340        &self,
1341        counterparty: &Address,
1342        min_value: B,
1343    ) -> errors::Result<()> {
1344        self.redeem_tickets_in_channel(&generate_channel_id(counterparty, &self.me_onchain()), min_value)
1345            .await
1346    }
1347
1348    pub async fn redeem_tickets_in_channel<B: Into<HoprBalance>>(
1349        &self,
1350        channel_id: &Hash,
1351        min_value: B,
1352    ) -> errors::Result<()> {
1353        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1354
1355        let channel = self
1356            .chain_api
1357            .channel_by_id(channel_id)
1358            .await
1359            .map_err(HoprLibError::chain)?
1360            .ok_or(HoprLibError::GeneralError("Channel not found".into()))?;
1361
1362        self.redemption_requests()?
1363            .send(
1364                TicketSelector::from(channel)
1365                    .with_amount(min_value.into()..)
1366                    .with_index_range(channel.ticket_index..)
1367                    .with_state(AcknowledgedTicketStatus::Untouched),
1368            )
1369            .await?;
1370
1371        Ok(())
1372    }
1373
1374    pub async fn redeem_ticket(&self, ack_ticket: AcknowledgedTicket) -> errors::Result<()> {
1375        self.error_if_not_in_state(HoprState::Running, "Node is not ready for on-chain operations".into())?;
1376
1377        self.redemption_requests()?
1378            .send(TicketSelector::from(&ack_ticket).with_state(AcknowledgedTicketStatus::Untouched))
1379            .await?;
1380
1381        Ok(())
1382    }
1383
1384    pub async fn peerid_to_chain_key(&self, peer_id: &PeerId) -> errors::Result<Option<Address>> {
1385        let peer_id = *peer_id;
1386        // PeerId -> OffchainPublicKey is a CPU-intensive blocking operation
1387        let pubkey = hopr_parallelize::cpu::spawn_blocking(
1388            move || prelude::OffchainPublicKey::from_peerid(&peer_id),
1389            "chainkey_lookup",
1390        )
1391        .await??;
1392
1393        self.chain_api
1394            .packet_key_to_chain_key(&pubkey)
1395            .await
1396            .map_err(HoprLibError::chain)
1397    }
1398
1399    pub async fn chain_key_to_peerid(&self, address: &Address) -> errors::Result<Option<PeerId>> {
1400        self.chain_api
1401            .chain_key_to_packet_key(address)
1402            .await
1403            .map(|pk| pk.map(|v| v.into()))
1404            .map_err(HoprLibError::chain)
1405    }
1406}
1407
1408impl<Chain, Db> Hopr<Chain, Db> {
1409    // === telemetry
1410    /// Prometheus formatted metrics collected by the hopr-lib components.
1411    pub fn collect_hopr_metrics() -> errors::Result<String> {
1412        cfg_if::cfg_if! {
1413            if #[cfg(all(feature = "prometheus", not(test)))] {
1414                hopr_metrics::gather_all_metrics().map_err(HoprLibError::other)
1415            } else {
1416                Err(HoprLibError::GeneralError("BUILT WITHOUT METRICS SUPPORT".into()))
1417            }
1418        }
1419    }
1420}