hopr_chain_types/
chain_events.rs

1//! Types that are related to events that are raised on-chain and extracted from chain logs.
2//!
3//! These events happen in response to actions (transactions, smart contract calls) done by a HOPR node on chain.
4//!
5//! See `chain-actions` and `chain-indexer` crates for details.
6use std::fmt::{Display, Formatter};
7
8use hopr_crypto_types::types::Hash;
9use hopr_internal_types::prelude::*;
10use hopr_primitive_types::prelude::*;
11use libp2p_identity::PeerId;
12use multiaddr::Multiaddr;
13
14/// Contains TX hash along with the Chain Event data.
15/// This could be used to pair up some events with [Action](crate::actions::Action).
16/// Each [Action](crate::actions::Action) is typically concluded by a significant chain event.
17#[derive(Debug, Clone, PartialEq)]
18pub struct SignificantChainEvent {
19    /// TX hash
20    pub tx_hash: Hash,
21    /// Chain event of interest
22    pub event_type: ChainEventType,
23}
24
25impl Display for SignificantChainEvent {
26    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27        write!(f, "{} @ tx {}", self.event_type, self.tx_hash)
28    }
29}
30
31/// Status of a node in network registry.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum NetworkRegistryStatus {
34    /// Connections to the node are allowed.
35    Allowed,
36    /// Connections to the node are not allowed.
37    Denied,
38}
39
40/// Enumeration of HOPR chain events.
41#[allow(clippy::large_enum_variant)] // TODO: Refactor the large enum variant
42#[derive(Debug, Clone, PartialEq)]
43pub enum ChainEventType {
44    /// Peer on-chain announcement event.
45    Announcement {
46        /// Announced peer id
47        peer: PeerId,
48        /// Announced on-chain address
49        address: Address,
50        /// Multiaddresses
51        multiaddresses: Vec<Multiaddr>,
52    },
53    /// New channel has been opened
54    ChannelOpened(ChannelEntry),
55    /// Channel closure has been initiated.
56    ChannelClosureInitiated(ChannelEntry),
57    /// Channel closure has been finalized.
58    ChannelClosed(ChannelEntry),
59    /// Channel balance has increased by an amount.
60    ChannelBalanceIncreased(ChannelEntry, HoprBalance),
61    /// Channel balance has decreased by an amount.
62    ChannelBalanceDecreased(ChannelEntry, HoprBalance),
63    /// Ticket has been redeemed on a channel.
64    /// If the channel is a node's own, it also contains the ticket that has been redeemed.
65    TicketRedeemed(ChannelEntry, Option<AcknowledgedTicket>),
66    /// Safe has been registered with the node.
67    NodeSafeRegistered(Address),
68    /// Network registry update for a node.
69    NetworkRegistryUpdate(Address, NetworkRegistryStatus),
70}
71
72impl Display for ChainEventType {
73    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74        match self {
75            ChainEventType::Announcement {
76                peer,
77                address,
78                multiaddresses,
79            } => write!(f, "announcement event of {peer} ({address}): {:?}", multiaddresses),
80            ChainEventType::ChannelOpened(c) => write!(f, "open channel event {}", c.get_id()),
81            ChainEventType::ChannelClosureInitiated(c) => write!(f, "close channel initiation event {}", c.get_id()),
82            ChainEventType::ChannelClosed(c) => write!(f, "close channel event {}", c.get_id()),
83            ChainEventType::ChannelBalanceIncreased(c, _) => write!(f, "channel increase balance event {}", c.get_id()),
84            ChainEventType::ChannelBalanceDecreased(c, _) => write!(f, "channel decrease balance event {}", c.get_id()),
85            ChainEventType::TicketRedeemed(c, _) => write!(f, "ticket redeem event in channel {}", c.get_id()),
86            ChainEventType::NodeSafeRegistered(s) => write!(f, "safe registered event {s}"),
87            ChainEventType::NetworkRegistryUpdate(a, s) => write!(f, "network registry update event {a}: {:?}", s),
88        }
89    }
90}