Skip to content

Drop unnecessary Result in RpcClient::new #3353

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

Merged
merged 2 commits into from
Oct 11, 2024
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
14 changes: 7 additions & 7 deletions lightning-block-sync/src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl RestClient {
/// Creates a new REST client connected to the given endpoint.
///
/// The endpoint should contain the REST path component (e.g., http://127.0.0.1:8332/rest).
pub fn new(endpoint: HttpEndpoint) -> std::io::Result<Self> {
Ok(Self { endpoint, client: Mutex::new(None) })
pub fn new(endpoint: HttpEndpoint) -> Self {
Self { endpoint, client: Mutex::new(None) }
}

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

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

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

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

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

let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();
Expand Down
22 changes: 11 additions & 11 deletions lightning-block-sync/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl RpcClient {
/// Creates a new RPC client connected to the given endpoint with the provided credentials. The
/// credentials should be a base64 encoding of a user name and password joined by a colon, as is
/// required for HTTP basic access authentication.
pub fn new(credentials: &str, endpoint: HttpEndpoint) -> std::io::Result<Self> {
Ok(Self {
pub fn new(credentials: &str, endpoint: HttpEndpoint) -> Self {
Self {
basic_auth: "Basic ".to_string() + credentials,
endpoint,
client: Mutex::new(None),
id: AtomicUsize::new(0),
})
}
}

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

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

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

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

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

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

match client.call_method::<u64>("getblockcount", &[]).await {
Err(e) => panic!("Unexpected error: {:?}", e),
Expand All @@ -293,7 +293,7 @@ mod tests {
async fn fails_to_fetch_spent_utxo() {
let response = serde_json::json!({ "result": null });
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
let client = RpcClient::new(CREDENTIALS, server.endpoint());
let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();
assert_eq!(unspent_output, false);
Expand All @@ -303,7 +303,7 @@ mod tests {
async fn fetches_utxo() {
let response = serde_json::json!({ "result": {"bestblock": 1, "confirmations": 42}});
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
let client = RpcClient::new(CREDENTIALS, server.endpoint());
let outpoint = OutPoint::new(bitcoin::Txid::from_byte_array([0; 32]), 0);
let unspent_output = client.is_output_unspent(outpoint).await.unwrap();
assert_eq!(unspent_output, true);
Expand Down
Loading