From 5717eab897d1ccdda220aa899f0bc8c2de5a49a8 Mon Sep 17 00:00:00 2001 From: Rob Date: Wed, 26 Jun 2024 11:39:16 -0400 Subject: [PATCH 1/2] make service fallible --- src/service.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/service.rs b/src/service.rs index 541b4fd8..cb263bf9 100644 --- a/src/service.rs +++ b/src/service.rs @@ -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::{ @@ -292,9 +292,9 @@ impl GlobalState { pub fn get_channel_for_matching_builder_or_highest_view_buider( &self, key: &(VidCommitment, Types::Time), - ) -> &BroadcastSender> { + ) -> Result<&BroadcastSender>, 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", @@ -304,7 +304,9 @@ impl GlobalState { // 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(), + }) } } @@ -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 { @@ -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:: { @@ -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:: { block_payload: block_info.block_payload.clone(), metadata: block_info.metadata.clone(), @@ -661,7 +668,10 @@ 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 = @@ -669,7 +679,9 @@ where &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, @@ -677,7 +689,9 @@ where &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:: { vid_commitment, @@ -836,7 +850,8 @@ pub async fn run_non_permissioned_standalone_builder_service( "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; @@ -909,7 +924,8 @@ pub async fn run_non_permissioned_standalone_builder_service( "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")?; } } } From f09c635f3841152d864b989f57ee921b2183611b Mon Sep 17 00:00:00 2001 From: Rob Date: Wed, 26 Jun 2024 11:42:33 -0400 Subject: [PATCH 2/2] fix test --- src/testing/basic_test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/testing/basic_test.rs b/src/testing/basic_test.rs index 5ba988b6..4c9e6b71 100644 --- a/src/testing/basic_test.rs +++ b/src/testing/basic_test.rs @@ -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();