1use std::{fmt::Debug, sync::Arc};
4
5use anyhow::bail;
6use async_trait::async_trait;
7use derivative::Derivative;
8use derive_more::From;
9use espresso_types::{v0::traits::SequencerPersistence, PubKey};
10use hotshot::types::Event;
11use hotshot_query_service::data_source::UpdateDataSource;
12use hotshot_types::traits::{network::ConnectedNetwork, node_implementation::Versions};
13
14use super::{data_source::SequencerDataSource, StorageState};
15use crate::{EventConsumer, SeqTypes};
16
17#[derive(Derivative, From)]
18#[derivative(Clone(bound = ""), Debug(bound = "D: Debug"))]
19pub(crate) struct ApiEventConsumer<N, P, D, V>
20where
21 N: ConnectedNetwork<PubKey>,
22 P: SequencerPersistence,
23 V: Versions,
24{
25 inner: Arc<StorageState<N, P, D, V>>,
26}
27
28#[async_trait]
29impl<N, P, D, V> EventConsumer for ApiEventConsumer<N, P, D, V>
30where
31 N: ConnectedNetwork<PubKey>,
32 P: SequencerPersistence,
33 D: SequencerDataSource + Debug + Send + Sync + 'static,
34 V: Versions,
35{
36 async fn handle_event(&self, event: &Event<SeqTypes>) -> anyhow::Result<()> {
37 if let Err(height) = self.inner.update(event).await {
38 bail!("failed to update API state after {height}: {event:?}",);
39 }
40 Ok(())
41 }
42}