From e90cc9b4633e7e6b897bf72cdae4078dd595d076 Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Sat, 9 Dec 2023 17:39:53 +0100 Subject: [PATCH] Drop set_time_offset() instruction The tests can now directly modify the Clock sysvar. --- .../src/instructions/mod.rs | 2 - .../src/instructions/set_time_offset.rs | 25 ----------- .../instructions/update_max_vote_weight.rs | 6 +-- programs/voter-stake-registry/src/lib.rs | 4 -- .../tests/program_test/addin.rs | 41 ++++++++----------- .../tests/program_test/mod.rs | 1 + .../tests/test_internal_transfer.rs | 6 +-- .../tests/test_reset_lockup.rs | 6 +-- 8 files changed, 24 insertions(+), 67 deletions(-) delete mode 100644 programs/voter-stake-registry/src/instructions/set_time_offset.rs diff --git a/programs/voter-stake-registry/src/instructions/mod.rs b/programs/voter-stake-registry/src/instructions/mod.rs index ff21e3d2..519f057e 100644 --- a/programs/voter-stake-registry/src/instructions/mod.rs +++ b/programs/voter-stake-registry/src/instructions/mod.rs @@ -11,7 +11,6 @@ pub use internal_transfer_locked::*; pub use internal_transfer_unlocked::*; pub use log_voter_info::*; pub use reset_lockup::*; -pub use set_time_offset::*; pub use update_max_vote_weight::*; pub use update_voter_weight_record::*; pub use withdraw::*; @@ -29,7 +28,6 @@ mod internal_transfer_locked; mod internal_transfer_unlocked; mod log_voter_info; mod reset_lockup; -mod set_time_offset; mod update_max_vote_weight; mod update_voter_weight_record; mod withdraw; diff --git a/programs/voter-stake-registry/src/instructions/set_time_offset.rs b/programs/voter-stake-registry/src/instructions/set_time_offset.rs deleted file mode 100644 index fc9f978a..00000000 --- a/programs/voter-stake-registry/src/instructions/set_time_offset.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::error::*; -use crate::state::*; -use anchor_lang::prelude::*; -use std::str::FromStr; - -#[derive(Accounts)] -#[instruction(time_offset: i64)] -pub struct SetTimeOffset<'info> { - #[account(mut, has_one = realm_authority)] - pub registrar: AccountLoader<'info, Registrar>, - pub realm_authority: Signer<'info>, -} - -/// A debug-only instruction that advances the time. -pub fn set_time_offset(ctx: Context, time_offset: i64) -> Result<()> { - let allowed_program = Pubkey::from_str("GovernanceProgramTest1111111111111111111111").unwrap(); - let registrar = &mut ctx.accounts.registrar.load_mut()?; - require_keys_eq!( - registrar.governance_program_id, - allowed_program, - VsrError::DebugInstruction - ); - registrar.time_offset = time_offset; - Ok(()) -} diff --git a/programs/voter-stake-registry/src/instructions/update_max_vote_weight.rs b/programs/voter-stake-registry/src/instructions/update_max_vote_weight.rs index 71b6606c..cf751fe9 100644 --- a/programs/voter-stake-registry/src/instructions/update_max_vote_weight.rs +++ b/programs/voter-stake-registry/src/instructions/update_max_vote_weight.rs @@ -21,8 +21,8 @@ pub struct UpdateMaxVoteWeight<'info> { pub fn update_max_vote_weight(ctx: Context) -> Result<()> { let registrar = &ctx.accounts.registrar.load()?; let _max_vote_weight = registrar.max_vote_weight(ctx.remaining_accounts)?; - // TODO: SPL governance has not yet implemented this feature. - // When it has, probably need to write the result into an account, - // similar to VoterWeightRecord. + + // TODO: Unfinished! Currently this does not work as an max vote weight plugin! + Ok(()) } diff --git a/programs/voter-stake-registry/src/lib.rs b/programs/voter-stake-registry/src/lib.rs index d929071e..8ff8262b 100644 --- a/programs/voter-stake-registry/src/lib.rs +++ b/programs/voter-stake-registry/src/lib.rs @@ -220,8 +220,4 @@ pub mod voter_stake_registry { ) -> Result<()> { instructions::log_voter_info(ctx, deposit_entry_begin, deposit_entry_count) } - - pub fn set_time_offset(ctx: Context, time_offset: i64) -> Result<()> { - instructions::set_time_offset(ctx, time_offset) - } } diff --git a/programs/voter-stake-registry/tests/program_test/addin.rs b/programs/voter-stake-registry/tests/program_test/addin.rs index 8057d983..e43b2bb1 100644 --- a/programs/voter-stake-registry/tests/program_test/addin.rs +++ b/programs/voter-stake-registry/tests/program_test/addin.rs @@ -1,3 +1,4 @@ +use std::cell::RefCell; use std::sync::Arc; use solana_sdk::pubkey::Pubkey; @@ -13,6 +14,7 @@ use crate::*; pub struct AddinCookie { pub solana: Arc, pub program_id: Pubkey, + pub time_offset: RefCell, } pub struct RegistrarCookie { @@ -815,36 +817,25 @@ impl AddinCookie { #[allow(dead_code)] pub async fn set_time_offset( &self, - registrar: &RegistrarCookie, - authority: &Keypair, + _registrar: &RegistrarCookie, + _authority: &Keypair, time_offset: i64, ) { - let data = - anchor_lang::InstructionData::data(&voter_stake_registry::instruction::SetTimeOffset { - time_offset, - }); - - let accounts = anchor_lang::ToAccountMetas::to_account_metas( - &voter_stake_registry::accounts::SetTimeOffset { - registrar: registrar.address, - realm_authority: authority.pubkey(), - }, - None, - ); - - let instructions = vec![Instruction { - program_id: self.program_id, - accounts, - data, - }]; - - // clone the secrets - let signer = Keypair::from_base58_string(&authority.to_base58_string()); + let old_offset = *self.time_offset.borrow(); + *self.time_offset.borrow_mut() = time_offset; - self.solana - .process_transaction(&instructions, Some(&[&signer])) + let old_clock = self + .solana + .context + .borrow_mut() + .banks_client + .get_sysvar::() .await .unwrap(); + + let mut new_clock = old_clock.clone(); + new_clock.unix_timestamp += time_offset - old_offset; + self.solana.context.borrow_mut().set_sysvar(&new_clock); } } diff --git a/programs/voter-stake-registry/tests/program_test/mod.rs b/programs/voter-stake-registry/tests/program_test/mod.rs index 8b268369..cd849633 100644 --- a/programs/voter-stake-registry/tests/program_test/mod.rs +++ b/programs/voter-stake-registry/tests/program_test/mod.rs @@ -228,6 +228,7 @@ impl TestContext { addin: AddinCookie { solana: solana.clone(), program_id: addin_program_id, + time_offset: RefCell::new(0), }, mints, users, diff --git a/programs/voter-stake-registry/tests/test_internal_transfer.rs b/programs/voter-stake-registry/tests/test_internal_transfer.rs index a5fed160..d3e6f944 100644 --- a/programs/voter-stake-registry/tests/test_internal_transfer.rs +++ b/programs/voter-stake-registry/tests/test_internal_transfer.rs @@ -12,9 +12,8 @@ async fn get_lockup_data( solana: &SolanaCookie, voter: Pubkey, index: u8, - time_offset: i64, ) -> (u64, u64, u64, u64, u64) { - let now = solana.get_clock().await.unix_timestamp + time_offset; + let now = solana.get_clock().await.unix_timestamp; let voter = solana .get_account::(voter) .await; @@ -108,8 +107,7 @@ async fn test_internal_transfer() -> Result<(), TransportError> { *time_offset.borrow_mut() += extra as i64; addin.set_time_offset(®istrar, &realm_authority, *time_offset.borrow()) }; - let lockup_status = - |index: u8| get_lockup_data(&context.solana, voter.address, index, *time_offset.borrow()); + let lockup_status = |index: u8| get_lockup_data(&context.solana, voter.address, index); let month = LockupKind::Monthly.period_secs(); let day = 24 * 60 * 60; diff --git a/programs/voter-stake-registry/tests/test_reset_lockup.rs b/programs/voter-stake-registry/tests/test_reset_lockup.rs index 1eb94de2..d2c25bbd 100644 --- a/programs/voter-stake-registry/tests/test_reset_lockup.rs +++ b/programs/voter-stake-registry/tests/test_reset_lockup.rs @@ -12,9 +12,8 @@ async fn get_lockup_data( solana: &SolanaCookie, voter: Pubkey, index: u8, - time_offset: i64, ) -> (u64, u64, u64, u64, u64) { - let now = solana.get_clock().await.unix_timestamp + time_offset; + let now = solana.get_clock().await.unix_timestamp; let voter = solana .get_account::(voter) .await; @@ -109,8 +108,7 @@ async fn test_reset_lockup() -> Result<(), TransportError> { *time_offset.borrow_mut() += extra as i64; addin.set_time_offset(®istrar, &realm_authority, *time_offset.borrow()) }; - let lockup_status = - |index: u8| get_lockup_data(&context.solana, voter.address, index, *time_offset.borrow()); + let lockup_status = |index: u8| get_lockup_data(&context.solana, voter.address, index); let month = LockupKind::Monthly.period_secs(); let day = 24 * 60 * 60;