Skip to content

Commit

Permalink
✨ Add support for Clock Sysvar manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukacan committed Oct 22, 2024
1 parent b5c85e2 commit 11f4fac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/fuzz/src/fuzz_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ use crate::error::*;

/// A trait providing methods to read and write (manipulate) accounts
pub trait FuzzClient {
/// Warp to specific epoch
fn warp_to_epoch(&mut self, warp_epoch: u64);

/// Warp to specific slot
fn warp_to_slot(&mut self, warp_slot: u64);

/// Forward in time
fn forward_in_time(&mut self, seconds: i64) -> Result<(), FuzzClientError>;

/// Create an empty account and add lamports to it
fn set_account(&mut self, lamports: u64) -> Keypair;

Expand Down
23 changes: 23 additions & 0 deletions crates/fuzz/src/program_test_client_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use solana_program_test::ProgramTest;
use solana_program_test::ProgramTestContext;
use solana_sdk::account::Account;
use solana_sdk::account_info::AccountInfo;
use solana_sdk::clock::Clock;
use solana_sdk::entrypoint::ProgramResult;
use solana_sdk::system_program::ID as SYSTEM_PROGRAM_ID;
use solana_sdk::{
Expand Down Expand Up @@ -243,4 +244,26 @@ impl FuzzClient for ProgramTestClientBlocking {
fn get_rent(&mut self) -> Result<Rent, FuzzClientError> {
Ok(self.rt.block_on(self.ctx.banks_client.get_rent())?)
}
fn forward_in_time(&mut self, seconds: i64) -> Result<(), FuzzClientError> {
// Get the current clock state from the program test context.
let mut clock = self
.rt
.block_on(self.ctx.banks_client.get_sysvar::<Clock>())?;

// Calculate the new timestamp after advancing time.
let new_timestamp = clock.unix_timestamp.saturating_add(seconds);

// Update the Clock instance with the new timestamp.
clock.unix_timestamp = new_timestamp;

// Update the sysvar in the program test context with the new Clock state.
self.ctx.set_sysvar(&clock);
Ok(())
}
fn warp_to_slot(&mut self, warp_slot: u64) {
let _ = self.ctx.warp_to_slot(warp_slot);
}
fn warp_to_epoch(&mut self, warp_epoch: u64) {
let _ = self.ctx.warp_to_epoch(warp_epoch);
}
}

0 comments on commit 11f4fac

Please sign in to comment.