Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split accounts test #258

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions staking/programs/staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ pub enum ErrorCode {
SplitZeroTokens,
#[msg("Can't split more tokens than are in the account")] // 6032
SplitTooManyTokens,
#[msg("Sanity check failed")] // 6033
SanityCheckFailed,
#[msg("Other")] //6034
Other,
#[msg("Can't split a token account with staking positions. Unstake your tokens first.")]
// 6035
// 6033
SplitWithStake,
#[msg("Sanity check failed")] // 6034
SanityCheckFailed,
#[msg("Other")] //6035
Other,
}
2 changes: 1 addition & 1 deletion staking/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ pub mod staking {
// This check allows us to create an empty positions account on behalf of the recipient and
// not worry about moving positions from the source account to the new account.
require!(
source_stake_account_positions.num_positions()? == 0,
source_stake_account_metadata.next_index == 0,
ErrorCode::SplitWithStake
);

Expand Down
11 changes: 0 additions & 11 deletions staking/programs/staking/src/state/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ impl PositionData {
}
Ok(exposure)
}

pub fn num_positions(&self) -> Result<usize> {
let mut count: usize = 0;
for i in 0..MAX_POSITIONS {
if let Some(_p) = self.read_position(i)? {
count += 1;
}
}
Ok(count)
}
}

pub trait TryBorsh {
Expand Down Expand Up @@ -448,7 +438,6 @@ pub mod tests {
}
}


#[quickcheck]
fn prop(input: Vec<DataOperation>) -> bool {
let mut position_data = PositionData::default();
Expand Down
45 changes: 36 additions & 9 deletions staking/programs/staking/src/state/vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,20 @@ pub mod tests {
);
}

const START_TIMESTAMP: i64 = 10;
const PERIOD_DURATION: u64 = 5;
const NUM_PERIODS: u64 = 4;

#[quickcheck]
fn test_split_with_args(transferred: u64, total: u64, initial_balance: u64) -> TestResult {
fn test_split_props(transferred: u64, total: u64, initial_balance: u64) -> TestResult {
if transferred > total || total == 0 {
return TestResult::discard();
}
let received = total - transferred;

let schedule = VestingSchedule::FullyVested;
let (remaining_schedule, transferred_schedule) = schedule
.split_vesting_schedule(total - transferred, transferred, total)
.split_vesting_schedule(received, transferred, total)
.unwrap();

assert_eq!(remaining_schedule, VestingSchedule::FullyVested);
Expand All @@ -657,12 +662,12 @@ pub mod tests {
let schedule = PeriodicVesting {
initial_balance,
// all of these fields should be preserved in the result
start_date: 203,
period_duration: 100,
num_periods: 12,
start_date: START_TIMESTAMP,
period_duration: PERIOD_DURATION,
num_periods: NUM_PERIODS,
};
let (remaining_schedule, transferred_schedule) = schedule
.split_vesting_schedule(total - transferred, transferred, total)
.split_vesting_schedule(received, transferred, total)
.unwrap();

match (remaining_schedule, transferred_schedule) {
Expand All @@ -685,11 +690,11 @@ pub mod tests {
let schedule = PeriodicVestingAfterListing {
initial_balance,
// all of these fields should be preserved in the result
period_duration: 100,
num_periods: 12,
period_duration: PERIOD_DURATION,
num_periods: NUM_PERIODS,
};
let (remaining_schedule, transferred_schedule) = schedule
.split_vesting_schedule(total - transferred, transferred, total)
.split_vesting_schedule(received, transferred, total)
.unwrap();

match (remaining_schedule, transferred_schedule) {
Expand All @@ -709,6 +714,28 @@ pub mod tests {
}
}

for timestamp in 0..(START_TIMESTAMP + (PERIOD_DURATION * NUM_PERIODS + 1) as i64) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could change the increment of this for loop to be PERIOD_DURATION and then it would run fast for big values of PERIOD_DURATION

let initial_unvested = schedule
.get_unvested_balance(timestamp, Some(START_TIMESTAMP))
.unwrap();
let remaining_unvested = remaining_schedule
.get_unvested_balance(timestamp, Some(START_TIMESTAMP))
.unwrap();
let transferred_unvested = transferred_schedule
.get_unvested_balance(timestamp, Some(START_TIMESTAMP))
.unwrap();

assert!(
initial_unvested.saturating_sub(2) <= (remaining_unvested + transferred_unvested)
&& (remaining_unvested + transferred_unvested) <= initial_unvested
);

if initial_unvested <= total {
assert!(transferred_unvested <= transferred);
assert!(remaining_unvested <= received);
}
}

TestResult::passed()
}

Expand Down
Loading