dusk_node_data/events.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
7mod blocks;
8mod transactions;
9
10pub mod contract;
11
12pub use blocks::{BlockEvent, BlockState};
13pub use transactions::TransactionEvent;
14
15/// Represents an event in the system, including its source (`component`),
16/// type (`topic`), associated entity (`entity`), and optional data (`data`).
17///
18/// - `component`: Source of the event (e.g., `"transaction"/"block"`).
19/// - `topic`: Type/category of the event (e.g., `"accepted"/"removed"`).
20/// - `entity`: Identifier for the related entity (e.g., `transaction hash`).
21/// - `data`: Optional JSON data with additional event details.
22#[derive(Clone, Debug)]
23pub struct Event {
24 pub component: &'static str,
25 pub topic: &'static str,
26 pub entity: String,
27 pub data: Option<serde_json::Value>,
28}
29
30trait EventSource {
31 const COMPONENT: &'static str;
32
33 fn topic(&self) -> &'static str;
34 fn entity(&self) -> String;
35 fn data(&self) -> Option<serde_json::Value>;
36}
37
38impl<ES: EventSource> From<ES> for Event {
39 fn from(value: ES) -> Self {
40 Self {
41 data: value.data(),
42 topic: value.topic(),
43 entity: value.entity(),
44 component: ES::COMPONENT,
45 }
46 }
47}