Skip to content

Commit

Permalink
add some unit tests for canceling proposals and fix a bug in cancel -…
Browse files Browse the repository at this point in the history
…> create
  • Loading branch information
moodysalem committed Mar 18, 2024
1 parent fa9e13a commit 20def98
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/governor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,16 @@ pub mod Governor {
let timestamp_current = get_block_timestamp();

let latest_proposal_id = self.latest_proposal_by_proposer.read(proposer);
assert(
latest_proposal_id.is_zero()
|| (self.get_proposal(latest_proposal_id).timestamps.created
if latest_proposal_id.is_non_zero() {
let latest_proposal_timestamps = self.get_proposal(latest_proposal_id).timestamps;

assert(
latest_proposal_timestamps.created
+ config.voting_start_delay
+ config.voting_period < timestamp_current),
'PROPOSER_HAS_ACTIVE_PROPOSAL'
);
+ config.voting_period < timestamp_current,
'PROPOSER_HAS_ACTIVE_PROPOSAL'
);
}

assert(
self
Expand Down Expand Up @@ -232,7 +235,7 @@ pub mod Governor {
fn cancel(ref self: ContractState, id: felt252) {
let config = self.config.read();
let voting_token = self.staker.read();
let mut proposal = self.proposals.read(id);
let proposal = self.proposals.read(id);

assert(proposal.proposer.is_non_zero(), 'DOES_NOT_EXIST');

Expand All @@ -258,17 +261,22 @@ pub mod Governor {
'VOTING_ENDED'
);

proposal =
ProposalInfo {
proposer: contract_address_const::<0>(),
timestamps: ProposalTimestamps { created: 0, executed: 0 },
yea: 0,
nay: 0
};
self
.proposals
.write(
id,
ProposalInfo {
proposer: contract_address_const::<0>(),
timestamps: ProposalTimestamps { created: 0, executed: 0 },
yea: 0,
nay: 0
}
);

self.proposals.write(id, proposal);
// allows the proposer to create a new proposal
self.latest_proposal_by_proposer.write(proposal.proposer, Zero::zero());

self.emit(Canceled { id, });
self.emit(Canceled { id });
}

fn execute(ref self: ContractState, call: Call) -> Span<felt252> {
Expand Down
40 changes: 40 additions & 0 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,46 @@ fn test_propose() {
);
}

#[test]
#[should_panic(expected: ('PROPOSER_HAS_ACTIVE_PROPOSAL', 'ENTRYPOINT_FAILED'))]
fn test_propose_has_active_proposal() {
let (staker, token, governor, config) = setup();

token.approve(staker.contract_address, config.proposal_creation_threshold.into());
staker.stake(proposer());
advance_time(config.voting_weight_smoothing_duration);

set_contract_address(proposer());
governor.propose(transfer_call(token, recipient(), amount: 100));
governor.propose(transfer_call(token, recipient(), amount: 101));
}

#[test]
fn test_proposer_can_cancel_and_re_propose() {
let (staker, token, governor, config) = setup();

token.approve(staker.contract_address, config.proposal_creation_threshold.into());
staker.stake(proposer());
advance_time(config.voting_weight_smoothing_duration);

set_contract_address(proposer());
let id_1 = governor.propose(transfer_call(token, recipient(), amount: 100));
governor.cancel(id_1);
let id_2 = governor.propose(transfer_call(token, recipient(), amount: 100));
assert_eq!(id_1, id_2);

assert_eq!(
governor.get_proposal(id_2),
ProposalInfo {
proposer: proposer(),
timestamps: ProposalTimestamps {
created: config.voting_weight_smoothing_duration, executed: 0
},
yea: 0,
nay: 0
}
);
}

#[test]
#[should_panic(expected: ('ALREADY_PROPOSED', 'ENTRYPOINT_FAILED'))]
Expand Down

0 comments on commit 20def98

Please sign in to comment.