Skip to content

Commit b6d780f

Browse files
committed
refactor: limit access to config's singleton
Work done by @lgalabru Commits of #247 but rebased on main / newer version
1 parent 42590b5 commit b6d780f

File tree

16 files changed

+688
-396
lines changed

16 files changed

+688
-396
lines changed

crates/lib/src/admin/token_util.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{
2+
config::Config,
23
error::KoraError,
34
state::{get_request_signer_with_signer_key, get_signer_pool},
45
token::token::TokenType,
@@ -91,10 +92,13 @@ pub async fn initialize_atas_with_chunk_size(
9192
compute_unit_limit: Option<u32>,
9293
chunk_size: usize,
9394
) -> Result<(), KoraError> {
95+
let config = get_config()?;
96+
9497
for address in addresses_to_initialize_atas {
9598
println!("Initializing ATAs for address: {address}");
9699

97-
let atas_to_create = find_missing_atas(rpc_client, address).await?;
100+
#[allow(clippy::needless_borrow)]
101+
let atas_to_create = find_missing_atas(&config, rpc_client, address).await?;
98102

99103
if atas_to_create.is_empty() {
100104
println!("✓ All required ATAs already exist for address: {address}");
@@ -245,11 +249,10 @@ async fn create_atas_for_signer(
245249
}
246250

247251
pub async fn find_missing_atas(
252+
config: &Config,
248253
rpc_client: &RpcClient,
249254
payment_address: &Pubkey,
250255
) -> Result<Vec<ATAToCreate>, KoraError> {
251-
let config = get_config()?;
252-
253256
// Parse all allowed SPL paid token mints
254257
let mut token_mints = Vec::new();
255258
for token_str in &config.validation.allowed_spl_paid_tokens {
@@ -273,14 +276,14 @@ pub async fn find_missing_atas(
273276
for mint in &token_mints {
274277
let ata = get_associated_token_address(payment_address, mint);
275278

276-
match CacheUtil::get_account(rpc_client, &ata, false).await {
279+
match CacheUtil::get_account(config, rpc_client, &ata, false).await {
277280
Ok(_) => {
278281
println!("✓ ATA already exists for token {mint}: {ata}");
279282
}
280283
Err(_) => {
281284
// Fetch mint account to determine if it's SPL or Token2022
282285
let mint_account =
283-
CacheUtil::get_account(rpc_client, mint, false).await.map_err(|e| {
286+
CacheUtil::get_account(config, rpc_client, mint, false).await.map_err(|e| {
284287
KoraError::RpcError(format!("Failed to fetch mint account for {mint}: {e}"))
285288
})?;
286289

@@ -328,7 +331,8 @@ mod tests {
328331
let rpc_client = create_mock_rpc_client_account_not_found();
329332
let payment_address = Pubkey::new_unique();
330333

331-
let result = find_missing_atas(&rpc_client, &payment_address).await.unwrap();
334+
let config = get_config().unwrap();
335+
let result = find_missing_atas(&config, &rpc_client, &payment_address).await.unwrap();
332336

333337
assert!(result.is_empty(), "Should return empty vec when no SPL tokens configured");
334338
}
@@ -366,9 +370,10 @@ mod tests {
366370
cache_ctx
367371
.expect()
368372
.times(3)
369-
.returning(move |_, _, _| responses_clone.lock().unwrap().pop_front().unwrap());
373+
.returning(move |_, _, _, _| responses_clone.lock().unwrap().pop_front().unwrap());
370374

371-
let result = find_missing_atas(&rpc_client, &payment_address).await;
375+
let config = get_config().unwrap();
376+
let result = find_missing_atas(&config, &rpc_client, &payment_address).await;
372377

373378
assert!(result.is_ok(), "Should handle SPL tokens with proper mocking");
374379
let atas = result.unwrap();

crates/lib/src/cache.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use solana_client::nonblocking::rpc_client::RpcClient;
55
use solana_sdk::{account::Account, pubkey::Pubkey};
66
use tokio::sync::OnceCell;
77

8-
use crate::{error::KoraError, sanitize_error};
8+
use crate::{config::Config, error::KoraError, sanitize_error};
99

1010
#[cfg(not(test))]
1111
use crate::state::get_config;
@@ -33,7 +33,8 @@ impl CacheUtil {
3333
pub async fn init() -> Result<(), KoraError> {
3434
let config = get_config()?;
3535

36-
let pool = if CacheUtil::is_cache_enabled() {
36+
#[allow(clippy::needless_borrow)]
37+
let pool = if CacheUtil::is_cache_enabled(&config) {
3738
let redis_url = config.kora.cache.url.as_ref().ok_or(KoraError::ConfigError)?;
3839

3940
let cfg = deadpool_redis::Config::from_url(redis_url);
@@ -179,23 +180,19 @@ impl CacheUtil {
179180
}
180181

181182
/// Check if cache is enabled and available
182-
fn is_cache_enabled() -> bool {
183-
match get_config() {
184-
Ok(config) => config.kora.cache.enabled && config.kora.cache.url.is_some(),
185-
Err(_) => false,
186-
}
183+
fn is_cache_enabled(config: &Config) -> bool {
184+
config.kora.cache.enabled && config.kora.cache.url.is_some()
187185
}
188186

189187
/// Get account from cache with optional force refresh
190188
pub async fn get_account(
189+
config: &Config,
191190
rpc_client: &RpcClient,
192191
pubkey: &Pubkey,
193192
force_refresh: bool,
194193
) -> Result<Account, KoraError> {
195-
let config = get_config()?;
196-
197194
// If cache is disabled or force refresh is requested, go directly to RPC
198-
if !CacheUtil::is_cache_enabled() {
195+
if !CacheUtil::is_cache_enabled(config) {
199196
return Self::get_account_from_rpc(rpc_client, pubkey).await;
200197
}
201198

@@ -264,7 +261,8 @@ mod tests {
264261
async fn test_is_cache_enabled_disabled() {
265262
let _m = ConfigMockBuilder::new().with_cache_enabled(false).build_and_setup();
266263

267-
assert!(!CacheUtil::is_cache_enabled());
264+
let config = get_config().unwrap();
265+
assert!(!CacheUtil::is_cache_enabled(&config));
268266
}
269267

270268
#[tokio::test]
@@ -275,7 +273,8 @@ mod tests {
275273
.build_and_setup();
276274

277275
// Without URL, cache should be disabled
278-
assert!(!CacheUtil::is_cache_enabled());
276+
let config = get_config().unwrap();
277+
assert!(!CacheUtil::is_cache_enabled(&config));
279278
}
280279

281280
#[tokio::test]
@@ -286,7 +285,8 @@ mod tests {
286285
.build_and_setup();
287286

288287
// Give time for config to be set up
289-
assert!(CacheUtil::is_cache_enabled());
288+
let config = get_config().unwrap();
289+
assert!(CacheUtil::is_cache_enabled(&config));
290290
}
291291

292292
#[tokio::test]
@@ -336,7 +336,8 @@ mod tests {
336336

337337
let rpc_client = RpcMockBuilder::new().with_account_info(&expected_account).build();
338338

339-
let result = CacheUtil::get_account(&rpc_client, &pubkey, false).await;
339+
let config = get_config().unwrap();
340+
let result = CacheUtil::get_account(&config, &rpc_client, &pubkey, false).await;
340341

341342
assert!(result.is_ok());
342343
let account = result.unwrap();
@@ -355,7 +356,8 @@ mod tests {
355356
let rpc_client = RpcMockBuilder::new().with_account_info(&expected_account).build();
356357

357358
// force_refresh = true should always go to RPC
358-
let result = CacheUtil::get_account(&rpc_client, &pubkey, true).await;
359+
let config = get_config().unwrap();
360+
let result = CacheUtil::get_account(&config, &rpc_client, &pubkey, true).await;
359361

360362
assert!(result.is_ok());
361363
let account = result.unwrap();

0 commit comments

Comments
 (0)