Skip to content

Commit 760d9cf

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 760d9cf

File tree

4 files changed

+77
-59
lines changed

4 files changed

+77
-59
lines changed

crates/btlightning/src/client.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,10 @@ mod tests {
17551755

17561756
#[test]
17571757
fn with_config_rejects_frame_payload_below_minimum() {
1758-
let mut cfg = LightningClientConfig::default();
1759-
cfg.max_frame_payload_bytes = 512;
1758+
let cfg = LightningClientConfig {
1759+
max_frame_payload_bytes: 512,
1760+
..LightningClientConfig::default()
1761+
};
17601762
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17611763
}
17621764

@@ -1767,23 +1769,30 @@ mod tests {
17671769
Ok(v) => v,
17681770
Err(_) => return,
17691771
};
1770-
let mut cfg = LightningClientConfig::default();
1771-
cfg.max_frame_payload_bytes = val;
1772-
cfg.max_stream_payload_bytes = val;
1772+
let cfg = LightningClientConfig {
1773+
max_frame_payload_bytes: val,
1774+
max_stream_payload_bytes: val,
1775+
..LightningClientConfig::default()
1776+
};
17731777
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17741778
}
17751779

17761780
#[test]
17771781
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;
1782+
let base = LightningClientConfig::default();
1783+
let cfg = LightningClientConfig {
1784+
max_stream_payload_bytes: base.max_frame_payload_bytes - 1,
1785+
..base
1786+
};
17801787
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17811788
}
17821789

17831790
#[test]
17841791
fn with_config_rejects_zero_stream_chunk_timeout() {
1785-
let mut cfg = LightningClientConfig::default();
1786-
cfg.stream_chunk_timeout = Some(Duration::ZERO);
1792+
let cfg = LightningClientConfig {
1793+
stream_chunk_timeout: Some(Duration::ZERO),
1794+
..LightningClientConfig::default()
1795+
};
17871796
assert!(LightningClient::with_config("hk".into(), cfg).is_err());
17881797
}
17891798

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)