Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion apps/fortuna/Cargo.lock

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

33 changes: 27 additions & 6 deletions apps/fortuna/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct ApiMetrics {

#[derive(Clone)]
pub struct ApiState {
pub chains: Arc<HashMap<ChainId, BlockchainState>>,
pub chains: Arc<RwLock<HashMap<ChainId, ApiBlockChainState>>>,

pub metrics_registry: Arc<RwLock<Registry>>,

Expand All @@ -53,7 +53,7 @@ pub struct ApiState {

impl ApiState {
pub async fn new(
chains: HashMap<ChainId, BlockchainState>,
chains: Arc<RwLock<HashMap<ChainId, ApiBlockChainState>>>,
metrics_registry: Arc<RwLock<Registry>>,
) -> ApiState {
let metrics = ApiMetrics {
Expand All @@ -68,7 +68,7 @@ impl ApiState {
);

ApiState {
chains: Arc::new(chains),
chains,
metrics: Arc::new(metrics),
metrics_registry,
}
Expand All @@ -94,6 +94,12 @@ pub struct BlockchainState {
pub confirmed_block_status: BlockStatus,
}

#[derive(Clone)]
pub enum ApiBlockChainState {
Uninitialized,
Initialized(BlockchainState),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


pub enum RestError {
/// The caller passed a sequence number that isn't within the supported range
InvalidSequenceNumber,
Expand All @@ -108,6 +114,9 @@ pub enum RestError {
/// The server cannot currently communicate with the blockchain, so is not able to verify
/// which random values have been requested.
TemporarilyUnavailable,
/// The server is not able to process the request because the blockchain initialization
/// has not been completed yet.
Uninitialized,
/// A catch-all error for all other types of errors that could occur during processing.
Unknown,
}
Expand Down Expand Up @@ -137,6 +146,11 @@ impl IntoResponse for RestError {
"This service is temporarily unavailable",
)
.into_response(),
RestError::Uninitialized => (
StatusCode::SERVICE_UNAVAILABLE,
"The service is not yet initialized for this chain, please try again in a few minutes",
)
.into_response(),
RestError::Unknown => (
StatusCode::INTERNAL_SERVER_ERROR,
"An unknown error occurred processing the request",
Expand Down Expand Up @@ -172,6 +186,7 @@ pub fn get_register_uri(base_uri: &str, chain_id: &str) -> Result<String> {

#[cfg(test)]
mod test {
use crate::api::ApiBlockChainState;
use {
crate::{
api::{self, ApiState, BinaryEncoding, Blob, BlockchainState, GetRandomValueResponse},
Expand Down Expand Up @@ -228,10 +243,16 @@ mod test {
};

let mut chains = HashMap::new();
chains.insert("ethereum".into(), eth_state);
chains.insert("avalanche".into(), avax_state);
chains.insert(
"ethereum".into(),
ApiBlockChainState::Initialized(eth_state),
);
chains.insert(
"avalanche".into(),
ApiBlockChainState::Initialized(avax_state),
);

let api_state = ApiState::new(chains, metrics_registry).await;
let api_state = ApiState::new(Arc::new(RwLock::new(chains)), metrics_registry).await;

let app = api::routes(api_state);
(TestServer::new(app).unwrap(), eth_read, avax_read)
Expand Down
8 changes: 7 additions & 1 deletion apps/fortuna/src/api/chain_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ responses(
pub async fn chain_ids(
State(state): State<crate::api::ApiState>,
) -> Result<Json<Vec<ChainId>>, RestError> {
let chain_ids = state.chains.iter().map(|(id, _)| id.clone()).collect();
let chain_ids = state
.chains
.read()
.await
.iter()
.map(|(id, _)| id.clone())
.collect();
Ok(Json(chain_ids))
}
13 changes: 12 additions & 1 deletion apps/fortuna/src/api/revelation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::api::ApiBlockChainState;
use crate::chain::reader::BlockNumber;
use {
crate::api::{ChainId, RequestLabel, RestError},
Expand Down Expand Up @@ -46,8 +47,18 @@ pub async fn revelation(

let state = state
.chains
.read()
.await
.get(&chain_id)
.ok_or(RestError::InvalidChainId)?;
.ok_or(RestError::InvalidChainId)?
.clone();

let state = match state {
ApiBlockChainState::Initialized(state) => state,
ApiBlockChainState::Uninitialized => {
return Err(RestError::Uninitialized);
}
};

let current_block_number_fut = state
.contract
Expand Down
5 changes: 3 additions & 2 deletions apps/fortuna/src/command/register_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ pub async fn register_provider_from_config(

let commitment_length = provider_config.chain_length;
tracing::info!("Generating hash chain");
let chain = PebbleHashChain::from_config(
let chain = PebbleHashChain::from_config_async(
&secret,
chain_id,
&private_key_string.parse::<LocalWallet>()?.address(),
&chain_config.contract_addr,
&random,
commitment_length,
provider_config.chain_sample_interval,
)?;
)
.await?;
tracing::info!("Done generating hash chain");

// Arguments to the contract to register our new provider.
Expand Down
Loading
Loading