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

client: Cluster parses custom urls from str #369

Merged
merged 3 commits into from
Jun 9, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ incremented for features.
### Features

* cli: Add `--program-name` option for build command to build a single program at a time ([#362](https://github.com/project-serum/anchor/pull/362)).
* cli, client: Parse custom cluster urls from str ([#369](https://github.com/project-serum/anchor/pull/369)).

### Fixes

Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ serde = { version = "1.0.122", features = ["derive"] }
solana-client = "1.6.6"
solana-sdk = "1.6.6"
thiserror = "1.0.20"
url = "2.2.2"
38 changes: 37 additions & 1 deletion client/src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use url::Url;

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Cluster {
Expand All @@ -27,6 +28,21 @@ impl FromStr for Cluster {
"d" | "devnet" => Ok(Cluster::Devnet),
"l" | "localnet" => Ok(Cluster::Localnet),
"g" | "debug" => Ok(Cluster::Debug),
url if url.contains("http") => {
let http_url = url;

// Websocket port is always +1 the http port.
let mut ws_url = Url::parse(http_url)?;
if let Some(port) = ws_url.port() {
ws_url.set_port(Some(port + 1))
.map_err(|_| anyhow!("Unable to set port"))?;
} else {
ws_url.set_port(Some(8900))
.map_err(|_| anyhow!("Unable to set port"))?;
}

Ok(Cluster::Custom(http_url.to_string(), ws_url.to_string()))
}
_ => Err(anyhow::Error::msg(
"Cluster must be one of [localnet, testnet, mainnet, devnet] or be an http or https url\n",
)),
Expand Down Expand Up @@ -94,4 +110,24 @@ mod tests {
let bad_url = "httq://my_custom_url.test.net";
Cluster::from_str(bad_url).unwrap();
}

#[test]
fn test_http_port() {
let url = "http://my-url.com:7000/";
let cluster = Cluster::from_str(url).unwrap();
assert_eq!(
Cluster::Custom(url.to_string(), "http://my-url.com:7001/".to_string()),
cluster
);
}

#[test]
fn test_http_no_port() {
let url = "http://my-url.com/";
let cluster = Cluster::from_str(url).unwrap();
assert_eq!(
Cluster::Custom(url.to_string(), "http://my-url.com:8900/".to_string()),
cluster
);
}
}