Skip to content

Commit

Permalink
Make cache key 32B long
Browse files Browse the repository at this point in the history
  • Loading branch information
fbac committed Feb 14, 2025
1 parent 3a8ec7d commit 573824e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 48 deletions.
58 changes: 11 additions & 47 deletions mls_validation_service/src/cached_signature_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,7 @@ use ethers::types::{BlockNumber, Bytes};
use xmtp_id::associations::AccountId;
use xmtp_id::scw_verifier::{SmartContractSignatureVerifier, ValidationResponse, VerifierError};

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct CacheKey {
pub address: String,
pub chain_id: String,
pub hash: [u8; 32],
pub signature: Vec<u8>,
pub block_number: Option<u64>,
}

impl CacheKey {
pub fn new(
account_id: &AccountId,
hash: [u8; 32],
signature: &Bytes,
block_number: Option<BlockNumber>,
) -> Self {
let block_number_u64 = block_number.and_then(|bn| bn.as_number().map(|n| n.as_u64()));
let address = account_id.get_account_address().to_string();
let chain_id = account_id.get_chain_id().to_string();

Self {
chain_id,
address,
hash,
signature: signature.to_vec(),
block_number: block_number_u64,
}
}
}
type CacheKey = [u8; 32];

/// A cached smart contract verifier.
///
Expand Down Expand Up @@ -67,11 +39,9 @@ impl SmartContractSignatureVerifier for CachedSmartContractSignatureVerifier {
signature: Bytes,
block_number: Option<BlockNumber>,
) -> Result<ValidationResponse, VerifierError> {
let key = CacheKey::new(&account_id, hash, &signature, block_number);

if let Some(cached_response) = {
let mut cache = self.cache.lock().await;
cache.get(&key).cloned()
cache.get(&hash).cloned()
} {
return Ok(cached_response);
}
Expand All @@ -82,7 +52,7 @@ impl SmartContractSignatureVerifier for CachedSmartContractSignatureVerifier {
.await?;

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

Ok(response)
}
Expand All @@ -101,16 +71,10 @@ mod tests {
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 account_id1 = AccountId::new(String::from("chain1"), String::from("account1"));
let account_id2 = AccountId::new(String::from("chain1"), String::from("account2"));
let hash = [0u8; 32];
let bytes = Bytes::from(vec![1, 2, 3]);
let block_number = Some(BlockNumber::Number(1.into()));

let key1 = CacheKey::new(&account_id1, hash, &bytes, block_number);
let key2 = CacheKey::new(&account_id2, hash, &bytes, block_number);
assert_ne!(key1, key2);
assert_ne!(hash1, hash2);

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

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

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

// And key2 is correctly cached.
let response2 = cache.get(&key2).unwrap();
let response2 = cache.get(&hash2).unwrap();
assert_eq!(response2.is_valid, val2.is_valid);
assert_eq!(response2.block_number, val2.block_number);
}
Expand Down
2 changes: 1 addition & 1 deletion mls_validation_service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub(crate) struct Args {
pub(crate) chain_urls: Option<String>,

// The size of the cache to use for the smart contract signature verifier.
#[arg(long, default_value_t = 1000)]
#[arg(long, default_value_t = 10000)]
pub(crate) cache_size: usize,
}

0 comments on commit 573824e

Please sign in to comment.