Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
pr feedback: use rwlock in test rather than unsafe const modification
Browse files Browse the repository at this point in the history
  • Loading branch information
AshwinSekar committed Nov 27, 2024
1 parent 9174be4 commit 5210f9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions slashing/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ thiserror = "1.0"
spl-pod = { version = "0.5.0", path = "../../libraries/pod" }

[dev-dependencies]
lazy_static = "1.5.0"
solana-program-test = "2.1.0"
solana-sdk = "2.1.0"
solana-ledger = "2.1.0"
Expand Down
34 changes: 17 additions & 17 deletions slashing/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ mod tests {
signature::Keypair,
signer::Signer,
},
std::sync::Arc,
std::sync::{Arc, RwLock},
};

const SLOT: Slot = 53084024;
static mut CLOCK_SLOT: Slot = SLOT;
lazy_static::lazy_static! {
static ref CLOCK_SLOT: Arc<RwLock<Slot>> = Arc::new(RwLock::new(SLOT));
}

fn generate_proof_data(leader: Arc<Keypair>) -> Vec<u8> {
let mut rng = rand::thread_rng();
Expand All @@ -113,22 +115,20 @@ mod tests {

#[test]
fn test_statue_of_limitations() {
unsafe {
CLOCK_SLOT = SLOT + 5;
verify_with_clock().unwrap();
*CLOCK_SLOT.write().unwrap() = SLOT + 5;
verify_with_clock().unwrap();

CLOCK_SLOT = SLOT - 1;
assert_eq!(
verify_with_clock().unwrap_err(),
ProgramError::ArithmeticOverflow
);
*CLOCK_SLOT.write().unwrap() = SLOT - 1;
assert_eq!(
verify_with_clock().unwrap_err(),
ProgramError::ArithmeticOverflow
);

CLOCK_SLOT = SLOT + DEFAULT_SLOTS_PER_EPOCH + 1;
assert_eq!(
verify_with_clock().unwrap_err(),
SlashingError::ExceedsStatueOfLimitations.into()
);
}
*CLOCK_SLOT.write().unwrap() = SLOT + DEFAULT_SLOTS_PER_EPOCH + 1;
assert_eq!(
verify_with_clock().unwrap_err(),
SlashingError::ExceedsStatueOfLimitations.into()
);
}

fn verify_with_clock() -> Result<(), ProgramError> {
Expand All @@ -137,7 +137,7 @@ mod tests {
fn sol_get_clock_sysvar(&self, var_addr: *mut u8) -> u64 {
unsafe {
let clock = Clock {
slot: CLOCK_SLOT,
slot: *CLOCK_SLOT.read().unwrap(),
..Clock::default()
};
*(var_addr as *mut _ as *mut Clock) = clock;
Expand Down

0 comments on commit 5210f9c

Please sign in to comment.