Temps & Sysvars

Méthodes pour manipuler le temps et accéder aux variables système. Chaque getter de sysvar possède un setter correspondant ; les structs sont de simples miroirs Go de la source Solana.

Manipulation de l'horloge

WarpToSlot

func (s *LiteSVM) WarpToSlot(slot uint64) error

Avance l'horloge interne jusqu'à un slot spécifique.

before, err := svm.Clock()
if err != nil {
t.Fatal(err)
}
t.Logf("current slot: %d", before.Slot)
if err := svm.WarpToSlot(10_000_000); err != nil {
t.Fatal(err)
}
after, err := svm.Clock()
if err != nil {
t.Fatal(err)
}
t.Logf("new slot: %d", after.Slot)

Clock

type Clock struct {
Slot uint64
EpochStartTimestamp int64
Epoch uint64
LeaderScheduleEpoch uint64
UnixTimestamp int64
}
func (s *LiteSVM) Clock() (Clock, error)

Lit le sysvar Clock.

c, err := svm.Clock()
if err != nil {
t.Fatal(err)
}
t.Logf("slot=%d epoch=%d unix=%d", c.Slot, c.Epoch, c.UnixTimestamp)

SetClock

func (s *LiteSVM) SetClock(c Clock) error

Écrase le sysvar Clock. Lisez-le, modifiez les champs, puis réécrivez-le lorsque vous avez besoin de contrôler Epoch ou UnixTimestamp (que WarpToSlot ne met pas à jour).

c, err := svm.Clock()
if err != nil {
t.Fatal(err)
}
c.Slot = 1_000
c.Epoch = 10
c.UnixTimestamp = 1_735_689_600
if err := svm.SetClock(c); err != nil {
t.Fatal(err)
}

WarpToSlot ne met à jour que Slot. Utilisez SetClock lorsque votre programme lit Epoch ou UnixTimestamp depuis le sysvar Clock.

rent

rent / SetRent

type Rent struct {
LamportsPerByteYear uint64
ExemptionThreshold float64
BurnPercent uint8
}
func (s *LiteSVM) Rent() (Rent, error)
func (s *LiteSVM) SetRent(r Rent) error
r, err := svm.Rent()
if err != nil {
t.Fatal(err)
}
t.Logf("lamports/byte-year: %d", r.LamportsPerByteYear)
t.Logf("exemption threshold: %.2f", r.ExemptionThreshold)
t.Logf("burn percent: %d", r.BurnPercent)

MinimumBalanceForRentExemption

func (s *LiteSVM) MinimumBalanceForRentExemption(dataLen int) (uint64, error)
for _, size := range []int{0, 100, 1024} {
min, err := svm.MinimumBalanceForRentExemption(size)
if err != nil {
t.Fatal(err)
}
t.Logf("%4d bytes -> %d lamports", size, min)
}

Epoch Schedule

type EpochSchedule struct {
SlotsPerEpoch uint64
LeaderScheduleSlotOffset uint64
Warmup bool
FirstNormalEpoch uint64
FirstNormalSlot uint64
}
func (s *LiteSVM) EpochSchedule() (EpochSchedule, error)
func (s *LiteSVM) SetEpochSchedule(e EpochSchedule) error
es, err := svm.EpochSchedule()
if err != nil {
t.Fatal(err)
}
t.Logf("slots per epoch: %d", es.SlotsPerEpoch)
t.Logf("first normal epoch: %d", es.FirstNormalEpoch)
t.Logf("first normal slot: %d", es.FirstNormalSlot)

Epoch Rewards

type EpochRewards struct {
DistributionStartingBlockHeight uint64
NumPartitions uint64
ParentBlockhash solana.Hash
TotalPointsLo uint64 // low 64 bits of the u128 total_points
TotalPointsHi uint64 // high 64 bits
TotalRewards uint64
DistributedRewards uint64
Active bool
}
func (s *LiteSVM) EpochRewards() (EpochRewards, error)
func (s *LiteSVM) SetEpochRewards(e EpochRewards) error

