Methoden voor het manipuleren van tijd en het benaderen van systeemvariabelen. Elke sysvar-getter
heeft een bijbehorende setter. De typen komen uit het pakket
github.com/gagliardetto/solana-go/sysvar
(geïmporteerd als sysvar), en getters retourneren pointers (of slices) die je
kunt aanpassen en weer aan de setter kunt doorgeven.
Klokmanipulatie
WarpToSlot
func (s *LiteSVM) WarpToSlot(slot uint64) error
Zet de interne klok vooruit naar een specifiek slot.
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
// sysvar is github.com/gagliardetto/solana-go/sysvar
type Clock struct {
Slot uint64
EpochStartTimestamp int64
Epoch uint64
LeaderScheduleEpoch uint64
UnixTimestamp int64
}
func (s *LiteSVM) Clock() (*sysvar.Clock, error)
Lees de Clock-sysvar uit.
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 *sysvar.Clock) error
Overschrijf de Clock-sysvar. Lees deze uit, pas de velden aan en schrijf hem terug wanneer je
controle nodig hebt over Epoch of UnixTimestamp (die WarpToSlot niet bijwerkt).
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 werkt alleen Slot bij. Gebruik SetClock wanneer je programma
Epoch of UnixTimestamp uit de Clock-sysvar leest.
Rent
Rent / SetRent
type Rent struct {
LamportsPerByte uint64
ExemptionThreshold float64
BurnPercent uint8
}
func (s *LiteSVM) Rent() (*sysvar.Rent, error)
func (s *LiteSVM) SetRent(r *sysvar.Rent) error
r, err := svm.Rent()
if err != nil {
t.Fatal(err)
}
t.Logf("lamports/byte: %d", r.LamportsPerByte)
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() (*sysvar.EpochSchedule, error)
func (s *LiteSVM) SetEpochSchedule(e *sysvar.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
TotalPoints bin.Uint128 // u128 total_points
TotalRewards uint64
DistributedRewards uint64
Active bool
}
func (s *LiteSVM) EpochRewards() (*sysvar.EpochRewards, error)
func (s *LiteSVM) SetEpochRewards(e *sysvar.EpochRewards) error
total_points is aan de Solana-kant een u128 en wordt beschikbaar gesteld als een
bin.Uint128 (uit github.com/gagliardetto/binary) met een Lo- en een Hi-helft.
LastRestartSlot / SetLastRestartSlot
type LastRestartSlot struct {
LastRestartSlot uint64
}
func (s *LiteSVM) LastRestartSlot() (*sysvar.LastRestartSlot, error)
func (s *LiteSVM) SetLastRestartSlot(l *sysvar.LastRestartSlot) error
last, err := svm.LastRestartSlot()
if err != nil {
t.Fatal(err)
}
if err := svm.SetLastRestartSlot(&sysvar.LastRestartSlot{LastRestartSlot: last.LastRestartSlot + 1}); err != nil {
t.Fatal(err)
}
SlotHashes / SetSlotHashes
type SlotHash struct {
Slot uint64
Hash solana.Hash
}
type SlotHashes []SlotHash
func (s *LiteSVM) SlotHashes() (sysvar.SlotHashes, error)
func (s *LiteSVM) SetSlotHashes(items sysvar.SlotHashes) error
SlotHistory
SlotHistory is een bitvec van ~128 KB. sysvar.SlotHistory is een gewone Go-waarde, dus
er hoeft niets afgesloten te worden.
sh := sysvar.NewSlotHistory()
sh.Add(42)
switch sh.Check(42) {
case sysvar.SlotHistoryFound:
// ...
case sysvar.SlotHistoryNotFound:
// ...
case sysvar.SlotHistoryTooOld:
// ...
case sysvar.SlotHistoryFuture:
// ...
}
if err := svm.SetSlotHistory(sh); err != nil {
t.Fatal(err)
}
Stake History
type StakeHistoryEntry struct {
Effective uint64
Activating uint64
Deactivating uint64
}
type StakeHistoryItem struct {
Epoch uint64
Entry StakeHistoryEntry
}
type StakeHistory []StakeHistoryItem
func (s *LiteSVM) StakeHistory() (sysvar.StakeHistory, error)
func (s *LiteSVM) SetStakeHistory(items sysvar.StakeHistory) error
Tijdafhankelijke logica testen
Gebruik WarpToSlot voor eenvoudig vooruitzetten op basis van slots en SetClock voor volledige controle:
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 ...
}