-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2022 Espresso Systems (espressosys.com) | ||
// This file is part of the HotShot Query Service library. | ||
// | ||
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
// General Public License as published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// You should have received a copy of the GNU General Public License along with this program. If not, | ||
// see <https://www.gnu.org/licenses/>. | ||
|
||
use std::fs; | ||
use std::path::Path; | ||
use tide_disco::api::{Api, ApiError}; | ||
use toml::{map::Entry, Value}; | ||
|
||
pub(crate) fn load_api<State, Error>( | ||
path: Option<impl AsRef<Path>>, | ||
default: &str, | ||
extensions: impl IntoIterator<Item = Value>, | ||
) -> Result<Api<State, Error>, ApiError> { | ||
let mut toml = match path { | ||
Some(path) => load_toml(path.as_ref())?, | ||
None => toml::from_str(default).map_err(|err| ApiError::CannotReadToml { | ||
reason: err.to_string(), | ||
})?, | ||
}; | ||
for extension in extensions { | ||
merge_toml(&mut toml, extension); | ||
} | ||
Api::new(toml) | ||
} | ||
|
||
fn merge_toml(into: &mut Value, from: Value) { | ||
if let (Value::Table(into), Value::Table(from)) = (into, from) { | ||
for (key, value) in from { | ||
match into.entry(key) { | ||
Entry::Occupied(mut entry) => merge_toml(entry.get_mut(), value), | ||
Entry::Vacant(entry) => { | ||
entry.insert(value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn load_toml(path: &Path) -> Result<Value, ApiError> { | ||
let bytes = fs::read(path).map_err(|err| ApiError::CannotReadToml { | ||
reason: err.to_string(), | ||
})?; | ||
let string = std::str::from_utf8(&bytes).map_err(|err| ApiError::CannotReadToml { | ||
reason: err.to_string(), | ||
})?; | ||
toml::from_str(string).map_err(|err| ApiError::CannotReadToml { | ||
reason: err.to_string(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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 serde::{Deserialize, Serialize}; | ||
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}}; | ||
|
||
#[derive(Args, Default)] | ||
pub struct Options { | ||
#[arg(long = "builder-api-path", env = "HOTSHOT_BUILDER_API_PATH")] | ||
pub api_path: Option<PathBuf>, | ||
|
||
/// Additional API specification files to merge with `builder-api-path`. | ||
/// | ||
/// These optional files may contain route definitions for application-specific routes that have | ||
/// been added as extensions to the basic builder API. | ||
#[arg( | ||
long = "builder-extension", | ||
env = "HOTSHOT_BUILDER_EXTENSIONS", | ||
value_delimiter = ',' | ||
)] | ||
pub extensions: Vec<toml::Value>, | ||
} | ||
|
||
#[derive(Clone, Debug, Snafu, Deserialize, Serialize)] | ||
#[snafu(visibility(pub))] | ||
pub enum BuildError { | ||
/// The requested resource does not exist or is not known to this builder service. | ||
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 building block from {resource}: {source}"))] | ||
#[from(ignore)] | ||
BlockAvailable { | ||
source: BuildError, | ||
resource: String, | ||
}, | ||
#[snafu(display("error claiming block {resource}: {source}"))] | ||
#[from(ignore)] | ||
BlockClaim { | ||
source: BuildError, | ||
resource: String, | ||
}, | ||
Custom { | ||
message: String, | ||
status: StatusCode, | ||
}, | ||
} | ||
|
||
|
||
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, | ||
{ | ||
let mut api = load_api::<State, Error>( | ||
options.api_path.as_ref(), | ||
include_str!("../api/builder.toml"), | ||
options.extensions.clone(), | ||
)?; | ||
api.with_version("0.0.1".parse().unwrap()) | ||
.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(), | ||
}) | ||
} | ||
.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(), | ||
}) | ||
} | ||
.boxed() | ||
})?; | ||
Ok(api) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use hotshot_types::{data::VidCommitment, traits::{node_implementation::NodeType, signature_key::SignatureKey}}; | ||
use commit::Committable; | ||
use hotshot_types::{data::VidCommitment, traits::{node_implementation::NodeType, signature_key::SignatureKey, BlockPayload}}; | ||
use tagged_base64::TaggedBase64; | ||
|
||
use crate::block_metadata::{BlockHash, BlockMetadata}; | ||
use crate::{block_metadata::{BlockHash, BlockMetadata}, builder::BuildError}; | ||
|
||
#[async_trait] | ||
pub trait BuilderDataSource<I: NodeType> { | ||
async fn get_available_blocks(&self, for_parent: &VidCommitment) -> Vec<BlockMetadata<I>>; | ||
async fn claim_block(&self, block_hash: BlockHash, signature: <<I as NodeType>::SignatureKey as SignatureKey>::PureAssembledSignatureType) -> Arc<Vec<u8>>; | ||
async fn submit_txn(&self, txn: <I as NodeType>::Transaction); | ||
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>; | ||
async fn submit_txn(&self, txn: <I as NodeType>::Transaction) -> Result<(), BuildError>; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod block_metadata; | ||
mod data_source; | ||
mod query_data; | ||
|
||
pub mod block_metadata; | ||
pub mod builder; | ||
pub mod data_source; | ||
pub mod query_data; | ||
mod api; |