Skip to content

Commit

Permalink
fix: uncomment test
Browse files Browse the repository at this point in the history
  • Loading branch information
pscott committed Aug 25, 2023
1 parent 104fb51 commit c9f9bae
Showing 1 changed file with 140 additions and 140 deletions.
280 changes: 140 additions & 140 deletions starknet/src/tests/proposal_validation_strategies/proposing_power.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -105,146 +105,146 @@ mod tests {
assert(is_validated, 'should have 2 VP');
}

// #[test]
// #[available_gas(10000000000)]
// fn test_merkle_whitelist_works() {
// starknet::testing::set_block_timestamp(1);

// // deploy merkle whitelist contract
// let (merkle_contract, _) = deploy_syscall(
// MerkleWhitelistVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(),
// 0,
// array![].span(),
// false
// )
// .unwrap();

// // create proposal validation strategy based on the deployed merkle whitelist contract
// let (proposal_validation_contract, _) = deploy_syscall(
// ProposingPowerProposalValidationStrategy::TEST_CLASS_HASH.try_into().unwrap(),
// 0,
// array![].span(),
// false
// )
// .unwrap();

// let contract = IProposalValidationStrategyDispatcher {
// contract_address: proposal_validation_contract,
// };

// // Generate leaves
// let voter1 = UserAddress::Starknet(contract_address_const::<0x111111>());
// let voter2 = UserAddress::Starknet(contract_address_const::<0x111112>());
// let voter3 = UserAddress::Starknet(contract_address_const::<0x111113>());
// let leaf1 = Leaf { address: voter1, voting_power: 1 };
// let leaf2 = Leaf { address: voter2, voting_power: 2 };
// let leaf3 = Leaf { address: voter3, voting_power: 3 };

// let members = array![leaf1, leaf2, leaf3];

// let merkle_data = generate_merkle_data(members.span());

// let proof1 = generate_proof(merkle_data.span(), 0);
// let proof2 = generate_proof(merkle_data.span(), 1);
// let proof3 = generate_proof(merkle_data.span(), 2);

// let mut user_params = ArrayTrait::<felt252>::new();
// leaf1.serialize(ref user_params);
// proof1.serialize(ref user_params);

// let root = generate_merkle_root(merkle_data.span());
// let merkle_whitelist_strategy = Strategy {
// address: merkle_contract, params: array![root],
// };
// let allowed_strategies = array![merkle_whitelist_strategy.clone()];
// let proposal_threshold =
// 2_u256; // voter1 should not hit threshold but voter2 and voter3 should

// let mut params = array![];
// proposal_threshold.serialize(ref params);
// allowed_strategies.serialize(ref params);

// // setup for voter1
// let author = leaf1.address;
// let mut indexed_params = array![];
// leaf1.serialize(ref indexed_params);
// proof1.serialize(ref indexed_params);
// let used_strategy = IndexedStrategy { index: 0, params: indexed_params, };
// let used_strategies = array![used_strategy.clone()];
// let mut user_params = array![];
// used_strategies.serialize(ref user_params);

// let is_validated = contract.validate(author, params.span(), user_params.span());
// assert(!is_validated, 'should not have enough VP');

// // setup for voter2
// let author = leaf2.address;
// let mut indexed_params = array![];
// leaf2.serialize(ref indexed_params);
// proof2.serialize(ref indexed_params);
// let used_strategy = IndexedStrategy { index: 0, params: indexed_params, };
// let used_strategies = array![used_strategy.clone()];
// let mut user_params = array![];
// used_strategies.serialize(ref user_params);

// let is_validated = contract.validate(author, params.span(), user_params.span());
// assert(is_validated, 'should have enough VP1');

// // setup for voter3
// let author = leaf3.address;
// let mut indexed_params = array![];
// leaf3.serialize(ref indexed_params);
// proof3.serialize(ref indexed_params);
// let used_strategy = IndexedStrategy { index: 0, params: indexed_params, };
// let used_strategies = array![used_strategy.clone()];
// let mut user_params = array![];
// used_strategies.serialize(ref user_params);

// let is_validated = contract.validate(author, params.span(), user_params.span());
// assert(is_validated, 'should have enough VP2');

// // -- Now let's mix merkle and vanilla voting strategies --

// // deploy vanilla voting strategy
// let (vanilla_contract, _) = deploy_syscall(
// VanillaVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
// )
// .unwrap();

// let vanilla_strategy = Strategy { address: vanilla_contract, params: array![], };

// let allowed_strategies = array![
// merkle_whitelist_strategy.clone(), vanilla_strategy.clone()
// ]; // update allowed strategies
// let proposal_threshold = proposal_threshold; // threshold is left unchanged
// let mut params = array![]; // update params
// proposal_threshold.serialize(ref params);
// allowed_strategies.serialize(ref params);

// // voter 1 should now have enough voting power!
// let author = leaf1.address;
// let vanilla = IndexedStrategy { index: 1, params: array![], };
// let mut indexed_params = array![];
// leaf1.serialize(ref indexed_params);
// proof1.serialize(ref indexed_params);
// let merkle = IndexedStrategy { index: 0, params: indexed_params, };

// let used_strategies = array![vanilla.clone(), merkle.clone()];
// let mut user_params = array![];
// used_strategies.serialize(ref user_params);

// let is_validated = contract.validate(author, params.span(), user_params.span());
// assert(is_validated, 'should have enough VP2');

// // and a random voter that doesn't use the whitelist should not have enough VP
// let author = UserAddress::Starknet(contract_address_const::<0x123456789>());
// let used_strategies = array![vanilla.clone()];
// let mut user_params = array![];
// used_strategies.serialize(ref user_params);

