PDA 创建

完整测试

tests/pda_test.rs
use litesvm::LiteSVM;
use solana_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!");
}

关键要点

  1. PDA 推导:使用 Pubkey::find_program_address() 推导 PDA 地址
  2. Seeds:PDA 由 seeds 和程序 ID 推导而来
  3. Bump Seed:bump seed 确保地址不在 ed25519 曲线上
  4. 账户创建:使用 set_account() 手动创建账户以进行测试
  5. 所有权:PDA 必须由派生它的程序所拥有

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有