diff --git a/node-data/src/events.rs b/node-data/src/events.rs index de86891339..da4d0cfa25 100644 --- a/node-data/src/events.rs +++ b/node-data/src/events.rs @@ -12,17 +12,17 @@ pub use transactions::TransactionEvent; #[derive(Clone, Debug)] pub struct Event { - pub target: &'static str, + pub component: &'static str, pub topic: &'static str, - pub id: String, - pub data: Option, + pub entity: String, + pub data: EventData, } pub trait EventSource { - fn target(&self) -> &'static str; + fn component(&self) -> &'static str; fn topic(&self) -> &'static str; - fn id(&self) -> String; - fn data(&self) -> Option; + fn entity(&self) -> String; + fn data(&self) -> EventData; } impl From for Event { @@ -30,8 +30,16 @@ impl From for Event { Self { data: value.data(), topic: value.topic(), - id: value.id(), - target: value.target(), + entity: value.entity(), + component: value.component(), } } } + +#[derive(Clone, Debug)] +pub enum EventData { + None, + Json(serde_json::Value), + Text(String), + Binary(Vec), +} diff --git a/node-data/src/events/blocks.rs b/node-data/src/events/blocks.rs index 26ff81dd77..cb09b9fa89 100644 --- a/node-data/src/events/blocks.rs +++ b/node-data/src/events/blocks.rs @@ -8,7 +8,7 @@ use super::*; use crate::ledger::{Block, Hash}; impl EventSource for BlockEvent<'_> { - fn target(&self) -> &'static str { + fn component(&self) -> &'static str { "blocks" } fn topic(&self) -> &'static str { @@ -17,18 +17,18 @@ impl EventSource for BlockEvent<'_> { Self::StateChange { .. } => "statechange", } } - fn data(&self) -> Option { + fn data(&self) -> EventData { match self { - Self::Accepted(_) => None, - Self::StateChange { state, height, .. } => Some(format!( - r#"{{ - "state": "{state}", - "atHeight": {height} - }}"#, - )), + Self::Accepted(_) => EventData::None, + Self::StateChange { state, height, .. } => { + EventData::Json(serde_json::json!({ + "state": state, + "atHeight": height, + })) + } } } - fn id(&self) -> String { + fn entity(&self) -> String { let hash = match self { Self::Accepted(block) => block.header().hash, Self::StateChange { hash, .. } => *hash, diff --git a/node-data/src/events/transactions.rs b/node-data/src/events/transactions.rs index db12d2c7b2..71518cfa93 100644 --- a/node-data/src/events/transactions.rs +++ b/node-data/src/events/transactions.rs @@ -15,7 +15,7 @@ pub enum TransactionEvent<'t> { } impl EventSource for TransactionEvent<'_> { - fn target(&self) -> &'static str { + fn component(&self) -> &'static str { "transactions" } fn topic(&self) -> &'static str { @@ -25,10 +25,10 @@ impl EventSource for TransactionEvent<'_> { Self::Included(_) => "included", } } - fn data(&self) -> Option { - None + fn data(&self) -> EventData { + EventData::None } - fn id(&self) -> String { + fn entity(&self) -> String { let hash = match self { Self::Removed(hash) => *hash, Self::Executed(tx) => tx.inner.hash(), diff --git a/rusk/src/lib/http/event.rs b/rusk/src/lib/http/event.rs index c5d66bcd90..0d035bcd6b 100644 --- a/rusk/src/lib/http/event.rs +++ b/rusk/src/lib/http/event.rs @@ -896,12 +896,23 @@ impl From for RuesEvent { fn from(value: node_data::events::Event) -> Self { Self { location: RuesLocation { - component: value.target.into(), - entity: Some(value.id), + component: value.component.into(), + entity: Some(value.entity), topic: value.topic.into(), }, - data: ResponseData::new(value.data.unwrap_or_default()), + data: ResponseData::new(value.data), + } + } +} +#[cfg(feature = "node")] +impl From for DataType { + fn from(value: node_data::events::EventData) -> Self { + match value { + node_data::events::EventData::Binary(b) => b.into(), + node_data::events::EventData::Json(j) => j.into(), + node_data::events::EventData::Text(t) => t.into(), + node_data::events::EventData::None => DataType::None, } } }