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

fix: Checkpoint lookup can return 0 voting weight #223

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
1 change: 1 addition & 0 deletions solana/programs/staking/src/state/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ pub fn find_checkpoint_le(
let checkpoint = read_checkpoint_at_index(account_info, mid)?;

if checkpoint.timestamp <= target_timestamp {
result = Some((mid, checkpoint));
low = mid + 1;
} else {
high = mid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub fn find_window_length_le(
let window_length = read_window_length_at_index(account_info, mid)?;

if window_length.timestamp <= target_timestamp {
result = Some((mid, window_length));
low = mid + 1;
} else {
high = mid;
Expand Down
53 changes: 53 additions & 0 deletions solana/tests/api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ describe("api", async () => {
let user4StakeConnection: StakeConnection;
let user5StakeConnection: StakeConnection;
let user6StakeConnection: StakeConnection;
let user7StakeConnection: StakeConnection;

let controller;
let owner;
let user2;
let user3;
let user4;
let user6;
let user7;
let delegate;

const confirm = async (signature: string): Promise<string> => {
Expand Down Expand Up @@ -163,6 +165,16 @@ describe("api", async () => {
WHTokenBalance.fromString("1000"),
);
user6 = user6StakeConnection.provider.wallet.publicKey;

user7StakeConnection = await newUserStakeConnection(
stakeConnection,
Keypair.generate(),
config,
whMintAccount,
whMintAuthority,
WHTokenBalance.fromString("1000"),
);
user7 = user7StakeConnection.provider.wallet.publicKey;
});

it("postSignatures", async () => {
Expand Down Expand Up @@ -967,6 +979,47 @@ describe("api", async () => {
assert.equal(abstainVotes.toString(), "22");
});

it("should cast vote with the correct weight", async () => {
let stakeAccountCheckpointsAddress;
let proposalIdInput;

// Create 6 checkpoints, 1 second apart
for (let i = 0; i < 6; i++) {
stakeAccountCheckpointsAddress = await user7StakeConnection.delegate(
user7,
WHTokenBalance.fromString("50"),
);

// Create a proposal with a start time 10 seconds in the future in iteration 5
// We do this because the vote weight window is 10 seconds
if (i == 4) {
proposalIdInput = await addTestProposal(
user7StakeConnection,
Math.floor(Date.now() / 1000) + 10,
);
}

await sleep(1000);
}

await user7StakeConnection.castVote(
proposalIdInput,
new BN(10),
new BN(20),
new BN(12),
0,
);

const { proposalId, againstVotes, forVotes, abstainVotes } =
await user7StakeConnection.proposalVotes(proposalIdInput);

assert.equal(proposalId.toString("hex"), proposalIdInput.toString("hex"));
assert.equal(againstVotes.toString(), "10");
assert.equal(forVotes.toString(), "20");
assert.equal(abstainVotes.toString(), "12");

});

it("should fail to castVote if next voter checkpoints are invalid", async () => {
await sleep(1000);
await user4StakeConnection.delegate(
Expand Down
Loading