Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Main

on:
push:
branches: [main,mollusk-tests-6-merge]
branches: [main,mollusk-tests-7-move-lamports]
pull_request:

env:
Expand Down
70 changes: 70 additions & 0 deletions program/tests/helpers/instruction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,73 @@ impl InstructionConfig for MergeConfig<'_> {
]
}
}

pub struct MoveLamportsConfig<'a> {
pub source: (&'a Pubkey, &'a AccountSharedData),
pub destination: (&'a Pubkey, &'a AccountSharedData),
pub amount: u64,
/// Override signer for testing wrong signer scenarios (defaults to ctx.staker)
pub override_signer: Option<&'a Pubkey>,
}

impl<'a> MoveLamportsConfig<'a> {
/// Helper to get the default source vote account from context
pub fn with_default_vote(self, ctx: &'a StakeTestContext) -> MoveLamportsFullConfig<'a> {
MoveLamportsFullConfig {
source: self.source,
destination: self.destination,
override_signer: self.override_signer,
amount: self.amount,
source_vote: (
ctx.vote_account.as_ref().expect("vote_account required"),
ctx.vote_account_data
.as_ref()
.expect("vote_account_data required"),
),
dest_vote: None,
}
}
}

impl InstructionConfig for MoveLamportsConfig<'_> {
fn build_instruction(&self, ctx: &StakeTestContext) -> Instruction {
let signer = self.override_signer.unwrap_or(&ctx.staker);
ixn::move_lamports(self.source.0, self.destination.0, signer, self.amount)
}

fn build_accounts(&self) -> Vec<(Pubkey, AccountSharedData)> {
vec![
(*self.source.0, self.source.1.clone()),
(*self.destination.0, self.destination.1.clone()),
]
}
}

pub struct MoveLamportsFullConfig<'a> {
pub source: (&'a Pubkey, &'a AccountSharedData),
pub destination: (&'a Pubkey, &'a AccountSharedData),
pub amount: u64,
/// Override signer for testing wrong signer scenarios (defaults to ctx.staker)
pub override_signer: Option<&'a Pubkey>,
pub source_vote: (&'a Pubkey, &'a AccountSharedData),
pub dest_vote: Option<(&'a Pubkey, &'a AccountSharedData)>,
}

impl InstructionConfig for MoveLamportsFullConfig<'_> {
fn build_instruction(&self, ctx: &StakeTestContext) -> Instruction {
let signer = self.override_signer.unwrap_or(&ctx.staker);
ixn::move_lamports(self.source.0, self.destination.0, signer, self.amount)
}

fn build_accounts(&self) -> Vec<(Pubkey, AccountSharedData)> {
let mut accounts = vec![
(*self.source.0, self.source.1.clone()),
(*self.destination.0, self.destination.1.clone()),
(*self.source_vote.0, self.source_vote.1.clone()),
];
if let Some((vote_pk, vote_acc)) = self.dest_vote {
accounts.push((*vote_pk, vote_acc.clone()));
}
accounts
}
}
39 changes: 39 additions & 0 deletions program/tests/helpers/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use {
crate::helpers::{lifecycle::StakeLifecycle, stake_tracker::StakeTracker},
mollusk_svm::Mollusk,
solana_account::{Account, AccountSharedData, ReadableAccount, WritableAccount},
solana_clock::Epoch,
Expand Down Expand Up @@ -126,3 +127,41 @@ pub fn get_effective_stake(mollusk: &Mollusk, stake_account: &AccountSharedData)
0
}
}

/// Synchronize a transient stake's epoch to the current epoch
/// Updates both the account data and the tracker.
pub fn true_up_transient_stake_epoch(
mollusk: &mut Mollusk,
tracker: &mut StakeTracker,
stake_pubkey: &Pubkey,
stake_account: &mut AccountSharedData,
lifecycle: StakeLifecycle,
) {
if lifecycle != StakeLifecycle::Activating && lifecycle != StakeLifecycle::Deactivating {
return;
}

let clock = mollusk.sysvars.clock.clone();
let mut stake_state: StakeStateV2 = bincode::deserialize(stake_account.data()).unwrap();

if let StakeStateV2::Stake(_, ref mut stake, _) = &mut stake_state {
match lifecycle {
StakeLifecycle::Activating => {
stake.delegation.activation_epoch = clock.epoch;

// Update tracker as well
if let Some(tracked) = tracker.delegations.get_mut(stake_pubkey) {
tracked.activation_epoch = clock.epoch;
}
}
StakeLifecycle::Deactivating => {
stake.delegation.deactivation_epoch = clock.epoch;

// Update tracker as well
tracker.track_deactivation(stake_pubkey, clock.epoch);
}
_ => (),
}
}
stake_account.set_data(bincode::serialize(&stake_state).unwrap());
}
Loading