Vollständiger Test
tests/cpi_test.rs
use litesvm::LiteSVM;use solana_sdk::{instruction::{AccountMeta, Instruction},pubkey::Pubkey,signature::{Keypair, Signer},transaction::Transaction,system_program,};#[test]fn test_cross_program_invocation() {let mut svm = LiteSVM::new();// Deploy both programslet caller_program = Pubkey::new_unique();let callee_program = Pubkey::new_unique();svm.add_program(caller_program,include_bytes!("../target/deploy/caller.so")).unwrap();svm.add_program(callee_program,include_bytes!("../target/deploy/callee.so")).unwrap();// Setup accountslet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10_000_000_000).unwrap();// Create instruction that will trigger CPIlet instruction = Instruction {program_id: caller_program,accounts: vec![AccountMeta::new(payer.pubkey(), true),AccountMeta::new_readonly(callee_program, false),AccountMeta::new_readonly(system_program::id(), false),],data: vec![1], // Instruction to trigger CPI};let tx = Transaction::new_signed_with_payer(&[instruction],Some(&payer.pubkey()),&[&payer],svm.latest_blockhash(),);let result = svm.send_transaction(tx).unwrap();// Verify both programs were invokedassert!(result.logs.iter().any(|log|log.contains(&format!("Program {} invoke", caller_program))));assert!(result.logs.iter().any(|log|log.contains(&format!("Program {} invoke", callee_program))));println!("CPI successful!");println!("Logs showing both programs:");for log in &result.logs {if log.contains("invoke") {println!(" {}", log);}}}
Wichtige Punkte
- Mehrere Programme: Alle Programme, die an der Cross Program Invocation-Kette beteiligt sind, deployen
- Programm-IDs: Die aufgerufene Programm-ID als Konten in der Anweisung übergeben
- System Program: Oft für Cross Program Invocation-Operationen erforderlich (Transfers, Konten- erstellung usw.)
- Protokollüberprüfung: Protokolle prüfen, um sicherzustellen, dass alle Programme in der Aufrufkette ausgeführt wurden
Für detailliertere Deployment-Informationen lesen Sie diesen Abschnitt.
CPI-Protokolle verstehen
Wenn eine Cross Program Invocation stattfindet, werden Protokolle wie diese angezeigt:
Program A invoke [1]Program B invoke [2]Program B successProgram A success
Die Zahlen in Klammern geben die Aufruftiefe an.
Is this page helpful?