Skip to content

Commit

Permalink
Add upgrade unit tests (#98)
Browse files Browse the repository at this point in the history
* add 'passed proposals' and 'already passed proposals' upgrades tests

* add 'failed proposal' and 'non-existent proposal' upgrades tests

* use prank instead of start_prank
  • Loading branch information
ptisserand authored Jun 1, 2024
1 parent de09649 commit dc23641
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
8 changes: 4 additions & 4 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ Runs tests on a specific, already deployed contract.

### Unit tests

[] Apply a passed proposal
[x] Apply a passed proposal

Test that a proposal marked as passed is correctly applied, including the appropriate contract upgrade.

[] Apply already applied proposal
[x] Apply already applied proposal

Ensure the contract rejects applying a proposal that has already been applied.

[] Apply failed proposal
[x] Apply failed proposal

Ensure the contract rejects applying a proposal that did not pass.

[] Apply non-existent proposal
[x] Apply non-existent proposal

Ensure the contract rejects applying a proposal that does not exist.

Expand Down
1 change: 1 addition & 0 deletions tests/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ mod basic;
mod test_treasury;
mod proposals_tests;
mod airdrop_tests;
mod upgrades_tests;
mod setup;
120 changes: 120 additions & 0 deletions tests/upgrades_tests.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use traits::Into;
use traits::TryInto;

use starknet::{ContractAddress, storage_access::storage_address_from_base};

use konoha::contract::IGovernanceDispatcher;
use konoha::contract::IGovernanceDispatcherTrait;
use konoha::proposals::IProposalsDispatcher;
use konoha::proposals::IProposalsDispatcherTrait;
use konoha::upgrades::IUpgradesDispatcher;
use konoha::upgrades::IUpgradesDispatcherTrait;
use konoha::constants;

use snforge_std::{CheatTarget, ContractClassTrait, prank, CheatSpan, get_class_hash};

use super::setup::{admin_addr, deploy_governance, deploy_and_distribute_gov_tokens,};


#[test]
fn test_apply_a_passed_proposal() {
let token_contract = deploy_and_distribute_gov_tokens(admin_addr.try_into().unwrap());
let gov_contract = deploy_governance(token_contract.contract_address);
let gov_contract_addr = gov_contract.contract_address;
let dispatcher = IProposalsDispatcher { contract_address: gov_contract_addr };
let class_hash: felt252 = get_class_hash(token_contract.contract_address).try_into().unwrap();

prank(
CheatTarget::One(gov_contract_addr),
admin_addr.try_into().unwrap(),
CheatSpan::TargetCalls(3)
);
let prop_id = dispatcher.submit_proposal(class_hash, 1); // class hash update
dispatcher.vote(prop_id, 1);

IUpgradesDispatcher { contract_address: gov_contract_addr }.apply_passed_proposal(prop_id);

assert_eq!(
get_class_hash(gov_contract_addr).try_into().unwrap(),
class_hash,
"Worng classhash after upgrade"
);
}

#[test]
#[should_panic(expected: ('Proposal already applied',))]
fn test_apply_an_already_passed_proposal() {
let token_contract = deploy_and_distribute_gov_tokens(admin_addr.try_into().unwrap());
let gov_contract = deploy_governance(token_contract.contract_address);
let gov_contract_addr = gov_contract.contract_address;
let dispatcher = IProposalsDispatcher { contract_address: gov_contract_addr };

prank(
CheatTarget::One(gov_contract_addr),
admin_addr.try_into().unwrap(),
CheatSpan::TargetCalls(4)
);
let prop_id = dispatcher.submit_proposal(42, 4); // no op
dispatcher.vote(prop_id, 1);

IUpgradesDispatcher { contract_address: gov_contract_addr }.apply_passed_proposal(prop_id);

// try to reapply the same proposal
IUpgradesDispatcher { contract_address: gov_contract_addr }.apply_passed_proposal(prop_id);
}

#[test]
#[should_panic(expected: ('prop not passed',))]
fn test_apply_a_failed_proposal() {
let token_contract = deploy_and_distribute_gov_tokens(admin_addr.try_into().unwrap());
let gov_contract = deploy_governance(token_contract.contract_address);
let gov_contract_addr = gov_contract.contract_address;
let dispatcher = IProposalsDispatcher { contract_address: gov_contract_addr };

prank(
CheatTarget::One(gov_contract_addr),
admin_addr.try_into().unwrap(),
CheatSpan::TargetCalls(3)
);
let prop_id = dispatcher.submit_proposal(42, 4); // no op
dispatcher.vote(prop_id, 2);

IUpgradesDispatcher { contract_address: gov_contract_addr }.apply_passed_proposal(prop_id);
}

#[test]
#[should_panic]
fn test_apply_a_non_existent_proposal() {
let token_contract = deploy_and_distribute_gov_tokens(admin_addr.try_into().unwrap());
let gov_contract = deploy_governance(token_contract.contract_address);
let gov_contract_addr = gov_contract.contract_address;
let prop_id = 4269;
prank(
CheatTarget::One(gov_contract_addr),
admin_addr.try_into().unwrap(),
CheatSpan::TargetCalls(1)
);

IUpgradesDispatcher { contract_address: gov_contract_addr }.apply_passed_proposal(prop_id);
}
// #[test]
// fn test_successfull_contract_upgrade() {
// assert_eq!(1, 0, "TODO");
// }
//
// #[test]
// fn test_upgrade_with_custom_proposal_execution() {
// assert_eq!(1, 0, "TODO");
// }
//
// #[test]
// fn test_multiple_contract_upgrades() {
// assert_eq!(1, 0, "TODO");
// }
//
// #[test]
// fn test_event_with_failed_upgrade_attempts() {
// assert_eq!(1, 0, "TODO");
// }


0 comments on commit dc23641

Please sign in to comment.