Skip to content

Commit

Permalink
Fix math and id() check for the ns (#5)
Browse files Browse the repository at this point in the history
* Fix math overflow

* Add id() check
  • Loading branch information
nothing0012 authored May 27, 2024
1 parent d45a2a2 commit 6b70325
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 22 deletions.
7 changes: 6 additions & 1 deletion programs/vetoken/src/ins_v1/init_proposal.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
errors::CustomError,
id,
states::{Namespace, Proposal},
};
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -30,6 +31,7 @@ pub struct InitProposal<'info> {
#[account(
mut,
has_one=review_council,
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,

Expand All @@ -51,6 +53,9 @@ pub fn handle<'info>(
proposal.owner = ctx.accounts.review_council.key();
proposal.nonce = ns.proposal_nonce;

ns.proposal_nonce += 1;
ns.proposal_nonce = ns
.proposal_nonce
.checked_add(1)
.expect("should not overflow");
Ok(())
}
12 changes: 10 additions & 2 deletions programs/vetoken/src/ins_v1/stake.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
errors::CustomError,
id,
states::{Lockup, Namespace},
};
use anchor_lang::{prelude::*, AnchorDeserialize};
Expand Down Expand Up @@ -54,6 +55,7 @@ pub struct Stake<'info> {
#[account(
mut,
has_one = token_mint,
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,

Expand Down Expand Up @@ -94,10 +96,16 @@ pub fn handle<'info>(ctx: Context<'_, '_, '_, 'info, Stake<'info>>, args: StakeA
}

lockup.ns = ns.key();
lockup.amount += args.amount;
lockup.owner = ctx.accounts.owner.key();

ns.lockup_amount += args.amount;
lockup.amount = lockup
.amount
.checked_add(args.amount)
.expect("should not overflow");
ns.lockup_amount = ns
.lockup_amount
.checked_add(args.amount)
.expect("should not overflow");

Ok(())
}
7 changes: 6 additions & 1 deletion programs/vetoken/src/ins_v1/stake_to.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
errors::CustomError,
id,
states::{Lockup, Namespace},
};
use anchor_lang::{prelude::*, AnchorDeserialize};
Expand Down Expand Up @@ -61,6 +62,7 @@ pub struct StakeTo<'info> {
mut,
has_one = token_mint,
has_one = security_council,
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,

Expand Down Expand Up @@ -101,7 +103,10 @@ pub fn handle<'info>(
false => ns.lockup_default_target_rewards_bp,
};

ns.lockup_amount += args.amount;
ns.lockup_amount = ns
.lockup_amount
.checked_add(args.amount)
.expect("should not overflow");

Ok(())
}
9 changes: 7 additions & 2 deletions programs/vetoken/src/ins_v1/unstake.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
errors::CustomError,
lockup_seeds,
id, lockup_seeds,
states::{Lockup, Namespace},
};
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -45,6 +45,7 @@ pub struct Unstake<'info> {
#[account(
mut,
has_one = token_mint,
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,

Expand Down Expand Up @@ -85,8 +86,12 @@ pub fn handle<'info>(ctx: Context<'_, '_, '_, 'info, Unstake<'info>>) -> Result<
))?;
}

ns.lockup_amount -= amount;
lockup.amount = 0;

ns.lockup_amount = ns
.lockup_amount
.checked_sub(amount)
.expect("underflow in reducing ns.lockup_amount");

Ok(())
}
3 changes: 2 additions & 1 deletion programs/vetoken/src/ins_v1/update_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::states::Namespace;
use crate::{id, states::Namespace};
use anchor_lang::prelude::*;

#[derive(AnchorSerialize, AnchorDeserialize)]
Expand All @@ -25,6 +25,7 @@ pub struct UpdateNamespace<'info> {
#[account(
mut,
has_one = security_council,
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,
}
Expand Down
6 changes: 5 additions & 1 deletion programs/vetoken/src/ins_v1/update_proposal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::states::{Namespace, Proposal};
use crate::{
id,
states::{Namespace, Proposal},
};
use anchor_lang::prelude::*;

