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

# 基于时间的测试

> 使用 LiteSVM 时钟操作测试依赖时间的逻辑

演示如何使用 `warpToSlot` 测试依赖时间的程序逻辑。

## 基本时钟操作

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

const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());

// Check initial clock
const initialClock = client.svm.getClock();
console.log("Initial state:");
console.log("  Slot:", initialClock.slot);
console.log("  Epoch:", initialClock.epoch);
console.log("  Unix timestamp:", initialClock.unixTimestamp);

// Warp 1,000 slots forward
client.svm.warpToSlot(initialClock.slot + 1000n);

const afterWarp = client.svm.getClock();
console.log("\nAfter warping 1,000 slots:");
console.log("  Slot:", afterWarp.slot);
console.log("  Epoch:", afterWarp.epoch);

// Warp further
client.svm.warpToSlot(initialClock.slot + 100_000n);

const afterWarp2 = client.svm.getClock();
console.log("\nAfter warping 100,000 slots:");
console.log("  Slot:", afterWarp2.slot);
console.log("  Epoch:", afterWarp2.epoch);
```

## 测试时间锁定逻辑

测试时间锁定功能的示例模式:

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

const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());

client.svm.withSigverify(false).withBlockhashCheck(false);

// Fund payer
client.svm.airdrop(client.payer.address, lamports(10_000_000_000n));

// Set up a time-locked vault account
// Structure: [u8 discriminator, u64 unlock_slot, u64 amount]
// LiteSVM starts at a recent mainnet slot, so set the unlock relative to it
const unlockSlot = client.svm.getClock().slot + 10_000n;
const lockedAmount = 5_000_000_000n;

const vaultData = new Uint8Array(1 + 8 + 8);
const view = new DataView(vaultData.buffer);
vaultData[0] = 0x01; // discriminator
view.setBigUint64(1, unlockSlot, true); // unlock_slot
view.setBigUint64(9, lockedAmount, true); // amount

const programId = (await generateKeyPairSigner()).address;
const vaultAddress = (await generateKeyPairSigner()).address;
const minBalance = client.svm.minimumBalanceForRentExemption(
  BigInt(vaultData.length)
);

client.svm.setAccount({
  address: vaultAddress,
  data: vaultData,
  executable: false,
  lamports: lamports(minBalance + lockedAmount),
  programAddress: programId,
  space: BigInt(vaultData.length)
});

console.log("Vault created with unlock slot:", unlockSlot);
console.log("Locked amount:", Number(lockedAmount) / 1e9, "SOL");

// Test 1: Try to withdraw before unlock (should fail)
console.log("\n--- Test 1: Before unlock ---");
const currentClock = client.svm.getClock();
console.log("Current slot:", currentClock.slot);

// In a real test, you would:
// 1. Build a withdraw instruction
// 2. Send it and expect it to fail
console.log("Withdrawal attempt: EXPECTED TO FAIL (too early)");

// Test 2: Warp to after unlock slot
console.log("\n--- Test 2: After unlock ---");
client.svm.warpToSlot(unlockSlot + 100n);

const newClock = client.svm.getClock();
console.log("Current slot:", newClock.slot);

// In a real test:
// 1. Build a withdraw instruction
// 2. Send it and expect it to succeed
console.log("Withdrawal attempt: EXPECTED TO SUCCEED (after unlock)");

// Verify vault state
const updatedVault = client.svm.getAccount(vaultAddress);
if (updatedVault.exists) {
  console.log("\nVault balance:", updatedVault.lamports);
}
```

## 读取 Epoch 计划表

你可以读取 epoch 计划表,以了解集群上 slot 与 epoch 之间的关系:

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

const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());

const epochSchedule = client.svm.getEpochSchedule();
console.log("Slots per epoch:", epochSchedule.slotsPerEpoch);
console.log("First normal epoch:", epochSchedule.firstNormalEpoch);
console.log("First normal slot:", epochSchedule.firstNormalSlot);
```

<Warning>
  `warpToSlot` 会推进时钟的 `slot` 字段,但**不会**自动更新 `epoch` 或 `unixTimestamp`。如需完全控制时钟字段,请使用 `setClock`。
</Warning>

## 手动操作时钟

当你的程序从 Clock sysvar 中读取 `epoch` 或 `unix_timestamp` 时,请使用 `setClock` 直接设置这些字段:

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

const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());
client.svm.withSysvars();

// Get clock, mutate, write back
const clock = client.svm.getClock();
clock.slot = 50000n;
clock.epoch = 5n;
clock.unixTimestamp = 1700000000n;
client.svm.setClock(clock);

const after = client.svm.getClock();
console.log("Slot:", after.slot); // 50000n
console.log("Epoch:", after.epoch); // 5n
console.log("Timestamp:", after.unixTimestamp); // 1700000000n
```

这与 Rust 中的 `set_sysvar(&clock)` 模式一致。若只需简单推进 slot,请使用 `warpToSlot`;若需要控制 epoch 或时间戳,请使用 `setClock`。

## 使用场景

时间操作适用于测试以下场景:

| 场景                | 方法                           |
| ----------------------- | ---------------------------------- |
| 代币归属(Vesting)           | 跳转到归属悬崖期/里程碑之后 |
| 拍卖结束         | 跳转到拍卖结束 slot 之后         |
| 质押奖励         | 使用 `setClock` 设置 epoch        |
| 时间锁定提款 | 跳转到解锁 slot 之后              |
| 速率限制           | 在允许的时间间隔之间跳转     |
| 订单过期         | 跳转到订单过期之后         |

<Info>
  请注意,`warpToSlot` 只会更改 slot。根据 SVM 实现的不同,unix 时间戳可能不会按比例更新。
</Info>

## 要点

1. **Sysvar 已就绪**:新建的客户端中已提供 Clock 及其他 sysvar。`withSysvars()` 会将它们重置为默认值,因此请勿在 `setClock` 或 `warpToSlot` 之后调用它
2. **向前跳转**:使用 `warpToSlot(slot)` 推进 slot
3. **完全控制**:当你需要设置 `epoch`、`unixTimestamp` 或其他时钟字段时,请使用 `setClock`
4. **读取时钟**:使用 `getClock()` 读取当前 slot、epoch 和时间戳
5. **测试模式**:在时间边界前后分别进行测试
