From ad8488f90f2e747bb01f6a2ade78b9427e86bde3 Mon Sep 17 00:00:00 2001 From: edwin Date: Tue, 9 Apr 2024 18:15:16 +0700 Subject: [PATCH] feat: use urls instead of SocketAddr for custom --- Cargo.lock | 17 ++++++++------- crates/da-rpc/Cargo.toml | 1 + crates/da-rpc/src/near/config.rs | 36 +++++++++++++++++++------------- gopkg/da-rpc/near_test.go | 2 +- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f9dd74..8741d8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1350,9 +1350,9 @@ checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" [[package]] name = "form_urlencoded" -version = "1.2.0" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" dependencies = [ "percent-encoding", ] @@ -1762,9 +1762,9 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "idna" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" dependencies = [ "unicode-bidi", "unicode-normalization", @@ -2376,6 +2376,7 @@ dependencies = [ "serde_json", "serde_with", "tokio", + "url", "which 5.0.0", ] @@ -3138,9 +3139,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.3.0" +version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "petgraph" @@ -4852,9 +4853,9 @@ dependencies = [ [[package]] name = "url" -version = "2.4.1" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" +checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" dependencies = [ "form_urlencoded", "idna", diff --git a/crates/da-rpc/Cargo.toml b/crates/da-rpc/Cargo.toml index 3ddd9dd..458c59b 100644 --- a/crates/da-rpc/Cargo.toml +++ b/crates/da-rpc/Cargo.toml @@ -10,6 +10,7 @@ eyre = { workspace = true } futures = { workspace = true } log = { workspace = true } tokio = { version = "1.0", features = [ "full" ] } +url = "2.5.0" # Serialization serde = { workspace = true, default-features = true } diff --git a/crates/da-rpc/src/near/config.rs b/crates/da-rpc/src/near/config.rs index ce637f9..aa5512f 100644 --- a/crates/da-rpc/src/near/config.rs +++ b/crates/da-rpc/src/near/config.rs @@ -1,7 +1,7 @@ use near_da_primitives::Namespace; use serde::{Deserialize, Deserializer}; -use std::net::SocketAddr; use std::{fmt::Display, path::PathBuf}; +use url::Url; #[derive(Debug, Clone, Deserialize)] pub enum KeyType { @@ -32,17 +32,15 @@ pub enum Network { Mainnet, #[default] Testnet, - // [ip]:[port] string format + // provide url Custom(String), } impl Network { fn parse_customnet(s: &str) -> Result { - s.parse::() + s.parse::() .map_err(|err| err.to_string()) - .and_then(|_| { - Ok(Network::Custom(s.into())) - }) + .and_then(|_| Ok(Network::Custom(s.into()))) } } @@ -55,7 +53,7 @@ impl<'de> Deserialize<'de> for Network { match s.to_lowercase().as_str() { "mainnet" => Ok(Network::Mainnet), "testnet" => Ok(Network::Testnet), - socket_addr => Self::parse_customnet(socket_addr).map_err(serde::de::Error::custom), + url => Self::parse_customnet(url).map_err(serde::de::Error::custom), } } } @@ -67,7 +65,7 @@ impl Network { match self { Self::Mainnet => MAINNET_RPC_ENDPOINT.into(), Self::Testnet => TESTNET_RPC_ENDPOINT.into(), - Self::Custom(socket_addr) => ["http://", socket_addr.as_str()].concat(), + Self::Custom(url) => url.clone(), } } pub fn archive_endpoint(&self) -> String { @@ -76,7 +74,7 @@ impl Network { match self { Self::Mainnet => MAINNET_RPC_ARCHIVE_ENDPOINT.into(), Self::Testnet => TESTNET_RPC_ARCHIVE_ENDPOINT.into(), - Self::Custom(socket_addr) => ["http://", socket_addr.as_str()].concat(), + Self::Custom(url) => url.clone(), } } } @@ -86,7 +84,7 @@ impl Display for Network { let s = match self { Self::Mainnet => "mainnet", Self::Testnet => "testnet", - Self::Custom(socket_addr) => socket_addr.as_str(), + Self::Custom(url) => url.as_str(), }; write!(f, "{}", s) } @@ -98,7 +96,7 @@ impl TryFrom<&str> for Network { match s.to_lowercase().as_str() { "mainnet" => Ok(Self::Mainnet), "testnet" => Ok(Self::Testnet), - socket_addr => Self::parse_customnet(socket_addr), + url => Self::parse_customnet(url), } } } @@ -118,15 +116,23 @@ mod tests { let network = Network::try_from("testnet").unwrap(); assert_eq!(network, Network::Testnet); - let url = "127.0.0.1:3030"; - let network = Network::try_from(url).unwrap(); - assert_eq!(network, Network::Custom(url.into())); + { + let url = "http://127.0.0.1:3030"; + let network = Network::try_from(url).unwrap(); + assert_eq!(network, Network::Custom(url.into())); + } + + { + let url = "ws://someurl:2754"; + let network = Network::try_from(url).unwrap(); + assert_eq!(network, Network::Custom(url.into())); + } } #[test] fn test_invalid_local_adress() { let network = Network::try_from("invalid").unwrap_err(); - assert_eq!(network, "invalid socket address syntax"); + assert_eq!(network, "relative URL without a base"); } #[test] diff --git a/gopkg/da-rpc/near_test.go b/gopkg/da-rpc/near_test.go index 3464c15..a5f1b70 100644 --- a/gopkg/da-rpc/near_test.go +++ b/gopkg/da-rpc/near_test.go @@ -64,7 +64,7 @@ func TestNewConfig(t *testing.T) { } func TestNewConfigFile(t *testing.T) { - config, err := near.NewConfigFile("keyPath", "contract", "127.0.0.1:3030", 1) + config, err := near.NewConfigFile("keyPath", "contract", "http://127.0.0.1:3030", 1) if err != nil { t.Error(err) }