// let is_validated = contract.validate(author, params.span(), user_params.span());
// assert(!is_validated, 'should not have enough VP');
// }
#[test]
#[available_gas(10000000000)]
fn test_merkle_whitelist_works() {
starknet::testing::set_block_timestamp(1);

// deploy merkle whitelist contract
let (merkle_contract, _) = deploy_syscall(
MerkleWhitelistVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(),
0,
array![].span(),
false
)
.unwrap();

// create proposal validation strategy based on the deployed merkle whitelist contract
let (proposal_validation_contract, _) = deploy_syscall(
ProposingPowerProposalValidationStrategy::TEST_CLASS_HASH.try_into().unwrap(),
0,
array![].span(),
false
)
.unwrap();

let contract = IProposalValidationStrategyDispatcher {
contract_address: proposal_validation_contract,
};

// Generate leaves
let voter1 = UserAddress::Starknet(contract_address_const::<0x111111>());
let voter2 = UserAddress::Starknet(contract_address_const::<0x111112>());
let voter3 = UserAddress::Starknet(contract_address_const::<0x111113>());
let leaf1 = Leaf { address: voter1, voting_power: 1 };
let leaf2 = Leaf { address: voter2, voting_power: 2 };
let leaf3 = Leaf { address: voter3, voting_power: 3 };

let members = array![leaf1, leaf2, leaf3];

let merkle_data = generate_merkle_data(members.span());

let proof1 = generate_proof(merkle_data.span(), 0);
let proof2 = generate_proof(merkle_data.span(), 1);
let proof3 = generate_proof(merkle_data.span(), 2);

let mut user_params = ArrayTrait::<felt252>::new();
leaf1.serialize(ref user_params);
proof1.serialize(ref user_params);

let root = generate_merkle_root(merkle_data.span());
let merkle_whitelist_strategy = Strategy {
address: merkle_contract, params: array![root],
};
let allowed_strategies = array![merkle_whitelist_strategy.clone()];
let proposal_threshold =
2_u256; // voter1 should not hit threshold but voter2 and voter3 should

let mut params = array![];
proposal_threshold.serialize(ref params);
allowed_strategies.serialize(ref params);

// setup for voter1
let author = leaf1.address;
let mut indexed_params = array![];
leaf1.serialize(ref indexed_params);
proof1.serialize(ref indexed_params);
let used_strategy = IndexedStrategy { index: 0, params: indexed_params, };
let used_strategies = array![used_strategy.clone()];
let mut user_params = array![];
used_strategies.serialize(ref user_params);

let is_validated = contract.validate(author, params.span(), user_params.span());
assert(!is_validated, 'should not have enough VP');

// setup for voter2
let author = leaf2.address;
let mut indexed_params = array![];
leaf2.serialize(ref indexed_params);
proof2.serialize(ref indexed_params);
let used_strategy = IndexedStrategy { index: 0, params: indexed_params, };
let used_strategies = array![used_strategy.clone()];
let mut user_params = array![];
used_strategies.serialize(ref user_params);

let is_validated = contract.validate(author, params.span(), user_params.span());
assert(is_validated, 'should have enough VP1');

// setup for voter3
let author = leaf3.address;
let mut indexed_params = array![];
leaf3.serialize(ref indexed_params);
proof3.serialize(ref indexed_params);
let used_strategy = IndexedStrategy { index: 0, params: indexed_params, };
let used_strategies = array![used_strategy.clone()];
let mut user_params = array![];
used_strategies.serialize(ref user_params);

let is_validated = contract.validate(author, params.span(), user_params.span());
assert(is_validated, 'should have enough VP2');

// -- Now let's mix merkle and vanilla voting strategies --

// deploy vanilla voting strategy
let (vanilla_contract, _) = deploy_syscall(
VanillaVotingStrategy::TEST_CLASS_HASH.try_into().unwrap(), 0, array![].span(), false
)
.unwrap();

let vanilla_strategy = Strategy { address: vanilla_contract, params: array![], };

let allowed_strategies = array![
merkle_whitelist_strategy.clone(), vanilla_strategy.clone()
]; // update allowed strategies
let proposal_threshold = proposal_threshold; // threshold is left unchanged
let mut params = array![]; // update params
proposal_threshold.serialize(ref params);
allowed_strategies.serialize(ref params);

// voter 1 should now have enough voting power!
let author = leaf1.address;
let vanilla = IndexedStrategy { index: 1, params: array![], };
let mut indexed_params = array![];
leaf1.serialize(ref indexed_params);
proof1.serialize(ref indexed_params);
let merkle = IndexedStrategy { index: 0, params: indexed_params, };

let used_strategies = array![vanilla.clone(), merkle.clone()];
let mut user_params = array![];
used_strategies.serialize(ref user_params);

let is_validated = contract.validate(author, params.span(), user_params.span());
assert(is_validated, 'should have enough VP2');

// and a random voter that doesn't use the whitelist should not have enough VP
let author = UserAddress::Starknet(contract_address_const::<0x123456789>());
let used_strategies = array![vanilla.clone()];
let mut user_params = array![];
used_strategies.serialize(ref user_params);

let is_validated = contract.validate(author, params.span(), user_params.span());
assert(!is_validated, 'should not have enough VP');
}

fn strategy_from_contract(token_contract: ContractAddress) -> Strategy {
let (contract, _) = deploy_syscall(
Expand Down

0 comments on commit c9f9bae

Please sign in to comment.