From d500ab69abdecdb1f106759368bef6cca0f12d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Fri, 27 Oct 2023 07:20:09 +0300 Subject: [PATCH 1/6] Governance-v2: Fix GovernanceProposal default --- energy-integration/governance-v2/src/proposal.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/energy-integration/governance-v2/src/proposal.rs b/energy-integration/governance-v2/src/proposal.rs index 8c60bc280..9ba50798c 100644 --- a/energy-integration/governance-v2/src/proposal.rs +++ b/energy-integration/governance-v2/src/proposal.rs @@ -65,3 +65,18 @@ pub struct GovernanceProposal { pub proposal_start_block: u64, pub fee_withdrawn: bool, } + + +impl Default for GovernanceProposal { + fn default() -> Self { + Self::new() + } +} + +impl GovernanceProposal { + pub fn new() -> Self { + GovernanceProposal { + ..Default::default() + } + } +} From b9361ef7c0c5140f10806b7ef21d1874e93468da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 30 Oct 2023 12:05:16 +0200 Subject: [PATCH 2/6] Governance-v2: Fix default GovernanceProposal --- .../governance-v2/src/proposal.rs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/energy-integration/governance-v2/src/proposal.rs b/energy-integration/governance-v2/src/proposal.rs index 9ba50798c..3c988b067 100644 --- a/energy-integration/governance-v2/src/proposal.rs +++ b/energy-integration/governance-v2/src/proposal.rs @@ -50,7 +50,7 @@ impl From> for GovernanceAction } } -#[derive(TypeAbi, TopEncode, TopDecode, PartialEq, Debug)] +#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Debug)] pub struct GovernanceProposal { pub proposal_id: usize, pub proposer: ManagedAddress, @@ -66,7 +66,6 @@ pub struct GovernanceProposal { pub fee_withdrawn: bool, } - impl Default for GovernanceProposal { fn default() -> Self { Self::new() @@ -76,7 +75,22 @@ impl Default for GovernanceProposal { impl GovernanceProposal { pub fn new() -> Self { GovernanceProposal { - ..Default::default() + proposal_id: 0, + proposer: ManagedAddress::default(), + actions: ArrayVec::default(), + description: ManagedBuffer::default(), + fee_payment: EsdtTokenPayment { + token_identifier: TokenIdentifier::from(""), + token_nonce: 0, + amount: BigUint::zero(), + }, + minimum_quorum: BigUint::default(), + voting_delay_in_blocks: 0, + voting_period_in_blocks: 0, + withdraw_percentage_defeated: 0, + total_quorum: BigUint::default(), + proposal_start_block: 0, + fee_withdrawn: false, } } } From 4c73589342600c770ed30264e816569a75c8b24b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 30 Oct 2023 14:05:44 +0200 Subject: [PATCH 3/6] Governance-v2: Update proposal_id consistency --- energy-integration/governance-v2/tests/gov_rust_test.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/energy-integration/governance-v2/tests/gov_rust_test.rs b/energy-integration/governance-v2/tests/gov_rust_test.rs index 929e36945..871060d4a 100644 --- a/energy-integration/governance-v2/tests/gov_rust_test.rs +++ b/energy-integration/governance-v2/tests/gov_rust_test.rs @@ -730,6 +730,11 @@ fn gov_propose_cancel_proposal_id_test() { &min_fee, None, ); + assert_eq!(proposal_id, 3); + gov_setup + .check_proposal_id_consistency(&first_user_addr, proposal_id) + .assert_ok(); + // Proposal ID = 4 let (result, proposal_id) = gov_setup.propose( From 77c159b7c9d10110dcd6c9ce957503fe2fdde750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 30 Oct 2023 15:24:33 +0200 Subject: [PATCH 4/6] Governance-v2: Retrieve cancelled proposal --- energy-integration/governance-v2/tests/gov_rust_test.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/energy-integration/governance-v2/tests/gov_rust_test.rs b/energy-integration/governance-v2/tests/gov_rust_test.rs index 871060d4a..3ec147d73 100644 --- a/energy-integration/governance-v2/tests/gov_rust_test.rs +++ b/energy-integration/governance-v2/tests/gov_rust_test.rs @@ -722,6 +722,11 @@ fn gov_propose_cancel_proposal_id_test() { gov_setup.cancel_proposal(&first_user_addr, 2).assert_ok(); + // Try to retrieve the cancelled proposal + gov_setup.b_mock.execute_tx(&gov_setup.first_user.clone(), &gov_setup.gov_wrapper, &rust_biguint!(0), |sc| { + sc.proposals().get(2); + }).assert_ok(); + // Check proposer balance (fee should be refunded) gov_setup.b_mock.check_nft_balance::( &first_user_addr, From 6d87cf574510d900a7dd329875fac99ae350f88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 30 Oct 2023 15:32:11 +0200 Subject: [PATCH 5/6] Governance-v2: Fix GovernanceProposal default --- .../governance-v2/src/proposal.rs | 12 ++++++-- .../governance-v2/tests/gov_rust_test.rs | 29 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/energy-integration/governance-v2/src/proposal.rs b/energy-integration/governance-v2/src/proposal.rs index 3c988b067..8237331cd 100644 --- a/energy-integration/governance-v2/src/proposal.rs +++ b/energy-integration/governance-v2/src/proposal.rs @@ -1,3 +1,5 @@ +use multiversx_sc::codec::{EncodeDefault, DecodeDefault}; + multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -50,7 +52,7 @@ impl From> for GovernanceAction } } -#[derive(TypeAbi, TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Debug)] +#[derive(TypeAbi, NestedEncode, NestedDecode, PartialEq, Debug, TopEncodeOrDefault, TopDecodeOrDefault)] pub struct GovernanceProposal { pub proposal_id: usize, pub proposer: ManagedAddress, @@ -66,7 +68,13 @@ pub struct GovernanceProposal { pub fee_withdrawn: bool, } -impl Default for GovernanceProposal { +impl EncodeDefault for GovernanceProposal { + fn is_default(&self) -> bool { + self.proposal_id == 0 + } +} + +impl DecodeDefault for GovernanceProposal { fn default() -> Self { Self::new() } diff --git a/energy-integration/governance-v2/tests/gov_rust_test.rs b/energy-integration/governance-v2/tests/gov_rust_test.rs index 3ec147d73..24b3ed517 100644 --- a/energy-integration/governance-v2/tests/gov_rust_test.rs +++ b/energy-integration/governance-v2/tests/gov_rust_test.rs @@ -723,9 +723,17 @@ fn gov_propose_cancel_proposal_id_test() { gov_setup.cancel_proposal(&first_user_addr, 2).assert_ok(); // Try to retrieve the cancelled proposal - gov_setup.b_mock.execute_tx(&gov_setup.first_user.clone(), &gov_setup.gov_wrapper, &rust_biguint!(0), |sc| { - sc.proposals().get(2); - }).assert_ok(); + gov_setup + .b_mock + .execute_tx( + &gov_setup.first_user.clone(), + &gov_setup.gov_wrapper, + &rust_biguint!(0), + |sc| { + sc.proposals().get(2); + }, + ) + .assert_ok(); // Check proposer balance (fee should be refunded) gov_setup.b_mock.check_nft_balance::( @@ -740,7 +748,6 @@ fn gov_propose_cancel_proposal_id_test() { .check_proposal_id_consistency(&first_user_addr, proposal_id) .assert_ok(); - // Proposal ID = 4 let (result, proposal_id) = gov_setup.propose( &first_user_addr, @@ -757,6 +764,20 @@ fn gov_propose_cancel_proposal_id_test() { gov_setup.cancel_proposal(&first_user_addr, 4).assert_ok(); + // Try to retrieve the cancelled proposal + gov_setup + .b_mock + .execute_tx( + &gov_setup.first_user.clone(), + &gov_setup.gov_wrapper, + &rust_biguint!(0), + |sc| { + sc.proposals().get(4); + }, + ) + .assert_ok(); + + // Proposal ID = 5 let (result, proposal_id) = gov_setup.propose( &first_user_addr, From 75ec9051666fa570e5ab9fe358dbcb43fcdd04cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Tue, 31 Oct 2023 08:56:15 +0200 Subject: [PATCH 6/6] Governance-v2: Clippy fix + format --- energy-integration/governance-v2/src/proposal.rs | 12 ++++++++++-- .../governance-v2/tests/gov_rust_test.rs | 5 ++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/energy-integration/governance-v2/src/proposal.rs b/energy-integration/governance-v2/src/proposal.rs index 8237331cd..94c66a131 100644 --- a/energy-integration/governance-v2/src/proposal.rs +++ b/energy-integration/governance-v2/src/proposal.rs @@ -1,4 +1,4 @@ -use multiversx_sc::codec::{EncodeDefault, DecodeDefault}; +use multiversx_sc::codec::{DecodeDefault, EncodeDefault}; multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -52,7 +52,9 @@ impl From> for GovernanceAction } } -#[derive(TypeAbi, NestedEncode, NestedDecode, PartialEq, Debug, TopEncodeOrDefault, TopDecodeOrDefault)] +#[derive( + TypeAbi, NestedEncode, NestedDecode, PartialEq, Debug, TopEncodeOrDefault, TopDecodeOrDefault, +)] pub struct GovernanceProposal { pub proposal_id: usize, pub proposer: ManagedAddress, @@ -80,6 +82,12 @@ impl DecodeDefault for GovernanceProposal { } } +impl Default for GovernanceProposal { + fn default() -> Self { + Self::new() + } +} + impl GovernanceProposal { pub fn new() -> Self { GovernanceProposal { diff --git a/energy-integration/governance-v2/tests/gov_rust_test.rs b/energy-integration/governance-v2/tests/gov_rust_test.rs index 24b3ed517..573278722 100644 --- a/energy-integration/governance-v2/tests/gov_rust_test.rs +++ b/energy-integration/governance-v2/tests/gov_rust_test.rs @@ -764,8 +764,8 @@ fn gov_propose_cancel_proposal_id_test() { gov_setup.cancel_proposal(&first_user_addr, 4).assert_ok(); - // Try to retrieve the cancelled proposal - gov_setup + // Try to retrieve the cancelled proposal + gov_setup .b_mock .execute_tx( &gov_setup.first_user.clone(), @@ -777,7 +777,6 @@ fn gov_propose_cancel_proposal_id_test() { ) .assert_ok(); - // Proposal ID = 5 let (result, proposal_id) = gov_setup.propose( &first_user_addr,