feat: optimize cache systems and add examples

This commit is contained in:
ysq
2025-09-04 16:32:34 +08:00
parent 692b0ce715
commit 597982653d
12 changed files with 558 additions and 272 deletions
+4 -7
View File
@@ -1,7 +1,4 @@
use solana_sdk::{
message::AddressLookupTableAccount,
pubkey::Pubkey,
};
use solana_sdk::{message::AddressLookupTableAccount, pubkey::Pubkey};
use crate::common::address_lookup_cache::get_address_lookup_table_account;
@@ -11,11 +8,11 @@ pub async fn get_address_lookup_table_accounts(
lookup_table_key: Option<Pubkey>,
) -> Vec<AddressLookupTableAccount> {
let mut address_lookup_table_accounts = vec![];
if let Some(lookup_table_key) = lookup_table_key {
let account = get_address_lookup_table_account(&lookup_table_key).await;
address_lookup_table_accounts.push(account);
}
address_lookup_table_accounts
}
}
+9 -18
View File
@@ -5,11 +5,11 @@ use solana_system_interface::instruction::advance_nonce_account;
use crate::common::nonce_cache::NonceCache;
/// 添加nonce消费指令到指令集合中
/// Add nonce advance instruction to the instruction set
///
/// 只有提供了nonce_pubkey时才使用nonce功能
/// 如果nonce被锁定、已使用或未准备好,将返回错误
/// 成功时会锁定并标记nonce为已使用
/// Nonce functionality is only used when nonce_pubkey is provided
/// Returns error if nonce is locked, already used, or not ready
/// On success, locks and marks nonce as used
pub fn add_nonce_instruction(
instructions: &mut Vec<Instruction>,
payer: &Keypair,
@@ -17,25 +17,16 @@ pub fn add_nonce_instruction(
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
// 只检查nonce_account是否存在
// Only check if nonce_account exists
if let Some(nonce_pubkey) = nonce_info.nonce_account {
// 暂不加锁
// if nonce_info.lock {
// return Err(anyhow!("Nonce is locked"));
// }
if nonce_info.used {
return Err(anyhow!("Nonce is used"));
}
if nonce_info.current_nonce == Hash::default() {
return Err(anyhow!("Nonce is not ready"));
}
// if nonce_info.next_buy_time == 0 || chrono::Utc::now().timestamp() < nonce_info.next_buy_time {
// return Err(anyhow!("Nonce is not ready"));
// }
// 加锁 - 暂不加锁
// nonce_cache.lock();
// 创建Solana系统nonce推进指令 - 使用系统程序ID
// Create Solana system nonce advance instruction - using system program ID
let nonce_advance_ix = advance_nonce_account(&nonce_pubkey, &payer.pubkey());
instructions.push(nonce_advance_ix);
@@ -44,8 +35,8 @@ pub fn add_nonce_instruction(
Ok(())
}
/// 获取用于交易的blockhash
/// 如果使用了nonce账户,返回nonce中的blockhash,否则返回传入的recent_blockhash
/// Get blockhash for transaction
/// If nonce account is used, return blockhash from nonce, otherwise return the provided recent_blockhash
pub fn get_transaction_blockhash(recent_blockhash: Hash) -> Hash {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();
@@ -57,7 +48,7 @@ pub fn get_transaction_blockhash(recent_blockhash: Hash) -> Hash {
}
}
/// 检查是否使用nonce账户
/// Check if using nonce account
pub fn is_using_nonce() -> bool {
let nonce_cache = NonceCache::get_instance();
let nonce_info = nonce_cache.get_nonce_info();