Skip to content

Commit cb4584a

Browse files
authored
Merge pull request #3353 from tnull/2024-10-simplify-block-sync
Drop unnecessary `Result` in `RpcClient::new`
2 parents 43e28fe + 88cbb4f commit cb4584a

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

lightning-block-sync/src/rest.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ impl RestClient {
2323
/// Creates a new REST client connected to the given endpoint.
2424
///
2525
/// The endpoint should contain the REST path component (e.g., http://127.0.0.1:8332/rest).
26-
pub fn new(endpoint: HttpEndpoint) -> std::io::Result<Self> {
27-
Ok(Self { endpoint, client: Mutex::new(None) })
26+
pub fn new(endpoint: HttpEndpoint) -> Self {
27+
Self { endpoint, client: Mutex::new(None) }
2828
}
2929

3030
/// Requests a resource encoded in `F` format and interpreted as type `T`.
@@ -120,7 +120,7 @@ mod tests {
120120
#[tokio::test]
121121
async fn request_unknown_resource() {
122122
let server = HttpServer::responding_with_not_found();
123-
let client = RestClient::new(server.endpoint()).unwrap();
123+
let client = RestClient::new(server.endpoint());
124124

125125
match client.request_resource::<BinaryResponse, u32>("/").await {
126126
Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::Other),
@@ -131,7 +131,7 @@ mod tests {
131131
#[tokio::test]
132132
async fn request_malformed_resource() {
133133
let server = HttpServer::responding_with_ok(MessageBody::Content("foo"));
134-
let client = RestClient::new(server.endpoint()).unwrap();
134+
let client = RestClient::new(server.endpoint());
135135

136136
match client.request_resource::<BinaryResponse, u32>("/").await {
137137
Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::InvalidData),
@@ -142,7 +142,7 @@ mod tests {
142142
#[tokio::test]
143143
async fn request_valid_resource() {
144144
let server = HttpServer::responding_with_ok(MessageBody::Content(42));
145-
let client = RestClient::new(server.endpoint()).unwrap();
145+
let client = RestClient::new(server.endpoint());
146146

147147
match client.request_resource::<BinaryResponse, u32>("/").await {
148148
Err(e) => panic!("Unexpected error: {:?}", e),
@@ -157,7 +157,7 @@ mod tests {
157157
// "bitmap" field, so this should suffice for testing
158158
"{\"chainHeight\": 1, \"bitmap\":\"0\",\"utxos\":[]}",
159159
));
160-
let client = RestClient::new(server.endpoint()).unwrap();
160+
let client = RestClient::new(server.endpoint());
161161

162162
let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
163163
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();
@@ -171,7 +171,7 @@ mod tests {
171171
// field, so this should suffice for testing
172172
"{\"chainHeight\": 1, \"bitmap\":\"1\",\"utxos\":[]}",
173173
));
174-
let client = RestClient::new(server.endpoint()).unwrap();
174+
let client = RestClient::new(server.endpoint());
175175

176176
let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
177177
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();

lightning-block-sync/src/rpc.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ impl RpcClient {
5050
/// Creates a new RPC client connected to the given endpoint with the provided credentials. The
5151
/// credentials should be a base64 encoding of a user name and password joined by a colon, as is
5252
/// required for HTTP basic access authentication.
53-
pub fn new(credentials: &str, endpoint: HttpEndpoint) -> std::io::Result<Self> {
54-
Ok(Self {
53+
pub fn new(credentials: &str, endpoint: HttpEndpoint) -> Self {
54+
Self {
5555
basic_auth: "Basic ".to_string() + credentials,
5656
endpoint,
5757
client: Mutex::new(None),
5858
id: AtomicUsize::new(0),
59-
})
59+
}
6060
}
6161

6262
/// Calls a method with the response encoded in JSON format and interpreted as type `T`.
@@ -204,7 +204,7 @@ mod tests {
204204
#[tokio::test]
205205
async fn call_method_returning_unknown_response() {
206206
let server = HttpServer::responding_with_not_found();
207-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
207+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
208208

209209
match client.call_method::<u64>("getblockcount", &[]).await {
210210
Err(e) => assert_eq!(e.kind(), std::io::ErrorKind::Other),
@@ -216,7 +216,7 @@ mod tests {
216216
async fn call_method_returning_malfomred_response() {
217217
let response = serde_json::json!("foo");
218218
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
219-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
219+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
220220

221221
match client.call_method::<u64>("getblockcount", &[]).await {
222222
Err(e) => {
@@ -233,7 +233,7 @@ mod tests {
233233
"error": { "code": -8, "message": "invalid parameter" },
234234
});
235235
let server = HttpServer::responding_with_server_error(response);
236-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
236+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
237237

238238
let invalid_block_hash = serde_json::json!("foo");
239239
match client.call_method::<u64>("getblock", &[invalid_block_hash]).await {
@@ -251,7 +251,7 @@ mod tests {
251251
async fn call_method_returning_missing_result() {
252252
let response = serde_json::json!({});
253253
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
254-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
254+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
255255

256256
match client.call_method::<u64>("getblockcount", &[]).await {
257257
Err(e) => {
@@ -266,7 +266,7 @@ mod tests {
266266
async fn call_method_returning_malformed_result() {
267267
let response = serde_json::json!({ "result": "foo" });
268268
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
269-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
269+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
270270

271271
match client.call_method::<u64>("getblockcount", &[]).await {
272272
Err(e) => {
@@ -281,7 +281,7 @@ mod tests {
281281
async fn call_method_returning_valid_result() {
282282
let response = serde_json::json!({ "result": 654470 });
283283
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
284-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
284+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
285285

286286
match client.call_method::<u64>("getblockcount", &[]).await {
287287
Err(e) => panic!("Unexpected error: {:?}", e),
@@ -293,7 +293,7 @@ mod tests {
293293
async fn fails_to_fetch_spent_utxo() {
294294
let response = serde_json::json!({ "result": null });
295295
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
296-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
296+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
297297
let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
298298
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();
299299
assert_eq!(unspent_output, false);
@@ -303,7 +303,7 @@ mod tests {
303303
async fn fetches_utxo() {
304304
let response = serde_json::json!({ "result": {"bestblock": 1, "confirmations": 42}});
305305
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
306-
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
306+
let client = RpcClient::new(CREDENTIALS, server.endpoint());
307307
let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
308308
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();
309309
assert_eq!(unspent_output, true);

0 commit comments

Comments
 (0)