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

Integrate cooldown #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ near view $ASTRA_ID get_policy
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down
6 changes: 6 additions & 0 deletions astra/TestPlan.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Confirm the default policy acts as it should.
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down Expand Up @@ -194,6 +195,7 @@ Each
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down Expand Up @@ -253,6 +255,7 @@ Each
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down Expand Up @@ -312,6 +315,7 @@ Each
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down Expand Up @@ -371,6 +375,7 @@ Each
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down Expand Up @@ -446,6 +451,7 @@ Each group council can have different threshold criteria for consensus. Confirm
},
"proposal_bond": "1000000000000000000000000",
"proposal_period": "604800000000000",
"cooldown": "0",
"bounty_bond": "1000000000000000000000000",
"bounty_forgiveness_period": "86400000000000"
}
Expand Down
28 changes: 25 additions & 3 deletions astra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ mod tests {
use near_sdk::{testing_env, VMContext};
use near_units::parse_near;

use crate::proposals::ProposalStatus;
use crate::proposals::{ProposalStatus, PolicyParameters};
use crate::test_utils::*;

use super::*;
Expand Down Expand Up @@ -322,6 +322,7 @@ mod tests {
default_vote_policy: VotePolicy::default(),
proposal_bond: U128(10u128.pow(24)),
proposal_period: U64::from(1_000_000_000 * 60 * 60 * 24 * 7),
cooldown: U64::from(0),
bounty_bond: U128(10u128.pow(24)),
bounty_forgiveness_period: U64::from(1_000_000_000 * 60 * 60 * 24),
}
Expand Down Expand Up @@ -354,7 +355,7 @@ mod tests {
ndc_trust()
);
let id = create_proposal(&mut context, &mut contract);
return (context.build(), contract, id)
(context.build(), contract, id)
}

#[test]
Expand Down Expand Up @@ -519,6 +520,27 @@ mod tests {
contract.act_proposal(id, Action::Execute, None, None);
}

#[test]
#[should_panic(expected = "ERR_PROPOSAL_COOLDOWN_LEFT")]
fn test_cooldown() {
let (_, mut contract, id) = setup_for_proposals();
let mut policy = contract.policy.get().unwrap().to_policy();
policy.update_parameters(&PolicyParameters{
cooldown: Some(U64::from(1_000_000_000 * 60 * 60)), proposal_bond: None,
proposal_period: None, bounty_bond: None,
bounty_forgiveness_period: None
});

contract.act_proposal(id, Action::VoteApprove, None, None);
// verify proposal wasn't executed during final vote
assert_eq!(
contract.get_proposal(id).proposal.status,
ProposalStatus::Approved
);

contract.act_proposal(id, Action::Execute, None, None);
}

