Skip to content

Commit

Permalink
Cargo format
Browse files Browse the repository at this point in the history
  • Loading branch information
nyospe committed Feb 2, 2024
1 parent 7b9b0c8 commit 212ec65
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
9 changes: 6 additions & 3 deletions src/block_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ use hotshot_types::traits::{node_implementation::NodeType, BlockPayload};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

pub struct HashableBlock<I: NodeType>(<I as NodeType>::BlockPayload, <<I as NodeType>::BlockPayload as BlockPayload>::Metadata);
pub struct HashableBlock<I: NodeType>(
<I as NodeType>::BlockPayload,
<<I as NodeType>::BlockPayload as BlockPayload>::Metadata,
);
pub type BlockHash<I: NodeType> = Commitment<HashableBlock<I>>;

Check failure on line 12 in src/block_metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

bounds on generic parameters are not enforced in type aliases

error: bounds on generic parameters are not enforced in type aliases --> src/block_metadata.rs:12:23 | 12 | pub type BlockHash<I: NodeType> = Commitment<HashableBlock<I>>; | ^^^^^^^^ | = note: `-D type-alias-bounds` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(type_alias_bounds)]` help: the bound will not be checked when the type alias is used, and should be removed | 12 - pub type BlockHash<I: NodeType> = Commitment<HashableBlock<I>>; 12 + pub type BlockHash<I> = Commitment<HashableBlock<I>>; |

Check failure on line 12 in src/block_metadata.rs

View workflow job for this annotation

GitHub Actions / clippy

bounds on generic parameters are not enforced in type aliases

error: bounds on generic parameters are not enforced in type aliases --> src/block_metadata.rs:12:23 | 12 | pub type BlockHash<I: NodeType> = Commitment<HashableBlock<I>>; | ^^^^^^^^ | = note: `-D type-alias-bounds` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(type_alias_bounds)]` help: the bound will not be checked when the type alias is used, and should be removed | 12 - pub type BlockHash<I: NodeType> = Commitment<HashableBlock<I>>; 12 + pub type BlockHash<I> = Commitment<HashableBlock<I>>; |
impl<I: NodeType> Default for HashableBlock<I> {
fn default() -> Self {
let (bp, bm) = <I as NodeType>::BlockPayload::from_transactions(Vec::new()).unwrap_or_else(|_|<I as NodeType>::BlockPayload::genesis());
let (bp, bm) = <I as NodeType>::BlockPayload::from_transactions(Vec::new())
.unwrap_or_else(|_| <I as NodeType>::BlockPayload::genesis());
Self(bp, bm)
}
}
Expand Down Expand Up @@ -37,4 +41,3 @@ pub struct BlockMetadata<I: NodeType> {
offered_fee: u64,
_phantom: PhantomData<I>,
}

40 changes: 27 additions & 13 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ use std::{fmt::Display, path::PathBuf};
use clap::Args;
use derive_more::From;
use futures::FutureExt;
use hotshot_types::{data::VidCommitment, traits::{node_implementation::NodeType, signature_key::SignatureKey}};
use hotshot_types::{
data::VidCommitment,

Check failure on line 7 in src/builder.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `data::VidCommitment`

error: unused import: `data::VidCommitment` --> src/builder.rs:7:5 | 7 | data::VidCommitment, | ^^^^^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_imports)]`

Check failure on line 7 in src/builder.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `data::VidCommitment`

error: unused import: `data::VidCommitment` --> src/builder.rs:7:5 | 7 | data::VidCommitment, | ^^^^^^^^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_imports)]`
traits::{node_implementation::NodeType, signature_key::SignatureKey},
};
use serde::{Deserialize, Serialize};
use snafu::{OptionExt, ResultExt, Snafu};

Check failure on line 11 in src/builder.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `OptionExt`

error: unused import: `OptionExt` --> src/builder.rs:11:13 | 11 | use snafu::{OptionExt, ResultExt, Snafu}; | ^^^^^^^^^

Check failure on line 11 in src/builder.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `OptionExt`

error: unused import: `OptionExt` --> src/builder.rs:11:13 | 11 | use snafu::{OptionExt, ResultExt, Snafu}; | ^^^^^^^^^
use tagged_base64::TaggedBase64;
use tide_disco::{api::ApiError, method::ReadState, Api, RequestError, StatusCode};

use crate::{api::load_api, block_metadata::BlockHash, data_source::{self, BuilderDataSource}};
use crate::{
api::load_api,
block_metadata::BlockHash,

Check failure on line 17 in src/builder.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `block_metadata::BlockHash`, `self`

error: unused imports: `block_metadata::BlockHash`, `self` --> src/builder.rs:17:5 | 17 | block_metadata::BlockHash, | ^^^^^^^^^^^^^^^^^^^^^^^^^ 18 | data_source::{self, BuilderDataSource}, | ^^^^

Check failure on line 17 in src/builder.rs

View workflow job for this annotation

GitHub Actions / clippy

unused imports: `block_metadata::BlockHash`, `self`

error: unused imports: `block_metadata::BlockHash`, `self` --> src/builder.rs:17:5 | 17 | block_metadata::BlockHash, | ^^^^^^^^^^^^^^^^^^^^^^^^^ 18 | data_source::{self, BuilderDataSource}, | ^^^^
data_source::{self, BuilderDataSource},
};

