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

Rpc-clients: Add creation function with separate address and port #718

Merged
merged 25 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3a22061
add new_with_url and change new to include port
haerdib Feb 9, 2024
30db27c
test works with sync-api
haerdib Feb 9, 2024
6b2b6ba
add new carog test to ci
haerdib Feb 9, 2024
cdeb9bc
remove new
haerdib Feb 15, 2024
8d16151
remove not needed changes
haerdib Feb 15, 2024
bd6e06b
fix cargo.lock
haerdib Feb 15, 2024
0a72a3f
add test to tungstenite
haerdib Feb 15, 2024
eda87ba
fix jsonrpsee
haerdib Feb 15, 2024
bf1d020
add jsonrpsee runtime tests
haerdib Feb 15, 2024
26e9838
do not change existing tests
haerdib Feb 15, 2024
4293399
fix comment
haerdib Feb 15, 2024
fdf3857
add missing await
haerdib Feb 15, 2024
c538d8c
Merge branch 'master' into bh/109-pass-port-independently
haerdib Feb 15, 2024
df024f4
fix dumping test
haerdib Feb 15, 2024
54ed60e
lets not make it a breaking change
haerdib Feb 15, 2024
ef8d551
fix jsonrpsee client
haerdib Feb 15, 2024
3ac4073
Merge branch 'master' into bh/109-pass-port-independently
haerdib Feb 15, 2024
2124bef
fix rpc clients
haerdib Feb 15, 2024
b7996ed
Merge branch 'master' into bh/109-pass-port-independently
haerdib Feb 15, 2024
37d31ee
Merge branch 'master' into bh/109-pass-port-independently
haerdib Feb 19, 2024
ffa56e0
Merge branch 'master' into bh/109-pass-port-independently
haerdib Feb 19, 2024
4f312aa
fix comment
haerdib Feb 19, 2024
05c6e63
Merge branch 'master' into bh/109-pass-port-independently
haerdib Feb 19, 2024
9c580cc
split unit tests
haerdib Feb 19, 2024
3d00be4
Merge branch 'bh/109-pass-port-independently' of https://github.com/s…
haerdib Feb 19, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ jobs:
chain_tests,
dispatch_errors_tests,
frame_system_tests,
jsonrpsee_tests,
keystore_tests,
pallet_balances_tests,
pallet_transaction_payment_tests,
Expand Down
11 changes: 11 additions & 0 deletions src/rpc/jsonrpsee_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ impl JsonrpseeClient {
Self::new("ws://127.0.0.1:9944").await
}

/// Create a new client with the given url string.
/// Example url input: "ws://127.0.0.1:9944"
pub async fn new(url: &str) -> Result<Self> {
let parsed_url: Url = url.parse().map_err(|e| Error::Client(Box::new(e)))?;
let (tx, rx) = WsTransportClientBuilder::default()
Expand All @@ -48,6 +50,15 @@ impl JsonrpseeClient {
.build_with_tokio(tx, rx);
Ok(Self { inner: Arc::new(client) })
}

/// Create a new client with the given address, port and max number of reconnection attempts.
/// Example input:
/// - address: "ws://127.0.0.1"
/// - port: 9944
pub async fn new_with_port(address: &str, port: u32) -> Result<Self> {
let url = format!("{address}:{port:?}");
Self::new(&url).await
}
}

#[maybe_async::async_impl(?Send)]
Expand Down
39 changes: 38 additions & 1 deletion src/rpc/tungstenite_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,25 @@ pub struct TungsteniteRpcClient {
}

