Havainnollistaa, miten warpToSlot-funktiolla testataan ajasta riippuvaa ohjelmalogiikkaa.
Kellon perusmanipulointi
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);
Aikalukitun logiikan testaus
Esimerkkimalli aikalukitun toiminnallisuuden testaamiseen:
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-aikataulun lukeminen
Voit lukea epoch-aikataulun ymmärtääksesi klusterin
slot/epoch-suhteen:
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);
warpToSlot siirtää kellon slot-kenttää eteenpäin, mutta ei päivitä
automaattisesti epoch- tai unixTimestamp-kenttiä. Käytä setClock-funktiota, kun haluat
täyden hallinnan kellokenttiin.
Kellon manuaalinen manipulointi
Kun ohjelmasi lukee Clock-sysvarista epoch- tai unix_timestamp-kentän, käytä
setClock-funktiota näiden kenttien asettamiseen suoraan:
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
Tämä vastaa Rustin set_sysvar(&clock)-mallia. Käytä warpToSlot-funktiota yksinkertaiseen
slot-arvon siirtämiseen eteenpäin ja setClock-funktiota, kun tarvitset epoch- tai aikaleimahallintaa.
Käyttötapaukset
Ajan manipulointi on hyödyllistä seuraavien testaamisessa:
Muista, että warpToSlot muuttaa vain slot-arvoa. Unix-aikaleima ei välttämättä
päivity suhteessa, SVM-toteutuksesta riippuen.
Tärkeimmät kohdat
- Sysvarit ovat valmiina: Clock ja muut sysvarit ovat käytettävissä uudessa
clientissä.
withSysvars() palauttaa ne oletusarvoihinsa, joten älä kutsu sitä
setClock- tai warpToSlot-kutsun jälkeen
- Siirry eteenpäin: Käytä
warpToSlot(slot)-funktiota slot-arvon siirtämiseen eteenpäin
- Täysi hallinta: Käytä
setClock-funktiota, kun tarvitset epoch-,
unixTimestamp- tai muiden kellokenttien asettamista
- Lue kello: Käytä
getClock()-funktiota lukeaksesi nykyisen slot-arvon, epoch-arvon ja aikaleiman
- Testimalli: Testaa ennen aikarajoja ja niiden jälkeen