#[derive(Args, Default)]
pub struct Options {
Expand Down Expand Up @@ -40,7 +47,6 @@ pub enum BuildError {
Error { message: String },
}


#[derive(Clone, Debug, From, Snafu, Deserialize, Serialize)]
#[snafu(visibility(pub))]
pub enum Error {
Expand All @@ -65,14 +71,16 @@ pub enum Error {
},
}


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 + BuilderDataSource<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,
<<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(),
Expand All @@ -83,21 +91,27 @@ where
.get("available_blocks", |req, state| {
async move {
let hash = req.blob_param("parent_hash")?;
state.get_available_blocks(&hash).await.context(BlockAvailableSnafu {
resource: hash.to_string(),
})
state
.get_available_blocks(&hash)
.await
.context(BlockAvailableSnafu {
resource: hash.to_string(),
})
}
.boxed()
})?
.get("claim_block", |req, state| {
async move {
let hash = req.blob_param("block_hash")?;
let signature = req.blob_param("signature")?;
state.claim_block(&hash, &signature).await.context(BlockClaimSnafu {
resource: hash.to_string(),
})
state
.claim_block(&hash, &signature)
.await
.context(BlockClaimSnafu {
resource: hash.to_string(),
})
}
.boxed()
})?;
Ok(api)
}
}
30 changes: 22 additions & 8 deletions src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@ use std::sync::Arc;

use async_trait::async_trait;
use commit::Committable;

Check failure on line 4 in src/data_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `commit::Committable`

error: unused import: `commit::Committable` --> src/data_source.rs:4:5 | 4 | use commit::Committable; | ^^^^^^^^^^^^^^^^^^^

Check failure on line 4 in src/data_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `commit::Committable`

error: unused import: `commit::Committable` --> src/data_source.rs:4:5 | 4 | use commit::Committable; | ^^^^^^^^^^^^^^^^^^^
use hotshot_types::{data::VidCommitment, traits::{node_implementation::NodeType, signature_key::SignatureKey, BlockPayload}};
use hotshot_types::{
data::VidCommitment,
traits::{node_implementation::NodeType, signature_key::SignatureKey, BlockPayload},

Check failure on line 7 in src/data_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `BlockPayload`

error: unused import: `BlockPayload` --> src/data_source.rs:7:74 | 7 | traits::{node_implementation::NodeType, signature_key::SignatureKey, BlockPayload}, | ^^^^^^^^^^^^

Check failure on line 7 in src/data_source.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `BlockPayload`

error: unused import: `BlockPayload` --> src/data_source.rs:7:74 | 7 | traits::{node_implementation::NodeType, signature_key::SignatureKey, BlockPayload}, | ^^^^^^^^^^^^
};
use tagged_base64::TaggedBase64;

use crate::{block_metadata::{BlockHash, BlockMetadata}, builder::BuildError};
use crate::{
block_metadata::{BlockHash, BlockMetadata},
builder::BuildError,
};

#[async_trait]
pub trait BuilderDataSource<I>
where I: NodeType,
<<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType: for<'a> TryFrom<&'a TaggedBase64> + Into<TaggedBase64>
{
async fn get_available_blocks(&self, for_parent: &VidCommitment) -> Result<Vec<BlockMetadata<I>>, BuildError>;
async fn claim_block(&self, block_hash: &BlockHash<I>, signature: &<<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType) -> Result<I::BlockPayload, BuildError>;
where
I: NodeType,
<<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType:
for<'a> TryFrom<&'a TaggedBase64> + Into<TaggedBase64>,
{
async fn get_available_blocks(
&self,
for_parent: &VidCommitment,
) -> Result<Vec<BlockMetadata<I>>, BuildError>;
async fn claim_block(
&self,
block_hash: &BlockHash<I>,
signature: &<<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType,
) -> Result<I::BlockPayload, BuildError>;
async fn submit_txn(&self, txn: <I as NodeType>::Transaction) -> Result<(), BuildError>;
}

2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod api;
pub mod block_metadata;
pub mod builder;
pub mod data_source;
pub mod query_data;
mod api;
1 change: 0 additions & 1 deletion src/query_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//
// TODO: License


use hotshot_types::traits::node_implementation::NodeType;
use serde::{Deserialize, Serialize};

Expand Down

0 comments on commit 212ec65

Please sign in to comment.