Skip to content

Commit

Permalink
Merge branch 'main' into governor-account-contract
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem authored Jun 13, 2024
2 parents df27c62 + 231e070 commit 2e72ccc
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 104 deletions.
56 changes: 0 additions & 56 deletions src/governor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ pub trait IGovernor<TContractState> {
// Cancel the proposal with the given ID. Only callable by the proposer.
fn cancel(ref self: TContractState, id: felt252);

// Report a breach in the proposer's voting weight below the proposal creation threshold, canceling the proposal.
// The breach timestamp must be between when the proposal was created and the end of the voting period for the proposal.
// This method can be called by anyone at any time before the proposal is executed.
fn report_breach(ref self: TContractState, id: felt252, breach_timestamp: u64);

// Attaches the given text to the proposal. Simply emits an event containing the proposal description.
fn describe(ref self: TContractState, id: felt252, description: ByteArray);

Expand Down Expand Up @@ -140,12 +135,6 @@ pub mod Governor {
pub id: felt252,
}

#[derive(starknet::Event, Drop)]
pub struct CreationThresholdBreached {
pub id: felt252,
pub breach_timestamp: u64,
}

#[derive(starknet::Event, Drop, PartialEq, Debug)]
pub struct Executed {
pub id: felt252,
Expand All @@ -165,7 +154,6 @@ pub mod Governor {
Described: Described,
Voted: Voted,
Canceled: Canceled,
CreationThresholdBreached: CreationThresholdBreached,
Executed: Executed,
Reconfigured: Reconfigured,
}
Expand Down Expand Up @@ -355,50 +343,6 @@ pub mod Governor {
self.emit(Canceled { id });
}

fn report_breach(ref self: ContractState, id: felt252, breach_timestamp: u64) {
let (mut proposal, config) = self.get_proposal_with_config(id);

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

assert(proposal.execution_state.canceled.is_zero(), 'ALREADY_CANCELED');
assert(proposal.execution_state.executed.is_zero(), 'ALREADY_EXECUTED');
assert(breach_timestamp >= proposal.execution_state.created, 'PROPOSAL_NOT_CREATED');

assert(
breach_timestamp < (proposal.execution_state.created
+ config.voting_start_delay
+ config.voting_period),
'VOTING_ENDED'
);

// check that at the given timestamp, during the voting period and after the proposal was created, the proposer's delegation
// fell below the proposal creation threshold
assert(
self
.get_staker()
.get_average_delegated(
delegate: proposal.proposer,
start: breach_timestamp - config.voting_weight_smoothing_duration,
end: breach_timestamp
) < config
.proposal_creation_threshold,
'THRESHOLD_NOT_BREACHED'
);

proposal
.execution_state =
ExecutionState {
created: proposal.execution_state.created,
// we asserted that it is not already executed
executed: 0,
canceled: get_block_timestamp()
};

self.proposals.write(id, proposal);

self.emit(CreationThresholdBreached { id, breach_timestamp });
}

fn get_config(self: @ContractState) -> Config {
self.get_config_version(self.latest_config_version.read())
}
Expand Down
48 changes: 0 additions & 48 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -586,54 +586,6 @@ fn test_cancel_by_non_proposer() {
governor.cancel(id);
}

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

let id = create_proposal(governor, token, staker);
staker.withdraw_amount(proposer(), recipient: Zero::zero(), amount: 25);

// Fast forward one smoothing duration
advance_time(config.voting_weight_smoothing_duration);

// A random user can now cancel the proposal
set_contract_address(anyone());
governor.report_breach(id, get_block_timestamp());

// Expect that proposal is no longer available
let proposal = governor.get_proposal(id);
assert_eq!(
proposal,
ProposalInfo {
calls_hash: hash_calls(
@array![transfer_call(token: token, recipient: recipient(), amount: 100)].span()
),
proposer: proposer(),
execution_state: ExecutionState {
created: config.voting_weight_smoothing_duration,
executed: 0,
canceled: config.voting_weight_smoothing_duration * 2
},
yea: 0,
nay: 0,
config_version: 0,
}
);
}

#[test]
#[should_panic(expected: ('THRESHOLD_NOT_BREACHED', 'ENTRYPOINT_FAILED'))]
fn test_cancel_by_non_proposer_threshold_not_breached_should_fail() {
let (staker, token, governor, _config) = setup();

let id = create_proposal(governor, token, staker);

// A random user can't now cancel the proposal because
// the proposer's voting power is still above threshold
set_contract_address(anyone());
governor.report_breach(id, get_block_timestamp());
}

#[test]
#[should_panic(expected: ('VOTING_STARTED', 'ENTRYPOINT_FAILED'))]
fn test_cancel_after_voting_end_should_fail() {
Expand Down

0 comments on commit 2e72ccc

Please sign in to comment.