Skip to content

Commit 9f09f50

Browse files
committed
Merge #139: chore: set rust edition to 2021, fix clippy, add ci fmt and clippy checks
0dd5140 chore(ci): add fmt and clippy checks (Steve Myers) e7af332 chore: fix clippy errors and set msrv to 1.63 (Steve Myers) 84f4f60 chore: set rust edition to 2021 (Steve Myers) Pull request description: A little house keeping to avoid the warning about missing the edition. After setting the rust edition to 2021 I had to fix clippy warnings. I also added CI fmt and clippy checks. ACKs for top commit: ValuedMammal: ACK 0dd5140 oleonardolima: ACK 0dd5140 Tree-SHA512: ee37c3ef06e217f76dcb622cdbb53e9b5c94163b789ca584037d5a8bb4bdef13c6cf251b555ac0e0ede309d7436295f1ede4e2ac6c0dc141c9bf2e3d6ecf9238
2 parents 746a0e6 + 0dd5140 commit 9f09f50

File tree

11 files changed

+76
-73
lines changed

11 files changed

+76
-73
lines changed

.github/workflows/cont_integration.yml

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@ on: [push, pull_request]
33
name: CI
44

55
jobs:
6-
test-fmt:
6+
test:
77
name: Test
88
runs-on: ubuntu-20.04
99
env:
1010
TEST_ELECTRUM_SERVER: electrum.blockstream.info:50001
11-
#TEST_ELECTRUM_SERVER: bitcoin.aranguren.org:50001
1211
strategy:
1312
matrix:
1413
rust:
1514
- stable # STABLE
1615
- 1.63.0 # MSRV
1716
steps:
1817
- name: Checkout
19-
uses: actions/checkout@v2
18+
uses: actions/checkout@v4
2019
- name: Cache
2120
uses: actions/cache@v2
2221
with:
@@ -25,14 +24,10 @@ jobs:
2524
~/.cargo/git
2625
target
2726
key: ${{ runner.os }}-cargo-${{ github.job }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
28-
- name: Install rustup
29-
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
30-
- name: Set default toolchain
31-
run: $HOME/.cargo/bin/rustup default ${{ matrix.rust }}
32-
- name: Set profile
33-
run: $HOME/.cargo/bin/rustup set profile minimal
34-
- name: Fmt
35-
run: cargo fmt -- --check --verbose
27+
- name: Install rust
28+
uses: dtolnay/rust-toolchain@stable
29+
with:
30+
toolchain: ${{ matrix.rust }}
3631
- name: Test
3732
run: cargo test --verbose --all-features
3833
- name: Setup iptables for the timeout test
@@ -46,3 +41,32 @@ jobs:
4641
- run: cargo check --verbose --no-default-features --features=proxy,use-openssl
4742
- run: cargo check --verbose --no-default-features --features=proxy,use-rustls
4843
- run: cargo check --verbose --no-default-features --features=proxy,use-rustls-ring
44+
45+
fmt:
46+
name: Rust fmt
47+
runs-on: ubuntu-latest
48+
steps:
49+
- name: Checkout
50+
uses: actions/checkout@v4
51+
- name: Install Rust toolchain
52+
uses: dtolnay/rust-toolchain@stable
53+
with:
54+
toolchain: stable
55+
components: rustfmt
56+
- name: Check fmt
57+
run: cargo fmt --all -- --config format_code_in_doc_comments=true --check
58+
59+
clippy_check:
60+
runs-on: ubuntu-latest
61+
steps:
62+
- uses: actions/checkout@v4
63+
- uses: dtolnay/rust-toolchain@stable
64+
with:
65+
toolchain: 1.78.0
66+
components: clippy
67+
- name: Rust Cache
68+
uses: Swatinem/rust-cache@v2.2.1
69+
- uses: actions-rs/clippy-check@v1
70+
with:
71+
token: ${{ secrets.GITHUB_TOKEN }}
72+
args: --all-features --all-targets -- -D warnings

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ description = "Bitcoin Electrum client library. Supports plaintext, TLS and Onio
1010
keywords = ["bitcoin", "electrum"]
1111
readme = "README.md"
1212
rust-version = "1.63.0"
13+
edition = "2021"
1314

1415
# loosely based on https://github.com/evgeniy-scherbina/rust-electrumx-client
1516

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msrv="1.63.0"

src/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::convert::TryInto;
66
use bitcoin::consensus::encode::{deserialize, serialize};
77
use bitcoin::{block, Script, Transaction, Txid};
88

9-
use batch::Batch;
10-
use types::*;
9+
use crate::batch::Batch;
10+
use crate::types::*;
1111

1212
/// API calls exposed by an Electrum client
1313
pub trait ElectrumApi {

src/batch.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use bitcoin::{Script, Txid};
66

7-
use types::{Call, Param, ToElectrumScriptHash};
7+
use crate::types::{Call, Param, ToElectrumScriptHash};
88

99
/// Helper structure that caches all the requests before they are actually sent to the server.
1010
///
@@ -16,6 +16,7 @@ use types::{Call, Param, ToElectrumScriptHash};
1616
/// [`Client`](../client/struct.Client.html), like
1717
/// [`batch_script_get_balance`](../client/struct.Client.html#method.batch_script_get_balance) to ask the
1818
/// server for the balance of multiple scripts with a single request.
19+
#[derive(Default)]
1920
pub struct Batch {
2021
calls: Vec<Call>,
2122
}
@@ -107,9 +108,3 @@ impl<'a> std::iter::Iterator for BatchIter<'a> {
107108
val
108109
}
109110
}
110-
111-
impl std::default::Default for Batch {
112-
fn default() -> Self {
113-
Batch { calls: Vec::new() }
114-
}
115-
}

src/client.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use log::{info, warn};
66

77
use bitcoin::{Script, Txid};
88

9-
use api::ElectrumApi;
10-
use batch::Batch;
11-
use config::Config;
12-
use raw_client::*;
9+
use crate::api::ElectrumApi;
10+
use crate::batch::Batch;
11+
use crate::config::Config;
12+
use crate::raw_client::*;
13+
use crate::types::*;
1314
use std::convert::TryFrom;
14-
use types::*;
1515

1616
/// Generalized Electrum client that supports multiple backends. This wraps
1717
/// [`RawClient`](client/struct.RawClient.html) and provides a more user-friendly
@@ -148,7 +148,6 @@ impl Client {
148148
/// If no prefix is specified, then `tcp://` is assumed.
149149
///
150150
/// See [Client::from_config] for more configuration options
151-
///
152151
pub fn new(url: &str) -> Result<Self, Error> {
153152
Self::from_config(url, Config::default())
154153
}
@@ -353,7 +352,7 @@ mod tests {
353352
fn more_failed_attempts_than_retries_means_exhausted() {
354353
let exhausted = retries_exhausted(10, 5);
355354

356-
assert_eq!(exhausted, true)
355+
assert!(exhausted)
357356
}
358357

359358
#[test]
@@ -362,21 +361,21 @@ mod tests {
362361

363362
let exhausted = retries_exhausted(failed_attempts, u8::MAX);
364363

365-
assert_eq!(exhausted, true)
364+
assert!(exhausted)
366365
}
367366

368367
#[test]
369368
fn less_failed_attempts_means_not_exhausted() {
370369
let exhausted = retries_exhausted(2, 5);
371370

372-
assert_eq!(exhausted, false)
371+
assert!(!exhausted)
373372
}
374373

375374
#[test]
376375
fn attempts_equals_retries_means_not_exhausted_yet() {
377376
let exhausted = retries_exhausted(2, 2);
378377

379-
assert_eq!(exhausted, false)
378+
assert!(!exhausted)
380379
}
381380

382381
#[test]
@@ -408,7 +407,7 @@ mod tests {
408407
sender.send(()).unwrap();
409408

410409
for _stream in listener.incoming() {
411-
loop {}
410+
std::thread::sleep(Duration::from_secs(60))
412411
}
413412
});
414413

src/raw_client.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ use rustls::{
3939
#[cfg(any(feature = "default", feature = "proxy"))]
4040
use crate::socks::{Socks5Stream, TargetAddr, ToTargetAddr};
4141

42-
use stream::ClonableStream;
42+
use crate::stream::ClonableStream;
4343

44-
use api::ElectrumApi;
45-
use batch::Batch;
46-
use types::*;
44+
use crate::api::ElectrumApi;
45+
use crate::batch::Batch;
46+
use crate::types::*;
4747

4848
macro_rules! impl_batch_call {
4949
( $self:expr, $data:expr, $call:ident ) => {{
@@ -83,7 +83,7 @@ pub trait ToSocketAddrsDomain: ToSocketAddrs {
8383

8484
impl ToSocketAddrsDomain for &str {
8585
fn domain(&self) -> Option<&str> {
86-
self.splitn(2, ':').next()
86+
self.split(':').next()
8787
}
8888
}
8989

@@ -298,7 +298,7 @@ impl RawClient<ElectrumSslStream> {
298298
not(feature = "use-openssl")
299299
))]
300300
mod danger {
301-
use raw_client::ServerName;
301+
use crate::raw_client::ServerName;
302302
use rustls::client::danger::ServerCertVerified;
303303
use rustls::pki_types::CertificateDer;
304304
use rustls::pki_types::UnixTime;
@@ -406,11 +406,11 @@ impl RawClient<ElectrumSslStream> {
406406
socket_addr.domain().ok_or(Error::MissingDomain)?;
407407

408408
let store = webpki_roots::TLS_SERVER_ROOTS
409-
.into_iter()
409+
.iter()
410410
.map(|t| TrustAnchor {
411411
subject: Der::from_slice(t.subject),
412412
subject_public_key_info: Der::from_slice(t.spki),
413-
name_constraints: t.name_constraints.map(|nc| Der::from_slice(nc)),
413+
name_constraints: t.name_constraints.map(Der::from_slice),
414414
})
415415
.collect::<RootCertStore>();
416416

@@ -605,7 +605,7 @@ impl<S: Read + Write> RawClient<S> {
605605
// No id, that's probably a notification.
606606
let mut resp = resp;
607607

608-
if let Some(ref method) = resp["method"].take().as_str() {
608+
if let Some(method) = resp["method"].take().as_str() {
609609
self.handle_notification(method, resp["params"].take())?;
610610
} else {
611611
warn!("Unexpected response: {:?}", resp);
@@ -722,7 +722,7 @@ impl<S: Read + Write> RawClient<S> {
722722
) -> Result<serde_json::Value, Error> {
723723
let req = Request::new_id(
724724
self.last_id.fetch_add(1, Ordering::SeqCst),
725-
&method_name,
725+
method_name,
726726
params,
727727
);
728728
let result = self.call(req)?;
@@ -763,7 +763,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
763763
for (method, params) in batch.iter() {
764764
let req = Request::new_id(
765765
self.last_id.fetch_add(1, Ordering::SeqCst),
766-
&method,
766+
method,
767767
params.to_vec(),
768768
);
769769
missing_responses.insert(req.id);
@@ -804,7 +804,7 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
804804
};
805805
}
806806

807-
Ok(answers.into_iter().map(|(_, r)| r).collect())
807+
Ok(answers.into_values().collect())
808808
}
809809

810810
fn block_headers_subscribe_raw(&self) -> Result<RawHeaderNotification, Error> {
@@ -1128,7 +1128,7 @@ mod test {
11281128
use crate::utils;
11291129

11301130
use super::RawClient;
1131-
use api::ElectrumApi;
1131+
use crate::api::ElectrumApi;
11321132

11331133
fn get_test_server() -> String {
11341134
std::env::var("TEST_ELECTRUM_SERVER").unwrap_or("electrum.blockstream.info:50001".into())
@@ -1426,7 +1426,7 @@ mod test {
14261426

14271427
#[test]
14281428
fn test_raw_call() {
1429-
use types::Param;
1429+
use crate::types::Param;
14301430

14311431
let client = RawClient::new(get_test_server(), None).unwrap();
14321432

src/socks/v4.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,15 @@ impl Socks4Stream {
109109
let _ = packet.write_u32::<BigEndian>(Ipv4Addr::new(0, 0, 0, 1).into());
110110
let _ = packet.write_all(userid.as_bytes());
111111
let _ = packet.write_u8(0);
112-
let _ = packet.extend(host.as_bytes());
112+
packet.extend(host.as_bytes());
113113
let _ = packet.write_u8(0);
114114
}
115115
}
116116

117117
socket.write_all(&packet)?;
118118
let proxy_addr = read_response(&mut socket)?;
119119

120-
Ok(Socks4Stream {
121-
socket: socket,
122-
proxy_addr: proxy_addr,
123-
})
120+
Ok(Socks4Stream { socket, proxy_addr })
124121
}
125122

126123
/// Returns the proxy-side address of the connection between the proxy and

src/socks/v5.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn write_addr(mut packet: &mut [u8], target: &TargetAddr) -> io::Result<usize> {
110110
}
111111
TargetAddr::Domain(ref domain, port) => {
112112
packet.write_u8(3).unwrap();
113-
if domain.len() > u8::max_value() as usize {
113+
if domain.len() > u8::MAX as usize {
114114
return Err(io::Error::new(
115115
io::ErrorKind::InvalidInput,
116116
"domain name too long",
@@ -144,11 +144,7 @@ impl<'a> Authentication<'a> {
144144
}
145145

146146
fn is_no_auth(&self) -> bool {
147-
if let Authentication::None = *self {
148-
true
149-
} else {
150-
false
151-
}
147+
matches!(*self, Authentication::None)
152148
}
153149
}
154150

@@ -257,24 +253,21 @@ impl Socks5Stream {
257253

258254
let proxy_addr = read_response(&mut socket)?;
259255

260-
Ok(Socks5Stream {
261-
socket: socket,
262-
proxy_addr: proxy_addr,
263-
})
256+
Ok(Socks5Stream { socket, proxy_addr })
264257
}
265258

266259
fn password_authentication(
267260
socket: &mut TcpStream,
268261
username: &str,
269262
password: &str,
270263
) -> io::Result<()> {
271-
if username.len() < 1 || username.len() > 255 {
264+
if username.is_empty() || username.len() > 255 {
272265
return Err(io::Error::new(
273266
io::ErrorKind::InvalidInput,
274267
"invalid username",
275268
));
276269
};
277-
if password.len() < 1 || password.len() > 255 {
270+
if password.is_empty() || password.len() > 255 {
278271
return Err(io::Error::new(
279272
io::ErrorKind::InvalidInput,
280273
"invalid password",
@@ -474,10 +467,7 @@ impl Socks5Datagram {
474467
let socket = UdpSocket::bind(addr)?;
475468
socket.connect(&stream.proxy_addr)?;
476469

477-
Ok(Socks5Datagram {
478-
socket: socket,
479-
stream: stream,
480-
})
470+
Ok(Socks5Datagram { socket, stream })
481471
}
482472

483473
/// Like `UdpSocket::send_to`.
@@ -526,11 +516,7 @@ impl Socks5Datagram {
526516
let addr = read_addr(&mut header)?;
527517

528518
unsafe {
529-
ptr::copy(
530-
buf.as_ptr(),
531-
buf.as_mut_ptr().offset(header.len() as isize),
532-
overflow,
533-
);
519+
ptr::copy(buf.as_ptr(), buf.as_mut_ptr().add(header.len()), overflow);
534520
}
535521
buf[..header.len()].copy_from_slice(header);
536522

src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl From<[u8; 32]> for Hex32Bytes {
8787
}
8888

8989
impl Hex32Bytes {
90-
pub(crate) fn to_hex(&self) -> String {
90+
pub(crate) fn to_hex(self) -> String {
9191
self.0.to_lower_hex_string()
9292
}
9393
}

0 commit comments

Comments
 (0)