Skip to content

Commit

Permalink
feat: add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Aug 17, 2023
1 parent 5542f47 commit 58c7fb1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 22 deletions.
2 changes: 1 addition & 1 deletion starknet/src/tests/mocks/erc20_votes_preset.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ mod ERC20VotesPreset {
let unsafe_state = ERC20Votes::unsafe_new_contract_state();
ERC20Votes::InternalImpl::_checkpoints(@unsafe_state, account, pos)
}
}
}
1 change: 0 additions & 1 deletion starknet/src/tests/test_upgrade.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod tests {
use option::OptionTrait;
use integer::u256_from_felt252;
use clone::Clone;
use debug::PrintTrait;
use serde::{Serde};

use sx::space::space::{Space, ISpaceDispatcher, ISpaceDispatcherTrait};
Expand Down
123 changes: 109 additions & 14 deletions starknet/src/tests/voting_strategies/erc20_votes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ mod tests {
VanillaAuthenticator, IVanillaAuthenticatorDispatcher, IVanillaAuthenticatorDispatcherTrait
};
use sx::voting_strategies::erc20_votes::ERC20VotesVotingStrategy;
use debug::PrintTrait;
use sx::types::{
Choice, Proposal, IndexedStrategy, Strategy, UpdateSettingsCalldata,
UpdateSettingsCalldataImpl, UserAddress
UpdateSettingsCalldataImpl, UserAddress, UserAddressTrait
};
use sx::tests::utils::strategy_trait::StrategyImpl;
use sx::utils::constants::{PROPOSE_SELECTOR, VOTE_SELECTOR};
Expand Down Expand Up @@ -110,7 +109,7 @@ mod tests {

// Setup 5 accounts with 5 tokens each
// They each self delegate
fn setup_accounts(voting_strategy: Strategy) -> Array<ContractAddress> {
fn setup_accounts(voting_strategy: Strategy) -> Array<UserAddress> {
let mut params = voting_strategy.params.span();
let contract = Serde::<ContractAddress>::deserialize(ref params).unwrap();

Expand Down Expand Up @@ -148,12 +147,18 @@ mod tests {
testing::set_contract_address(OWNER());

// Return them
return array![account0, account1, account2, account3, account4];
return array![
UserAddress::Starknet(account0),
UserAddress::Starknet(account1),
UserAddress::Starknet(account2),
UserAddress::Starknet(account3),
UserAddress::Starknet(account4),
];
}

#[test]
#[available_gas(1000000000)]
fn test_works() {
fn works() {
let (config, space) = setup_space();
let vanilla_execution_strategy = get_vanilla_execution_strategy();
let accounts = setup_accounts(space.voting_strategies(1));
Expand All @@ -169,16 +174,15 @@ mod tests {
ArrayTrait::<felt252>::new().serialize(ref propose_calldata);
ArrayTrait::<felt252>::new().serialize(ref propose_calldata);

testing::set_block_number(100);

// Create Proposal
// Create a proposal
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

// Increasing block block_number by 1 to pass voting delay
testing::set_block_number(101);
// Advance to vote start + 1
let current = info::get_block_timestamp();
testing::set_block_timestamp(current + config.voting_delay + 1);

let mut vote_calldata = array::ArrayTrait::<felt252>::new();
let voter = UserAddress::Starknet(contract_address_const::<0x8765>());
let voter = *accounts.at(0);
voter.serialize(ref vote_calldata);
let proposal_id = 1_u256;
proposal_id.serialize(ref vote_calldata);
Expand All @@ -189,13 +193,104 @@ mod tests {
user_voting_strategies.serialize(ref vote_calldata);
ArrayTrait::<felt252>::new().serialize(ref vote_calldata);

// Vote on Proposal
// Vote on proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);

testing::set_block_number(102);
testing::set_block_timestamp(current + config.max_voting_duration);

// Execute Proposal
// Execute proposal
space.execute(1_u256, vanilla_execution_strategy.params);
}

#[test]
#[available_gas(1000000000)]
#[should_panic]
fn revert_if_queried_at_vote_start() {
let (config, space) = setup_space();
let vanilla_execution_strategy = get_vanilla_execution_strategy();
let accounts = setup_accounts(space.voting_strategies(1));

let authenticator = IVanillaAuthenticatorDispatcher {
contract_address: *config.authenticators.at(0),
};

let author = UserAddress::Starknet(contract_address_const::<0x5678>());
let mut propose_calldata = array::ArrayTrait::<felt252>::new();
author.serialize(ref propose_calldata);
vanilla_execution_strategy.serialize(ref propose_calldata);
ArrayTrait::<felt252>::new().serialize(ref propose_calldata);
ArrayTrait::<felt252>::new().serialize(ref propose_calldata);

// Create proposal
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

// Move to the exact voting period start so the strategy will revert.
let current = info::get_block_timestamp();
testing::set_block_timestamp(current + config.voting_delay);

let mut vote_calldata = array::ArrayTrait::<felt252>::new();
let voter = *accounts.at(0);
voter.serialize(ref vote_calldata);
let proposal_id = 1_u256;
proposal_id.serialize(ref vote_calldata);
let choice = Choice::For(());
choice.serialize(ref vote_calldata);
let mut user_voting_strategies = array![];
user_voting_strategies.append(IndexedStrategy { index: 1, params: array![] });
user_voting_strategies.serialize(ref vote_calldata);
ArrayTrait::<felt252>::new().serialize(ref vote_calldata);

// Vote on proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);
}

#[test]
#[available_gas(1000000000)]
#[should_panic]
fn no_delegation_means_no_voting_power() {
let (config, space) = setup_space();
let vanilla_execution_strategy = get_vanilla_execution_strategy();
let accounts = setup_accounts(space.voting_strategies(1));

let authenticator = IVanillaAuthenticatorDispatcher {
contract_address: *config.authenticators.at(0),
};

// Account0 will delegate to another account, so he should not have any voting power
let token_contract = IVotesDispatcher {
contract_address: space.voting_strategies(1).address,
};
testing::set_contract_address((*accounts.at(0)).to_starknet_address());
token_contract.delegate((contract_address_const::<0xdeadbeef>()));

let author = UserAddress::Starknet(contract_address_const::<0x5678>());
let mut propose_calldata = array::ArrayTrait::<felt252>::new();
author.serialize(ref propose_calldata);
vanilla_execution_strategy.serialize(ref propose_calldata);
ArrayTrait::<felt252>::new().serialize(ref propose_calldata);
ArrayTrait::<felt252>::new().serialize(ref propose_calldata);

// Create proposal
authenticator.authenticate(space.contract_address, PROPOSE_SELECTOR, propose_calldata);

// Move to the exact voting period start + 1
let current = info::get_block_timestamp();
testing::set_block_timestamp(current + config.voting_delay + 1);

let mut vote_calldata = array::ArrayTrait::<felt252>::new();
let voter = *accounts.at(0);
voter.serialize(ref vote_calldata);
let proposal_id = 1_u256;
proposal_id.serialize(ref vote_calldata);
let choice = Choice::For(());
choice.serialize(ref vote_calldata);
let mut user_voting_strategies = array![];
user_voting_strategies.append(IndexedStrategy { index: 1, params: array![] });
user_voting_strategies.serialize(ref vote_calldata);
ArrayTrait::<felt252>::new().serialize(ref vote_calldata);

// Vote on proposal
authenticator.authenticate(space.contract_address, VOTE_SELECTOR, vote_calldata);
}
}

1 change: 0 additions & 1 deletion starknet/src/utils/merkle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use serde::Serde;
use sx::types::UserAddress;
use clone::Clone;
use hash::{LegacyHash};
use debug::PrintTrait;
use sx::utils::legacy_hash::LegacyHashSpanFelt252;

/// Leaf struct for the merkle tree
Expand Down
6 changes: 2 additions & 4 deletions starknet/src/voting_strategies/erc20_votes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod ERC20VotesVotingStrategy {
IVotes, IVotesDispatcher, IVotesDispatcherTrait
};
use traits::Into;
use debug::PrintTrait;

use option::OptionTrait;

Expand All @@ -21,7 +20,7 @@ mod ERC20VotesVotingStrategy {
impl ERC20VotesVotingStrategy of IVotingStrategy<ContractState> {
fn get_voting_power(
self: @ContractState,
block_number: u32,
timestamp: u32,
voter: UserAddress,
params: Array<felt252>,
user_params: Array<felt252>,
Expand All @@ -37,8 +36,7 @@ mod ERC20VotesVotingStrategy {

let erc20 = IVotesDispatcher { contract_address: erc20_contract_address, };

block_number.print();
erc20.get_past_votes(voter, block_number.into() - 1) // TODO; verify - 1
erc20.get_past_votes(voter, timestamp.into()) // TODO; verify - 1
}
}
}
1 change: 0 additions & 1 deletion starknet/src/voting_strategies/merkle_whitelist.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod MerkleWhitelistVotingStrategy {
use array::{ArrayTrait, Span, SpanTrait};
use option::OptionTrait;
use sx::utils::merkle::{assert_valid_proof, Leaf};
use debug::PrintTrait;

const LEAF_SIZE: usize = 4; // Serde::<Leaf>::serialize().len()

Expand Down

0 comments on commit 58c7fb1

Please sign in to comment.