total_points est un u128 côté Solana ; litesvm-go expose les deux moitiés explicitement afin que les appelants Go n'aient pas besoin d'une bibliothèque u128. Pour les valeurs tenant sur 64 bits, TotalPointsHi vaut 0 et TotalPointsLo contient la valeur.

Informations sur le slot

LastRestartSlot / SetLastRestartSlot

func (s *LiteSVM) LastRestartSlot() (uint64, error)
func (s *LiteSVM) SetLastRestartSlot(slot uint64) error
last, err := svm.LastRestartSlot()
if err != nil {
t.Fatal(err)
}
if err := svm.SetLastRestartSlot(last + 1); err != nil {
t.Fatal(err)
}

SlotHashes / SetSlotHashes

type SlotHash struct {
Slot uint64
Hash solana.Hash
}
func (s *LiteSVM) SlotHashes() ([]SlotHash, error)
func (s *LiteSVM) SetSlotHashes(items []SlotHash) error

SlotHistory

SlotHistory est un bitvec d'environ 128 Ko ; litesvm-go l'expose comme un handle plutôt qu'une slice.

sh, err := litesvm.NewSlotHistory()
if err != nil {
t.Fatal(err)
}
defer sh.Close()
sh.Add(42)
switch sh.Check(42) {
case litesvm.SlotHistoryFound:
// ...
case litesvm.SlotHistoryNotFound:
// ...
case litesvm.SlotHistoryTooOld:
// ...
case litesvm.SlotHistoryFuture:
// ...
}
if err := svm.SetSlotHistory(sh); err != nil {
t.Fatal(err)
}
MéthodeDescription
litesvm.NewSlotHistory() (*SlotHistory, error)Handle vide
sh.Add(slot uint64)Enregistrer un slot
sh.Check(slot uint64) SlotHistoryCheckFuture / TooOld / Found / NotFound
sh.Oldest() uint64Slot le plus ancien suivi
sh.Newest() uint64Slot le plus récent suivi
sh.NextSlot() uint64Prochain slot à enregistrer
sh.SetNextSlot(slot uint64) errorRemplacer le marqueur du prochain slot
sh.Close()Libérer le handle
svm.SlotHistory() (*SlotHistory, error)Lire dans un nouveau handle
svm.SetSlotHistory(*SlotHistory) errorInstaller sur la SVM

Historique des stakes

type StakeHistoryItem struct {
Epoch uint64
Effective uint64
Activating uint64
Deactivating uint64
}
func (s *LiteSVM) StakeHistory() ([]StakeHistoryItem, error)
func (s *LiteSVM) SetStakeHistory(items []StakeHistoryItem) error

Tester la logique dépendante du temps

Utilisez WarpToSlot pour une progression simple basée sur les slots, SetClock pour un contrôle total :

func TestTimeLockedVault(t *testing.T) {
svm, err := litesvm.New()
if err != nil {
t.Fatal(err)
}
defer svm.Close()
const unlockSlot = uint64(10_000)
// Setup: create the time-locked account ...
// Test 1: Try to withdraw before unlock (should fail)
before, err := svm.Clock()
if err != nil {
t.Fatal(err)
}
t.Logf("current slot: %d", before.Slot)
// ... attempt withdrawal, expect failure ...
// Warp time forward past unlock slot
if err := svm.WarpToSlot(unlockSlot + 1); err != nil {
t.Fatal(err)
}
// Test 2: Try to withdraw after unlock (should succeed)
after, err := svm.Clock()
if err != nil {
t.Fatal(err)
}
t.Logf("new slot: %d", after.Slot)
// ... attempt withdrawal, expect success ...
}

Is this page helpful?

Table des matières

Modifier la page
© 2026 Fondation Solana. Tous droits réservés.