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

[solana] Add timestamp check in CastVote instruction #221

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions solana/programs/staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub enum ErrorCode {
ExceedsMaxAllowableVoteWeightWindowLength,
#[msg("Invalid next voter checkpoints")]
InvalidNextVoterCheckpoints,
#[msg("Proposal inactive")]
ProposalInactive,
#[msg("Other")]
Other,
}
Expand Down
5 changes: 5 additions & 0 deletions solana/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,12 @@ pub mod staking {
let proposal = &mut ctx.accounts.proposal;
let config = &ctx.accounts.config;

let current_timestamp: u64 = utils::clock::get_current_time().try_into()?;
let vote_start = proposal.vote_start;
require!(
current_timestamp > vote_start,
ErrorCode::ProposalInactive
);

let (_, window_length) = find_window_length_le(
&ctx.accounts.vote_weight_window_lengths.to_account_info(),
Expand Down
57 changes: 55 additions & 2 deletions solana/tests/api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -876,17 +876,66 @@ describe("api", async () => {
});

describe("castVote", () => {
it("should fail to castVote if proposal inactive", async () => {
await user6StakeConnection.delegate(
user6,
WHTokenBalance.fromString("50"),
);

let proposalIdInput = await addTestProposal(
user6StakeConnection,
Math.floor(Date.now() / 1000) + 20,
);

let stakeAccountMetadataAddress =
await user6StakeConnection.getStakeMetadataAddress(
user6StakeConnection.userPublicKey(),
);
let previousStakeAccountCheckpointsAddress =
await user6StakeConnection.getStakeAccountCheckpointsAddressByMetadata(
stakeAccountMetadataAddress,
false,
);

const { proposalAccount } =
await user6StakeConnection.fetchProposalAccount(proposalIdInput);

try {
await user6StakeConnection.program.methods
.castVote(
Array.from(proposalIdInput),
new BN(10),
new BN(20),
new BN(12),
0,
)
.accountsPartial({
proposal: proposalAccount,
voterCheckpoints: previousStakeAccountCheckpointsAddress,
voterCheckpointsNext: null,
})
.rpc();

assert.fail("Expected an error but none was thrown");
} catch (e) {
assert(
(e as AnchorError).error?.errorCode?.code === "ProposalInactive",
);
}
});

it("should fail to castVote if votes were added in the voteWeightWindow", async () => {
await user6StakeConnection.delegate(
user6,
WHTokenBalance.fromString("150"),
WHTokenBalance.fromString("100"),
);

// voteWeightWindow is 10s
let proposalIdInput = await addTestProposal(
user6StakeConnection,
Math.floor(Date.now() / 1000) + 3,
);
await sleep(3000);

let stakeAccountMetadataAddress =
await user6StakeConnection.getStakeMetadataAddress(
Expand Down Expand Up @@ -935,6 +984,7 @@ describe("api", async () => {
user3StakeConnection,
Math.floor(Date.now() / 1000) + 12,
);
await sleep(12000);

await user3StakeConnection.castVote(
proposalIdInput,
Expand Down Expand Up @@ -979,6 +1029,7 @@ describe("api", async () => {
user4StakeConnection,
voteStart,
);

const { proposalAccount } =
await user4StakeConnection.fetchProposalAccount(proposalIdInput);

Expand All @@ -990,6 +1041,7 @@ describe("api", async () => {
WHTokenBalance.fromString("5"),
);
}
await sleep(4000);

let currentStakeAccountCheckpointsAddress =
await user4StakeConnection.getStakeAccountCheckpointsAddress(
Expand Down Expand Up @@ -1054,8 +1106,9 @@ describe("api", async () => {

let proposalIdInput = await addTestProposal(
user4StakeConnection,
Math.floor(Date.now() / 1000) + 12,
Math.floor(Date.now() / 1000) + 11,
);
await sleep(11000);

const { proposalAccount } =
await user4StakeConnection.fetchProposalAccount(proposalIdInput);
Expand Down
Loading