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

# Tempo e Sysvars

> Metodi per manipolare il tempo e le variabili di sistema

## Manipolazione del tempo

### warp_to_slot

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

Salta a uno slot specifico. Aggiorna solo `Clock.slot`; l'epoch e
`unix_timestamp` restano invariati. L'uso previsto è l'avanzamento, ma i
salti all'indietro non vengono rifiutati.

```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);
```

### Manipolazione manuale del Clock

```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>
  Usa `warp_to_slot()` per la maggior parte delle manipolazioni del tempo. Modifica manualmente il clock
  solo quando hai bisogno di un controllo preciso su più campi.
</Info>

## Sysvars

### get_sysvar

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

Legge il valore di una 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>,
```

Scrive il valore di una 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);
```

## Sysvar comuni

### 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);
```

## Pattern di test basati sul tempo

### Testare le funzionalità basate sugli 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());
```

### Testare le funzionalità basate sul 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);
```

### Testare i piani di 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());
```

## Casi d'uso comuni

- Piani di vesting
- Prelievi con blocco temporale
- Periodi di cooldown per stake/unstake
- Orari di chiusura delle aste
- Periodi di noleggio
- Ricompense basate sulle epoch

## Note importanti

1. **Solo in avanti** - Per convenzione, avanza nel tempo. LiteSVM non rifiuta un
   warp all'indietro, ma i programmi che dipendono dal tempo potrebbero comportarsi in modo imprevisto
2. **Da slot a tempo** - Gli slot di Mainnet puntano a 200 ms (0,2 secondi); gli slot osservati
   durano leggermente di più. LiteSVM non lega gli slot al tempo reale
3. **Sysvar Clock** - `warp_to_slot()` aggiorna solo il campo `slot`. Imposta
   `unix_timestamp` ed `epoch` manualmente con `set_sysvar` quando il tuo programma
   li legge
4. **Nessun aggiornamento automatico** - A differenza dei validator reali, il tempo non avanza
   automaticamente tra una transazione e l'altra