#[test]
#[should_panic(expected = "ERR_INVALID_POLICY")]
fn test_fails_adding_invalid_policy() {
Expand Down Expand Up @@ -635,7 +657,7 @@ mod tests {
assert!(!res.roles.is_empty());

context.predecessor_account_id = acc_voting_body();
testing_env!(context.clone());
testing_env!(context);
contract.dissolve_hook();
res = contract.policy.get().unwrap().to_policy();
assert!(res.roles.is_empty());
Expand Down
7 changes: 7 additions & 0 deletions astra/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ pub struct Policy {
pub proposal_bond: U128,
/// Expiration period for proposals.
pub proposal_period: U64,
/// Proposal can be executed only after cooldown period is complete( in milliseconds )
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
pub cooldown: U64,
/// Bond for claiming a bounty.
pub bounty_bond: U128,
/// Period in which giving up on bounty is not punished.
Expand Down Expand Up @@ -204,6 +206,7 @@ pub fn default_policy(council: Vec<AccountId>) -> Policy {
default_vote_policy: VotePolicy::default(),
proposal_bond: U128(10u128.pow(24)),
proposal_period: U64::from(1_000_000_000 * 60 * 60 * 24 * 7),
cooldown: U64::from(0),
bounty_bond: U128(10u128.pow(24)),
bounty_forgiveness_period: U64::from(1_000_000_000 * 60 * 60 * 24),
}
Expand Down Expand Up @@ -274,6 +277,9 @@ impl Policy {
if parameters.proposal_period.is_some() {
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
self.proposal_period = parameters.proposal_period.unwrap();
}
if parameters.cooldown.is_some() {
self.cooldown = parameters.cooldown.unwrap();
}
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
if parameters.bounty_bond.is_some() {
self.bounty_bond = parameters.bounty_bond.unwrap();
}
Expand Down Expand Up @@ -616,6 +622,7 @@ mod tests {
proposal_bond: Some(U128(10u128.pow(26))),
proposal_period: None,
bounty_bond: None,
cooldown: None,
bounty_forgiveness_period: Some(U64::from(1_000_000_000 * 60 * 60 * 24 * 5)),
};
policy.update_parameters(&new_parameters);
Expand Down
15 changes: 14 additions & 1 deletion astra/src/proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct ActionCall {
pub struct PolicyParameters {
pub proposal_bond: Option<U128>,
pub proposal_period: Option<U64>,
pub cooldown: Option<U64>,
pub bounty_bond: Option<U128>,
pub bounty_forgiveness_period: Option<U64>,
}
Expand Down Expand Up @@ -545,9 +546,10 @@ impl Contract {
if self.status == ContractStatus::Dissolved {
panic!("Cannot perform this action, dao is dissolved!")
}
let execute = !skip_execution.unwrap_or(false);
let mut proposal: Proposal = self.proposals.get(&id).expect("ERR_NO_PROPOSAL").into();
let policy = self.policy.get().unwrap().to_policy();

let execute = !skip_execution.unwrap_or(false) && self.assert_cooldown(proposal.submission_time, policy.cooldown);
// Check permissions for the given action.
let (roles, allowed) =
policy.can_execute_action(self.internal_user_info(), &proposal.kind, &action);
Expand Down Expand Up @@ -600,6 +602,7 @@ impl Contract {
// - if the number of votes in the group has changed (new members has been added) -
// the proposal can loose it's approved state. In this case new proposal needs to be made, this one can only expire.
Action::Finalize => {
require!(self.assert_cooldown(proposal.submission_time, policy.cooldown), "ERR_PROPOSAL_COOLDOWN_LEFT");
proposal.status = policy.proposal_status(
&proposal,
policy.roles.iter().map(|r| r.name.clone()).collect(),
Expand All @@ -622,6 +625,7 @@ impl Contract {
Action::MoveToHub => false,
Action::Execute => {
require!(proposal.status != ProposalStatus::Executed, "ERR_PROPOSAL_ALREADY_EXECUTED");
require!(self.assert_cooldown(proposal.submission_time, policy.cooldown), "ERR_PROPOSAL_COOLDOWN_LEFT");
// recompute status to check if the proposal is not expired.
proposal.status = policy.proposal_status(&proposal, roles, self.total_delegation_amount);
require!(proposal.status == ProposalStatus::Approved, "ERR_PROPOSAL_NOT_APPROVED");
Expand Down Expand Up @@ -668,4 +672,13 @@ impl Contract {
.insert(&proposal_id, &VersionedProposal::Default(proposal));
result
}

/// Returns true if cooldown is over else false
#[private]
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
pub fn assert_cooldown(&mut self, submission_time: U64, cooldown: U64) -> bool {
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
if env::block_timestamp_ms() > cooldown.0 + submission_time.0 {
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
false
}
}
1 change: 1 addition & 0 deletions astra/tests/test_general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ async fn test_multi_council() -> anyhow::Result<()> {
default_vote_policy: VotePolicy::default(),
proposal_bond: U128(10u128.pow(24)),
proposal_period: U64::from(1_000_000_000 * 60 * 60 * 24 * 7),
cooldown: U64::from(0),
bounty_bond: U128(10u128.pow(24)),
bounty_forgiveness_period: U64::from(1_000_000_000 * 60 * 60 * 24),
};
Expand Down
Loading