Skip to content

Commit 0b0cdfb

Browse files
authored
[solana] Skip last checkpoint in filled checkpoints account (#261)
* Fix cast_vote: skip last checkpoint in filled checkpoints account * Refactoring tests for castVote instruction * Add more tests for castVote instruction
1 parent 56eb4c9 commit 0b0cdfb

File tree

3 files changed

+1009
-698
lines changed

3 files changed

+1009
-698
lines changed

solana/programs/staking/src/lib.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,19 @@ pub mod staking {
561561
// 2. It breaks when a checkpoint's timestamp exceeds the `vote_start` timestamp.
562562
// This ensures that the loop will not run indefinitely
563563
loop {
564+
// We have to skip the last checkpoint in the filled checkpoints account.
565+
// Instead of the last checkpoint of a filled checkpoints account,
566+
// we should consider the first checkpoint of the next checkpoints account.
564567
if !reading_from_next_account
565-
&& (checkpoint_index as u32) == config.max_checkpoints_account_limit
568+
&& config.max_checkpoints_account_limit == (checkpoint_index as u32) + 1
566569
{
570+
let voter_checkpoints_loader = &ctx.accounts.voter_checkpoints;
571+
let voter_checkpoints_data = voter_checkpoints_loader.load()?;
572+
if checkpoint_index >= voter_checkpoints_data.next_index as usize {
573+
// If the checkpoint account is not completely filled, exit the loop
574+
break;
575+
}
576+
567577
// Switch to the next account
568578

569579
// Ensure the next voter checkpoints account exists
@@ -609,9 +619,7 @@ pub mod staking {
609619
(voter_checkpoints_loader, voter_checkpoints_data)
610620
};
611621

612-
let next_index = voter_checkpoints_data.next_index;
613-
614-
if checkpoint_index >= next_index as usize {
622+
if checkpoint_index >= voter_checkpoints_data.next_index as usize {
615623
// No more checkpoints in account
616624
break;
617625
}

0 commit comments

Comments
 (0)