dusk_node_data/events/
blocks.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use super::*;
8use crate::ledger::{Block, Hash};
9
10/// Represents the state of an accepted block in the chain.
11///
12/// # Variants
13///
14/// - `Confirmed` - The block has been confirmed. This means a succeeding block
15///   has been accepted.
16/// - `Finalized` - The block has been finalized i.e., reached finality.
17///
18/// # Methods
19///
20/// - `as_str() -> &'static str` - Returns the string representation of the
21///   block state.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub enum BlockState {
24    Confirmed,
25    Finalized,
26}
27
28impl BlockState {
29    const BLOCK_CONFIRMED: &'static str = "confirmed";
30    const BLOCK_FINALIZED: &'static str = "finalized";
31
32    pub const fn as_str(&self) -> &'static str {
33        match self {
34            Self::Confirmed => Self::BLOCK_CONFIRMED,
35            Self::Finalized => Self::BLOCK_FINALIZED,
36        }
37    }
38}
39
40/// Represents events related to blocks in the chain.
41///
42/// # Variants
43///
44/// - `Accepted(&'b Block)`
45///
46///     Indicates that a block has been accepted into the chain.
47///
48/// - `StateChange`
49///
50///     Represents a change in the state of an accepted block.
51///
52///     - `hash: Hash` The unique identifier of the block whose state has
53///       changed.
54///
55///     - `state: BlockState` Describes the new state of the block (can be
56///       either `Confirmed` or `Finalized`).
57///
58///     - `height: u64` Indicates at which block height the state changed.
59///
60/// - `Reverted`
61///
62///     Indicates that a block has been removed from the chain because it got
63///     reverted during consensus.
64#[derive(Clone, Debug)]
65pub enum BlockEvent<'b> {
66    Accepted(&'b Block),
67    StateChange {
68        hash: Hash,
69        state: BlockState,
70        height: u64,
71    },
72    Reverted {
73        hash: Hash,
74        height: u64,
75    },
76}
77
78impl EventSource for BlockEvent<'_> {
79    const COMPONENT: &'static str = "blocks";
80
81    fn topic(&self) -> &'static str {
82        match self {
83            Self::Accepted(_) => "accepted",
84            Self::StateChange { .. } => "statechange",
85            Self::Reverted { .. } => "reverted",
86        }
87    }
88    fn data(&self) -> Option<serde_json::Value> {
89        let data = match self {
90            Self::Accepted(b) => {
91                let header = b.header();
92                let header = serde_json::to_value(header)
93                    .expect("json to be serialized");
94                let txs: Vec<_> =
95                    b.txs().iter().map(|t| hex::encode(t.id())).collect();
96                serde_json::json!({
97                    "header": header,
98                    "transactions": txs,
99                })
100            }
101            Self::StateChange { state, height, .. } => {
102                serde_json::json!({
103                    "state": state.as_str(),
104                    "atHeight": height,
105                })
106            }
107            BlockEvent::Reverted { height, .. } => {
108                serde_json::json!({
109                    "atHeight": height,
110                })
111            }
112        };
113        Some(data)
114    }
115    fn entity(&self) -> String {
116        let hash = match self {
117            Self::Accepted(block) => block.header().hash,
118            Self::StateChange { hash, .. } => *hash,
119            Self::Reverted { hash, .. } => *hash,
120        };
121        hex::encode(hash)
122    }
123}