Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #185 from EspressoSystems/rm/make-fallible
Browse files Browse the repository at this point in the history
Make service fallible
  • Loading branch information
rob-maron authored Jun 26, 2024
2 parents 4526897 + f09c635 commit 4af4e58
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::builder_state::{
};
use crate::builder_state::{MessageType, RequestMessage, ResponseMessage};
use crate::WaitAndKeep;
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use async_broadcast::Sender as BroadcastSender;
pub use async_broadcast::{broadcast, RecvError, TryRecvError};
use async_compatibility_layer::{
Expand Down Expand Up @@ -292,9 +292,9 @@ impl<Types: NodeType> GlobalState<Types> {
pub fn get_channel_for_matching_builder_or_highest_view_buider(
&self,
key: &(VidCommitment, Types::Time),
) -> &BroadcastSender<MessageType<Types>> {
) -> Result<&BroadcastSender<MessageType<Types>>, BuildError> {
if let Some(channel) = self.spawned_builder_states.get(key) {
channel
Ok(channel)
} else {
tracing::info!(
"failed to recover builder for parent {:?}@{:?}, using higest view num builder",
Expand All @@ -304,7 +304,9 @@ impl<Types: NodeType> GlobalState<Types> {
// get the sender for the highest view number builder
self.spawned_builder_states
.get(&self.highest_view_num_builder_id)
.expect("failed to recover highest view num builder")
.ok_or_else(|| BuildError::Error {
message: "No builder state found".to_string(),
})
}
}

Expand Down Expand Up @@ -421,7 +423,7 @@ where
.global_state
.read_arc()
.await
.get_channel_for_matching_builder_or_highest_view_buider(&(*for_parent, view_num))
.get_channel_for_matching_builder_or_highest_view_buider(&(*for_parent, view_num))?
.broadcast(MessageType::RequestMessage(req_msg.clone()))
.await
{
Expand Down Expand Up @@ -484,7 +486,9 @@ where
response.offered_fee,
&response.builder_hash,
)
.expect("Available block info signing failed");
.map_err(|e| BuildError::Error {
message: format!("Signing over block info failed: {:?}", e),
})?;

// insert the block info into local hashmap
let initial_block_info = AvailableBlockInfo::<Types> {
Expand Down Expand Up @@ -571,7 +575,10 @@ where
&sign_key,
response_block_hash.as_ref(),
)
.expect("Claim block signing failed");
.map_err(|e| BuildError::Error {
message: format!("Signing over builder commitment failed: {:?}", e),
})?;

let block_data = AvailableBlockData::<Types> {
block_payload: block_info.block_payload.clone(),
metadata: block_info.metadata.clone(),
Expand Down Expand Up @@ -661,23 +668,30 @@ where
view_number
);
if response_received.is_ok() {
let (vid_commitment, vid_precompute_data) = response_received.unwrap();
let (vid_commitment, vid_precompute_data) =
response_received.map_err(|err| BuildError::Error {
message: format!("Error getting vid commitment: {:?}", err),
})?;

// sign over the vid commitment
let signature_over_vid_commitment =
<Types as NodeType>::BuilderSignatureKey::sign_builder_message(
&sign_key,
vid_commitment.as_ref(),
)
.expect("Claim block header input message signing failed");
.map_err(|e| BuildError::Error {
message: format!("Failed to sign VID commitment: {:?}", e),
})?;

let signature_over_fee_info = Types::BuilderSignatureKey::sign_fee(
&sign_key,
block_info.offered_fee,
&block_info.metadata,
&vid_commitment,
)
.expect("Claim block header input fee signing failed");
.map_err(|e| BuildError::Error {
message: format!("Failed to sign fee info: {:?}", e),
})?;

let response = AvailableBlockHeaderInput::<Types> {
vid_commitment,
Expand Down Expand Up @@ -836,7 +850,8 @@ pub async fn run_non_permissioned_standalone_builder_service<Types: NodeType>(
"failed to connect to API at {hotshot_events_api_url}"
));
}
let (mut subscribed_events, mut membership) = connected.unwrap();
let (mut subscribed_events, mut membership) =
connected.context("Failed to connect to events service")?;

loop {
let event = subscribed_events.next().await;
Expand Down Expand Up @@ -909,7 +924,8 @@ pub async fn run_non_permissioned_standalone_builder_service<Types: NodeType>(
"failed to reconnect to API at {hotshot_events_api_url}"
));
}
(subscribed_events, membership) = connected.unwrap();
(subscribed_events, membership) =
connected.context("Failed to reconnect to events service")?;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/testing/basic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ mod tests {
.read_arc()
.await
.get_channel_for_matching_builder_or_highest_view_buider(&req_msg.1)
.expect("Failed to get channel for matching builder or highest view builder")
.broadcast(req_msg.2.clone())
.await
.unwrap();
Expand Down

0 comments on commit 4af4e58

Please sign in to comment.