Skip to content

Commit 78d5e52

Browse files
authoredMar 12, 2025··
Merge pull request #1518 from jbesraa/2025-02-27/remove-url-utils
Remove `stratum_common::url`
2 parents d7ac19b + 732cc88 commit 78d5e52

File tree

8 files changed

+33
-33
lines changed

8 files changed

+33
-33
lines changed
 

‎common/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
#[cfg(feature = "bitcoin")]
66
pub use bitcoin;
77
pub use secp256k1;
8-
pub mod url;

‎roles/jd-server/src/lib/config.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,11 @@ mod tests {
185185
settings.try_deserialize().expect("Failed to parse config")
186186
}
187187

188-
#[tokio::test]
189-
async fn test_invalid_rpc_url() {
190-
let mut config = load_config("config-examples/jds-config-hosted-example.toml");
191-
config.set_core_rpc_url("invalid".to_string());
192-
assert!(JobDeclaratorServer::new(config).is_err());
193-
}
194-
195188
#[tokio::test]
196189
async fn test_offline_rpc_url() {
197190
let mut config = load_config("config-examples/jds-config-hosted-example.toml");
198191
config.set_core_rpc_url("http://127.0.0.1".to_string());
199-
let jd = JobDeclaratorServer::new(config).unwrap();
192+
let jd = JobDeclaratorServer::new(config);
200193
assert!(jd.start().await.is_err());
201194
}
202195

