From 8517f122c8c099143b3f3086a5464c5688011902 Mon Sep 17 00:00:00 2001 From: N Date: Mon, 17 Jun 2024 20:18:43 -0400 Subject: [PATCH] chore: final cleanup --- core/firewall/src/rate_limiting.rs | 14 ++++---------- core/firewall/src/service.rs | 15 +++++++-------- core/rpc/src/lib.rs | 5 +++-- core/rpc/src/server.rs | 2 +- core/types/src/firewall.rs | 2 +- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/core/firewall/src/rate_limiting.rs b/core/firewall/src/rate_limiting.rs index 4b2ffd9ad..ce77e0bfa 100644 --- a/core/firewall/src/rate_limiting.rs +++ b/core/firewall/src/rate_limiting.rs @@ -133,7 +133,7 @@ impl RateLimiting { .filter_map(|(k, v)| { // take the user rules if theyre are not global if v.first().map_or(false, |policy| !policy.is_global) { - return Some((k, v.into_iter().map(IsGlobal::take).collect())); + return Some((k, v.into_iter().map(IsGlobal::inner).collect())); } None @@ -212,7 +212,7 @@ impl RateLimiting { // there is no policy for this ip, use the global policy let mut policies: Vec> = global .iter() - .cloned() + .copied() .map(|p| IsGlobal::yes(p.into())) .collect(); @@ -230,13 +230,7 @@ impl RateLimiting { impl Default for RateLimiting { fn default() -> Self { - Self::WithGlobal { - global: vec![RateLimitingRule { - period: Period::Second, - max_requests: 10, - }], - per: HashMap::new(), - } + Self::None } } @@ -335,7 +329,7 @@ impl IsGlobal { } } - fn take(self) -> T { + fn inner(self) -> T { self.value } } diff --git a/core/firewall/src/service.rs b/core/firewall/src/service.rs index 7ec4b1078..734c56bdd 100644 --- a/core/firewall/src/service.rs +++ b/core/firewall/src/service.rs @@ -129,29 +129,28 @@ where mut self: Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> std::task::Poll { - let mut this = self.as_mut(); - // If we haven't called the firewall yet, do so - if !this.called { - let pinned = Pin::new(&mut this.firewall_fut); + if !self.called { + let pinned = Pin::new(&mut self.firewall_fut); + match ready!(pinned.poll(cx)) { Ok(_) => {}, Err(e) => return Poll::Ready(Ok(e.into())), } - this.called = true; + self.called = true; } loop { - if let Some(fut) = this.svc_fut.as_mut() { + if let Some(fut) = self.svc_fut.as_mut() { match Pin::new(fut).poll(cx) { Poll::Ready(res) => return Poll::Ready(res), Poll::Pending => return Poll::Pending, } } - let req = this.req.take().expect("req is none"); - this.svc_fut = Some(this.svc.call(req)); + let req = self.req.take().expect("req is none"); + self.svc_fut = Some(self.svc.call(req)); } } } diff --git a/core/rpc/src/lib.rs b/core/rpc/src/lib.rs index 3815cde65..f64e94d4f 100644 --- a/core/rpc/src/lib.rs +++ b/core/rpc/src/lib.rs @@ -36,8 +36,9 @@ static HMAC_SECRET: OnceLock<[u8; 32]> = OnceLock::new(); pub static HMAC_NONCE: AtomicUsize = AtomicUsize::new(0); pub static HMAC_SALT: &[u8] = b"lightning-hmac-salt"; -/// Tries to read the hmac secret from the given path or the default FS location -/// or it will generate one and write it to disk +/// Tries to read the hmac secret from the given path or the default location if empty +/// if the file exists it will read the secret from it otherwise +/// it will generate one and write it to disk in both cases pub fn hmac_secret(secret_dir_path: Option) -> anyhow::Result<&'static [u8; 32]> { match HMAC_SECRET.get() { Some(secret) => Ok(secret), diff --git a/core/rpc/src/server.rs b/core/rpc/src/server.rs index 5195c004e..340dfeca2 100644 --- a/core/rpc/src/server.rs +++ b/core/rpc/src/server.rs @@ -245,7 +245,7 @@ fn verify_hmac(hmac: &str, ts: &str, nonce: &str) -> Result<(), BoxedError> { } // assume hmac secret is loaded by now - // we have verified that params are valid so lets creat the correct one + // we have verified that params are valid so lets create the correct one let correct_hmac = create_hmac(super::hmac_secret(None)?, u64_ts, usize_nonce)?; if hmac != correct_hmac { diff --git a/core/types/src/firewall.rs b/core/types/src/firewall.rs index fe00efa91..ac16e45d3 100644 --- a/core/types/src/firewall.rs +++ b/core/types/src/firewall.rs @@ -37,7 +37,7 @@ pub enum RateLimitingConfig { }, } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub struct RateLimitingRule { pub period: Period, pub max_requests: u64,