Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:snapshot-labs/sx-starknet into c…
Browse files Browse the repository at this point in the history
…hore_space_contract_parity
  • Loading branch information
pscott committed Sep 1, 2023
2 parents d4e7476 + 1c97753 commit cd8be1d
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 54 deletions.
5 changes: 5 additions & 0 deletions starknet/src/execution_strategies/simple_quorum.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ mod SimpleQuorumExecutionStrategy {
self._quorum.write(quorum);
}

#[internal]
fn quorum(self: @ContractState) -> u256 {
self._quorum.read()
}

#[internal]
fn get_proposal_status(
self: @ContractState,
Expand Down
13 changes: 12 additions & 1 deletion starknet/src/execution_strategies/vanilla.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
#[starknet::contract]
mod VanillaExecutionStrategy {
use sx::interfaces::IExecutionStrategy;
use sx::interfaces::{IExecutionStrategy, IQuorum};
use sx::types::{Proposal, ProposalStatus};
use sx::execution_strategies::simple_quorum::SimpleQuorumExecutionStrategy;


#[storage]
struct Storage {
_num_executed: felt252
}

#[external(v0)]
impl QuorumImpl of IQuorum<ContractState> {
fn quorum(self: @ContractState) -> u256 {
let mut state: SimpleQuorumExecutionStrategy::ContractState =
SimpleQuorumExecutionStrategy::unsafe_new_contract_state();

SimpleQuorumExecutionStrategy::quorum(@state)
}
}

#[external(v0)]
impl VanillaExecutionStrategy of IExecutionStrategy<ContractState> {
fn execute(
Expand Down
2 changes: 2 additions & 0 deletions starknet/src/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod i_voting_strategy;
mod i_execution_strategy;
mod i_proposal_validation_strategy;
mod i_account;
mod i_quorum;

use i_voting_strategy::{IVotingStrategy, IVotingStrategyDispatcher, IVotingStrategyDispatcherTrait};
use i_execution_strategy::{
Expand All @@ -15,3 +16,4 @@ use i_account::{
AccountABI, AccountABIDispatcher, AccountABIDispatcherTrait, AccountCamelABI,
AccountCamelABIDispatcher, AccountCamelABIDispatcherTrait
};
use i_quorum::{IQuorum, IQuorumDispatcher, IQuorumDispatcherTrait};
4 changes: 4 additions & 0 deletions starknet/src/interfaces/i_quorum.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[starknet::interface]
trait IQuorum<TContractState> {
fn quorum(self: @TContractState) -> u256;
}
49 changes: 25 additions & 24 deletions starknet/src/space/space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mod Space {
types::{
UserAddress, Choice, FinalizationStatus, Strategy, IndexedStrategy, Proposal,
PackedProposal, IndexedStrategyTrait, IndexedStrategyImpl, UpdateSettingsCalldata,
NoUpdateU32, NoUpdateStrategy, NoUpdateArray
NoUpdateTrait, NoUpdateString,
},
utils::{
reinitializable::{Reinitializable}, ReinitializableImpl, bits::BitSetter,
Expand Down Expand Up @@ -610,8 +610,7 @@ mod Space {
let _min_voting_duration = input.min_voting_duration;
let _max_voting_duration = input.max_voting_duration;

if NoUpdateU32::should_update(@_max_voting_duration)
&& NoUpdateU32::should_update(@_min_voting_duration) {
if _max_voting_duration.should_update() && _min_voting_duration.should_update() {
// Check that min and max voting durations are valid
// We don't use the internal `_set_min_voting_duration` and `_set_max_voting_duration` functions because
// it would revert when `_min_voting_duration > max_voting_duration` (when the new `_min` is
Expand All @@ -637,7 +636,7 @@ mod Space {
}
)
);
} else if NoUpdateU32::should_update(@_min_voting_duration) {
} else if _min_voting_duration.should_update() {
_set_min_voting_duration(ref self, input.min_voting_duration);
self
.emit(
Expand All @@ -647,7 +646,7 @@ mod Space {
}
)
);
} else if NoUpdateU32::should_update(@_max_voting_duration) {
} else if _max_voting_duration.should_update() {
_set_max_voting_duration(ref self, input.max_voting_duration);
self
.emit(
Expand All @@ -659,7 +658,7 @@ mod Space {
);
}

if NoUpdateU32::should_update(@input.voting_delay) {
if input.voting_delay.should_update() {
_set_voting_delay(ref self, input.voting_delay);

self
Expand All @@ -670,20 +669,7 @@ mod Space {
);
}

if NoUpdateArray::should_update((@input).metadata_URI) {
self
.emit(
Event::MetadataUriUpdated(
MetadataUriUpdated { metadata_URI: input.metadata_URI.span() }
)
);
}

if NoUpdateArray::should_update((@input).dao_URI) {
self.emit(Event::DaoUriUpdated(DaoUriUpdated { dao_URI: input.dao_URI.span() }));
}

if NoUpdateStrategy::should_update((@input).proposal_validation_strategy) {
if input.proposal_validation_strategy.should_update() {
_set_proposal_validation_strategy(
ref self, input.proposal_validation_strategy.clone()
);
Expand All @@ -702,7 +688,7 @@ mod Space {
);
}

if NoUpdateArray::should_update((@input).authenticators_to_add) {
if input.authenticators_to_add.should_update() {
_add_authenticators(ref self, input.authenticators_to_add.span());
self
.emit(
Expand All @@ -714,7 +700,7 @@ mod Space {
);
}

if NoUpdateArray::should_update((@input).authenticators_to_remove) {
if input.authenticators_to_remove.should_update() {
_remove_authenticators(ref self, input.authenticators_to_remove.span());
self
.emit(
Expand All @@ -726,7 +712,7 @@ mod Space {
);
}

if NoUpdateArray::should_update((@input).voting_strategies_to_add) {
if input.voting_strategies_to_add.should_update() {
_add_voting_strategies(ref self, input.voting_strategies_to_add.span());
self
.emit(
Expand All @@ -741,7 +727,7 @@ mod Space {
);
}

if NoUpdateArray::should_update((@input).voting_strategies_to_remove) {
if input.voting_strategies_to_remove.should_update() {
_remove_voting_strategies(ref self, input.voting_strategies_to_remove.span());
self
.emit(
Expand All @@ -752,6 +738,21 @@ mod Space {
)
);
}

// TODO: test once #506 is merged
if NoUpdateString::should_update((@input).metadata_URI) {
self
.emit(
Event::MetadataUriUpdated(
MetadataUriUpdated { metadata_URI: input.metadata_URI.span() }
)
);
}

// TODO: test once #506 is merged
if NoUpdateString::should_update((@input).dao_URI) {
self.emit(Event::DaoUriUpdated(DaoUriUpdated { dao_URI: input.dao_URI.span() }));
}
}

fn vote_registry(self: @ContractState, proposal_id: u256, voter: UserAddress) -> bool {
Expand Down
1 change: 1 addition & 0 deletions starknet/src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod test_factory;
mod test_space;
mod test_upgrade;
mod test_stark_tx_auth;
mod test_update_settings;

mod execution_strategies;
mod proposal_validation_strategies;
Expand Down
23 changes: 23 additions & 0 deletions starknet/src/tests/execution_strategies/vanilla.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(test)]
mod tests {
use sx::interfaces::{IQuorum, IQuorumDispatcher, IQuorumDispatcherTrait};
use sx::execution_strategies::vanilla::{VanillaExecutionStrategy};
use sx::execution_strategies::simple_quorum::SimpleQuorumExecutionStrategy;
use starknet::syscalls::{deploy_syscall};

#[test]
#[available_gas(10000000)]
Expand All @@ -14,4 +17,24 @@ mod tests {

assert(strategy_type == 'SimpleQuorumVanilla', 'invalid strategy type');
}

#[test]
#[available_gas(10000000)]
fn get_quorum() {
let quorum = 42_u256;
let mut constructor_calldata: Array<felt252> = array![];
quorum.serialize(ref constructor_calldata);

let (contract, _) = deploy_syscall(
VanillaExecutionStrategy::TEST_CLASS_HASH.try_into().unwrap(),
0,
constructor_calldata.span(),
false,
)
.unwrap();

let strat = IQuorumDispatcher { contract_address: contract, };

assert(strat.quorum() == quorum, 'invalid quorum');
}
}
18 changes: 9 additions & 9 deletions starknet/src/tests/setup/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ mod setup {
#[derive(Drop)]
struct Config {
owner: ContractAddress,
min_voting_duration: u64,
max_voting_duration: u64,
voting_delay: u64,
min_voting_duration: u32,
max_voting_duration: u32,
voting_delay: u32,
proposal_validation_strategy: Strategy,
voting_strategies: Array<Strategy>,
authenticators: Array<ContractAddress>,
Expand All @@ -38,9 +38,9 @@ mod setup {

// Space Settings
let owner = contract_address_const::<0x123456789>();
let max_voting_duration = 2_u64;
let min_voting_duration = 1_u64;
let voting_delay = 1_u64;
let max_voting_duration = 2_u32;
let min_voting_duration = 1_u32;
let voting_delay = 1_u32;
let quorum = u256_from_felt252(1);

// Deploy Vanilla Authenticator
Expand Down Expand Up @@ -99,9 +99,9 @@ mod setup {

fn get_initialize_calldata(
owner: @ContractAddress,
min_voting_duration: @u64,
max_voting_duration: @u64,
voting_delay: @u64,
min_voting_duration: @u32,
max_voting_duration: @u32,
voting_delay: @u32,
proposal_validation_strategy: @Strategy,
voting_strategies: @Array<Strategy>,
authenticators: @Array<ContractAddress>
Expand Down
24 changes: 16 additions & 8 deletions starknet/src/tests/test_space.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ mod tests {
authenticator
.authenticate(space.contract_address, UPDATE_PROPOSAL_SELECTOR, update_calldata);

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand All @@ -380,7 +380,9 @@ mod tests {
// Vote on Proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

// Execute Proposal
space.execute(u256_from_felt252(1), new_payload);
Expand Down Expand Up @@ -477,7 +479,7 @@ mod tests {

assert(space.next_proposal_id() == 2_u256, 'next_proposal_id should be 2');

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

let mut vote_calldata = array![];
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
Expand All @@ -493,7 +495,9 @@ mod tests {
// Vote on Proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

// Execute Proposal
space.execute(u256_from_felt252(1), vanilla_execution_strategy.params.clone());
Expand Down Expand Up @@ -531,7 +535,9 @@ mod tests {
// Create Proposal
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

// Execute Proposal
space.execute(1, array!['random', 'stuff']);
Expand Down Expand Up @@ -568,7 +574,7 @@ mod tests {
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);
let proposal_id = u256_from_felt252(1);

testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());
let proposal = space.proposals(proposal_id);
assert(proposal.finalization_status == FinalizationStatus::Pending(()), 'pending');

Expand Down Expand Up @@ -647,7 +653,9 @@ mod tests {
// Create Proposal
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

testing::set_block_timestamp(config.voting_delay + config.max_voting_duration);
testing::set_block_timestamp(
config.voting_delay.into() + config.max_voting_duration.into()
);

// Execute Proposal
space.execute(1, array![]);
Expand Down Expand Up @@ -845,7 +853,7 @@ mod tests {
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

// Skip voting delay
testing::set_block_timestamp(config.voting_delay);
testing::set_block_timestamp(config.voting_delay.into());

// Try to update Proposal
let mut update_calldata = array::ArrayTrait::<felt252>::new();
Expand Down
Loading

0 comments on commit cd8be1d

Please sign in to comment.