LiteSVM을 사용하는 이유

개요

LiteSVM은 테스트 함수 내에서 실행되는 Solana VM이라고 생각하면 됩니다. 별도의 프로세스나 네트워크 서비스가 아니라, 코드 내의 단순한 라이브러리 호출입니다.

전통적인 테스트 방식

// Traditional approach - slow and complex
let validator = TestValidator::new().await; // Starts separate process
let client = RpcClient::new(validator.url()); // Network connection
// Every operation is async and goes over network
let balance = client.get_balance(&pubkey).await?;
tokio::time::sleep(Duration::from_secs(1)).await; // Wait for confirmation

LiteSVM 테스트 방식

// LiteSVM - fast and simple
let mut svm = LiteSVM::new(); // Just a struct in memory
// Everything is synchronous and immediate
let balance = svm.get_balance(&pubkey).unwrap_or(0);

핵심 원칙

1. 모든 것이 동기적으로 처리됩니다

async도, await도, 지연도 없습니다:

// Send transaction and check result immediately
svm.send_transaction(tx).unwrap();
let balance = svm.get_balance(&account).unwrap(); // Already updated!

2. 직접적인 상태 조작

// Create any account with any data
svm.set_account(pubkey, Account {
lamports: 1_000_000_000,
data: vec![1, 2, 3, 4],
owner: program_id,
executable: false,
rent_epoch: 0,
}).unwrap();

3. 시간은 당신이 제어합니다

// Jump to any slot instantly
svm.warp_to_slot(1000);
// Expire blockhashes on demand
svm.expire_blockhash();
// Set any sysvar
let mut clock = svm.get_sysvar::<Clock>();
clock.unix_timestamp = 1735689600; // Jan 1, 2025
svm.set_sysvar(&clock);

4. 오류는 즉각적이고 명확합니다

match svm.send_transaction(tx) {
Ok(meta) => {
// Transaction succeeded
println!("Compute units: {}", meta.compute_units_consumed);
}
Err(e) => {
// Error with full details
println!("Error: {:?}", e.err);
println!("Logs: {:?}", e.meta.logs);
}
}

다음 단계

프로그램 테스트하기 → 나만의 Solana 프로그램을 테스트하는 방법을 알아보세요
예제 → 일반적인 시나리오에 대한 복사-붙여넣기 솔루션과 전체 저장소 예제를 확인하세요
API 레퍼런스 → 고급 기능 및 커스텀 설정을 알아보세요

Is this page helpful?

목차

페이지 편집