From 1b6262a1c3b2c96e50a4b0082642df93d6b45745 Mon Sep 17 00:00:00 2001 From: geminilclaw Date: Sun, 8 Mar 2026 18:21:38 +0800 Subject: [PATCH] feat: fix voting program bugs (poll_account mut and time checks) --- .../anchor/programs/voting/src/lib.rs | 19 +++++++++++++++++++ .../anchor/programs/voting/src/lib.rs | 1 + 2 files changed, 20 insertions(+) diff --git a/project-13-getting-to-production/anchor/programs/voting/src/lib.rs b/project-13-getting-to-production/anchor/programs/voting/src/lib.rs index 9e8c1ec..611384e 100644 --- a/project-13-getting-to-production/anchor/programs/voting/src/lib.rs +++ b/project-13-getting-to-production/anchor/programs/voting/src/lib.rs @@ -35,6 +35,17 @@ pub mod voting { pub fn vote(ctx: Context, _candidate_name: String, _poll_id: u64) -> Result<()> { let candidate = &mut ctx.accounts.candidate; + let poll = &ctx.accounts.poll; + let current_time = Clock::get()?.unix_timestamp; + + if current_time < (poll.poll_start as i64) { + return Err(ErrorCode::VotingNotStarted.into()); + } + + if current_time > (poll.poll_end as i64) { + return Err(ErrorCode::VotingEnded.into()); + } + candidate.candidate_votes += 1; msg!("Voted for candidate: {}", candidate.candidate_name); msg!("Votes: {}", candidate.candidate_votes); @@ -120,4 +131,12 @@ pub struct Poll { pub poll_start: u64, pub poll_end: u64, pub candidate_amount: u64, +} + +#[error_code] +pub enum ErrorCode { + #[msg("Voting has not started yet")] + VotingNotStarted, + #[msg("Voting has ended")] + VotingEnded, } \ No newline at end of file diff --git a/project-2-voting/anchor/programs/voting/src/lib.rs b/project-2-voting/anchor/programs/voting/src/lib.rs index 396c9bd..5520d88 100644 --- a/project-2-voting/anchor/programs/voting/src/lib.rs +++ b/project-2-voting/anchor/programs/voting/src/lib.rs @@ -70,6 +70,7 @@ pub struct InitializeCandidate<'info> { #[account(mut)] pub signer: Signer<'info>, + #[account(mut)] pub poll_account: Account<'info, PollAccount>, #[account(