Skip to content

Commit

Permalink
Drop set_time_offset() instruction
Browse files Browse the repository at this point in the history
The tests can now directly modify the Clock sysvar.
  • Loading branch information
ckamm committed Dec 10, 2023
1 parent 9698998 commit e90cc9b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 67 deletions.
2 changes: 0 additions & 2 deletions programs/voter-stake-registry/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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;
25 changes: 0 additions & 25 deletions programs/voter-stake-registry/src/instructions/set_time_offset.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct UpdateMaxVoteWeight<'info> {
pub fn update_max_vote_weight(ctx: Context<UpdateMaxVoteWeight>) -> 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(())
}
4 changes: 0 additions & 4 deletions programs/voter-stake-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SetTimeOffset>, time_offset: i64) -> Result<()> {
instructions::set_time_offset(ctx, time_offset)
}
}
41 changes: 16 additions & 25 deletions programs/voter-stake-registry/tests/program_test/addin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::RefCell;
use std::sync::Arc;

use solana_sdk::pubkey::Pubkey;
Expand All @@ -13,6 +14,7 @@ use crate::*;
pub struct AddinCookie {
pub solana: Arc<solana::SolanaCookie>,
pub program_id: Pubkey,
pub time_offset: RefCell<i64>,
}

pub struct RegistrarCookie {
Expand Down Expand Up @@ -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::<solana_program::clock::Clock>()
.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);
}
}

Expand Down
1 change: 1 addition & 0 deletions programs/voter-stake-registry/tests/program_test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl TestContext {
addin: AddinCookie {
solana: solana.clone(),
program_id: addin_program_id,
time_offset: RefCell::new(0),
},
mints,
users,
Expand Down
6 changes: 2 additions & 4 deletions programs/voter-stake-registry/tests/test_internal_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_stake_registry::state::Voter>(voter)
.await;
Expand Down Expand Up @@ -108,8 +107,7 @@ async fn test_internal_transfer() -> Result<(), TransportError> {
*time_offset.borrow_mut() += extra as i64;
addin.set_time_offset(&registrar, &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;
Expand Down
6 changes: 2 additions & 4 deletions programs/voter-stake-registry/tests/test_reset_lockup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_stake_registry::state::Voter>(voter)
.await;
Expand Down Expand Up @@ -109,8 +108,7 @@ async fn test_reset_lockup() -> Result<(), TransportError> {
*time_offset.borrow_mut() += extra as i64;
addin.set_time_offset(&registrar, &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;
Expand Down

0 comments on commit e90cc9b

Please sign in to comment.