Yêu cầu cargo add --dev litesvm solana-sdk@4
Bài kiểm thử hoàn chỉnh
use litesvm::LiteSVM;
use solana_sdk::account::Account;
use solana_sdk::pubkey::Pubkey;
#[test]
fn test_pda_creation() {
let mut svm = LiteSVM::new();
// Program that owns the PDA
let program_id = Pubkey::new_unique();
// Derive PDA address
let seed = b"my_pda";
let (pda, bump) = Pubkey::find_program_address(
&[seed],
&program_id
);
println!("PDA: {}", pda);
println!("Bump: {}", bump);
// Create PDA account manually (for testing)
svm.set_account(pda, Account {
lamports: 1_000_000,
data: vec![bump], // Store bump seed in data
owner: program_id,
executable: false,
rent_epoch: 0,
}).unwrap();
// Verify PDA was created
let account = svm.get_account(&pda).unwrap();
assert_eq!(account.owner, program_id);
assert_eq!(account.data[0], bump);
println!("PDA created successfully!");
}
Điểm chính
- Dẫn xuất PDA: Sử dụng
Pubkey::find_program_address() để dẫn xuất các địa chỉ
PDA
- Seed: Các PDA được dẫn xuất từ seed và ID chương trình
- Bump Seed: bump seed đảm bảo địa chỉ nằm ngoài đường cong ed25519
- Tạo tài khoản: Sử dụng
set_account() để tạo tài khoản thủ công phục vụ
kiểm thử
- Quyền sở hữu: Địa chỉ của một PDA được dẫn xuất từ ID chương trình, và chỉ
chương trình đó mới có thể ký cho nó. Chủ sở hữu là chương trình mà tài khoản
được gán cho