#[derive(AnchorSerialize, AnchorDeserialize)]
Expand All @@ -22,6 +25,7 @@ pub struct UpdateProposal<'info> {

#[account(
has_one=review_council,
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,
}
Expand Down
5 changes: 4 additions & 1 deletion programs/vetoken/src/ins_v1/vote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
errors::CustomError,
id,
states::{Lockup, Namespace, Proposal, VoteRecord},
};
use anchor_lang::prelude::*;
Expand Down Expand Up @@ -40,7 +41,9 @@ pub struct Vote<'info> {
)]
vote_record: Box<Account<'info, VoteRecord>>,

#[account()]
#[account(
constraint = *ns.to_account_info().owner == id(),
)]
ns: Box<Account<'info, Namespace>>,

system_program: Program<'info, System>,
Expand Down
70 changes: 57 additions & 13 deletions programs/vetoken/src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ impl Lockup {
// it's not used in this program, but will be consumed by other programs
#[allow(dead_code)]
pub fn rewards_power(&self, ns: &Namespace) -> u64 {
self.voting_power(ns) * (self.target_rewards_bp as u64) / 10000
self.voting_power(ns)
.checked_mul(self.target_rewards_bp as u64)
.expect("should not overflow")
.checked_div(10000)
.expect("should not overflow")
}
}

Expand Down Expand Up @@ -143,12 +147,42 @@ impl Proposal {

pub fn cast_vote(&mut self, choice: u8, voting_power: u64) {
match choice {
0 => self.num_choice_0 += voting_power,
1 => self.num_choice_1 += voting_power,
2 => self.num_choice_2 += voting_power,
3 => self.num_choice_3 += voting_power,
4 => self.num_choice_4 += voting_power,
5 => self.num_choice_5 += voting_power,
0 => {
self.num_choice_0 = self
.num_choice_0
.checked_add(voting_power)
.expect("should not overflow")
}
1 => {
self.num_choice_1 = self
.num_choice_1
.checked_add(voting_power)
.expect("should not overflow")
}
2 => {
self.num_choice_2 = self
.num_choice_2
.checked_add(voting_power)
.expect("should not overflow")
}
3 => {
self.num_choice_3 = self
.num_choice_3
.checked_add(voting_power)
.expect("should not overflow")
}
4 => {
self.num_choice_4 = self
.num_choice_4
.checked_add(voting_power)
.expect("should not overflow")
}
5 => {
self.num_choice_5 = self
.num_choice_5
.checked_add(voting_power)
.expect("should not overflow")
}
_ => panic!("Invalid choice"),
}
}
Expand Down Expand Up @@ -177,11 +211,16 @@ impl Proposal {

pub fn total_votes(&self) -> u64 {
self.num_choice_0
+ self.num_choice_1
+ self.num_choice_2
+ self.num_choice_3
+ self.num_choice_4
+ self.num_choice_5
.checked_add(self.num_choice_1)
.expect("should not overflow")
.checked_add(self.num_choice_2)
.expect("should not overflow")
.checked_add(self.num_choice_3)
.expect("should not overflow")
.checked_add(self.num_choice_4)
.expect("should not overflow")
.checked_add(self.num_choice_5)
.expect("should not overflow")
}

pub fn has_quorum(&self, ns: &Namespace) -> bool {
Expand All @@ -192,7 +231,12 @@ impl Proposal {
if !self.has_quorum(ns) {
return false;
}
let pass_threshold = self.total_votes() * (ns.proposal_min_pass_bp as u64) / 10000;
let pass_threshold = self
.total_votes()
.checked_mul(ns.proposal_min_pass_bp as u64)
.expect("should not overflow")
.checked_div(10000)
.expect("should not overflow");
self.num_choice_0 > pass_threshold
|| self.num_choice_1 > pass_threshold
|| self.num_choice_2 > pass_threshold
Expand Down

0 comments on commit 6b70325

Please sign in to comment.