refactor(rpc): support default port in URL (#1122)

* refactor: support default port in URL

Update jsonrpsee to v0.20 to support the default port number in URLs.

* fix nit, revert web feature

* fix lightclient code
This commit is contained in:
Niklas Adolfsson
2023-08-15 10:53:26 +02:00
committed by GitHub
parent 751e738d8f
commit 8b4fea0b07
15 changed files with 112 additions and 113 deletions
+5 -5
View File
@@ -18,7 +18,7 @@ use crate::error::CodegenError;
use crate::{
ir,
types::{CompositeDef, CompositeDefFields, TypeGenerator, TypeSubstitutes},
utils::{fetch_metadata_bytes_blocking, MetadataVersion, Uri},
utils::{fetch_metadata_bytes_blocking, MetadataVersion, Url},
CratePath,
};
use codec::Decode;
@@ -144,21 +144,21 @@ impl GenerateRuntimeApi {
/// # Warning
///
/// Not recommended to be used in production environments.
pub fn generate_from_url(self, url: &Uri) -> Result<TokenStream2, CodegenError> {
fn fetch_metadata(url: &Uri, version: MetadataVersion) -> Result<Metadata, CodegenError> {
pub fn generate_from_url(self, url: Url) -> Result<TokenStream2, CodegenError> {
fn fetch_metadata(url: Url, version: MetadataVersion) -> Result<Metadata, CodegenError> {
let bytes = fetch_metadata_bytes_blocking(url, version)?;
Ok(Metadata::decode(&mut &bytes[..])?)
}
let metadata = self
.unstable_metadata
.then(|| fetch_metadata(url, MetadataVersion::Unstable).ok())
.then(|| fetch_metadata(url.clone(), MetadataVersion::Unstable).ok())
.flatten();
let metadata = if let Some(unstable) = metadata {
unstable
} else {
match fetch_metadata(url, MetadataVersion::Version(15)) {
match fetch_metadata(url.clone(), MetadataVersion::Version(15)) {
Ok(metadata) => metadata,
Err(_) => fetch_metadata(url, MetadataVersion::Version(14))?,
}
+14 -17
View File
@@ -6,7 +6,7 @@ use crate::error::FetchMetadataError;
use codec::{Decode, Encode};
use jsonrpsee::{
async_client::ClientBuilder,
client_transport::ws::{Uri, WsTransportClientBuilder},
client_transport::ws::{Url, WsTransportClientBuilder},
core::{client::ClientT, Error},
http_client::HttpClientBuilder,
rpc_params,
@@ -46,7 +46,7 @@ impl std::str::FromStr for MetadataVersion {
/// Returns the metadata bytes from the provided URL, blocking the current thread.
pub fn fetch_metadata_bytes_blocking(
url: &Uri,
url: Url,
version: MetadataVersion,
) -> Result<Vec<u8>, FetchMetadataError> {
tokio_block_on(fetch_metadata_bytes(url, version))
@@ -54,7 +54,7 @@ pub fn fetch_metadata_bytes_blocking(
/// Returns the raw, 0x prefixed metadata hex from the provided URL, blocking the current thread.
pub fn fetch_metadata_hex_blocking(
url: &Uri,
url: Url,
version: MetadataVersion,
) -> Result<String, FetchMetadataError> {
tokio_block_on(fetch_metadata_hex(url, version))
@@ -71,16 +71,13 @@ fn tokio_block_on<T, Fut: std::future::Future<Output = T>>(fut: Fut) -> T {
/// Returns the metadata bytes from the provided URL.
pub async fn fetch_metadata_bytes(
url: &Uri,
url: Url,
version: MetadataVersion,
) -> Result<Vec<u8>, FetchMetadataError> {
let bytes = match url.scheme_str() {
Some("http") | Some("https") => fetch_metadata_http(url, version).await,
Some("ws") | Some("wss") => fetch_metadata_ws(url, version).await,
invalid_scheme => {
let scheme = invalid_scheme.unwrap_or("no scheme");
Err(FetchMetadataError::InvalidScheme(scheme.to_owned()))
}
let bytes = match url.scheme() {
"http" | "https" => fetch_metadata_http(url, version).await,
"ws" | "wss" => fetch_metadata_ws(url, version).await,
invalid_scheme => Err(FetchMetadataError::InvalidScheme(invalid_scheme.to_owned())),
}?;
Ok(bytes)
@@ -88,7 +85,7 @@ pub async fn fetch_metadata_bytes(
/// Returns the raw, 0x prefixed metadata hex from the provided URL.
pub async fn fetch_metadata_hex(
url: &Uri,
url: Url,
version: MetadataVersion,
) -> Result<String, FetchMetadataError> {
let bytes = fetch_metadata_bytes(url, version).await?;
@@ -97,29 +94,29 @@ pub async fn fetch_metadata_hex(
}
async fn fetch_metadata_ws(
url: &Uri,
url: Url,
version: MetadataVersion,
) -> Result<Vec<u8>, FetchMetadataError> {
let (sender, receiver) = WsTransportClientBuilder::default()
.build(url.to_string().parse::<Uri>().unwrap())
.build(url)
.await
.map_err(|e| Error::Transport(e.into()))?;
let client = ClientBuilder::default()
.request_timeout(Duration::from_secs(180))
.max_notifs_per_subscription(4096)
.max_buffer_capacity_per_subscription(4096)
.build_with_tokio(sender, receiver);
fetch_metadata(client, version).await
}
async fn fetch_metadata_http(
url: &Uri,
url: Url,
version: MetadataVersion,
) -> Result<Vec<u8>, FetchMetadataError> {
let client = HttpClientBuilder::default()
.request_timeout(Duration::from_secs(180))
.build(url.to_string())?;
.build(url)?;
fetch_metadata(client, version).await
}
+1 -1
View File
@@ -7,7 +7,7 @@
mod fetch_metadata;
// easy access to this type needed for fetching metadata:
pub use jsonrpsee::client_transport::ws::Uri;
pub use jsonrpsee::client_transport::ws::Url;
pub use fetch_metadata::{
fetch_metadata_bytes, fetch_metadata_bytes_blocking, fetch_metadata_hex,