Skip to content

Commit

Permalink
Revert "instead of account contract, use a simulate function"
Browse files Browse the repository at this point in the history
This reverts commit bb592fe.
  • Loading branch information
moodysalem committed Jun 19, 2024
1 parent bb592fe commit 3a4ea49
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
32 changes: 19 additions & 13 deletions src/governor.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,9 @@ pub trait IGovernor<TContractState> {

// Replaces the code at this address. This must be self-called via a proposal.
fn upgrade(ref self: TContractState, class_hash: ClassHash);

// Can be called only via simulateTransaction to determine the effect of the governor making a set of calls
fn simulate(ref self: TContractState, calls: Span<Call>) -> Span<Span<felt252>>;
}

#[starknet::contract]
#[starknet::contract(account)]
pub mod Governor {
use core::hash::{HashStateTrait, HashStateExTrait};
use core::num::traits::zero::{Zero};
Expand All @@ -102,7 +99,7 @@ pub mod Governor {
use governance::staker::{IStakerDispatcherTrait};
use starknet::{
get_block_timestamp, get_caller_address, get_contract_address,
syscalls::{replace_class_syscall}, get_tx_info
syscalls::{replace_class_syscall}, AccountContract, get_tx_info
};
use super::{
IStakerDispatcher, ContractAddress, IGovernor, Config, ProposalInfo, Call, ExecutionState,
Expand Down Expand Up @@ -450,19 +447,28 @@ pub mod Governor {

replace_class_syscall(class_hash).unwrap();
}
}

fn simulate(ref self: ContractState, mut calls: Span<Call>) -> Span<Span<felt252>> {
let tx_version = get_tx_info().unbox().version;
assert(
tx_version == 0x100000000000000000000000000000001
|| tx_version == 0x100000000000000000000000000000003,
'Simulation only'
);
// This implementation exists solely for the purpose of allowing simulation of calls from the governor with the flag to skip validation
#[abi(embed_v0)]
impl GovernorAccountContractForSimulation of AccountContract<ContractState> {
fn __validate_declare__(self: @ContractState, class_hash: felt252) -> felt252 {
panic!("Not allowed");
0
}
fn __validate__(ref self: ContractState, calls: Array<Call>) -> felt252 {
panic!("Not allowed");
0
}
fn __execute__(ref self: ContractState, mut calls: Array<Call>) -> Array<Span<felt252>> {
assert(get_caller_address().is_zero(), 'Invalid caller');
let tx_version = get_tx_info().unbox().version.into();
assert(tx_version == 1 || tx_version == 3, 'Invalid TX version');
let mut results: Array<Span<felt252>> = array![];
while let Option::Some(call) = calls.pop_front() {
results.append(call.execute());
};
results.span()
results
}
}
}
32 changes: 26 additions & 6 deletions src/governor_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use starknet::account::{Call};
use starknet::{
get_contract_address, syscalls::deploy_syscall, ClassHash, contract_address_const,
ContractAddress, get_block_timestamp,
testing::{set_block_timestamp, set_contract_address, pop_log, set_version}
testing::{set_block_timestamp, set_contract_address, pop_log, set_version},
account::{AccountContractDispatcher, AccountContractDispatcherTrait}
};

fn recipient() -> ContractAddress {
Expand Down Expand Up @@ -1015,19 +1016,38 @@ fn test_reconfigure_fails_if_not_self_call() {
}

#[test]
#[should_panic(expected: ('Simulation only', 'ENTRYPOINT_FAILED'))]
fn test_governor_simulate_fails_tx_version() {
#[should_panic(expected: ("Not allowed", 'ENTRYPOINT_FAILED'))]
fn test_governor_validate_fails() {
let (_staker, _token, governor, _config) = setup();
governor.simulate(array![].span());
AccountContractDispatcher { contract_address: governor.contract_address }
.__validate__(array![]);
}

#[test]
#[should_panic(expected: ("Not allowed", 'ENTRYPOINT_FAILED'))]
fn test_governor_validate_declare_fails() {
let (_staker, _token, governor, _config) = setup();
AccountContractDispatcher { contract_address: governor.contract_address }
.__validate_declare__(123);
}

#[test]
#[should_panic(expected: ('Invalid caller', 'ENTRYPOINT_FAILED'))]
fn test_governor_execute_fails_from_non_zero() {
let (_staker, _token, governor, _config) = setup();
set_contract_address(contract_address_const::<1>());
AccountContractDispatcher { contract_address: governor.contract_address }.__execute__(array![]);
}

#[test]
#[should_panic(expected: ('Invalid TX version', 'ENTRYPOINT_FAILED'))]
fn test_governor_execute_fails_invalid_tx_version() {
let (_staker, _token, governor, _config) = setup();
set_version(0x100000000000000000000000000000001);
governor.simulate(array![].span());
set_version(0);
AccountContractDispatcher { contract_address: governor.contract_address }.__execute__(array![]);
}


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

0 comments on commit 3a4ea49

Please sign in to comment.