Skip to content

Commit

Permalink
fix final comments
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Sep 21, 2022
1 parent 370482e commit fe157a1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 53 deletions.
18 changes: 3 additions & 15 deletions examples/examples/subscribe_runtime_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Start a new tokio task to perform the runtime updates while
// utilizing the API for other use cases.
let updater = api.subscribe_to_updates();
let update_client = api.subscribe_to_updates();
tokio::spawn(async move {
let mut update_stream = updater.runtime_updates().await.unwrap();

while let Some(Ok(update)) = update_stream.next().await {
let version = update.runtime_version().spec_version;

match updater.apply_update(update) {
Ok(()) => {
println!("Upgrade to version: {} successful", version)
}
Err(e) => {
println!("Upgrade to version {} failed {:?}", version, e);
}
};
}
let result = update_client.perform_runtime_updates().await;
println!("Runtime update failed with result={:?}", result);
});

// If this client is kept in use a while, it'll update its metadata and such
Expand Down
48 changes: 10 additions & 38 deletions subxt/src/client/online_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use super::{
OfflineClient,
OfflineClientT,
};
use super::{OfflineClient, OfflineClientT};
use crate::{
constants::ConstantsClient,
error::Error,
events::EventsClient,
rpc::{
Rpc,
RpcClientT,
RuntimeVersion,
Subscription,
},
rpc::{Rpc, RpcClientT, RuntimeVersion, Subscription},
storage::StorageClient,
tx::TxClient,
Config,
Metadata,
Config, Metadata,
};
use derivative::Derivative;
use futures::future;
Expand Down Expand Up @@ -243,7 +234,7 @@ impl<T: Config> ClientRuntimeUpdater<T> {
/// Tries to apply a new update.
pub fn apply_update(&self, update: Update) -> Result<(), UpgradeError> {
if !self.is_runtime_version_different(&update.runtime_version) {
return Err(UpgradeError::SameVersion)
return Err(UpgradeError::SameVersion);
}

self.do_update(update);
Expand All @@ -257,25 +248,13 @@ impl<T: Config> ClientRuntimeUpdater<T> {
/// would be to run it in a separate background task.
pub async fn perform_runtime_updates(&self) -> Result<(), Error> {
// Obtain an update subscription to further detect changes in the runtime version of the node.
let mut runtime_version_stream = self.0.rpc().subscribe_runtime_version().await?;

while let Some(new_runtime_version) = runtime_version_stream.next().await {
// The Runtime Version obtained via subscription.
let new_runtime_version = new_runtime_version?;

// Fetch new metadata.
let metadata = self.0.rpc.metadata().await?;
let mut runtime_version_stream = self.runtime_updates().await?;

// Ignore this update if there is no difference.
if !self.is_runtime_version_different(&new_runtime_version) {
continue
}
while let Some(update) = runtime_version_stream.next().await {
let update = update?;

// Ignore if the update fails.
let _ = self.apply_update(Update {
metadata,
runtime_version: new_runtime_version,
});
let _ = self.apply_update(update);
}

Ok(())
Expand Down Expand Up @@ -354,17 +333,10 @@ impl Update {
mod jsonrpsee_helpers {
pub use jsonrpsee::{
client_transport::ws::{
InvalidUri,
Receiver,
Sender,
Uri,
WsTransportClientBuilder,
InvalidUri, Receiver, Sender, Uri, WsTransportClientBuilder,
},
core::{
client::{
Client,
ClientBuilder,
},
client::{Client, ClientBuilder},
Error,
},
};
Expand Down

0 comments on commit fe157a1

Please sign in to comment.