Skip to content

Commit 9529138

Browse files
committed
fix: Checkpoint lookup can return 0 voting weight
1 parent 98fbba2 commit 9529138

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-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

+53
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ describe("api", async () => {
7878
let user4StakeConnection: StakeConnection;
7979
let user5StakeConnection: StakeConnection;
8080
let user6StakeConnection: StakeConnection;
81+
let user7StakeConnection: StakeConnection;
8182

8283
let controller;
8384
let owner;
8485
let user2;
8586
let user3;
8687
let user4;
8788
let user6;
89+
let user7;
8890
let delegate;
8991

9092
const confirm = async (signature: string): Promise<string> => {
@@ -163,6 +165,16 @@ describe("api", async () => {
163165
WHTokenBalance.fromString("1000"),
164166
);
165167
user6 = user6StakeConnection.provider.wallet.publicKey;
168+
169+
user7StakeConnection = await newUserStakeConnection(
170+
stakeConnection,
171+
Keypair.generate(),
172+
config,
173+
whMintAccount,
174+
whMintAuthority,
175+
WHTokenBalance.fromString("1000"),
176+
);
177+
user7 = user7StakeConnection.provider.wallet.publicKey;
166178
});
167179

168180
it("postSignatures", async () => {
@@ -967,6 +979,47 @@ describe("api", async () => {
967979
assert.equal(abstainVotes.toString(), "22");
968980
});
969981

982+
it("should cast vote with the correct weight", async () => {
983+
let stakeAccountCheckpointsAddress;
984+
let proposalIdInput;
985+
986+
// Create 6 checkpoints, 1 second apart
987+
for (let i = 0; i < 6; i++) {
988+
stakeAccountCheckpointsAddress = await user7StakeConnection.delegate(
989+
user7,
990+
WHTokenBalance.fromString("50"),
991+
);
992+
993+
// Create a proposal with a start time 10 seconds in the future in iteration 5
994+
// We do this because the vote weight window is 10 seconds
995+
if (i == 4) {
996+
proposalIdInput = await addTestProposal(
997+
user7StakeConnection,
998+
Math.floor(Date.now() / 1000) + 10,
999+
);
1000+
}
1001+
1002+
await sleep(1000);
1003+
}
1004+
1005+
await user7StakeConnection.castVote(
1006+
proposalIdInput,
1007+
new BN(10),
1008+
new BN(20),
1009+
new BN(12),
1010+
0,
1011+
);
1012+
1013+
const { proposalId, againstVotes, forVotes, abstainVotes } =
1014+
await user7StakeConnection.proposalVotes(proposalIdInput);
1015+
1016+
assert.equal(proposalId.toString("hex"), proposalIdInput.toString("hex"));
1017+
assert.equal(againstVotes.toString(), "10");
1018+
assert.equal(forVotes.toString(), "20");
1019+
assert.equal(abstainVotes.toString(), "12");
1020+
1021+
});
1022+
9701023
it("should fail to castVote if next voter checkpoints are invalid", async () => {
9711024
await sleep(1000);
9721025
await user4StakeConnection.delegate(

0 commit comments

Comments
 (0)