Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial API defs #9

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "hs-builder-api"
name = "hotshot-events-service"
version = "0.1.1"
edition = "2021"

Expand All @@ -10,7 +10,7 @@ async-trait = "0.1"
clap = { version = "4.4", features = ["derive", "env"] }
derive_more = "0.99"
futures = "0.3"
hotshot-types = { git = "https://github.com/EspressoSystems/HotShot.git", tag = "0.5.21" }
hotshot-types = { git = "https://github.com/EspressoSystems/hotshot-types.git", tag = "v0.1.0" }
serde = { version = "1.0", features = ["derive"] }
snafu = { version = "0.7", features = ["backtraces"] }
tagged-base64 = { git = "https://github.com/EspressoSystems/tagged-base64", tag = "0.3.4" }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# hotshot-events-service

Minimal dependencies shared API definitions for Internal HotShot Events
Minimal dependencies shared API definitions to serve Internal HotShot Events

# HotShot Consensus Module
123 changes: 123 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::{fmt::Display, path::PathBuf};

use clap::Args;
use derive_more::From;
use futures::FutureExt;
use hotshot_types::{
traits::{node_implementation::NodeType, signature_key::SignatureKey},
utils::BuilderCommitment,

Check failure on line 8 in src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `utils::BuilderCommitment`

error: unused import: `utils::BuilderCommitment` --> src/events.rs:8:5 | 8 | utils::BuilderCommitment, | ^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_imports)]`
};
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use tagged_base64::TaggedBase64;
use tide_disco::{
api::ApiError,
method::{ReadState, WriteState},

Check failure on line 15 in src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `WriteState`

error: unused import: `WriteState` --> src/events.rs:15:25 | 15 | method::{ReadState, WriteState}, | ^^^^^^^^^^
Api, RequestError, StatusCode,
};

use crate::{
api::load_api,
events_source::{EventsSource},
};

#[derive(Args, Default)]
pub struct Options {
#[arg(long = "hotshot-events-service-api-path", env = "HOTSHOT_EVENTS_SERVICE_API_PATH")]
pub api_path: Option<PathBuf>,

/// Additional API specification files to merge with `hotshot-events-service-api-path`.
///
/// These optional files may contain route definitions for application-specific routes that have
/// been added as extensions to the basic hotshot-events-service API.
#[arg(
long = "hotshot-events-extension",
env = "HOTSHOT_EVENTS_SERVICE_EXTENSIONS",
value_delimiter = ','
)]
pub extensions: Vec<toml::Value>,
}

#[derive(Clone, Debug, Snafu, Deserialize, Serialize)]
#[snafu(visibility(pub))]
pub enum EventError {
/// The requested resource does not exist or is not known to this hotshot node.
NotFound,
/// The requested resource exists but is not currently available.
Missing,
/// There was an error while trying to fetch the requested resource.
#[snafu(display("Failed to fetch requested resource: {message}"))]
Error { message: String },
}


#[derive(Clone, Debug, From, Snafu, Deserialize, Serialize)]
#[snafu(visibility(pub))]
pub enum Error {
Request {
source: RequestError,
},
#[snafu(display("error receiving events {resource}: {source}"))]
#[from(ignore)]
EventAvailable {
source: EventError,
resource: String,
},
Custom {
message: String,
status: StatusCode,
},
}

impl tide_disco::error::Error for Error {
fn catch_all(status: StatusCode, msg: String) -> Self {
Error::Custom {
message: msg,
status,
}
}

fn status(&self) -> StatusCode {
match self {
Error::Request { .. } => StatusCode::BadRequest,
Error::EventAvailable { source, .. } => match source
{
EventError::NotFound => StatusCode::NotFound,
EventError::Missing => StatusCode::NotFound,
EventError::Error { .. } => StatusCode::InternalServerError,
},
}
}
}

pub fn define_api<State, Types: NodeType>(options: &Options) -> Result<Api<State, Error>, ApiError>
where
State: 'static + Send + Sync + ReadState,
<State as ReadState>::State: Send + Sync + EventsSource<Types>,
Types: NodeType,
<<Types as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType:
for<'a> TryFrom<&'a TaggedBase64> + Into<TaggedBase64> + Display,
for<'a> <<<Types as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType as TryFrom<
&'a TaggedBase64,
>>::Error: Display,
{
let mut api = load_api::<State, Error>(
options.api_path.as_ref(),
include_str!("../api/hotshot_events.toml"),
options.extensions.clone(),
)?;
api.with_version("0.0.1".parse().unwrap())
.get("available_hotshot_events", |req, state| {

Check failure on line 110 in src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

the trait bound `events_info::EventInfo<Types>: events::_::_serde::Serialize` is not satisfied

error[E0277]: the trait bound `events_info::EventInfo<Types>: events::_::_serde::Serialize` is not satisfied --> src/events.rs:110:10 | 110 | .get("available_hotshot_events", |req, state| { | ^^^ the trait `events::_::_serde::Serialize` is not implemented for `events_info::EventInfo<Types>` | = help: the following other types implement trait `events::_::_serde::Serialize`: bool char isize i8 i16 i32 i64 i128 and 285 others = note: required for `std::vec::Vec<events_info::EventInfo<Types>>` to implement `events::_::_serde::Serialize` note: required by a bound in `tide_disco::Api::<State, Error>::get` --> /home/runner/.cargo/git/checkouts/tide-disco-f959d6bfd2507cc7/4ced31c/src/api.rs:634:12 | 628 | pub fn get<F, T>(&mut self, name: &str, handler: F) -> Result<&mut Self, ApiError> | --- required by a bound in this associated function ... 634 | T: Serialize, | ^^^^^^^^^ required by this bound in `Api::<State, Error>::get`
async move {
let view_number= req.blob_param("view_number")?;

Check failure on line 112 in src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

the trait bound `hotshot_types::data::ViewNumber: std::convert::From<&tagged_base64::TaggedBase64>` is not satisfied

error[E0277]: the trait bound `hotshot_types::data::ViewNumber: std::convert::From<&tagged_base64::TaggedBase64>` is not satisfied --> src/events.rs:112:38 | 112 | let view_number= req.blob_param("view_number")?; | ^^^^^^^^^^ the trait `std::convert::From<&tagged_base64::TaggedBase64>` is not implemented for `hotshot_types::data::ViewNumber` | = note: required for `&tagged_base64::TaggedBase64` to implement `std::convert::Into<hotshot_types::data::ViewNumber>` = note: required for `hotshot_types::data::ViewNumber` to implement `std::convert::TryFrom<&tagged_base64::TaggedBase64>`
state
.get_available_hotshot_events(view_number)
.await
.context(EventAvailableSnafu {
resource: view_number.to_string(),
})
}
.boxed()
})?;
Ok(api)
}
16 changes: 16 additions & 0 deletions src/events_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use hotshot_types::event::Event;
use hotshot_types::{
traits::{node_implementation::NodeType, signature_key::SignatureKey},
utils::BuilderCommitment,

Check failure on line 4 in src/events_info.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `utils::BuilderCommitment`, `vid::VidCommitment`

error: unused imports: `utils::BuilderCommitment`, `vid::VidCommitment` --> src/events_info.rs:4:5 | 4 | utils::BuilderCommitment, | ^^^^^^^^^^^^^^^^^^^^^^^^ 5 | vid::VidCommitment, | ^^^^^^^^^^^^^^^^^^
vid::VidCommitment,
};
use serde::{Deserialize, Serialize};

Check failure on line 7 in src/events_info.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `Deserialize`, `Serialize`

error: unused imports: `Deserialize`, `Serialize` --> src/events_info.rs:7:13 | 7 | use serde::{Deserialize, Serialize}; | ^^^^^^^^^^^ ^^^^^^^^^
use snafu::{ResultExt, Snafu};

Check failure on line 8 in src/events_info.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `ResultExt`, `Snafu`

error: unused imports: `ResultExt`, `Snafu` --> src/events_info.rs:8:13 | 8 | use snafu::{ResultExt, Snafu}; | ^^^^^^^^^ ^^^^^
use std::{hash::Hash, marker::PhantomData};

Check failure on line 9 in src/events_info.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `hash::Hash`

error: unused import: `hash::Hash` --> src/events_info.rs:9:11 | 9 | use std::{hash::Hash, marker::PhantomData}; | ^^^^^^^^^^
#[derive(Clone, Debug)]
pub struct EventInfo<I: NodeType> {
pub event: Event<I>,
pub signature: <<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType,
pub sender: <I as NodeType>::SignatureKey,
pub _phantom: PhantomData<I>,
}
25 changes: 25 additions & 0 deletions src/events_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use async_trait::async_trait;
use std::{hash::Hash, marker::PhantomData};

Check failure on line 2 in src/events_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `hash::Hash`, `marker::PhantomData`

error: unused imports: `hash::Hash`, `marker::PhantomData` --> src/events_source.rs:2:11 | 2 | use std::{hash::Hash, marker::PhantomData}; | ^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
use hotshot_types::event::Event;

Check failure on line 3 in src/events_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `hotshot_types::event::Event`

error: unused import: `hotshot_types::event::Event` --> src/events_source.rs:3:5 | 3 | use hotshot_types::event::Event; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
use serde::{Deserialize, Serialize};

Check failure on line 4 in src/events_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `Deserialize`, `Serialize`

error: unused imports: `Deserialize`, `Serialize` --> src/events_source.rs:4:13 | 4 | use serde::{Deserialize, Serialize}; | ^^^^^^^^^^^ ^^^^^^^^^
use hotshot_types::{
traits::{node_implementation::NodeType, signature_key::SignatureKey},
utils::BuilderCommitment,

Check failure on line 7 in src/events_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `utils::BuilderCommitment`, `vid::VidCommitment`

error: unused imports: `utils::BuilderCommitment`, `vid::VidCommitment` --> src/events_source.rs:7:5 | 7 | utils::BuilderCommitment, | ^^^^^^^^^^^^^^^^^^^^^^^^ 8 | vid::VidCommitment, | ^^^^^^^^^^^^^^^^^^
vid::VidCommitment,
data::ViewNumber
};
use tagged_base64::TaggedBase64;
use crate::{events_info::{EventInfo}, events::EventError};

#[async_trait]
pub trait EventsSource<I>
where
I: NodeType,
<<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType:
for<'a> TryFrom<&'a TaggedBase64> + Into<TaggedBase64>,
{
async fn get_available_hotshot_events(
&self,
view_number: ViewNumber,
) -> Result<Vec<EventInfo<I>>, EventError>;
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
mod api;
pub mod events;
pub mod events_info;
pub mod events_source;
Loading