Skip to content

Commit

Permalink
Use parking_lot mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Feb 17, 2025
1 parent 0723b4e commit 49d5294
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mls_validation_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ hex = { workspace = true }
lru = "0.13.0"
openmls = { workspace = true }
openmls_rust_crypto = { workspace = true }
parking_lot.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["signal", "rt-multi-thread"] }
tonic = { workspace = true }
Expand Down
32 changes: 18 additions & 14 deletions mls_validation_service/src/cached_signature_verifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use lru::LruCache;
use parking_lot::Mutex;
use std::num::NonZeroUsize;
use tokio::sync::Mutex;

use ethers::types::{BlockNumber, Bytes};
use xmtp_id::associations::AccountId;
Expand Down Expand Up @@ -40,7 +40,7 @@ impl SmartContractSignatureVerifier for CachedSmartContractSignatureVerifier {
block_number: Option<BlockNumber>,
) -> Result<ValidationResponse, VerifierError> {
if let Some(cached_response) = {
let mut cache = self.cache.lock().await;
let mut cache = self.cache.lock();
cache.get(&hash).cloned()
} {
return Ok(cached_response);
Expand All @@ -51,7 +51,7 @@ impl SmartContractSignatureVerifier for CachedSmartContractSignatureVerifier {
.is_valid_signature(account_id, hash, signature, block_number)
.await?;

let mut cache = self.cache.lock().await;
let mut cache = self.cache.lock();
cache.put(hash, response.clone());

Ok(response)
Expand All @@ -67,14 +67,14 @@ mod tests {
VerifierError,
};

#[test]
fn test_cache_eviction() {
#[tokio::test]
async fn test_cache_eviction() {
let mut cache: LruCache<CacheKey, ValidationResponse> =
LruCache::new(NonZeroUsize::new(1).unwrap());
let hash1 = [0u8; 32];
let hash2 = [1u8; 32];
let key1 = [0u8; 32];
let key2 = [1u8; 32];

assert_ne!(hash1, hash2);
assert_ne!(key1, key2);

let val1: ValidationResponse = ValidationResponse {
is_valid: true,
Expand All @@ -87,16 +87,20 @@ mod tests {
error: None,
};

cache.put(hash1, val1.clone());
let response = cache.get(&hash1).unwrap();
cache.put(key1, val1.clone());
let response = cache.get(&key1).unwrap();

// key1 is correctly cached
assert_eq!(response.is_valid, val1.is_valid);
assert_eq!(response.block_number, val1.block_number);

cache.put(hash2, val2.clone());
assert!(cache.get(&hash1).is_none());
cache.put(key2, val2.clone());

// key1 is evicted, shouldn't exist
assert!(cache.get(&key1).is_none());

// And key2 is correctly cached.
let response2 = cache.get(&hash2).unwrap();
// And key2 is correctly cached
let response2 = cache.get(&key2).unwrap();
assert_eq!(response2.is_valid, val2.is_valid);
assert_eq!(response2.block_number, val2.block_number);
}
Expand Down
1 change: 1 addition & 0 deletions mls_validation_service/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use std::num::NonZeroUsize;

// Gather the command line arguments into a struct
#[derive(Parser, Debug)]
#[command(about = "MLS Validation Server")]
Expand Down

0 comments on commit 49d5294

Please sign in to comment.