Skip to content

Commit

Permalink
QoL API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Jun 25, 2024
1 parent e64c3cf commit 6b11325
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 58 deletions.
47 changes: 3 additions & 44 deletions harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod sysvar;
use {
crate::{
result::{Check, InstructionResult},
sysvar::MolluskSysvars,
sysvar::Sysvars,
},
solana_program_runtime::{
compute_budget::ComputeBudget, invoke_context::InvokeContext,
Expand All @@ -44,17 +44,11 @@ use {
},
solana_sdk::{
account::AccountSharedData,
clock::Clock,
epoch_rewards::EpochRewards,
epoch_schedule::EpochSchedule,
feature_set::FeatureSet,
hash::Hash,
instruction::Instruction,
pubkey::Pubkey,
rent::Rent,
slot_hashes::SlotHashes,
stake_history::StakeHistory,
sysvar::last_restart_slot::LastRestartSlot,
transaction_context::{InstructionAccount, TransactionContext},
},
std::sync::Arc,
Expand All @@ -73,7 +67,7 @@ pub struct Mollusk {
pub program_account: AccountSharedData,
pub program_cache: LoadedProgramsForTxBatch,
pub program_id: Pubkey,
pub sysvars: MolluskSysvars,
pub sysvars: Sysvars,
}

impl Default for Mollusk {
Expand All @@ -91,7 +85,7 @@ impl Default for Mollusk {
program_account,
program_cache: program::default_program_cache(),
program_id,
sysvars: MolluskSysvars::default(),
sysvars: Sysvars::default(),
}
}
}
Expand Down Expand Up @@ -136,41 +130,6 @@ impl Mollusk {
);
}

/// Get the `Clock` sysvar.
pub fn get_clock(&self) -> &Clock {
&self.sysvars.clock
}

/// Get the `EpochRewards` sysvar.
pub fn get_epoch_rewards(&self) -> &EpochRewards {
&self.sysvars.epoch_rewards
}

/// Get the `EpochSchedule` sysvar.
pub fn get_epoch_schedule(&self) -> &EpochSchedule {
&self.sysvars.epoch_schedule
}

/// Get the `LastRestartSlot` sysvar.
pub fn get_last_restart_slot(&self) -> &LastRestartSlot {
&self.sysvars.last_restart_slot
}

/// Get the `Rent` sysvar.
pub fn get_rent(&self) -> &Rent {
&self.sysvars.rent
}

/// Get the `SlotHashes` sysvar.
pub fn get_slot_hashes(&self) -> &SlotHashes {
&self.sysvars.slot_hashes
}

/// Get the `StakeHistory` sysvar.
pub fn get_stake_history(&self) -> &StakeHistory {
&self.sysvars.stake_history
}

/// Warp the test environment to a slot by updating sysvars.
pub fn warp_to_slot(&mut self, slot: u64) {
self.sysvars.warp_to_slot(slot)
Expand Down
6 changes: 3 additions & 3 deletions harness/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl InstructionResult {
check_lamports, actual_lamports
);
}
if let Some(check_owner) = &account.check_owner {
if let Some(check_owner) = account.check_owner {
let actual_owner = resulting_account.owner();
assert_eq!(
check_owner, actual_owner,
Expand Down Expand Up @@ -200,7 +200,7 @@ struct AccountCheck<'a> {
pubkey: Pubkey,
check_data: Option<&'a [u8]>,
check_lamports: Option<u64>,
check_owner: Option<Pubkey>,
check_owner: Option<&'a Pubkey>,
check_state: Option<AccountStateCheck>,
}

Expand Down Expand Up @@ -242,7 +242,7 @@ impl<'a> AccountCheckBuilder<'a> {
self
}

pub fn owner(mut self, owner: Pubkey) -> Self {
pub fn owner(mut self, owner: &'a Pubkey) -> Self {
self.check.check_owner = Some(owner);
self
}
Expand Down
12 changes: 6 additions & 6 deletions harness/src/sysvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
// around it for modifying its contents.
/// Mollusk sysvars.
#[derive(Default)]
pub struct MolluskSysvars {
pub struct Sysvars {
pub clock: Clock,
pub epoch_rewards: EpochRewards,
pub epoch_schedule: EpochSchedule,
Expand All @@ -28,7 +28,7 @@ pub struct MolluskSysvars {
pub stake_history: StakeHistory,
}

impl MolluskSysvars {
impl Sysvars {
/// Warp the test environment to a slot by updating sysvars.
pub fn warp_to_slot(&mut self, slot: Slot) {
// First update `Clock`.
Expand All @@ -54,8 +54,8 @@ impl MolluskSysvars {
}
}

impl From<&MolluskSysvars> for SysvarCache {
fn from(mollusk_cache: &MolluskSysvars) -> Self {
impl From<&Sysvars> for SysvarCache {
fn from(mollusk_cache: &Sysvars) -> Self {
let mut sysvar_cache = SysvarCache::default();
sysvar_cache.fill_missing_entries(|pubkey, set_sysvar| {
if pubkey.eq(&Clock::id()) {
Expand Down Expand Up @@ -90,7 +90,7 @@ mod tests {

#[test]
fn test_warp_to_slot() {
let mut sysvars = MolluskSysvars::default();
let mut sysvars = Sysvars::default();
assert_eq!(sysvars.clock.slot, 0);

sysvars.warp_to_slot(200);
Expand Down Expand Up @@ -133,7 +133,7 @@ mod tests {
stake_history
};

let sysvars = MolluskSysvars {
let sysvars = Sysvars {
clock,
epoch_rewards,
epoch_schedule,
Expand Down
10 changes: 5 additions & 5 deletions harness/tests/bpf_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn test_write_data() {

let data = &[1, 2, 3, 4, 5];
let space = data.len();
let lamports = mollusk.get_rent().minimum_balance(space);
let lamports = mollusk.sysvars.rent.minimum_balance(space);

let key = Pubkey::new_unique();
let account = AccountSharedData::new(lamports, space, &program_id);
Expand Down Expand Up @@ -80,7 +80,7 @@ fn test_write_data() {
Check::account(&key)
.data(data)
.lamports(lamports)
.owner(program_id)
.owner(&program_id)
.build(),
],
);
Expand Down Expand Up @@ -230,7 +230,7 @@ fn test_close_account() {
Check::account(&key)
.data(&[])
.lamports(0)
.owner(system_program::id())
.owner(&system_program::id())
.closed()
.build(),
],
Expand All @@ -248,7 +248,7 @@ fn test_cpi() {

let data = &[1, 2, 3, 4, 5];
let space = data.len();
let lamports = mollusk.get_rent().minimum_balance(space);
let lamports = mollusk.sysvars.rent.minimum_balance(space);

let key = Pubkey::new_unique();
let account = AccountSharedData::new(lamports, space, &cpi_target_program_id);
Expand Down Expand Up @@ -362,7 +362,7 @@ fn test_cpi() {
Check::account(&key)
.data(data)
.lamports(lamports)
.owner(cpi_target_program_id)
.owner(&cpi_target_program_id)
.build(),
],
);
Expand Down

0 comments on commit 6b11325

Please sign in to comment.