Skip to content

Commit a964552

Browse files
committed
fix: Checkpoint lookup can return 0 voting weight
1 parent 2ad5093 commit a964552

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

solana/programs/staking/src/state/checkpoints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ pub fn find_checkpoint_le(
311311
let checkpoint = read_checkpoint_at_index(account_info, mid)?;
312312

313313
if checkpoint.timestamp <= target_timestamp {
314+
result = Some((mid, checkpoint));
314315
low = mid + 1;
315316
} else {
316317
high = mid;

solana/programs/staking/src/state/vote_weight_window_lengths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub fn find_window_length_le(
150150
let window_length = read_window_length_at_index(account_info, mid)?;
151151

152152
if window_length.timestamp <= target_timestamp {
153+
result = Some((mid, window_length));
153154
low = mid + 1;
154155
} else {
155156
high = mid;

solana/tests/api_test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,47 @@ describe("api", async () => {
967967
assert.equal(abstainVotes.toString(), "22");
968968
});
969969

970+
it("should cast vote with the correct weight", async () => {
971+
let stakeAccountCheckpointsAddress;
972+
let proposalIdInput;
973+
974+
// Create 6 checkpoints, 1 second apart
975+
for (let i = 0; i < 6; i++) {
976+
stakeAccountCheckpointsAddress = await user3StakeConnection.delegate(
977+
user3,
978+
WHTokenBalance.fromString("50"),
979+
);
980+
981+
// Create a proposal with a start time 10 seconds in the future in iteration 5
982+
// We do this because the vote weight window is 10 seconds
983+
if (i == 4) {
984+
proposalIdInput = await addTestProposal(
985+
user3StakeConnection,
986+
Math.floor(Date.now() / 1000) + 10,
987+
);
988+
}
989+
990+
await sleep(1000);
991+
}
992+
993+
await user3StakeConnection.castVote(
994+
proposalIdInput,
995+
new BN(10),
996+
new BN(20),
997+
new BN(12),
998+
0,
999+
);
1000+
1001+
const { proposalId, againstVotes, forVotes, abstainVotes } =
1002+
await user3StakeConnection.proposalVotes(proposalIdInput);
1003+
1004+
assert.equal(proposalId.toString("hex"), proposalIdInput.toString("hex"));
1005+
assert.equal(againstVotes.toString(), "10");
1006+
assert.equal(forVotes.toString(), "20");
1007+
assert.equal(abstainVotes.toString(), "12");
1008+
1009+
});
1010+
9701011
it("should fail to castVote if next voter checkpoints are invalid", async () => {
9711012
await sleep(1000);
9721013
await user4StakeConnection.delegate(

0 commit comments

Comments
 (0)