> ## 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.

# Thời gian & Sysvars

> Các phương thức để thao tác thời gian và biến hệ thống

## Thao tác thời gian

### warp_to_slot

```rust
pub fn warp_to_slot(&mut self, slot: u64)
```

Nhảy đến một slot cụ thể. Thao tác này chỉ cập nhật `Clock.slot`; epoch và
`unix_timestamp` được giữ nguyên. Mục đích sử dụng là warp tiến, nhưng warp
lùi cũng không bị từ chối.

```rust
// Get current slot
let clock: Clock = svm.get_sysvar();
let current_slot = clock.slot;

// Jump forward 100 slots
svm.warp_to_slot(current_slot + 100);

// Verify
let new_clock: Clock = svm.get_sysvar();
assert_eq!(new_clock.slot, current_slot + 100);
```

### Thao tác đồng hồ thủ công

```rust
use solana_sdk::clock::Clock;

// Read clock
let mut clock: Clock = svm.get_sysvar();

// Modify fields
clock.slot = 1000;
clock.epoch = 10;
clock.unix_timestamp = 1735689600;

// Write back
svm.set_sysvar(&clock);
```

<Info>
  Hãy dùng `warp_to_slot()` cho hầu hết các thao tác thời gian. Chỉ chỉnh sửa đồng hồ thủ công
  khi bạn cần kiểm soát chính xác nhiều trường.
</Info>

## Sysvars

### get_sysvar

```rust
pub fn get_sysvar<T>(&self) -> T
where
    T: Sysvar + SysvarId + DeserializeOwned<Dst = T>,
```

Đọc giá trị sysvar.

```rust
use solana_sdk::clock::Clock;
use solana_sdk::rent::Rent;
use solana_sdk::epoch_schedule::EpochSchedule;

let clock: Clock = svm.get_sysvar();
let rent: Rent = svm.get_sysvar();
let epoch_schedule: EpochSchedule = svm.get_sysvar();
```

### set_sysvar

```rust
pub fn set_sysvar<T>(&mut self, sysvar: &T)
where
    T: Sysvar + SysvarId + Serialize<Src = T>,
```

Ghi giá trị sysvar.

```rust
use solana_sdk::rent::Rent;

// Read the current rent, change a field, and write it back
let mut rent: Rent = svm.get_sysvar();
rent.lamports_per_byte = 3480;

svm.set_sysvar(&rent);
```

## Các Sysvar phổ biến

### Clock

```rust
use solana_sdk::clock::Clock;

let clock: Clock = svm.get_sysvar();
println!("Slot: {}", clock.slot);
println!("Timestamp: {}", clock.unix_timestamp);
println!("Epoch: {}", clock.epoch);
```

### Rent

```rust
use solana_sdk::rent::Rent;

let rent: Rent = svm.get_sysvar();
let min_balance = rent.minimum_balance(data_len);
```

### Epoch Schedule

```rust
use solana_sdk::epoch_schedule::EpochSchedule;

let schedule: EpochSchedule = svm.get_sysvar();
println!("Slots per epoch: {}", schedule.slots_per_epoch);
```

### Stake History

```rust
// cargo add --dev solana-sysvar@4.2
use solana_sysvar::stake_history::{StakeHistory, StakeHistoryEntry};

let mut stake_history = StakeHistory::default();
stake_history.add(
    0, // epoch
    StakeHistoryEntry {
        effective: 1_000_000,
        activating: 0,
        deactivating: 0,
    },
);

svm.set_sysvar(&stake_history);
```

## Mẫu kiểm thử dựa trên thời gian

### Kiểm thử các tính năng dựa trên slot

```rust
let mut svm = LiteSVM::new();

// Get starting slot
let start_slot = svm.get_sysvar::<Clock>().slot;

// Try action before unlock slot (should fail)
let result = call_locked_program(&mut svm);
assert!(result.is_err());

// Warp to unlock slot
svm.warp_to_slot(start_slot + 1000);

// Now it should succeed
let result = call_locked_program(&mut svm);
assert!(result.is_ok());
```

### Kiểm thử các tính năng dựa trên timestamp

```rust
let mut svm = LiteSVM::new();

// Get current timestamp
let clock: Clock = svm.get_sysvar();
let current_time = clock.unix_timestamp;

// Warp forward 1 hour (3600 seconds)
// Mainnet slots target 200ms (0.2 seconds)
let target_slot = clock.slot + (3600.0 / 0.2) as u64;
// `warp_to_slot` only moves the slot, so set the timestamp yourself
let mut warped: Clock = svm.get_sysvar();
warped.slot = target_slot;
warped.unix_timestamp = current_time + 3600;
svm.set_sysvar(&warped);

// Verify timestamp increased
let new_clock: Clock = svm.get_sysvar();
assert!(new_clock.unix_timestamp >= current_time + 3600);
```

### Kiểm thử lịch vesting

```rust
// Setup vesting with 100 slot period
let vesting_period = 100;
let start_slot = svm.get_sysvar::<Clock>().slot;

// Test before vesting completes
let result = claim_vested_tokens(&mut svm);
assert!(result.is_err()); // Too early

// Warp past vesting period
svm.warp_to_slot(start_slot + vesting_period + 1);

// Now claim should succeed
let result = claim_vested_tokens(&mut svm);
assert!(result.is_ok());
```

## Các trường hợp sử dụng phổ biến

- Lịch vesting
- Rút tiền có khóa thời gian
- Thời gian chờ khi stake/unstake
- Thời điểm kết thúc đấu giá
- Thời hạn thuê
- Phần thưởng theo epoch

## Lưu ý quan trọng

1. **Chỉ tiến** - Theo quy ước, chỉ warp tiến. LiteSVM không từ chối warp
   lùi, nhưng các chương trình phụ thuộc vào thời gian có thể hoạt động không như mong đợi
2. **Từ slot sang thời gian** - Slot trên Mainnet nhắm đến 200ms (0,2 giây); slot
   quan sát được thường dài hơn một chút. LiteSVM không gắn slot với thời gian thực
3. **Clock sysvar** - `warp_to_slot()` chỉ cập nhật trường `slot`. Hãy tự đặt
   `unix_timestamp` và `epoch` bằng `set_sysvar` khi chương trình của bạn
   đọc các giá trị này
4. **Không tự động cập nhật** - Không giống validator thực, thời gian không tự động
   tiến lên giữa các giao dịch
