Tổng Quan
Hãy nghĩ về LiteSVM như một Solana VM chạy bên trong hàm kiểm thử của bạn. Không phải là một tiến trình riêng biệt, không phải là một dịch vụ mạng - nó chỉ là một lời gọi thư viện trong mã của bạn.
Kiểm Thử Truyền Thống
// Traditional approach - slow and complexlet validator = TestValidator::new().await; // Starts separate processlet client = RpcClient::new(validator.url()); // Network connection// Every operation is async and goes over networklet balance = client.get_balance(&pubkey).await?;tokio::time::sleep(Duration::from_secs(1)).await; // Wait for confirmation
Kiểm Thử Với LiteSVM
// LiteSVM - fast and simplelet mut svm = LiteSVM::new(); // Just a struct in memory// Everything is synchronous and immediatelet balance = svm.get_balance(&pubkey).unwrap_or(0);
Các Nguyên Tắc Cốt Lõi
1. Mọi Thứ Đều Đồng Bộ
Không có async, không có await, không có độ trễ:
// Send transaction and check result immediatelysvm.send_transaction(tx).unwrap();let balance = svm.get_balance(&account).unwrap(); // Already updated!
2. Thao Tác Trạng Thái Trực Tiếp
// Create any account with any datasvm.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. Thời Gian Nằm Trong Tầm Kiểm Soát Của Bạn
// Jump to any slot instantlysvm.warp_to_slot(1000);// Expire blockhashes on demandsvm.expire_blockhash();// Set any sysvarlet mut clock = svm.get_sysvar::<Clock>();clock.unix_timestamp = 1735689600; // Jan 1, 2025svm.set_sysvar(&clock);
4. Lỗi Được Hiển Thị Ngay Lập Tức và Rõ Ràng
match svm.send_transaction(tx) {Ok(meta) => {// Transaction succeededprintln!("Compute units: {}", meta.compute_units_consumed);}Err(e) => {// Error with full detailsprintln!("Error: {:?}", e.err);println!("Logs: {:?}", e.meta.logs);}}
Các Bước Tiếp Theo
Kiểm Tra Chương Trình Của Bạn
→ Tìm hiểu cách kiểm tra chương
trình Solana của riêng bạn
Ví Dụ
→ Xem các giải pháp sao chép-dán cho các
tình huống phổ biến và ví dụ kho lưu trữ đầy đủ
Tài Liệu API
→ Tìm hiểu các tính năng nâng cao và
cấu hình tùy chỉnh
Is this page helpful?