substrate_api_client/api/rpc_api/
runtime_update.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
	Copyright 2023 Supercomputing Systems AG
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
		http://www.apache.org/licenses/LICENSE-2.0
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
use crate::{api::Error, rpc::Subscribe, rpc_api::EventSubscriptionFor, Result};
use alloc::sync::Arc;
use codec::{Decode, Encode};
use core::sync::atomic::{AtomicBool, Ordering};
use serde::de::DeserializeOwned;

/// Struct to support waiting for runtime updates.
pub struct RuntimeUpdateDetector<Hash, Client>
where
	Hash: DeserializeOwned + Copy + Decode,
	Client: Subscribe,
{
	subscription: EventSubscriptionFor<Client, Hash>,
	external_cancellation: Option<Arc<AtomicBool>>,
}

impl<Hash, Client> RuntimeUpdateDetector<Hash, Client>
where
	Hash: DeserializeOwned + Copy + Encode + Decode,
	Client: Subscribe,
{
	pub fn new(subscription: EventSubscriptionFor<Client, Hash>) -> Self {
		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<Client, Hash>,
		cancellation: Arc<AtomicBool>,
	) -> Self {
		Self { subscription, external_cancellation: Some(cancellation) }
	}

	/// Returns true if a runtime update was detected, false if the wait was cancelled
	/// If not cancelled, this method only returns/resolves once a runtime update is detected.
	#[maybe_async::maybe_async(?Send)]
	pub async fn detect_runtime_update(&mut self) -> Result<bool> {
		'outer: loop {
			if let Some(canceled) = &self.external_cancellation {
				if canceled.load(Ordering::SeqCst) {
					return Ok(false)
				}
			}
			let event_records = self
				.subscription
				.next_events_from_metadata()
				.await
				.ok_or(Error::Other("Error receiving events".into()))??;
			let event_iter = event_records.iter();
			for event in event_iter {
				if event?.is_code_update() {
					break 'outer
				}
			}
		}
		Ok(true)
	}
}