Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support for Clock Sysvar manipulation #217

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading