From 870b5281af8cbe96bbeadbd206f6106e3d75f7c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 24 Jul 2023 15:18:40 +0300 Subject: [PATCH 1/2] Governance-v2: Fix div by 0 error + unit test --- energy-integration/governance-v2/src/views.rs | 4 ++ .../governance-v2/tests/gov_rust_test.rs | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/energy-integration/governance-v2/src/views.rs b/energy-integration/governance-v2/src/views.rs index f5e93b2aa..c17e62687 100644 --- a/energy-integration/governance-v2/src/views.rs +++ b/energy-integration/governance-v2/src/views.rs @@ -83,6 +83,10 @@ pub trait ViewsModule: let proposal = self.proposals().get(proposal_id); let total_energy_for_proposal = proposal.total_energy; + if total_energy_for_proposal == 0u64 { + return false + } + let required_minimum_percentage = proposal.minimum_quorum; let current_quorum = self.proposal_votes(proposal_id).get().quorum; diff --git a/energy-integration/governance-v2/tests/gov_rust_test.rs b/energy-integration/governance-v2/tests/gov_rust_test.rs index 50f88b42d..91d79c595 100644 --- a/energy-integration/governance-v2/tests/gov_rust_test.rs +++ b/energy-integration/governance-v2/tests/gov_rust_test.rs @@ -82,6 +82,44 @@ fn gov_propose_test() { .assert_ok(); } +#[test] +fn gov_propose_total_energy_0_test() { + let mut gov_setup = GovSetup::new(governance_v2::contract_obj); + + let first_user_addr = gov_setup.first_user.clone(); + let sc_addr = gov_setup.gov_wrapper.address_ref().clone(); + let min_fee = rust_biguint!(MIN_FEE_FOR_PROPOSE) * DECIMALS_CONST; + // Give proposer the minimum fee + gov_setup + .b_mock + .set_nft_balance(&first_user_addr, WXMEX_TOKEN_ID, 1, &min_fee, &Empty); + + let (result, proposal_id) = gov_setup.propose( + &first_user_addr, + &min_fee, + &sc_addr, + b"changeTODO", + vec![1_000u64.to_be_bytes().to_vec()], + ); + result.assert_ok(); + assert_eq!(proposal_id, 1); + gov_setup.increment_block_nonce(VOTING_PERIOD_BLOCKS + VOTING_DELAY_BLOCKS); + + gov_setup + .b_mock + .execute_query(&gov_setup.gov_wrapper, |sc| { + let mut proposal = sc.proposals().get(1); + proposal.total_energy = managed_biguint!(0); + sc.proposals().set(1, &proposal); + assert!( + sc.get_proposal_status(1) == GovernanceProposalStatus::Defeated, + "Action should have been Defeated" + ); + }) + .assert_ok(); +} + + #[test] fn gov_no_veto_vote_test() { let mut gov_setup = GovSetup::new(governance_v2::contract_obj); From 932b88a27d34b61a1ff1d84c398aaad7f667bf6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 24 Jul 2023 18:43:59 +0300 Subject: [PATCH 2/2] governance-v2: File compilation updates --- energy-integration/governance-v2/elrond.json | 3 --- energy-integration/governance-v2/wasm/src/lib.rs | 8 ++------ 2 files changed, 2 insertions(+), 9 deletions(-) delete mode 100644 energy-integration/governance-v2/elrond.json diff --git a/energy-integration/governance-v2/elrond.json b/energy-integration/governance-v2/elrond.json deleted file mode 100644 index 736553962..000000000 --- a/energy-integration/governance-v2/elrond.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "language": "rust" -} \ No newline at end of file diff --git a/energy-integration/governance-v2/wasm/src/lib.rs b/energy-integration/governance-v2/wasm/src/lib.rs index f8dfaa7d6..e70fd2cb9 100644 --- a/energy-integration/governance-v2/wasm/src/lib.rs +++ b/energy-integration/governance-v2/wasm/src/lib.rs @@ -5,9 +5,9 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 31 +// Endpoints: 27 // Async Callback (empty): 1 -// Total number of exported functions: 33 +// Total number of exported functions: 29 #![no_std] #![feature(alloc_error_handler, lang_items)] @@ -38,10 +38,6 @@ multiversx_sc_wasm_adapter::endpoints! { getUserVotedProposals getProposalVotes getProposalStatus - getCurrentQuorum - getProposer - getProposalDescription - getProposalActions getFeesCollectorAddress setEnergyFactoryAddress getEnergyFactoryAddress