diff --git a/CHANGELOG.md b/CHANGELOG.md index f74c5a37..a6bacccd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/fuzz/src/fuzz_client.rs b/crates/fuzz/src/fuzz_client.rs index 6fe11334..3106b228 100644 --- a/crates/fuzz/src/fuzz_client.rs +++ b/crates/fuzz/src/fuzz_client.rs @@ -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; diff --git a/crates/fuzz/src/program_test_client_blocking.rs b/crates/fuzz/src/program_test_client_blocking.rs index 227f81d7..fc9f57ca 100644 --- a/crates/fuzz/src/program_test_client_blocking.rs +++ b/crates/fuzz/src/program_test_client_blocking.rs @@ -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::{ @@ -243,4 +244,26 @@ impl FuzzClient for ProgramTestClientBlocking { fn get_rent(&mut self) -> Result { 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::())?; + + // 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); + } }