Configurazione
Prima di testare il tuo programma, assicurati di:
- Compilare il programma per generare il file
.so
- Aggiungere
litesvm come dipendenza di sviluppo nel file Cargo.toml
- Aggiungere eventuali altre dipendenze di sviluppo necessarie, come
solana-sdk@4 e
litesvm-token
- Creare un file di test nella directory
tests
Struttura base di un test del programma
Ecco tutti i passaggi di base necessari per chiamare ed eseguire un’istruzione in un
programma Solana.
use litesvm::LiteSVM;
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::Transaction,
};
use my_program;
#[test]
fn test_my_program() {
// Initialize the test environment
let mut svm = LiteSVM::new();
// Deploy your program to the test environment. If your program uses an
// older solana-program (such as 2.2), its ID is a different Pubkey type,
// so convert it through its bytes.
let program_id = Pubkey::new_from_array(my_program::ID.to_bytes());
let program_bytes = include_bytes!("../../../target/deploy/my_program.so");
svm.add_program(program_id, program_bytes).unwrap();
// Create and fund test accounts
let payer = Keypair::new();
svm.airdrop(&payer.pubkey(), 10_000_000_000).unwrap();
// Create your instruction
let instruction = Instruction {
program_id,
accounts: vec![
AccountMeta::new(payer.pubkey(), true),
],
data: vec![],
};
// Build transaction
let tx = Transaction::new_signed_with_payer(
&[instruction],
Some(&payer.pubkey()),
&[&payer],
svm.latest_blockhash(),
);
// Send transaction
let result = svm.send_transaction(tx).unwrap();
// Check transaction succeeded
println!("Transaction logs: {:?}", result.logs);
println!("Program executed successfully!");
}
L’ambiente di test predefinito generato con LiteSVM::new() abilita
le funzionalità del runtime attualmente attive su mainnet-beta, i sysvar predefiniti, i programmi spl,
sigverify e tutti i programmi integrati, come il system program.
I precompilati (ed25519, secp256k1, secp256r1) sono inclusi quando la funzionalità precompiles
di litesvm è abilitata.
Per costruire l’intero ambiente a partire da un altro set di funzionalità, come
FeatureSet::all_enabled() del crate agave-feature-set, parti da
LiteSVM::default() in modo che i sysvar e gli account delle funzionalità corrispondano:
use agave_feature_set::FeatureSet;
use litesvm::LiteSVM;
use solana_sdk::native_token::LAMPORTS_PER_SOL;
let mut svm = LiteSVM::default()
.with_feature_set(FeatureSet::all_enabled())
.with_builtins()
.with_lamports(1_000_000 * LAMPORTS_PER_SOL)
.with_sysvars()
.with_feature_accounts()
.with_default_programs()
.with_sigverify(true)
.with_blockhash_check(true);
Concatenare with_feature_set a LiteSVM::new() sostituisce soltanto il feature set:
il sysvar Rent e gli account delle funzionalità restano come li ha impostati new() per
le funzionalità di mainnet.
Se vuoi interagire con un programma che non è incluso nell’ambiente di test
predefinito, devi effettuarne il deploy nell’ambiente di test.
Prossimi passi
Nella prossima sezione scopriremo tutto sul deploy dei programmi nell’ambiente
di test di litesvm.