Skip to content

Commit e8a1066

Browse files
committed
rustfmt: Run on lightning-block-sync/src/rpc.rs
1 parent a23f72c commit e8a1066

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

lightning-block-sync/src/rpc.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Simple RPC client implementation which implements [`BlockSource`] against a Bitcoin Core RPC
22
//! endpoint.
33
4-
use crate::{BlockData, BlockHeaderData, BlockSource, AsyncBlockSourceResult};
5-
use crate::http::{HttpClient, HttpEndpoint, HttpError, JsonResponse};
64
use crate::gossip::UtxoSource;
5+
use crate::http::{HttpClient, HttpEndpoint, HttpError, JsonResponse};
6+
use crate::{AsyncBlockSourceResult, BlockData, BlockHeaderData, BlockSource};
77

88
use bitcoin::hash_types::BlockHash;
99
use bitcoin::OutPoint;
@@ -28,9 +28,9 @@ pub struct RpcError {
2828
}
2929

3030
impl fmt::Display for RpcError {
31-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32-
write!(f, "RPC error {}: {}", self.code, self.message)
33-
}
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
write!(f, "RPC error {}: {}", self.code, self.message)
33+
}
3434
}
3535

3636
impl Error for RpcError {}
@@ -63,8 +63,12 @@ impl RpcClient {
6363
///
6464
/// When an `Err` is returned, [`std::io::Error::into_inner`] may contain an [`RpcError`] if
6565
/// [`std::io::Error::kind`] is [`std::io::ErrorKind::Other`].
66-
pub async fn call_method<T>(&self, method: &str, params: &[serde_json::Value]) -> std::io::Result<T>
67-
where JsonResponse: TryFrom<Vec<u8>, Error = std::io::Error> + TryInto<T, Error = std::io::Error> {
66+
pub async fn call_method<T>(
67+
&self, method: &str, params: &[serde_json::Value],
68+
) -> std::io::Result<T>
69+
where
70+
JsonResponse: TryFrom<Vec<u8>, Error = std::io::Error> + TryInto<T, Error = std::io::Error>,
71+
{
6872
let host = format!("{}:{}", self.endpoint.host(), self.endpoint.port());
6973
let uri = self.endpoint.path();
7074
let content = serde_json::json!({
@@ -73,9 +77,13 @@ impl RpcClient {
7377
"id": &self.id.fetch_add(1, Ordering::AcqRel).to_string()
7478
});
7579

76-
let mut client = if let Some(client) = self.client.lock().unwrap().take() { client }
77-
else { HttpClient::connect(&self.endpoint)? };
78-
let http_response = client.post::<JsonResponse>(&uri, &host, &self.basic_auth, content).await;
80+
let mut client = if let Some(client) = self.client.lock().unwrap().take() {
81+
client
82+
} else {
83+
HttpClient::connect(&self.endpoint)?
84+
};
85+
let http_response =
86+
client.post::<JsonResponse>(&uri, &host, &self.basic_auth, content).await;
7987
*self.client.lock().unwrap() = Some(client);
8088

8189
let mut response = match http_response {
@@ -93,38 +101,49 @@ impl RpcClient {
93101
};
94102

95103
if !response.is_object() {
96-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON object"));
104+
return Err(std::io::Error::new(
105+
std::io::ErrorKind::InvalidData,
106+
"expected JSON object",
107+
));
97108
}
98109

99110
let error = &response["error"];
100111
if !error.is_null() {
101112
// TODO: Examine error code for a more precise std::io::ErrorKind.
102-
let rpc_error = RpcError {
103-
code: error["code"].as_i64().unwrap_or(-1),
104-
message: error["message"].as_str().unwrap_or("unknown error").to_string()
113+
let rpc_error = RpcError {
114+
code: error["code"].as_i64().unwrap_or(-1),
115+
message: error["message"].as_str().unwrap_or("unknown error").to_string(),
105116
};
106117
return Err(std::io::Error::new(std::io::ErrorKind::Other, rpc_error));
107118
}
108119

109120
let result = match response.get_mut("result") {
110121
Some(result) => result.take(),
111-
None =>
112-
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON result")),
122+
None => {
123+
return Err(std::io::Error::new(
124+
std::io::ErrorKind::InvalidData,
125+
"expected JSON result",
126+
))
127+
},
113128
};
114129

115130
JsonResponse(result).try_into()
116131
}
117132
}
118133

119134
impl BlockSource for RpcClient {
120-
fn get_header<'a>(&'a self, header_hash: &'a BlockHash, _height: Option<u32>) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
135+
fn get_header<'a>(
136+
&'a self, header_hash: &'a BlockHash, _height: Option<u32>,
137+
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
121138
Box::pin(async move {
122139
let header_hash = serde_json::json!(header_hash.to_string());
123140
Ok(self.call_method("getblockheader", &[header_hash]).await?)
124141
})
125142
}
126143

127-
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, BlockData> {
144+
fn get_block<'a>(
145+
&'a self, header_hash: &'a BlockHash,
146+
) -> AsyncBlockSourceResult<'a, BlockData> {
128147
Box::pin(async move {
129148
let header_hash = serde_json::json!(header_hash.to_string());
130149
let verbosity = serde_json::json!(0);
@@ -133,14 +152,14 @@ impl BlockSource for RpcClient {
133152
}
134153

135154
fn get_best_block<'a>(&'a self) -> AsyncBlockSourceResult<'a, (BlockHash, Option<u32>)> {
136-
Box::pin(async move {
137-
Ok(self.call_method("getblockchaininfo", &[]).await?)
138-
})
155+
Box::pin(async move { Ok(self.call_method("getblockchaininfo", &[]).await?) })
139156
}
140157
}
141158

142159
impl UtxoSource for RpcClient {
143-
fn get_block_hash_by_height<'a>(&'a self, block_height: u32) -> AsyncBlockSourceResult<'a, BlockHash> {
160+
fn get_block_hash_by_height<'a>(
161+
&'a self, block_height: u32,
162+
) -> AsyncBlockSourceResult<'a, BlockHash> {
144163
Box::pin(async move {
145164
let height_param = serde_json::json!(block_height);
146165
Ok(self.call_method("getblockhash", &[height_param]).await?)
@@ -152,8 +171,8 @@ impl UtxoSource for RpcClient {
152171
let txid_param = serde_json::json!(outpoint.txid.to_string());
153172
let vout_param = serde_json::json!(outpoint.vout);
154173
let include_mempool = serde_json::json!(false);
155-
let utxo_opt: serde_json::Value = self.call_method(
156-
"gettxout", &[txid_param, vout_param, include_mempool]).await?;
174+
let utxo_opt: serde_json::Value =
175+
self.call_method("gettxout", &[txid_param, vout_param, include_mempool]).await?;
157176
Ok(!utxo_opt.is_null())
158177
})
159178
}
@@ -229,7 +248,7 @@ mod tests {
229248

230249
#[tokio::test]
231250
async fn call_method_returning_missing_result() {
232-
let response = serde_json::json!({ });
251+
let response = serde_json::json!({});
233252
let server = HttpServer::responding_with_ok(MessageBody::Content(response));
234253
let client = RpcClient::new(CREDENTIALS, server.endpoint()).unwrap();
235254

rustfmt_excluded_files

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
./lightning-background-processor/src/lib.rs
22
./lightning-block-sync/src/lib.rs
3-
./lightning-block-sync/src/rpc.rs
43
./lightning-block-sync/src/test_utils.rs
54
./lightning-block-sync/src/utils.rs
65
./lightning-custom-message/src/lib.rs

0 commit comments

Comments
 (0)