From 63f8c2f085e8a1889768c42713629ce757be5e5d Mon Sep 17 00:00:00 2001 From: Scott Piriou <30843220+pscott@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:56:21 +0200 Subject: [PATCH 1/4] add min / max voting duration tests --- starknet/src/tests.cairo | 1 + starknet/src/tests/setup/setup.cairo | 18 +-- starknet/src/tests/test_update_settings.cairo | 118 ++++++++++++++++++ 3 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 starknet/src/tests/test_update_settings.cairo diff --git a/starknet/src/tests.cairo b/starknet/src/tests.cairo index f8471302..9872ad97 100644 --- a/starknet/src/tests.cairo +++ b/starknet/src/tests.cairo @@ -3,6 +3,7 @@ mod test_factory; mod test_space; mod test_upgrade; mod test_stark_tx_auth; +mod test_update_settings; mod mocks; mod setup; diff --git a/starknet/src/tests/setup/setup.cairo b/starknet/src/tests/setup/setup.cairo index dd7bccd5..253e4131 100644 --- a/starknet/src/tests/setup/setup.cairo +++ b/starknet/src/tests/setup/setup.cairo @@ -22,9 +22,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, authenticators: Array, @@ -37,9 +37,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 @@ -98,9 +98,9 @@ mod setup { fn get_constructor_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, authenticators: @Array diff --git a/starknet/src/tests/test_update_settings.cairo b/starknet/src/tests/test_update_settings.cairo new file mode 100644 index 00000000..f5313b97 --- /dev/null +++ b/starknet/src/tests/test_update_settings.cairo @@ -0,0 +1,118 @@ +#[cfg(test)] +mod tests { + use sx::space::space::{Space, ISpaceDispatcher, ISpaceDispatcherTrait}; + use sx::tests::setup::setup::setup::{setup, deploy, Config}; + use sx::types::{UpdateSettingsCalldata, UpdateSettingsCalldataImpl}; + use sx::tests::utils::strategy_trait::{StrategyImpl}; + use starknet::testing; + use starknet::info; + use starknet::contract_address_const; + use clone::Clone; + + fn setup_update_settings() -> (Config, ISpaceDispatcher) { + let config = setup(); + let (_, space) = deploy(@config); + + testing::set_caller_address(config.owner); + testing::set_contract_address(config.owner); + + (config, space) + } + + #[test] + #[available_gas(10000000000)] + #[should_panic(expected: ('Caller is not the owner', 'ENTRYPOINT_FAILED'))] + fn update_unauthorized() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + + testing::set_contract_address(contract_address_const::<'unauthorized'>()); + space.update_settings(input); + } + + #[test] + #[available_gas(10000000000)] + fn update_min_voting_duration() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.min_voting_duration = config.min_voting_duration + 1; + + space.update_settings(input.clone()); + + assert( + space.min_voting_duration() == input.min_voting_duration, + 'Min voting duration not updated' + ); + } + + #[test] + #[available_gas(10000000000)] + // #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] // TODO: uncomment once PR is merged + fn update_min_voting_duration_too_big() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.min_voting_duration = config.max_voting_duration + 1; + + space.update_settings(input.clone()); + } + + + #[test] + #[available_gas(10000000000)] + fn update_max_voting_duration() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.max_voting_duration = config.max_voting_duration + 1; + + space.update_settings(input.clone()); + + assert( + space.max_voting_duration() == input.max_voting_duration, + 'Max voting duration not updated' + ); + } + + #[test] + #[available_gas(10000000000)] + // #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] // TODO: uncomment once PR is merged + fn update_max_voting_duration_too_small() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.max_voting_duration = config.min_voting_duration - 1; + + space.update_settings(input.clone()); + } + + #[test] + #[available_gas(10000000000)] + fn update_min_max_voting_duration_at_once() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.min_voting_duration = config.max_voting_duration + 1; + input.max_voting_duration = config.max_voting_duration + 2; + + space.update_settings(input.clone()); + assert( + space.min_voting_duration() == input.min_voting_duration, + 'Min voting duration not updated' + ); + assert( + space.max_voting_duration() == input.max_voting_duration, + 'Max voting duration not updated' + ); + } + + #[test] + #[available_gas(10000000000)] + // #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] // TODO: uncomment once PR is merged + fn update_min_max_voting_duration_at_once_invalid() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.min_voting_duration = config.max_voting_duration + 1; + input + .max_voting_duration = config + .max_voting_duration; // min is bigger than max, should fail + + space.update_settings(input.clone()); + } +} From 301e69d878f00cb4ed433c876b44ed06313e6ca3 Mon Sep 17 00:00:00 2001 From: Scott Piriou <30843220+pscott@users.noreply.github.com> Date: Sat, 19 Aug 2023 13:42:46 +0200 Subject: [PATCH 2/4] add tests part1 --- starknet/src/tests/test_update_settings.cairo | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/starknet/src/tests/test_update_settings.cairo b/starknet/src/tests/test_update_settings.cairo index f5313b97..e875de5a 100644 --- a/starknet/src/tests/test_update_settings.cairo +++ b/starknet/src/tests/test_update_settings.cairo @@ -8,6 +8,8 @@ mod tests { use starknet::info; use starknet::contract_address_const; use clone::Clone; + use array::{ArrayTrait, SpanTrait}; + use serde::Serde; fn setup_update_settings() -> (Config, ISpaceDispatcher) { let config = setup(); @@ -43,6 +45,7 @@ mod tests { space.min_voting_duration() == input.min_voting_duration, 'Min voting duration not updated' ); + // TODO: check event once it's been added } #[test] @@ -70,6 +73,7 @@ mod tests { space.max_voting_duration() == input.max_voting_duration, 'Max voting duration not updated' ); + // TODO: check event once it's been added } #[test] @@ -81,6 +85,7 @@ mod tests { input.max_voting_duration = config.min_voting_duration - 1; space.update_settings(input.clone()); + // TODO: check event once it's been added } #[test] @@ -100,6 +105,7 @@ mod tests { space.max_voting_duration() == input.max_voting_duration, 'Max voting duration not updated' ); + // TODO: check event once it's been added } #[test] @@ -114,5 +120,103 @@ mod tests { .max_voting_duration; // min is bigger than max, should fail space.update_settings(input.clone()); + // TODO: check event once it's been added } + + #[test] + #[available_gas(10000000000)] + fn update_voting_delay() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + input.voting_delay = config.voting_delay + 1; + + space.update_settings(input.clone()); + + assert(space.voting_delay() == input.voting_delay, 'Voting delay not updated'); + // TODO: check event once it's been added + } + + #[test] + #[available_gas(10000000000)] + fn metadata_uri() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + let mut arr = array![]; + 'hello!'.serialize(ref arr); + input.metadata_URI = arr; + + space.update_settings(input.clone()); + // TODO: check event once it's been added + } + + #[test] + #[available_gas(10000000000)] + fn dao_uri() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + let mut arr = array![]; + 'hello!'.serialize(ref arr); + input.dao_URI = arr; + + space.update_settings(input.clone()); + // TODO: check event once it's been added + } + + #[test] + #[available_gas(10000000000)] + fn proposal_validation_strategy() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + let randomStrategy = StrategyImpl::from_address( + contract_address_const::<'randomStrategy'>() + ); + input.proposal_validation_strategy = randomStrategy; + let mut arr = array![]; + 'hello!'.serialize(ref arr); + input.proposal_validation_strategy_metadata_URI = arr; + + space.update_settings(input.clone()); + + assert( + space.proposal_validation_strategy() == input.proposal_validation_strategy, + 'Proposal strategy not updated' + ); + // TODO: check event once it's been added + } + + #[test] + #[available_gas(10000000000)] + fn add_authenticators() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + let auth1 = contract_address_const::<'authenticator1'>(); + let auth2 = contract_address_const::<'authenticator2'>(); + let mut arr = array![auth1, auth2]; + input.authenticators_to_add = arr; + + space.update_settings(input.clone()); + + assert(space.authenticators(auth1) == true, 'Authenticator 1 not added'); + + assert(space.authenticators(auth2) == true, 'Authenticator 2 not added'); + // TODO: check event once it's been added + } + + #[test] + #[available_gas(10000000000)] + fn remove_authenticators() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + let auth1 = *config.authenticators.at(0); + let mut arr = array![auth1]; + input.authenticators_to_remove = arr; + + space.update_settings(input.clone()); + + assert(space.authenticators(auth1) == false, 'Authenticator not removed'); + // TODO: check event once it's been added + } +// voting_strategies_to_add: Array, +// voting_strategies_metadata_URIs_to_add: Array>, +// voting_strategies_to_remove: Array, } From 2fee75974b4f79098da08eda2077810b57fe1660 Mon Sep 17 00:00:00 2001 From: Scott Piriou <30843220+pscott@users.noreply.github.com> Date: Mon, 21 Aug 2023 12:09:39 +0200 Subject: [PATCH 3/4] add update settings tests p2 --- starknet/src/tests/test_update_settings.cairo | 63 ++++++++++++++++++- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/starknet/src/tests/test_update_settings.cairo b/starknet/src/tests/test_update_settings.cairo index e875de5a..27cf4548 100644 --- a/starknet/src/tests/test_update_settings.cairo +++ b/starknet/src/tests/test_update_settings.cairo @@ -216,7 +216,64 @@ mod tests { assert(space.authenticators(auth1) == false, 'Authenticator not removed'); // TODO: check event once it's been added } -// voting_strategies_to_add: Array, -// voting_strategies_metadata_URIs_to_add: Array>, -// voting_strategies_to_remove: Array, + + #[test] + #[available_gas(10000000000)] + fn add_voting_strategies() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + + let vs1 = StrategyImpl::from_address(contract_address_const::<'votingStrategy1'>()); + let vs2 = StrategyImpl::from_address(contract_address_const::<'votingStrategy2'>()); + + let mut arr = array![vs1.clone(), vs2.clone()]; + input.voting_strategies_to_add = arr; + + space.update_settings(input); + + assert(space.voting_strategies(1) == vs1, 'Voting strategy 1 not added'); + assert(space.voting_strategies(2) == vs2, 'Voting strategy 2 not added'); + assert(space.active_voting_strategies() == 0b111, 'Voting strategies not active'); + // TODO: check event once it's been added + // voting_strategies_metadata_URIs_to_add: Array>, + } + + + #[test] + #[available_gas(10000000000)] + fn remove_voting_strategies() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + + // First, add a new voting strategy + let vs1 = StrategyImpl::from_address(contract_address_const::<'votingStrategy1'>()); + let mut arr = array![vs1.clone()]; + input.voting_strategies_to_add = arr; + space.update_settings(input); + assert(space.voting_strategies(1) == vs1, 'Voting strategy 1 not added'); + assert(space.active_voting_strategies() == 0b11, 'Voting strategy not active'); + + // Now, remove the first voting strategy + let mut input = UpdateSettingsCalldataImpl::default(); + let mut arr = array![0]; + input.voting_strategies_to_remove = arr; + + space.update_settings(input); + assert(space.active_voting_strategies() == 0b10, 'strategy not removed'); + // TODO: check event once it's been added + } + + #[test] + #[available_gas(10000000000)] + #[should_panic(expected: ('No active voting strategy left', 'ENTRYPOINT_FAILED'))] + fn remove_all_voting_strategies() { + let (config, space) = setup_update_settings(); + let mut input = UpdateSettingsCalldataImpl::default(); + + // Remove the first voting strategy + let mut arr = array![0]; + input.voting_strategies_to_remove = arr; + + space.update_settings(input); + } } From ed2007d26337be3aeac34098c8a2e909c43dce2c Mon Sep 17 00:00:00 2001 From: Scott Piriou <30843220+pscott@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:30:06 +0200 Subject: [PATCH 4/4] use .into --- starknet/src/tests/test_space.cairo | 14 ++++++++----- starknet/src/tests/test_update_settings.cairo | 6 +++--- starknet/src/tests/vote.cairo | 20 +++++++++++-------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/starknet/src/tests/test_space.cairo b/starknet/src/tests/test_space.cairo index ba651290..f809ca75 100644 --- a/starknet/src/tests/test_space.cairo +++ b/starknet/src/tests/test_space.cairo @@ -264,7 +264,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>()); @@ -280,7 +280,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); @@ -377,7 +379,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>()); @@ -393,7 +395,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()); @@ -439,7 +443,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'); diff --git a/starknet/src/tests/test_update_settings.cairo b/starknet/src/tests/test_update_settings.cairo index 27cf4548..b46caed1 100644 --- a/starknet/src/tests/test_update_settings.cairo +++ b/starknet/src/tests/test_update_settings.cairo @@ -50,7 +50,7 @@ mod tests { #[test] #[available_gas(10000000000)] - // #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] // TODO: uncomment once PR is merged + #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] fn update_min_voting_duration_too_big() { let (config, space) = setup_update_settings(); let mut input = UpdateSettingsCalldataImpl::default(); @@ -78,7 +78,7 @@ mod tests { #[test] #[available_gas(10000000000)] - // #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] // TODO: uncomment once PR is merged + #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] fn update_max_voting_duration_too_small() { let (config, space) = setup_update_settings(); let mut input = UpdateSettingsCalldataImpl::default(); @@ -110,7 +110,7 @@ mod tests { #[test] #[available_gas(10000000000)] - // #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] // TODO: uncomment once PR is merged + #[should_panic(expected: ('Invalid duration', 'ENTRYPOINT_FAILED'))] fn update_min_max_voting_duration_at_once_invalid() { let (config, space) = setup_update_settings(); let mut input = UpdateSettingsCalldataImpl::default(); diff --git a/starknet/src/tests/vote.cairo b/starknet/src/tests/vote.cairo index a66822c6..5fabd727 100644 --- a/starknet/src/tests/vote.cairo +++ b/starknet/src/tests/vote.cairo @@ -75,7 +75,7 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Increasing block timestamp pass voting delay - 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>()); @@ -109,7 +109,7 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Increasing block timestamp pass voting delay - 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>()); @@ -143,7 +143,7 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Increasing block timestamp by voting delay - 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>()); @@ -209,7 +209,9 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Fast forward to end of voting period - testing::set_block_timestamp(config.voting_delay + config.max_voting_duration); + testing::set_block_timestamp( + config.voting_delay.into() + config.max_voting_duration.into() + ); let mut vote_calldata = array![]; let voter = UserAddress::Starknet(contract_address_const::<0x8765>()); @@ -240,7 +242,7 @@ mod tests { create_proposal(authenticator, space, execution_strategy); - testing::set_block_timestamp(config.voting_delay); + testing::set_block_timestamp(config.voting_delay.into()); space .execute( @@ -276,7 +278,9 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Fast forward to end of voting period - testing::set_block_timestamp(config.voting_delay + config.max_voting_duration); + testing::set_block_timestamp( + config.voting_delay.into() + config.max_voting_duration.into() + ); let voter = UserAddress::Starknet(contract_address_const::<0x8765>()); let proposal_id = 1_u256; @@ -304,7 +308,7 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Increasing block timestamp pass voting delay - 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>()); @@ -352,7 +356,7 @@ mod tests { create_proposal(authenticator, space, execution_strategy); // Increasing block timestamp pass voting delay - 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>());