Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch-metadata: remove tokio #1822

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ description = "Generate an API for interacting with a substrate node from FRAME

[features]
default = []
fetch-metadata = ["dep:jsonrpsee", "dep:tokio", "dep:frame-metadata"]
web = ["jsonrpsee?/async-wasm-client", "jsonrpsee?/client-web-transport", "getrandom/js"]
fetch-metadata = ["dep:jsonrpsee", "dep:frame-metadata", "dep:url"]
# NOTE: This is just hack that only enables `getrandom/js` and doesn't do anything else.
web = ["getrandom/js"]

[dependencies]
codec = { package = "parity-scale-codec", workspace = true, features = ["derive"] }
Expand All @@ -25,11 +26,11 @@ quote = { workspace = true }
syn = { workspace = true }
scale-info = { workspace = true }
subxt-metadata = { workspace = true }
jsonrpsee = { workspace = true, features = ["async-client", "client-ws-transport-tls", "http-client"], optional = true }
jsonrpsee = { workspace = true, features = ["ws-client", "http-client"], optional = true }
hex = { workspace = true, features = ["std"] }
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
thiserror = { workspace = true }
scale-typegen = { workspace = true }
url = { workspace = true, optional = true }

# Included if "web" feature is enabled, to enable its js feature.
getrandom = { workspace = true, optional = true }
Expand Down
35 changes: 5 additions & 30 deletions codegen/src/fetch_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
use crate::error::FetchMetadataError;
use codec::{Decode, Encode};
use jsonrpsee::{
async_client::ClientBuilder,
client_transport::ws::WsTransportClientBuilder,
core::client::{ClientT, Error},
http_client::HttpClientBuilder,
rpc_params,
core::client::ClientT, http_client::HttpClientBuilder, rpc_params, ws_client::WsClientBuilder,
};
use std::time::Duration;

pub use jsonrpsee::client_transport::ws::Url;
pub use url::Url;

/// The metadata version that is fetched from the node.
#[derive(Default, Debug, Clone, Copy)]
Expand Down Expand Up @@ -60,23 +56,6 @@ pub fn fetch_metadata_from_file_blocking(
Ok(bytes)
}

/// Returns the metadata bytes from the provided URL, blocking the current thread.
pub fn fetch_metadata_from_url_blocking(
url: Url,
version: MetadataVersion,
) -> Result<Vec<u8>, FetchMetadataError> {
tokio_block_on(fetch_metadata_from_url(url, version))
}

// Block on some tokio runtime for sync contexts
fn tokio_block_on<T, Fut: std::future::Future<Output = T>>(fut: Fut) -> T {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(fut)
}

/// Returns the metadata bytes from the provided URL.
pub async fn fetch_metadata_from_url(
url: Url,
Expand All @@ -95,15 +74,11 @@ async fn fetch_metadata_ws(
url: Url,
version: MetadataVersion,
) -> Result<Vec<u8>, FetchMetadataError> {
let (sender, receiver) = WsTransportClientBuilder::default()
.build(url)
.await
.map_err(|e| Error::Transport(e.into()))?;

let client = ClientBuilder::default()
let client = WsClientBuilder::new()
.request_timeout(Duration::from_secs(180))
.max_buffer_capacity_per_subscription(4096)
.build_with_tokio(sender, receiver);
.build(url)
.await?;

fetch_metadata(client, version).await
}
Expand Down
1 change: 1 addition & 0 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ quote = { workspace = true }
subxt-codegen = { workspace = true, features = ["fetch-metadata"] }
scale-typegen = { workspace = true }
polkadot-sdk = { workspace = true, optional = true, features = ["sp-io", "sc-executor-common", "sp-state-machine", "sp-maybe-compressed-blob", "sc-executor"] }
futures = { workspace = true, features = ["executor"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess one reason for tokio was just that it was already likely to be included as a dep. I wonder if this adds any new deps to the tree or whether it was also in use somewhere anyways?

Copy link
Member Author

@niklasad1 niklasad1 Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: It will bring in futures-executor/futures-task which is small and it should be much more lite-weight than tokio rt and it compiles for WASM

If one brings in futures with the default features this is already enabled and most likely already enabled for most cases I think...

niklasad1 marked this conversation as resolved.
Show resolved Hide resolved

[lints]
workspace = true
13 changes: 8 additions & 5 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use scale_typegen::typegen::{
};
use subxt_codegen::{
fetch_metadata::{
fetch_metadata_from_file_blocking, fetch_metadata_from_url_blocking, MetadataVersion, Url,
fetch_metadata_from_file_blocking, fetch_metadata_from_url, MetadataVersion, Url,
},
CodegenBuilder, CodegenError, Metadata,
};
Expand Down Expand Up @@ -256,10 +256,13 @@ fn fetch_metadata(args: &RuntimeMetadataArgs) -> Result<subxt_codegen::Metadata,
false => MetadataVersion::Latest,
};

fetch_metadata_from_url_blocking(url, version)
.map_err(CodegenError::from)
.and_then(|b| subxt_codegen::Metadata::decode(&mut &*b).map_err(Into::into))
.map_err(|e| e.into_compile_error())?
futures::executor::block_on(async {
fetch_metadata_from_url(url, version)
.await
.map_err(CodegenError::from)
.and_then(|b| subxt_codegen::Metadata::decode(&mut &*b).map_err(Into::into))
.map_err(|e| e.into_compile_error())
})?
}
#[cfg(feature = "runtime-path")]
(None, None) => {
Expand Down
Loading