Skip to content

Commit

Permalink
rusk: adapts Event fields to RFC
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Aug 6, 2024
1 parent 0481850 commit e1e9806
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
24 changes: 16 additions & 8 deletions node-data/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,34 @@ 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<String>,
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<String>;
fn entity(&self) -> String;
fn data(&self) -> EventData;
}

impl<B: EventSource> From<B> for Event {
fn from(value: B) -> Self {
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<u8>),
}
20 changes: 10 additions & 10 deletions node-data/src/events/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,18 +17,18 @@ impl EventSource for BlockEvent<'_> {
Self::StateChange { .. } => "statechange",
}
}
fn data(&self) -> Option<String> {
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,
Expand Down
8 changes: 4 additions & 4 deletions node-data/src/events/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,10 +25,10 @@ impl EventSource for TransactionEvent<'_> {
Self::Included(_) => "included",
}
}
fn data(&self) -> Option<String> {
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(),
Expand Down
17 changes: 14 additions & 3 deletions rusk/src/lib/http/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,12 +896,23 @@ impl From<node_data::events::Event> 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<node_data::events::EventData> 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,
}
}
}
Expand Down

0 comments on commit e1e9806

Please sign in to comment.