Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions crates/blockchain/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ethlambda_types::ShortRoot;
use ethlambda_types::checkpoint::Checkpoint;
use ethlambda_types::primitives::H256;
use serde::Serialize;
use std::str::FromStr;
use tokio::sync::broadcast;
use tracing::warn;

Expand Down Expand Up @@ -44,6 +45,26 @@ impl Topic {
}
}

/// Error returned by [`Topic::from_str`] for a name matching no topic.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unknown topic: '{0}'")]
pub struct UnknownTopic(String);

impl FromStr for Topic {
type Err = UnknownTopic;

/// Exact inverse of [`Topic::as_str`].
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"head" => Ok(Topic::Head),
"block" => Ok(Topic::Block),
"justified_checkpoint" => Ok(Topic::JustifiedCheckpoint),
"finalized_checkpoint" => Ok(Topic::FinalizedCheckpoint),
other => Err(UnknownTopic(other.to_string())),
}
}
}

/// A consensus event published by the blockchain actor.
///
/// Fields mirror the Ethereum beacon-API eventstream payloads so the SSE
Expand Down Expand Up @@ -276,6 +297,13 @@ mod tests {
}
}

const ALL_TOPICS: [Topic; 4] = [
Topic::Head,
Topic::Block,
Topic::JustifiedCheckpoint,
Topic::FinalizedCheckpoint,
];

#[tokio::test]
async fn subscriber_receives_emitted_event() {
let bus = EventBus::default();
Expand All @@ -289,6 +317,15 @@ mod tests {
}
}

#[test]
fn topic_from_str_inverts_as_str() {
for topic in ALL_TOPICS {
assert_eq!(topic.as_str().parse::<Topic>().unwrap(), topic);
}
let err = "bogus".parse::<Topic>().unwrap_err();
assert_eq!(err.to_string(), "unknown topic: 'bogus'");
}

#[test]
fn emit_without_subscribers_is_a_noop() {
let bus = EventBus::default();
Expand Down
2 changes: 1 addition & 1 deletion crates/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::block_builder::ProposerConfig;
use crate::events::ChainEventSnapshot;
use crate::store::StoreError;

pub use events::{ChainEvent, EventBus, Topic};
pub use events::{ChainEvent, EventBus, Topic, UnknownTopic};

pub mod aggregation;
pub mod block_builder;
Expand Down
3 changes: 1 addition & 2 deletions crates/net/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ serde_json.workspace = true
hex.workspace = true
tracing.workspace = true
jemalloc_pprof.workspace = true
tokio-stream = { version = "0.1", features = ["sync"] }
futures-core = "0.3"
futures-util = "0.3"

[dev-dependencies]
ethlambda-types.workspace = true
Expand Down
Loading
Loading