‎roles/jd-server/src/lib/mempool/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ pub struct TransactionWithHash {
1919
pub struct JDsMempool {
2020
pub mempool: HashMap<Txid, Option<(Transaction, u32)>>,
2121
auth: mini_rpc_client::Auth,
22-
url: String,
22+
url: rpc_sv2::Uri,
2323
new_block_receiver: Receiver<String>,
2424
}
2525

2626
impl JDsMempool {
2727
pub fn get_client(&self) -> Option<mini_rpc_client::MiniRpcClient> {
28-
let url = self.url.as_str();
28+
let url = self.url.to_string();
2929
if url.contains("http") {
30-
let client = mini_rpc_client::MiniRpcClient::new(url.to_string(), self.auth.clone());
30+
let client = mini_rpc_client::MiniRpcClient::new(self.url.clone(), self.auth.clone());
3131
Some(client)
3232
} else {
3333
None
@@ -44,7 +44,7 @@ impl JDsMempool {
4444
}
4545

4646
pub fn new(
47-
url: String,
47+
url: rpc_sv2::Uri,
4848
username: String,
4949
password: String,
5050
new_block_receiver: Receiver<String>,

‎roles/jd-server/src/lib/mod.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use error_handling::handle_result;
1111
use job_declarator::JobDeclarator;
1212
use mempool::error::JdsMempoolError;
1313
use roles_logic_sv2::{parsers::AnyMessage as JdsMessages, utils::Mutex};
14-
use std::{ops::Sub, sync::Arc};
15-
use stratum_common::url::is_valid_url;
14+
pub use rpc_sv2::Uri;
15+
use std::{ops::Sub, str::FromStr, sync::Arc};
1616
use tokio::{select, task};
1717
use tracing::{error, info, warn};
1818

@@ -26,23 +26,25 @@ pub struct JobDeclaratorServer {
2626
}
2727

2828
impl JobDeclaratorServer {
29-
pub fn new(config: JobDeclaratorServerConfig) -> Result<Self, Box<JdsError>> {
30-
let url = config.core_rpc_url().to_string() + ":" + &config.core_rpc_port().to_string();
31-
if !is_valid_url(&url) {
32-
return Err(Box::new(JdsError::InvalidRPCUrl));
33-
}
34-
Ok(Self { config })
29+
pub fn new(config: JobDeclaratorServerConfig) -> Self {
30+
Self { config }
3531
}
3632
pub async fn start(&self) -> Result<(), JdsError> {
37-
let config = self.config.clone();
33+
let mut config = self.config.clone();
34+
// In case the url came with a trailing slash, we remove it to make sure we end up with
35+
// `{scheme}://{host}:{port}` format.
36+
if config.core_rpc_url().ends_with('/') {
37+
config.set_core_rpc_url(config.core_rpc_url().trim_end_matches('/').to_string());
38+
}
3839
let url = config.core_rpc_url().to_string() + ":" + &config.core_rpc_port().to_string();
3940
let username = config.core_rpc_user();
4041
let password = config.core_rpc_pass();
4142
// TODO should we manage what to do when the limit is reaced?
4243
let (new_block_sender, new_block_receiver): (Sender<String>, Receiver<String>) =
4344
bounded(10);
45+
let url = Uri::from_str(&url.clone()).expect("Invalid core rpc url");
4446
let mempool = Arc::new(Mutex::new(mempool::JDsMempool::new(
45-
url.clone(),
47+
url,
4648
username.to_string(),
4749
password.to_string(),
4850
new_block_receiver,
@@ -51,7 +53,7 @@ impl JobDeclaratorServer {
5153
let mempool_cloned_ = mempool.clone();
5254
let mempool_cloned_1 = mempool.clone();
5355
if let Err(e) = mempool::JDsMempool::health(mempool_cloned_1.clone()).await {
54-
error!("{:?}", e);
56+
error!("JDS Connection with bitcoin core failed {:?}", e);
5557
return Err(JdsError::MempoolError(e));
5658
}
5759
let (status_tx, status_rx) = unbounded();

‎roles/jd-server/src/main.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,5 @@ async fn main() {
104104
}
105105
};
106106

107-
let _ = lib::JobDeclaratorServer::new(config)
108-
.expect("Failed to start JDS")
109-
.start()
110-
.await;
107+
let _ = lib::JobDeclaratorServer::new(config).start().await;
111108
}

‎roles/roles-utils/rpc/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ pub struct Amount(f64);
1010

1111
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
1212
pub struct BlockHash(Hash);
13+
14+
pub use hyper::Uri;

‎roles/roles-utils/rpc/src/mini_rpc_client.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ use super::BlockHash;
2121
#[derive(Clone, Debug)]
2222
pub struct MiniRpcClient {
2323
client: Client<HttpConnector, Full<Bytes>>,
24-
url: String,
24+
url: hyper::Uri,
2525
auth: Auth,
2626
}
2727

2828
impl MiniRpcClient {
29-
pub fn new(url: String, auth: Auth) -> MiniRpcClient {
29+
pub fn new(url: hyper::Uri, auth: Auth) -> MiniRpcClient {
3030
let client: Client<_, Full<Bytes>> = Client::builder(TokioExecutor::new()).build_http();
3131
MiniRpcClient { client, url, auth }
3232
}
@@ -120,7 +120,7 @@ impl MiniRpcClient {
120120

121121
let req = Request::builder()
122122
.method("POST")
123-
.uri(self.url.as_str())
123+
.uri(self.url.clone())
124124
.header(CONTENT_TYPE, "application/json")
125125
.header(
126126
AUTHORIZATION,

‎roles/tests-integration/lib/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,15 @@ pub async fn start_jds(tp_rpc_connection: &ConnectParams) -> (JobDeclaratorServe
184184
"036adc3bdf21e6f9a0f0fb0066bf517e5b7909ed1563d6958a10993849a7554075".to_string(),
185185
)];
186186
if let Ok(Some(CookieValues { user, password })) = tp_rpc_connection.get_cookie_values() {
187+
let ip = tp_rpc_connection.rpc_socket.ip().to_string();
188+
let url = jd_server::Uri::builder()
189+
.scheme("http")
190+
.authority(ip)
191+
.path_and_query("")
192+
.build()
193+
.unwrap();
187194
let core_rpc = CoreRpc::new(
188-
format!("http://{}", tp_rpc_connection.rpc_socket.ip()).to_string(),
195+
url.to_string(),
189196
tp_rpc_connection.rpc_socket.port(),
190197
user,
191198
password,
@@ -199,7 +206,7 @@ pub async fn start_jds(tp_rpc_connection: &ConnectParams) -> (JobDeclaratorServe
199206
core_rpc,
200207
std::time::Duration::from_secs(1),
201208
);
202-
let job_declarator_server = JobDeclaratorServer::new(config).unwrap();
209+
let job_declarator_server = JobDeclaratorServer::new(config);
203210
let job_declarator_server_clone = job_declarator_server.clone();
204211
tokio::spawn(async move {
205212
job_declarator_server_clone.start().await.unwrap();

0 commit comments

Comments
 (0)
Please sign in to comment.