Skip to content

Commit 09fabda

Browse files
committed
Resolve all clippy warnings and replace fixed sleep with bounded poll
Eliminate field_reassign_with_default in client config tests using struct update syntax. Replace unnecessary_get_then_check in registry with contains_key. Reorder signing.rs to place feature-gated items before test module. Replace unconditional 5s sleep in integration test with bounded poll loop that retries at 250ms intervals with a 10s deadline.
1 parent 4994c7d commit 09fabda

File tree

4 files changed

+79
-62
lines changed

4 files changed

+79
-62
lines changed

crates/btlightning/src/client.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,8 @@ impl LightningClient {
872872
for addr_key in state.registry.connection_addrs() {
873873
let status = match state.registry.get_connection(addr_key) {
874874
Some(conn) => {
875-
let close_reason = conn.close_reason();
876-
if close_reason.is_some() {
877-
format!("closed({:?})", close_reason.unwrap())
875+
if let Some(reason) = conn.close_reason() {
876+
format!("closed({:?})", reason)
878877
} else {
879878
"active".to_string()
880879
}
@@ -1755,8 +1754,10 @@ mod tests {
17551754

17561755
#[test]
17571756
fn with_config_rejects_frame_payload_below_minimum() {
1758-
let mut cfg = LightningClientConfig::default();
1759-
cfg.max_frame_payload_bytes = 512;
1757+
let cfg = LightningClientConfig {
1758+
max_frame_payload_bytes: 512,
1759+
..LightningClientConfig::default()
1760+
};
17601761
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17611762
}
17621763

@@ -1767,23 +1768,30 @@ mod tests {
17671768
Ok(v) => v,
17681769
Err(_) => return,
17691770
};
1770-
let mut cfg = LightningClientConfig::default();
1771-
cfg.max_frame_payload_bytes = val;
1772-
cfg.max_stream_payload_bytes = val;
1771+
let cfg = LightningClientConfig {
1772+
max_frame_payload_bytes: val,
1773+
max_stream_payload_bytes: val,
1774+
..LightningClientConfig::default()
1775+
};
17731776
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17741777
}
17751778

17761779
#[test]
17771780
fn with_config_rejects_stream_below_frame() {
1778-
let mut cfg = LightningClientConfig::default();
1779-
cfg.max_stream_payload_bytes = cfg.max_frame_payload_bytes - 1;
1781+
let base = LightningClientConfig::default();
1782+
let cfg = LightningClientConfig {
1783+
max_stream_payload_bytes: base.max_frame_payload_bytes - 1,
1784+
..base
1785+
};
17801786
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17811787
}
17821788

17831789
#[test]
17841790
fn with_config_rejects_zero_stream_chunk_timeout() {
1785-
let mut cfg = LightningClientConfig::default();
1786-
cfg.stream_chunk_timeout = Some(Duration::ZERO);
1791+
let cfg = LightningClientConfig {
1792+
stream_chunk_timeout: Some(Duration::ZERO),
1793+
..LightningClientConfig::default()
1794+
};
17871795
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17881796
}
17891797

crates/btlightning/src/registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ mod tests {
329329
reg.register(QuicAxonInfo::new("hk1".into(), "1.2.3.4".into(), 8080, 4));
330330
let rs = reg.reconnect_state_or_insert(old_addr.clone());
331331
rs.attempts = 3;
332-
assert!(reg.reconnect_states.get(&old_addr).is_some());
332+
assert!(reg.reconnect_states.contains_key(&old_addr));
333333

334334
reg.register(QuicAxonInfo::new("hk1".into(), "5.6.7.8".into(), 9090, 4));
335-
assert!(reg.reconnect_states.get(&old_addr).is_none());
335+
assert!(!reg.reconnect_states.contains_key(&old_addr));
336336
reg.assert_invariants();
337337
}
338338

crates/btlightning/src/signing.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,47 @@ impl<F: Fn(&[u8]) -> Result<Vec<u8>> + Send + Sync> Signer for CallbackSigner<F>
8686
}
8787
}
8888

89+
/// [`Signer`] backed by a `btwallet` keypair loaded from the Bittensor wallet directory.
90+
///
91+
/// Requires the `btwallet` feature.
92+
#[cfg(feature = "btwallet")]
93+
pub struct BtWalletSigner {
94+
keypair: bittensor_wallet::Keypair,
95+
}
96+
97+
#[cfg(feature = "btwallet")]
98+
impl BtWalletSigner {
99+
/// Wraps an existing `btwallet::Keypair`.
100+
pub fn new(keypair: bittensor_wallet::Keypair) -> Self {
101+
Self { keypair }
102+
}
103+
104+
/// Loads the hotkey keypair from `~/<path>/<name>/hotkeys/<hotkey_name>`.
105+
pub fn from_wallet(name: &str, path: &str, hotkey_name: &str) -> Result<Self> {
106+
let wallet = bittensor_wallet::Wallet::new(
107+
Some(name.to_string()),
108+
Some(path.to_string()),
109+
Some(hotkey_name.to_string()),
110+
None,
111+
);
112+
let keypair = wallet
113+
.get_hotkey(Some(hotkey_name.to_string()))
114+
.map_err(|e| {
115+
LightningError::Config(format!("failed to load hotkey from wallet: {}", e))
116+
})?;
117+
Ok(Self { keypair })
118+
}
119+
}
120+
121+
#[cfg(feature = "btwallet")]
122+
impl Signer for BtWalletSigner {
123+
fn sign(&self, message: &[u8]) -> Result<Vec<u8>> {
124+
self.keypair
125+
.sign(message.to_vec())
126+
.map_err(LightningError::Signing)
127+
}
128+
}
129+
89130
#[cfg(test)]
90131
mod tests {
91132
use super::*;
@@ -157,44 +198,3 @@ mod tests {
157198
assert!(err.to_string().contains("network down"));
158199
}
159200
}
160-
161-
/// [`Signer`] backed by a `btwallet` keypair loaded from the Bittensor wallet directory.
162-
///
163-
/// Requires the `btwallet` feature.
164-
#[cfg(feature = "btwallet")]
165-
pub struct BtWalletSigner {
166-
keypair: bittensor_wallet::Keypair,
167-
}
168-
169-
#[cfg(feature = "btwallet")]
170-
impl BtWalletSigner {
171-
/// Wraps an existing `btwallet::Keypair`.
172-
pub fn new(keypair: bittensor_wallet::Keypair) -> Self {
173-
Self { keypair }
174-
}
175-
176-
/// Loads the hotkey keypair from `~/<path>/<name>/hotkeys/<hotkey_name>`.
177-
pub fn from_wallet(name: &str, path: &str, hotkey_name: &str) -> Result<Self> {
178-
let wallet = bittensor_wallet::Wallet::new(
179-
Some(name.to_string()),
180-
Some(path.to_string()),
181-
Some(hotkey_name.to_string()),
182-
None,
183-
);
184-
let keypair = wallet
185-
.get_hotkey(Some(hotkey_name.to_string()))
186-
.map_err(|e| {
187-
LightningError::Config(format!("failed to load hotkey from wallet: {}", e))
188-
})?;
189-
Ok(Self { keypair })
190-
}
191-
}
192-
193-
#[cfg(feature = "btwallet")]
194-
impl Signer for BtWalletSigner {
195-
fn sign(&self, message: &[u8]) -> Result<Vec<u8>> {
196-
self.keypair
197-
.sign(message.to_vec())
198-
.map_err(LightningError::Signing)
199-
}
200-
}

crates/btlightning/tests/integration.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,13 +2010,22 @@ async fn query_works_via_update_miner_registry_after_delay() {
20102010
.await
20112011
.unwrap();
20122012

2013-
tokio::time::sleep(Duration::from_secs(5)).await;
2014-
2015-
let resp = client
2016-
.query_axon_with_timeout(axon.clone(), build_request("echo"), Duration::from_secs(5))
2017-
.await
2018-
.unwrap();
2019-
assert!(resp.success);
2013+
let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
2014+
loop {
2015+
match client
2016+
.query_axon_with_timeout(axon.clone(), build_request("echo"), Duration::from_secs(2))
2017+
.await
2018+
{
2019+
Ok(resp) => {
2020+
assert!(resp.success);
2021+
break;
2022+
}
2023+
Err(_) if tokio::time::Instant::now() < deadline => {
2024+
tokio::time::sleep(Duration::from_millis(250)).await;
2025+
}
2026+
Err(e) => panic!("query did not succeed within deadline: {e}"),
2027+
}
2028+
}
20202029

20212030
client.close_all_connections().await.unwrap();
20222031
env.shutdown().await;

0 commit comments

Comments
 (0)