> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Vì sao chọn LiteSVM

> Tìm hiểu cách LiteSVM hoạt động và điểm khác biệt của nó

## Tổng quan

Hãy hình dung LiteSVM như một Solana VM chạy ngay bên trong hàm kiểm thử của bạn. Không phải một tiến trình riêng biệt, cũng không phải một dịch vụ mạng - nó chỉ là một lệnh gọi thư viện trong
mã của bạn.

### Kiểm thử truyền thống

<Tabs>
<Tab title="Rust">

```rust
// 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
```

</Tab>
<Tab title="TypeScript">

```typescript
// Traditional approach - slow and complex
const validator = await startTestValidator(); // Starts separate process
const rpc = createSolanaRpc(validator.url);

// Every operation is async and goes over network
const balance = await rpc.getBalance(address).send();
```

</Tab>
</Tabs>

### Kiểm thử với LiteSVM

<Tabs>
<Tab title="Rust">

```rust
// 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);
```

</Tab>
<Tab title="TypeScript">

```typescript
import { createClient, generateKeyPairSigner } from "@solana/kit";
import { litesvm } from "@solana/kit-plugin-litesvm";
import { signer } from "@solana/kit-plugin-signer";

// LiteSVM - fast and simple
const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());

// Everything is synchronous and immediate
const balance = client.svm.getBalance(pubkey) ?? 0n;
```

</Tab>
</Tabs>

## Nguyên tắc chính

### 1. Mọi thứ đều đồng bộ

Không có `async`, không có `await`, không có độ trễ:

<Tabs>
<Tab title="Rust">

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

</Tab>
<Tab title="TypeScript">

```typescript
// Send transaction and check result immediately
client.svm.sendTransaction(signedTx);
const balance = client.svm.getBalance(account); // Already updated!
```

</Tab>
</Tabs>

### 2. Thao tác trực tiếp với trạng thái

<Tabs>
<Tab title="Rust">

```rust
// 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();
```

</Tab>
<Tab title="TypeScript">

```typescript
import { lamports } from "@solana/kit";

// Create any account with any data
client.svm.setAccount({
  address: pubkey,
  lamports: lamports(1_000_000_000n),
  data: new Uint8Array([1, 2, 3, 4]),
  programAddress: programId,
  executable: false,
  space: 4n
});
```

</Tab>
</Tabs>

### 3. Bạn kiểm soát thời gian

<Tabs>
<Tab title="Rust">

```rust
// 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);
```

</Tab>
<Tab title="TypeScript">

```typescript
// Jump to any slot instantly
client.svm.warpToSlot(1000n);

// Expire blockhashes on demand
client.svm.expireBlockhash();

// Set the clock sysvar: read it, change a field, write it back
const clock = client.svm.getClock();
clock.unixTimestamp = 1735689600n; // Jan 1, 2025
client.svm.setClock(clock);
```

</Tab>
</Tabs>

### 4. Lỗi hiển thị ngay lập tức và rõ ràng

<Tabs>
<Tab title="Rust">

```rust
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);
    }
}
```

</Tab>
<Tab title="TypeScript">

```typescript
// FailedTransactionMetadata comes from the litesvm package, which the
// LiteSVM plugin installs (add it to your dependencies with `npm install litesvm`)
import { FailedTransactionMetadata } from "litesvm";

const result = client.svm.sendTransaction(signedTx);

if (result instanceof FailedTransactionMetadata) {
  // Error with full details
  console.log("Error:", result.err());
  console.log("Logs:", result.meta().logs());
} else {
  // Transaction succeeded
  console.log("Compute units:", result.computeUnitsConsumed());
}
```

</Tab>
</Tabs>

## Các bước tiếp theo

<Tabs>
  <Tab title="Rust">
    **[Kiểm thử chương trình của bạn →](/developers/docs/tools/litesvm/testing-your-program)** Tìm hiểu
    cách kiểm thử chương trình Solana của riêng bạn <br /> **[Ví dụ
    →](/developers/docs/tools/litesvm/examples)** Xem các giải pháp có thể sao chép và dán cho những
    tình huống phổ biến cùng các ví dụ repository đầy đủ <br /> **[Tài liệu tham khảo API
    →](/developers/docs/tools/litesvm/api-reference)** Tìm hiểu các tính năng nâng cao và cấu hình
    tùy chỉnh
  </Tab>
  <Tab title="TypeScript">
    **[Kiểm thử chương trình của bạn →](/developers/docs/tools/litesvm/typescript/getting-started)**
    Tìm hiểu cách kiểm thử chương trình Solana của riêng bạn bằng LiteSVM cho TypeScript{" "}
    <br /> **[Ví dụ →](/developers/docs/tools/litesvm/typescript/examples)** Xem
    các giải pháp có thể sao chép và dán cho những tình huống phổ biến <br /> **[Tài liệu tham khảo API
    →](/developers/docs/tools/litesvm/typescript/api-reference)** Tìm hiểu các tính năng nâng cao
    và cấu hình tùy chỉnh
  </Tab>
</Tabs>
