From aeffb05581a9f1e21d3a88f22799279a85a7bea6 Mon Sep 17 00:00:00 2001 From: Thomas Niederberger Date: Thu, 27 Jul 2023 13:28:36 +0200 Subject: [PATCH] Error handling and documentation --- examples/examples/runtime_update_sync.rs | 2 +- src/api/rpc_api/runtime_update.rs | 26 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/examples/examples/runtime_update_sync.rs b/examples/examples/runtime_update_sync.rs index cd7f80718..d195cf79d 100644 --- a/examples/examples/runtime_update_sync.rs +++ b/examples/examples/runtime_update_sync.rs @@ -45,7 +45,7 @@ async fn main() { println!("Current spec_version: {}", api.spec_version()); let handler = thread::spawn(move || { - let runtime_update_detected = update_detector.detect_runtime_update(); + let runtime_update_detected = update_detector.detect_runtime_update().unwrap(); println!("Detected runtime update: {runtime_update_detected}"); }); diff --git a/src/api/rpc_api/runtime_update.rs b/src/api/rpc_api/runtime_update.rs index f9a1d8276..355725d79 100644 --- a/src/api/rpc_api/runtime_update.rs +++ b/src/api/rpc_api/runtime_update.rs @@ -10,10 +10,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -use crate::{ac_primitives::Config, rpc::Subscribe, rpc_api::EventSubscriptionFor}; +use crate::{ + ac_primitives::Config, api::Error, rpc::Subscribe, rpc_api::EventSubscriptionFor, Result, +}; use alloc::sync::Arc; use core::sync::atomic::{AtomicBool, Ordering}; +/// Struct to support waiting for runtime updates pub struct RuntimeUpdateDetector where T: Config, @@ -32,6 +35,8 @@ where Self { subscription, external_cancellation: None } } + /// Provide the `RuntimeUpdateDetector` with the additional option to cancel the waiting + /// from the outside pub fn new_with_cancellation( subscription: EventSubscriptionFor, cancellation: Arc, @@ -39,24 +44,29 @@ where Self { subscription, external_cancellation: Some(cancellation) } } - /// Returns true if a runtime update was detected, false if the wait was cancelled for some other reason + /// Returns true if a runtime update was detected, false if the wait was cancelled + /// In principle this method only returns/resolves once a runtime update is detected #[maybe_async::maybe_async(?Send)] - pub async fn detect_runtime_update(&mut self) -> bool { + pub async fn detect_runtime_update(&mut self) -> Result { 'outer: loop { if let Some(canceled) = &self.external_cancellation { if canceled.load(Ordering::SeqCst) { - return false + return Ok(false) } } - let event_records = - self.subscription.next_events_from_metadata().await.unwrap().unwrap(); + let event_records = self + .subscription + .next_events_from_metadata() + .await + .ok_or(Error::Other("Error receiving events".into()))?; + let event_records = event_records?; let event_iter = event_records.iter(); for event in event_iter { - if event.unwrap().is_code_update() { + if event?.is_code_update() { break 'outer } } } - true + Ok(true) } }