impl TungsteniteRpcClient {
/// Create a new client with the given url string.
/// Example url input: "ws://127.0.0.1:9944"
pub fn new(url: &str, max_attempts: u8) -> Result<Self> {
Ok(Self { url: Url::parse(url)?, max_attempts })
let url: Url = Url::parse(url)?;
Ok(Self { url, max_attempts })
}

/// Create a new client with the given address, port and max number of reconnection attempts.
/// Example input:
/// - address: "ws://127.0.0.1"
/// - port: 9944
pub fn new_with_port(address: &str, port: u32, max_attempts: u8) -> Result<Self> {
let url = format!("{address}:{port:?}");
Self::new(&url, max_attempts)
}

/// Create a new client with a local address and default Substrate node port.
pub fn with_default_url(max_attempts: u8) -> Self {
// This unwrap is safe as is only regards the url parsing, which is tested.
Self::new("ws://127.0.0.1:9944", max_attempts).unwrap()
}
}
Expand Down Expand Up @@ -210,3 +224,26 @@ fn read_until_text_message(socket: &mut MySocket) -> Result<String> {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn client_new() {
let port = 9944;
let address = "ws://127.0.0.1";
let client = TungsteniteRpcClient::new_with_port(address, port, 1).unwrap();

let expected_url = Url::parse("ws://127.0.0.1:9944").unwrap();
assert_eq!(client.url, expected_url);
}

#[test]
fn client_with_default_url() {
let expected_url = Url::parse("ws://127.0.0.1:9944").unwrap();
let client = TungsteniteRpcClient::with_default_url(1);

assert_eq!(client.url, expected_url);
}
}
39 changes: 38 additions & 1 deletion src/rpc/ws_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ pub struct WsRpcClient {
}

impl WsRpcClient {
/// Create a new client with the given url string.
/// Example url input: "ws://127.0.0.1:9944"
pub fn new(url: &str) -> Result<Self> {
Ok(Self { url: Url::parse(url)? })
let url = Url::parse(url)?;
Ok(Self { url })
}

/// Create a new client with the given address and port.
/// Example input:
/// - address: "ws://127.0.0.1"
/// - port: 9944
pub fn new_with_port(address: &str, port: u32) -> Result<Self> {
let url = format!("{address}:{port:?}");
Self::new(&url)
}

/// Create a new client with a local address and default Substrate node port.
pub fn with_default_url() -> Self {
// This unwrap is safe as is only regards the url parsing, which is tested.
Self::new("ws://127.0.0.1:9944").unwrap()
}
}
Expand Down Expand Up @@ -127,3 +141,26 @@ impl WsRpcClient {
Ok(result_out.recv()?)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn client_new() {
let port = 9944;
let address = "ws://127.0.0.1";
let client = WsRpcClient::new_with_port(address, port).unwrap();

let expected_url = Url::parse("ws://127.0.0.1:9944").unwrap();
assert_eq!(client.url, expected_url);
}

#[test]
fn client_with_default_url() {
let expected_url = Url::parse("ws://127.0.0.1:9944").unwrap();
let client = WsRpcClient::with_default_url();

assert_eq!(client.url, expected_url);
}
}
4 changes: 3 additions & 1 deletion testing/async/examples/dump_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use substrate_api_client::{

#[tokio::main]
async fn main() {
let client = JsonrpseeClient::new("wss://kusama-rpc.polkadot.io:443").await.unwrap();
let client = JsonrpseeClient::new_with_port("wss://kusama-rpc.polkadot.io", 443)
.await
.unwrap();
let metadata_bytes: Bytes = client.request("state_getMetadata", rpc_params![]).await.unwrap();
let mut file = File::create("new_ksm_metadata.bin").unwrap();
file.write_all(&metadata_bytes.0).unwrap();
Expand Down
33 changes: 33 additions & 0 deletions testing/async/examples/jsonrpsee_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2024 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.
*/

//! Tests for the Jsonrpseeclient Wrapper. Should happen during runtime, otherwise no connection can be etablished.

use substrate_api_client::rpc::JsonrpseeClient;

#[tokio::main]
async fn main() {
let port = 9944;
let address = "ws://127.0.0.1";

let client = JsonrpseeClient::with_default_url().await;
let client2 = JsonrpseeClient::new_with_port(address, port).await;
let client3 = JsonrpseeClient::new_with_port(address, 9994).await;

assert!(client.is_ok());
assert!(client2.is_ok());
// No node running at this port - creation should fail.
assert!(client3.is_err());
}
Loading