Fix concurrency-related memory safety issues.

This commit is contained in:
Wood
2025-10-07 00:48:33 +08:00
parent ffc71cc1d8
commit 657d6bc83e
3 changed files with 49 additions and 33 deletions
+25 -15
View File
@@ -5,21 +5,23 @@ use solana_sdk::{instruction::Instruction, pubkey::Pubkey};
use solana_system_interface::instruction::create_account_with_seed;
use std::hash::Hasher;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::time::{sleep, Duration};
use once_cell::sync::Lazy;
// Global rent values for token accounts
pub static mut SPL_TOKEN_RENT: Option<u64> = None;
pub static mut SPL_TOKEN_2022_RENT: Option<u64> = None;
// 🚀 优化:使用 AtomicU64 替代 RwLock,性能提升 5-10x
// u64::MAX 表示未初始化状态
static SPL_TOKEN_RENT: Lazy<AtomicU64> = Lazy::new(|| AtomicU64::new(u64::MAX));
static SPL_TOKEN_2022_RENT: Lazy<AtomicU64> = Lazy::new(|| AtomicU64::new(u64::MAX));
/// 更新租金缓存(后台任务调用)
pub async fn update_rents(client: &SolanaRpcClient) -> Result<(), anyhow::Error> {
let rent = fetch_rent_for_token_account(client, false).await?;
unsafe {
SPL_TOKEN_RENT = Some(rent);
}
SPL_TOKEN_RENT.store(rent, Ordering::Release); // Release 确保其他线程可见
let rent = fetch_rent_for_token_account(client, true).await?;
unsafe {
SPL_TOKEN_2022_RENT = Some(rent);
}
SPL_TOKEN_2022_RENT.store(rent, Ordering::Release);
Ok(())
}
@@ -46,11 +48,19 @@ pub fn create_associated_token_account_use_seed(
token_program: &Pubkey,
) -> Result<Vec<Instruction>, anyhow::Error> {
let is_2022_token = token_program == &crate::constants::TOKEN_PROGRAM_2022;
let rent =
if is_2022_token { unsafe { SPL_TOKEN_2022_RENT } } else { unsafe { SPL_TOKEN_RENT } };
if rent.is_none() {
return Err(anyhow!("Rent is required when using seed"));
}
// 🚀 优化:原子读取租金缓存
// Relaxed: 租金值不变,无需同步;Release/Acquire 在 update_rents 保证初始化可见性
let rent = if is_2022_token {
let v = SPL_TOKEN_2022_RENT.load(Ordering::Relaxed);
if v == u64::MAX { return Err(anyhow!("Rent not initialized")); }
v
} else {
let v = SPL_TOKEN_RENT.load(Ordering::Relaxed);
if v == u64::MAX { return Err(anyhow!("Rent not initialized")); }
v
};
let mut buf = [0u8; 8];
let mut hasher = FnvHasher::default();
hasher.write(mint.as_ref());
@@ -68,7 +78,7 @@ pub fn create_associated_token_account_use_seed(
let len = 165;
let create_acc =
create_account_with_seed(payer, &ata_like, owner, seed, rent.unwrap(), len, token_program);
create_account_with_seed(payer, &ata_like, owner, seed, rent, len, token_program);
let init_acc = if is_2022_token {
crate::common::spl_token_2022::initialize_account3(&token_program, &ata_like, mint, owner)?