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 24, 2024
1 parent b5c85e2 commit a3eec72
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ incremented upon a breaking change and the patch version will be incremented for

## [dev] - Unreleased

**Added**

- add/ add support for Clock sysvar manipulations with the client(i.e. warp to slot/epoch and forward in time) ([217](https://github.com/Ackee-Blockchain/trident/pull/217))

## [0.8.0] - 2024-10-21

Expand Down
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 by the desired number of seconds
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 a3eec72

Please sign in to comment.