Skip to content

Commit

Permalink
call it call_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed May 6, 2024
1 parent 49068dc commit c901128
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
26 changes: 9 additions & 17 deletions src/e2e_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use governance::factory::{
};
use governance::factory_test::{deploy as deploy_factory};
use governance::governor::{Config as GovernorConfig};
use governance::governor::{Governor, Governor::{to_call_id}};
use governance::governor::{Governor};
use governance::governor::{IGovernorDispatcherTrait};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::staker::{Staker, IStakerDispatcherTrait};
Expand Down Expand Up @@ -76,11 +76,12 @@ impl SetupTraitImpl of SetupTrait {
set_contract_address(address_before);
}

fn create_proposal_from(self: @Setup, delegate: ContractAddress, call: Call) {
fn create_proposal_from(self: @Setup, delegate: ContractAddress, call: Call) -> felt252 {
let address_before = get_contract_address();
set_contract_address(delegate);
(*self.deployment.governor).propose(call);
let id = (*self.deployment.governor).propose(call);
set_contract_address(address_before);
id
}

fn vote_from(self: @Setup, delegate: ContractAddress, id: felt252, yea: bool) {
Expand All @@ -107,28 +108,17 @@ fn test_create_proposal_that_fails() {
s.delegate_amount(delegate_no, delegated_amount + 1);

advance_time(s.deployment.governor.get_config().voting_weight_smoothing_duration);
s
let id = s
.create_proposal_from(
delegate_yes,
Call { to: delegate_yes, selector: 'wont-succeed', calldata: array![].span() }
);

let id = to_call_id(
@Call { to: delegate_yes, selector: 'wont-succeed', calldata: array![].span() }
);

advance_time(s.deployment.governor.get_config().voting_start_delay);
s.vote_from(delegate_yes, id, true);
s.vote_from(delegate_no, id, false);

let proposal_info = s
.deployment
.governor
.get_proposal(
to_call_id(
@Call { to: delegate_yes, selector: 'wont-succeed', calldata: array![].span() }
)
);
let proposal_info = s.deployment.governor.get_proposal(id);

assert_eq!(proposal_info.proposer, delegate_yes);
assert_eq!(
Expand All @@ -147,5 +137,7 @@ fn test_create_proposal_that_fails() {
s
.deployment
.governor
.execute(id, Call { to: delegate_yes, selector: 'wont-succeed', calldata: array![].span() });
.execute(
id, Call { to: delegate_yes, selector: 'wont-succeed', calldata: array![].span() }
);
}
12 changes: 6 additions & 6 deletions src/governor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet::{ContractAddress, storage_access::{StorePacking}};
#[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)]
pub struct ProposalInfo {
// the ID of the call that this proposal represents
pub call_id: felt252,
pub call_hash: felt252,
// the address of the proposer
pub proposer: ContractAddress,
// the execution state of the proposal
Expand Down Expand Up @@ -150,9 +150,9 @@ pub mod Governor {
.finalize()
}

pub fn to_call_id(call: @Call) -> felt252 {
pub fn hash_call(call: @Call) -> felt252 {
PoseidonTrait::new()
.update(selector!("governance::governor::Governor::to_call_id"))
.update(selector!("governance::governor::Governor::hash_call"))
.update_with(call)
.finalize()
}
Expand Down Expand Up @@ -199,7 +199,7 @@ pub mod Governor {
.write(
id,
ProposalInfo {
call_id: to_call_id(@call),
call_hash: hash_call(@call),
proposer,
execution_state: ExecutionState {
created: timestamp_current,
Expand Down Expand Up @@ -317,12 +317,12 @@ pub mod Governor {
}

fn execute(ref self: ContractState, id: felt252, call: Call) -> Span<felt252> {
let call_id = to_call_id(@call);
let call_hash = hash_call(@call);

let config = self.config.read();
let mut proposal = self.proposals.read(id);

assert(proposal.call_id == call_id, 'CALL_ID_MISMATCH');
assert(proposal.call_hash == call_hash, 'CALL_HASH_MISMATCH');
assert(proposal.proposer.is_non_zero(), 'DOES_NOT_EXIST');
assert(proposal.execution_state.executed.is_zero(), 'ALREADY_EXECUTED');
assert(proposal.execution_state.canceled.is_zero(), 'PROPOSAL_CANCELED');
Expand Down
19 changes: 10 additions & 9 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use governance::call_trait::{CallTrait};
use governance::execution_state::{ExecutionState};
use governance::governor::{
IGovernorDispatcher, IGovernorDispatcherTrait, Governor, Config, ProposalInfo,
Governor::{to_call_id}
Governor::{hash_call}
};
use governance::interfaces::erc20::{IERC20Dispatcher, IERC20DispatcherTrait};
use governance::staker::{IStakerDispatcher, IStakerDispatcherTrait};
Expand Down Expand Up @@ -108,16 +108,16 @@ fn create_proposal_with_call(
}

#[test]
fn test_to_call_id() {
fn test_hash_call() {
assert_eq!(
to_call_id(
hash_call(
@Call {
to: contract_address_const::<'to'>(),
selector: 'selector',
calldata: array![1, 2, 3].span()
}
),
1066148822741379800704886069792510413495941578585564382784326423848733446336
1740338028730353233444923211377046042821284356597274888496499093775825440467
);
}

Expand Down Expand Up @@ -147,7 +147,7 @@ fn test_propose() {
assert_eq!(
proposal,
ProposalInfo {
call_id: to_call_id(@transfer_call(token, recipient(), amount: 100)),
call_hash: hash_call(@transfer_call(token, recipient(), amount: 100)),
proposer: proposer(),
execution_state: ExecutionState {
created: config.voting_weight_smoothing_duration, executed: 0, canceled: 0
Expand Down Expand Up @@ -218,9 +218,10 @@ fn test_proposer_can_cancel_and_propose_different() {

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

let id_1 = create_proposal(governor, token, staker);
advance_time(config.voting_start_delay + config.voting_period);
let id_2 = create_proposal(governor, token, staker);
assert_ne!(id_1, id_2);
}
Expand Down Expand Up @@ -465,7 +466,7 @@ fn test_cancel_by_proposer() {
assert_eq!(
proposal,
ProposalInfo {
call_id: to_call_id(@transfer_call(token, recipient(), amount: 100)),
call_hash: hash_call(@transfer_call(token, recipient(), amount: 100)),
proposer: proposer(),
execution_state: ExecutionState {
created: config.voting_weight_smoothing_duration,
Expand Down Expand Up @@ -513,7 +514,7 @@ fn test_cancel_by_non_proposer() {
assert_eq!(
proposal,
ProposalInfo {
call_id: to_call_id(@transfer_call(token, recipient(), amount: 100)),
call_hash: hash_call(@transfer_call(token, recipient(), amount: 100)),
proposer: proposer(),
execution_state: ExecutionState {
created: config.voting_weight_smoothing_duration,
Expand Down Expand Up @@ -747,7 +748,7 @@ fn test_execute_already_executed_should_fail() {
}

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

Expand Down

0 comments on commit c901128

Please sign in to comment.