From a3eec720c6fb7ffa955328d80fa989902e7be5f2 Mon Sep 17 00:00:00 2001 From: lukacan Date: Tue, 22 Oct 2024 23:05:56 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20Clock=20Sysva?= =?UTF-8?q?r=20manipulation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +++ crates/fuzz/src/fuzz_client.rs | 9 ++++++++ .../fuzz/src/program_test_client_blocking.rs | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f74c5a37a..a6bacccd0 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 6fe11334f..3106b2286 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 227f81d79..fc9f57ca5 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); + } }