From 241c99c6e64feb41fb927e10a5d9f671c0af55e0 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 13 Dec 2024 14:33:56 +0200 Subject: [PATCH 01/27] Added new structs for Sovereign Chain Config Signed-off-by: Andrei Baltariu --- common/transaction/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/transaction/src/lib.rs b/common/transaction/src/lib.rs index b17fca1e..6030a8f0 100644 --- a/common/transaction/src/lib.rs +++ b/common/transaction/src/lib.rs @@ -34,6 +34,22 @@ pub type OptionalValueTransferDataTuple = OptionalValue, ManagedVec>>>; pub type StakeMultiArg = MultiValue2, BigUint>; +#[type_abi] +#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] +pub struct StakeArgs { + pub token_id: TokenIdentifier, + pub amount: BigUint, +} + +#[type_abi] +#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] +pub struct SovereignConfig { + pub opt_min_validators: Option, + pub opt_max_validators: Option, + pub opt_min_stake: Option>, + pub opt_additional_stake_required: Option>>, +} + #[type_abi] #[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] pub struct Operation { From 42c8e60f7eb3334eafc7fcdec907f472719084f8 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 13 Dec 2024 14:47:26 +0200 Subject: [PATCH 02/27] Updated `init` and `update` endpoints to use new struct and storage Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 42 ++++++++--------------------- chain-config/src/validator_rules.rs | 6 ++--- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index e7cb54f0..b9bcc5fb 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -1,8 +1,7 @@ #![no_std] use multiversx_sc_modules::only_admin; -use transaction::StakeMultiArg; -use validator_rules::TokenIdAmountPair; +use transaction::{SovereignConfig, StakeArgs}; multiversx_sc::imports!(); @@ -19,54 +18,35 @@ pub trait ChainConfigContract: max_validators: u64, min_stake: BigUint, admin: ManagedAddress, - additional_stake_required: MultiValueEncoded>, + additional_stake_required: MultiValueEncoded>, ) { require!( min_validators <= max_validators, "Invalid min/max validator numbers" ); - let mut additional_stake_vec = ManagedVec::new(); - for multi_value in additional_stake_required { - let (token_id, amount) = multi_value.into_tuple(); - let value = TokenIdAmountPair { token_id, amount }; - - additional_stake_vec.push(value); - } - self.min_validators().set(min_validators); self.max_validators().set(max_validators); self.min_stake().set(min_stake); self.add_admin(admin); - self.additional_stake_required().set(additional_stake_vec); + self.additional_stake_required() + .extend(additional_stake_required); } #[only_admin] - fn update_config( - &self, - opt_min_validators: Option, - opt_max_validators: Option, - opt_min_stake: Option, - opt_additional_stake_required: Option>>, - ) { - if let Some(min_validators) = opt_min_validators { + fn update_config(&self, new_config: SovereignConfig) { + if let Some(min_validators) = new_config.opt_min_validators { self.min_validators().set(min_validators); } - if let Some(max_validators) = opt_max_validators { + if let Some(max_validators) = new_config.opt_max_validators { self.max_validators().set(max_validators); } - if let Some(min_stake) = opt_min_stake { + if let Some(min_stake) = new_config.opt_min_stake { self.min_stake().set(min_stake); } - if let Some(additional_stake_required) = opt_additional_stake_required { - let mut additional_stake_vec = ManagedVec::new(); - for multi_value in additional_stake_required { - let (token_id, amount) = multi_value.into_tuple(); - let value = TokenIdAmountPair { token_id, amount }; - - additional_stake_vec.push(value); - } - self.additional_stake_required().set(additional_stake_vec); + if let Some(additional_stake_required) = new_config.opt_additional_stake_required { + self.additional_stake_required() + .extend(&additional_stake_required); } } diff --git a/chain-config/src/validator_rules.rs b/chain-config/src/validator_rules.rs index 334f5392..8d2dd931 100644 --- a/chain-config/src/validator_rules.rs +++ b/chain-config/src/validator_rules.rs @@ -1,3 +1,5 @@ +use transaction::StakeArgs; + multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -49,9 +51,7 @@ pub trait ValidatorRulesModule { // TODO: Read user stake and verify #[view(getAdditionalStakeRequired)] #[storage_mapper("additionalStakeRequired")] - fn additional_stake_required( - &self, - ) -> SingleValueMapper>>; + fn additional_stake_required(&self) -> UnorderedSetMapper>; #[view(wasPreviouslySlashed)] #[storage_mapper("wasPreviouslySlashed")] From 50d668b0c1fc118690bfb1d683a225578cb62a77 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 13 Dec 2024 15:57:05 +0200 Subject: [PATCH 03/27] Removed any use of the `StakeMultiArg` Signed-off-by: Andrei Baltariu --- Cargo.lock | 1 + chain-factory/src/factory.rs | 4 ++-- chain-factory/tests/chain_factory_tests.rs | 16 +++++++--------- common/proxies/src/chain_config_proxy.rs | 14 ++------------ common/proxies/src/chain_factory_proxy.rs | 2 +- common/proxies/src/sovereign_forge_proxy.rs | 2 +- common/transaction/src/lib.rs | 14 +++++++++++++- sovereign-forge/interactor/Cargo.toml | 3 +++ sovereign-forge/interactor/src/interact.rs | 9 ++------- sovereign-forge/src/common/sc_deploy.rs | 4 ++-- sovereign-forge/src/phases.rs | 4 ++-- .../tests/sovereign_forge_unit_tests.rs | 4 ++-- 12 files changed, 38 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1971f644..4792425b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -691,6 +691,7 @@ dependencies = [ "serde", "sovereign-forge", "toml", + "transaction", ] [[package]] diff --git a/chain-factory/src/factory.rs b/chain-factory/src/factory.rs index 3021d809..8b98cb7e 100644 --- a/chain-factory/src/factory.rs +++ b/chain-factory/src/factory.rs @@ -7,7 +7,7 @@ use proxies::{ fee_market_proxy::{FeeMarketProxy, FeeStruct}, header_verifier_proxy::HeaderverifierProxy, }; -use transaction::StakeMultiArg; +use transaction::StakeArgs; multiversx_sc::derive_imports!(); #[multiversx_sc::module] @@ -19,7 +19,7 @@ pub trait FactoryModule: only_admin::OnlyAdminModule { min_validators: u64, max_validators: u64, min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + additional_stake_required: MultiValueEncoded>, ) -> ManagedAddress { let caller = self.blockchain().get_caller(); let source_address = self.chain_config_template().get(); diff --git a/chain-factory/tests/chain_factory_tests.rs b/chain-factory/tests/chain_factory_tests.rs index d2618810..b4165567 100644 --- a/chain-factory/tests/chain_factory_tests.rs +++ b/chain-factory/tests/chain_factory_tests.rs @@ -9,7 +9,7 @@ use multiversx_sc_scenario::{ use proxies::{ chain_config_proxy::ChainConfigContractProxy, chain_factory_proxy::ChainFactoryContractProxy, }; -use transaction::StakeMultiArg; +use transaction::StakeArgs; const FACTORY_ADDRESS: TestSCAddress = TestSCAddress::new("chain-factory"); const CODE_PATH: MxscPath = MxscPath::new("output/chain-factory.mxsc.json"); @@ -38,9 +38,7 @@ struct ChainFactoryTestState { } impl ChainFactoryTestState { - fn new( - additional_stake_required: &MultiValueEncoded>, - ) -> Self { + fn new(additional_stake_required: &MultiValueEncoded>) -> Self { let mut world = world(); world.account(OWNER).balance(OWNER_BALANCE).nonce(1); @@ -87,7 +85,7 @@ impl ChainFactoryTestState { min_validators: usize, max_validators: usize, min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + additional_stake_required: MultiValueEncoded>, expected_result: Option>, ) { let transaction = self @@ -114,8 +112,8 @@ impl ChainFactoryTestState { #[test] fn deploy() { - let additional_stake: StakeMultiArg = - (TokenIdentifier::from("TEST-TOKEN"), BigUint::from(100u64)).into(); + let additional_stake = + StakeArgs::new(TokenIdentifier::from("TEST-TOKEN"), BigUint::from(100u64)); let mut additional_stake_required = MultiValueEncoded::new(); additional_stake_required.push(additional_stake); @@ -125,8 +123,8 @@ fn deploy() { #[test] fn deploy_chain_config_from_factory() { - let additional_stake: StakeMultiArg = - (TokenIdentifier::from("TEST-TOKEN"), BigUint::from(100u64)).into(); + let additional_stake = + StakeArgs::new(TokenIdentifier::from("TEST-TOKEN"), BigUint::from(100u64)); let mut additional_stake_required = MultiValueEncoded::new(); additional_stake_required.push(additional_stake); diff --git a/common/proxies/src/chain_config_proxy.rs b/common/proxies/src/chain_config_proxy.rs index 9e676c53..10bf20a4 100644 --- a/common/proxies/src/chain_config_proxy.rs +++ b/common/proxies/src/chain_config_proxy.rs @@ -48,7 +48,7 @@ where Arg1: ProxyArg, Arg2: ProxyArg>, Arg3: ProxyArg>, - Arg4: ProxyArg, BigUint>>>, + Arg4: ProxyArg>>, >( self, min_validators: Arg0, @@ -126,7 +126,7 @@ where pub fn additional_stake_required( self, - ) -> TxTypedCall>> { + ) -> TxTypedCall>> { self.wrapped_tx .payment(NotPayable) .raw_call("getAdditionalStakeRequired") @@ -194,13 +194,3 @@ where .original_result() } } - -#[type_abi] -#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem)] -pub struct TokenIdAmountPair -where - Api: ManagedTypeApi, -{ - pub token_id: TokenIdentifier, - pub amount: BigUint, -} diff --git a/common/proxies/src/chain_factory_proxy.rs b/common/proxies/src/chain_factory_proxy.rs index f586d96c..23adefda 100644 --- a/common/proxies/src/chain_factory_proxy.rs +++ b/common/proxies/src/chain_factory_proxy.rs @@ -101,7 +101,7 @@ where Arg0: ProxyArg, Arg1: ProxyArg, Arg2: ProxyArg>, - Arg3: ProxyArg, BigUint>>>, + Arg3: ProxyArg>>, >( self, min_validators: Arg0, diff --git a/common/proxies/src/sovereign_forge_proxy.rs b/common/proxies/src/sovereign_forge_proxy.rs index 48de77cb..f8aa0f34 100644 --- a/common/proxies/src/sovereign_forge_proxy.rs +++ b/common/proxies/src/sovereign_forge_proxy.rs @@ -130,7 +130,7 @@ where Arg0: ProxyArg, Arg1: ProxyArg, Arg2: ProxyArg>, - Arg3: ProxyArg, BigUint>>>, + Arg3: ProxyArg>>, >( self, min_validators: Arg0, diff --git a/common/transaction/src/lib.rs b/common/transaction/src/lib.rs index 6030a8f0..e614b188 100644 --- a/common/transaction/src/lib.rs +++ b/common/transaction/src/lib.rs @@ -32,7 +32,6 @@ pub type ExtractedFeeResult = MultiValue2>, ManagedVec>>; pub type OptionalValueTransferDataTuple = OptionalValue, ManagedVec>>>; -pub type StakeMultiArg = MultiValue2, BigUint>; #[type_abi] #[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] @@ -41,6 +40,19 @@ pub struct StakeArgs { pub amount: BigUint, } +impl StakeArgs { + pub fn new(token_id: TokenIdentifier, amount: BigUint) -> Self { + StakeArgs { token_id, amount } + } + + pub fn default() -> Self { + StakeArgs { + token_id: TokenIdentifier::from(""), + amount: BigUint::default(), + } + } +} + #[type_abi] #[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] pub struct SovereignConfig { diff --git a/sovereign-forge/interactor/Cargo.toml b/sovereign-forge/interactor/Cargo.toml index 71b1d326..2a8f67bd 100644 --- a/sovereign-forge/interactor/Cargo.toml +++ b/sovereign-forge/interactor/Cargo.toml @@ -21,6 +21,9 @@ version = "0.54.5" [dependencies.multiversx-sc] version = "0.54.5" +[dependencies.transaction] +path = "../../common/transaction" + [dependencies.proxies] path = "../../common/proxies" diff --git a/sovereign-forge/interactor/src/interact.rs b/sovereign-forge/interactor/src/interact.rs index 81e727ec..44e4ff97 100644 --- a/sovereign-forge/interactor/src/interact.rs +++ b/sovereign-forge/interactor/src/interact.rs @@ -17,6 +17,7 @@ use std::{ io::{Read, Write}, path::Path, }; +use transaction::StakeArgs; const STATE_FILE: &str = "state.toml"; const CHAIN_CONFIG_CODE_PATH: &str = "../../chain-config/output/chain-config.mxsc.json"; @@ -400,13 +401,7 @@ impl ContractInteract { let min_validators = 1u64; let max_validators = 3u64; let min_stake = BigUint::::from(0u128); - let additional_stake_required = MultiValueVec::from(vec![MultiValue2::< - TokenIdentifier, - BigUint, - >::from(( - TokenIdentifier::from_esdt_bytes(&b""[..]), - BigUint::::from(0u128), - ))]); + let additional_stake_required = MultiValueVec::from(vec![StakeArgs::default()]); let response = self .interactor diff --git a/sovereign-forge/src/common/sc_deploy.rs b/sovereign-forge/src/common/sc_deploy.rs index 765013de..7ec2ff61 100644 --- a/sovereign-forge/src/common/sc_deploy.rs +++ b/sovereign-forge/src/common/sc_deploy.rs @@ -1,7 +1,7 @@ use crate::err_msg; use multiversx_sc::types::{MultiValueEncoded, ReturnsResult}; use proxies::{chain_factory_proxy::ChainFactoryContractProxy, fee_market_proxy::FeeStruct}; -use transaction::StakeMultiArg; +use transaction::StakeArgs; #[multiversx_sc::module] pub trait ScDeployModule: super::utils::UtilsModule + super::storage::StorageModule { @@ -11,7 +11,7 @@ pub trait ScDeployModule: super::utils::UtilsModule + super::storage::StorageMod min_validators: u64, max_validators: u64, min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + additional_stake_required: MultiValueEncoded>, ) -> ManagedAddress { self.tx() .to(self.get_chain_factory_address()) diff --git a/sovereign-forge/src/phases.rs b/sovereign-forge/src/phases.rs index 777a69a4..4c722bfc 100644 --- a/sovereign-forge/src/phases.rs +++ b/sovereign-forge/src/phases.rs @@ -1,9 +1,9 @@ use crate::err_msg; use core::ops::Deref; use proxies::fee_market_proxy::FeeStruct; -use transaction::StakeMultiArg; use multiversx_sc::{require, types::MultiValueEncoded}; +use transaction::StakeArgs; use crate::common::{ self, @@ -49,7 +49,7 @@ pub trait PhasesModule: min_validators: u64, max_validators: u64, min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + additional_stake_required: MultiValueEncoded>, ) { self.require_setup_complete(); diff --git a/sovereign-forge/tests/sovereign_forge_unit_tests.rs b/sovereign-forge/tests/sovereign_forge_unit_tests.rs index c6af19e5..23a3bbfc 100644 --- a/sovereign-forge/tests/sovereign_forge_unit_tests.rs +++ b/sovereign-forge/tests/sovereign_forge_unit_tests.rs @@ -16,7 +16,7 @@ use sovereign_forge::common::{ storage::StorageModule, utils::{ScArray, UtilsModule}, }; -use transaction::StakeMultiArg; +use transaction::StakeArgs; const FORGE_ADDRESS: TestSCAddress = TestSCAddress::new("sovereign-forge"); const FORGE_CODE_PATH: MxscPath = MxscPath::new("output/sovereign-forge.mxsc.json"); @@ -244,7 +244,7 @@ impl SovereignForgeTestState { min_validators: u64, max_validators: u64, min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + additional_stake_required: MultiValueEncoded>, expected_result: Option, ) { let transaction = self From cd3b25490b117e98a3b5508763f92c8d08c6f10c Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 13 Dec 2024 16:22:45 +0200 Subject: [PATCH 04/27] Clippy fix Signed-off-by: Andrei Baltariu --- common/transaction/src/lib.rs | 2 +- sovereign-forge/interactor/src/interact.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/common/transaction/src/lib.rs b/common/transaction/src/lib.rs index e614b188..01321156 100644 --- a/common/transaction/src/lib.rs +++ b/common/transaction/src/lib.rs @@ -45,7 +45,7 @@ impl StakeArgs { StakeArgs { token_id, amount } } - pub fn default() -> Self { + pub fn get_default() -> Self { StakeArgs { token_id: TokenIdentifier::from(""), amount: BigUint::default(), diff --git a/sovereign-forge/interactor/src/interact.rs b/sovereign-forge/interactor/src/interact.rs index 44e4ff97..996be5e1 100644 --- a/sovereign-forge/interactor/src/interact.rs +++ b/sovereign-forge/interactor/src/interact.rs @@ -401,7 +401,7 @@ impl ContractInteract { let min_validators = 1u64; let max_validators = 3u64; let min_stake = BigUint::::from(0u128); - let additional_stake_required = MultiValueVec::from(vec![StakeArgs::default()]); + let additional_stake_required = MultiValueVec::from(vec![StakeArgs::get_default()]); let response = self .interactor From d86f2d28d72a591aefa5d10ab1ea07c447a0c42b Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 10:11:08 +0200 Subject: [PATCH 05/27] Chain-Config tests Signed-off-by: Andrei Baltariu --- chain-config/Cargo.toml | 3 + chain-config/tests/chain_config_unit_tests.rs | 63 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 chain-config/tests/chain_config_unit_tests.rs diff --git a/chain-config/Cargo.toml b/chain-config/Cargo.toml index 9f735c71..2e052264 100644 --- a/chain-config/Cargo.toml +++ b/chain-config/Cargo.toml @@ -23,5 +23,8 @@ version = "=0.54.5" [dependencies.transaction] path = "../common/transaction" +[dependencies.proxies] +path = "../common/proxies" + [dependencies.setup-phase] path = "../common/setup-phase" diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs new file mode 100644 index 00000000..47f9cc0e --- /dev/null +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -0,0 +1,63 @@ +use multiversx_sc::types::{BigUint, MultiValueEncoded, TestAddress, TestSCAddress}; +use multiversx_sc_scenario::{api::StaticApi, imports::MxscPath, ScenarioTxRun, ScenarioWorld}; +use proxies::chain_config_proxy::ChainConfigContractProxy; +use transaction::StakeArgs; + +const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("config-address"); +const CONFIG_CODE_PATH: MxscPath = MxscPath::new("output/chain-config.mxsc.json"); + +const OWNER: TestAddress = TestAddress::new("owner"); +const OWNER_BALANCE: u64 = 100_000_000_000; + +fn world() -> ScenarioWorld { + let mut blockchain = ScenarioWorld::new(); + + blockchain.register_contract(CONFIG_CODE_PATH, chain_config::ContractBuilder); + + blockchain +} + +struct ChainConfigTestState { + world: ScenarioWorld, +} + +impl ChainConfigTestState { + fn new() -> Self { + let mut world = world(); + + world.account(OWNER).balance(OWNER_BALANCE).nonce(1); + + Self { world } + } + + fn deploy_chain_config( + &mut self, + min_validators: u64, + max_validators: u64, + min_stake: BigUint, + admin: TestAddress, + additional_stake_required: MultiValueEncoded>, + ) { + self.world + .tx() + .from(OWNER) + .typed(ChainConfigContractProxy) + .init( + min_validators, + max_validators, + min_stake, + admin, + additional_stake_required, + ) + .code(CONFIG_CODE_PATH) + .new_address(CONFIG_ADDRESS) + .run(); + } +} + +#[test] +fn deploy_chain_config() { + let mut state = ChainConfigTestState::new(); + + state.deploy_chain_config(0, 1, BigUint::default(), OWNER, MultiValueEncoded::new()); +} From e10eb27ba1957ca762ddd7ad92e2fdfd4be97785 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 10:11:18 +0200 Subject: [PATCH 06/27] Updated root `Cargo.lock` Signed-off-by: Andrei Baltariu --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 4792425b..e42fe2ba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -234,6 +234,7 @@ dependencies = [ "multiversx-sc-modules", "multiversx-sc-scenario", "num-bigint", + "proxies", "setup-phase", "transaction", ] From 4f2aeeb5548da8974810191eeec6c344230ce21f Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 11:23:39 +0200 Subject: [PATCH 07/27] Modified logic for config update Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 43 ++++++++++++++--------------- chain-config/src/validator_rules.rs | 19 ++++++++++--- common/transaction/src/lib.rs | 6 ++-- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index b9bcc5fb..3c12254f 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -1,7 +1,7 @@ #![no_std] use multiversx_sc_modules::only_admin; -use transaction::{SovereignConfig, StakeArgs}; +use transaction::SovereignConfig; multiversx_sc::imports!(); @@ -12,38 +12,37 @@ pub trait ChainConfigContract: validator_rules::ValidatorRulesModule + only_admin::OnlyAdminModule + setup_phase::SetupPhaseModule { #[init] - fn init( - &self, - min_validators: u64, - max_validators: u64, - min_stake: BigUint, - admin: ManagedAddress, - additional_stake_required: MultiValueEncoded>, - ) { + fn init(&self, config: SovereignConfig, admin: ManagedAddress) { require!( - min_validators <= max_validators, + config.min_validators <= config.max_validators, "Invalid min/max validator numbers" ); - self.min_validators().set(min_validators); - self.max_validators().set(max_validators); - self.min_stake().set(min_stake); + self.min_validators().set(config.min_validators); + self.max_validators().set(config.max_validators); + self.min_stake().set(config.min_stake); self.add_admin(admin); - self.additional_stake_required() - .extend(additional_stake_required); + + if let Some(additional_stake_required) = config.opt_additional_stake_required { + self.additional_stake_required() + .extend(&additional_stake_required); + } } #[only_admin] fn update_config(&self, new_config: SovereignConfig) { - if let Some(min_validators) = new_config.opt_min_validators { - self.min_validators().set(min_validators); + if !self.is_new_min_validators_value(new_config.min_validators) { + self.min_validators().set(new_config.min_validators); } - if let Some(max_validators) = new_config.opt_max_validators { - self.max_validators().set(max_validators); + + if !self.is_new_max_validators_value(new_config.max_validators) { + self.max_validators().set(new_config.max_validators); } - if let Some(min_stake) = new_config.opt_min_stake { - self.min_stake().set(min_stake); + + if !self.is_new_min_stake_value(&new_config.min_stake) { + self.min_stake().set(new_config.min_stake); } + if let Some(additional_stake_required) = new_config.opt_additional_stake_required { self.additional_stake_required() .extend(&additional_stake_required); @@ -58,7 +57,7 @@ pub trait ChainConfigContract: self.require_config_set(); // validator set in header verifier - // change ownership to header-verifier + // self.owner self.setup_phase_complete().set(true); } diff --git a/chain-config/src/validator_rules.rs b/chain-config/src/validator_rules.rs index 8d2dd931..e715e30c 100644 --- a/chain-config/src/validator_rules.rs +++ b/chain-config/src/validator_rules.rs @@ -28,10 +28,21 @@ pub trait ValidatorRulesModule { !self.min_stake().is_empty(), "The mininum number of stake is not set" ); - require!( - !self.additional_stake_required().is_empty(), - "The additional stake criteria is not set" - ); + } + + #[inline] + fn is_new_min_validators_value(&self, new_min_validatrs: u64) -> bool { + self.min_validators().get() == new_min_validatrs + } + + #[inline] + fn is_new_max_validators_value(&self, new_max_validatrs: u64) -> bool { + self.max_validators().get() == new_max_validatrs + } + + #[inline] + fn is_new_min_stake_value(&self, min_stake: &BigUint) -> bool { + self.min_stake().get() == *min_stake } #[view(getMinValidators)] diff --git a/common/transaction/src/lib.rs b/common/transaction/src/lib.rs index 01321156..52791f0a 100644 --- a/common/transaction/src/lib.rs +++ b/common/transaction/src/lib.rs @@ -56,9 +56,9 @@ impl StakeArgs { #[type_abi] #[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] pub struct SovereignConfig { - pub opt_min_validators: Option, - pub opt_max_validators: Option, - pub opt_min_stake: Option>, + pub min_validators: u64, + pub max_validators: u64, + pub min_stake: BigUint, pub opt_additional_stake_required: Option>>, } From 195f96bf342f9aa6fee7d8c31b14705dbb73b8c5 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 11:49:06 +0200 Subject: [PATCH 08/27] Added ownership change transaction Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index 3c12254f..2a712d8c 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -50,14 +50,18 @@ pub trait ChainConfigContract: } #[only_owner] - fn complete_setup_phase(&self) { + fn complete_setup_phase(&self, header_verifier_address: ManagedAddress) { if self.is_setup_phase_complete() { return; } self.require_config_set(); // validator set in header verifier - // self.owner + self.tx() + .to(ESDTSystemSCAddress) + .typed(UserBuiltinProxy) + .change_owner_address(&header_verifier_address) + .sync_call(); self.setup_phase_complete().set(true); } From 2baa979bba7d37559a7dfaba809469b583eec2b7 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 11:52:04 +0200 Subject: [PATCH 09/27] Whiteline Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index 2a712d8c..e04e8558 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -62,6 +62,7 @@ pub trait ChainConfigContract: .typed(UserBuiltinProxy) .change_owner_address(&header_verifier_address) .sync_call(); + self.setup_phase_complete().set(true); } From ce60191b66a5a8acee6b400522db1cbc214a5659 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 11:58:53 +0200 Subject: [PATCH 10/27] Build + proxy Signed-off-by: Andrei Baltariu --- common/proxies/src/chain_config_proxy.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/common/proxies/src/chain_config_proxy.rs b/common/proxies/src/chain_config_proxy.rs index 10bf20a4..6f7092f7 100644 --- a/common/proxies/src/chain_config_proxy.rs +++ b/common/proxies/src/chain_config_proxy.rs @@ -44,27 +44,18 @@ where Gas: TxGas, { pub fn init< - Arg0: ProxyArg, - Arg1: ProxyArg, - Arg2: ProxyArg>, - Arg3: ProxyArg>, - Arg4: ProxyArg>>, + Arg0: ProxyArg>, + Arg1: ProxyArg>, >( self, - min_validators: Arg0, - max_validators: Arg1, - min_stake: Arg2, - admin: Arg3, - additional_stake_required: Arg4, + config: Arg0, + admin: Arg1, ) -> TxTypedDeploy { self.wrapped_tx .payment(NotPayable) .raw_deploy() - .argument(&min_validators) - .argument(&max_validators) - .argument(&min_stake) + .argument(&config) .argument(&admin) - .argument(&additional_stake_required) .original_result() } } From 1518ac243e816191469e60505d5c766c5411860e Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 12:02:20 +0200 Subject: [PATCH 11/27] Added `new()` function for `SovereignConfig` Signed-off-by: Andrei Baltariu --- common/transaction/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/common/transaction/src/lib.rs b/common/transaction/src/lib.rs index 52791f0a..4deb9392 100644 --- a/common/transaction/src/lib.rs +++ b/common/transaction/src/lib.rs @@ -62,6 +62,22 @@ pub struct SovereignConfig { pub opt_additional_stake_required: Option>>, } +impl SovereignConfig { + pub fn new( + min_validators: u64, + max_validators: u64, + min_stake: BigUint, + opt_additional_stake_required: Option>>, + ) -> Self { + SovereignConfig { + min_validators, + max_validators, + min_stake, + opt_additional_stake_required, + } + } +} + #[type_abi] #[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)] pub struct Operation { From 31d75ab09111ebb478aad8feed2ffe7b88222a45 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 12:04:16 +0200 Subject: [PATCH 12/27] Update chain-factory endpoint to use new struct Signed-off-by: Andrei Baltariu --- chain-factory/src/factory.rs | 15 +++------------ common/proxies/src/chain_factory_proxy.rs | 15 +++------------ 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/chain-factory/src/factory.rs b/chain-factory/src/factory.rs index 8b98cb7e..f1301f01 100644 --- a/chain-factory/src/factory.rs +++ b/chain-factory/src/factory.rs @@ -7,7 +7,7 @@ use proxies::{ fee_market_proxy::{FeeMarketProxy, FeeStruct}, header_verifier_proxy::HeaderverifierProxy, }; -use transaction::StakeArgs; +use transaction::SovereignConfig; multiversx_sc::derive_imports!(); #[multiversx_sc::module] @@ -16,10 +16,7 @@ pub trait FactoryModule: only_admin::OnlyAdminModule { #[endpoint(deploySovereignChainConfigContract)] fn deploy_sovereign_chain_config_contract( &self, - min_validators: u64, - max_validators: u64, - min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + config: SovereignConfig, ) -> ManagedAddress { let caller = self.blockchain().get_caller(); let source_address = self.chain_config_template().get(); @@ -27,13 +24,7 @@ pub trait FactoryModule: only_admin::OnlyAdminModule { self.tx() .typed(ChainConfigContractProxy) - .init( - min_validators, - max_validators, - min_stake, - &caller, - additional_stake_required, - ) + .init(config, &caller) .gas(60_000_000) .from_source(source_address) .code_metadata(metadata) diff --git a/common/proxies/src/chain_factory_proxy.rs b/common/proxies/src/chain_factory_proxy.rs index 23adefda..cb131a56 100644 --- a/common/proxies/src/chain_factory_proxy.rs +++ b/common/proxies/src/chain_factory_proxy.rs @@ -98,24 +98,15 @@ where Gas: TxGas, { pub fn deploy_sovereign_chain_config_contract< - Arg0: ProxyArg, - Arg1: ProxyArg, - Arg2: ProxyArg>, - Arg3: ProxyArg>>, + Arg0: ProxyArg>, >( self, - min_validators: Arg0, - max_validators: Arg1, - min_stake: Arg2, - additional_stake_required: Arg3, + config: Arg0, ) -> TxTypedCall> { self.wrapped_tx .payment(NotPayable) .raw_call("deploySovereignChainConfigContract") - .argument(&min_validators) - .argument(&max_validators) - .argument(&min_stake) - .argument(&additional_stake_required) + .argument(&config) .original_result() } From 2e722c1805331fb611476fdd7e7c984212093202 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 12:07:36 +0200 Subject: [PATCH 13/27] Update unit test + build Signed-off-by: Andrei Baltariu --- chain-config/tests/chain_config_unit_tests.rs | 22 +++++-------------- .../wasm-enshrine-esdt-safe-full/Cargo.lock | 3 ++- .../wasm-enshrine-esdt-safe-view/Cargo.lock | 3 ++- enshrine-esdt-safe/wasm/Cargo.lock | 3 ++- .../wasm-token-handler-full/Cargo.lock | 3 ++- .../wasm-token-handler-view/Cargo.lock | 3 ++- token-handler/wasm/Cargo.lock | 3 ++- 7 files changed, 17 insertions(+), 23 deletions(-) diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index 47f9cc0e..9be79c56 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -1,7 +1,7 @@ use multiversx_sc::types::{BigUint, MultiValueEncoded, TestAddress, TestSCAddress}; use multiversx_sc_scenario::{api::StaticApi, imports::MxscPath, ScenarioTxRun, ScenarioWorld}; use proxies::chain_config_proxy::ChainConfigContractProxy; -use transaction::StakeArgs; +use transaction::{SovereignConfig, StakeArgs}; const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("config-address"); const CONFIG_CODE_PATH: MxscPath = MxscPath::new("output/chain-config.mxsc.json"); @@ -30,25 +30,12 @@ impl ChainConfigTestState { Self { world } } - fn deploy_chain_config( - &mut self, - min_validators: u64, - max_validators: u64, - min_stake: BigUint, - admin: TestAddress, - additional_stake_required: MultiValueEncoded>, - ) { + fn deploy_chain_config(&mut self, config: SovereignConfig, admin: TestAddress) { self.world .tx() .from(OWNER) .typed(ChainConfigContractProxy) - .init( - min_validators, - max_validators, - min_stake, - admin, - additional_stake_required, - ) + .init(config, admin) .code(CONFIG_CODE_PATH) .new_address(CONFIG_ADDRESS) .run(); @@ -59,5 +46,6 @@ impl ChainConfigTestState { fn deploy_chain_config() { let mut state = ChainConfigTestState::new(); - state.deploy_chain_config(0, 1, BigUint::default(), OWNER, MultiValueEncoded::new()); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_chain_config(config, OWNER); } diff --git a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock index 9c9627c2..98e08ff7 100644 --- a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock +++ b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock index d7375f07..fd9482f1 100644 --- a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock +++ b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/enshrine-esdt-safe/wasm/Cargo.lock b/enshrine-esdt-safe/wasm/Cargo.lock index 4412ebd7..b3db833e 100644 --- a/enshrine-esdt-safe/wasm/Cargo.lock +++ b/enshrine-esdt-safe/wasm/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/token-handler/wasm-token-handler-full/Cargo.lock b/token-handler/wasm-token-handler-full/Cargo.lock index 8f224a75..fb43fd0d 100644 --- a/token-handler/wasm-token-handler-full/Cargo.lock +++ b/token-handler/wasm-token-handler-full/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/token-handler/wasm-token-handler-view/Cargo.lock b/token-handler/wasm-token-handler-view/Cargo.lock index 13b9442c..19ae3a96 100644 --- a/token-handler/wasm-token-handler-view/Cargo.lock +++ b/token-handler/wasm-token-handler-view/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/token-handler/wasm/Cargo.lock b/token-handler/wasm/Cargo.lock index f5c6a7b6..3f0f1447 100644 --- a/token-handler/wasm/Cargo.lock +++ b/token-handler/wasm/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] From 866dac51f4d480de627f15ba2f13c2abf70acc42 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 13:52:02 +0200 Subject: [PATCH 14/27] Fixed tests Signed-off-by: Andrei Baltariu --- chain-factory/tests/chain_factory_tests.rs | 60 +++----- common/proxies/src/sovereign_forge_proxy.rs | 15 +- sovereign-forge/interactor/src/interact.rs | 23 +-- sovereign-forge/src/common/sc_deploy.rs | 17 +-- sovereign-forge/src/phases.rs | 17 +-- .../tests/sovereign_forge_unit_tests.rs | 144 ++++-------------- 6 files changed, 63 insertions(+), 213 deletions(-) diff --git a/chain-factory/tests/chain_factory_tests.rs b/chain-factory/tests/chain_factory_tests.rs index b4165567..c6547356 100644 --- a/chain-factory/tests/chain_factory_tests.rs +++ b/chain-factory/tests/chain_factory_tests.rs @@ -9,7 +9,7 @@ use multiversx_sc_scenario::{ use proxies::{ chain_config_proxy::ChainConfigContractProxy, chain_factory_proxy::ChainFactoryContractProxy, }; -use transaction::StakeArgs; +use transaction::{SovereignConfig, StakeArgs}; const FACTORY_ADDRESS: TestSCAddress = TestSCAddress::new("chain-factory"); const CODE_PATH: MxscPath = MxscPath::new("output/chain-factory.mxsc.json"); @@ -44,22 +44,6 @@ impl ChainFactoryTestState { world.account(OWNER).balance(OWNER_BALANCE).nonce(1); // deploy chain-config - world - .tx() - .from(OWNER.to_managed_address()) - .typed(ChainConfigContractProxy) - .init( - 1usize, - 2usize, - managed_biguint!(10), - OWNER.to_managed_address(), - additional_stake_required, - ) - .code(CONFIG_CODE_PATH) - .new_address(CONFIG_ADDRESS) - .code_metadata(CodeMetadata::UPGRADEABLE) - .run(); - Self { world } } @@ -82,10 +66,7 @@ impl ChainFactoryTestState { fn propose_deploy_chain_config_from_factory( &mut self, - min_validators: usize, - max_validators: usize, - min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + config: SovereignConfig, expected_result: Option>, ) { let transaction = self @@ -94,12 +75,7 @@ impl ChainFactoryTestState { .from(CONFIG_ADDRESS) .to(FACTORY_ADDRESS) .typed(ChainFactoryContractProxy) - .deploy_sovereign_chain_config_contract( - min_validators, - max_validators, - min_stake, - additional_stake_required, - ); + .deploy_sovereign_chain_config_contract(config); match expected_result { Some(error) => { @@ -108,6 +84,20 @@ impl ChainFactoryTestState { None => transaction.run(), } } + + fn deploy_chain_config(&mut self) { + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + + self.world + .tx() + .from(OWNER.to_managed_address()) + .typed(ChainConfigContractProxy) + .init(config, OWNER.to_managed_address()) + .code(CONFIG_CODE_PATH) + .new_address(CONFIG_ADDRESS) + .code_metadata(CodeMetadata::UPGRADEABLE) + .run(); + } } #[test] @@ -129,20 +119,12 @@ fn deploy_chain_config_from_factory() { additional_stake_required.push(additional_stake); let mut state = ChainFactoryTestState::new(&additional_stake_required); - - let min_validators = 1; - let max_validators = 4; - let min_stake = BigUint::from(100_000u64); - state.deploy_chain_factory(); + state.deploy_chain_config(); println!("{}", current_dir().unwrap().to_str().unwrap()); - state.propose_deploy_chain_config_from_factory( - min_validators, - max_validators, - min_stake, - additional_stake_required, - None, - ); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + + state.propose_deploy_chain_config_from_factory(config, None); } diff --git a/common/proxies/src/sovereign_forge_proxy.rs b/common/proxies/src/sovereign_forge_proxy.rs index f8aa0f34..47659d41 100644 --- a/common/proxies/src/sovereign_forge_proxy.rs +++ b/common/proxies/src/sovereign_forge_proxy.rs @@ -127,23 +127,14 @@ where } pub fn deploy_phase_one< - Arg0: ProxyArg, - Arg1: ProxyArg, - Arg2: ProxyArg>, - Arg3: ProxyArg>>, + Arg0: ProxyArg>, >( self, - min_validators: Arg0, - max_validators: Arg1, - min_stake: Arg2, - additional_stake_required: Arg3, + config: Arg0, ) -> TxTypedCall { self.wrapped_tx .raw_call("deployPhaseOne") - .argument(&min_validators) - .argument(&max_validators) - .argument(&min_stake) - .argument(&additional_stake_required) + .argument(&config) .original_result() } diff --git a/sovereign-forge/interactor/src/interact.rs b/sovereign-forge/interactor/src/interact.rs index 996be5e1..ac315f2f 100644 --- a/sovereign-forge/interactor/src/interact.rs +++ b/sovereign-forge/interactor/src/interact.rs @@ -17,7 +17,7 @@ use std::{ io::{Read, Write}, path::Path, }; -use transaction::StakeArgs; +use transaction::SovereignConfig; const STATE_FILE: &str = "state.toml"; const CHAIN_CONFIG_CODE_PATH: &str = "../../chain-config/output/chain-config.mxsc.json"; @@ -224,19 +224,14 @@ impl ContractInteract { } pub async fn deploy_chain_config_template(&mut self) { + let config = SovereignConfig::new(0, 1, BigUint::default(), None); let new_address = self .interactor .tx() .from(&self.wallet_address) .gas(50_000_000u64) .typed(ChainConfigContractProxy) - .init( - 1u64, - 2u64, - BigUint::from(100u64), - &self.wallet_address, - MultiValueEncoded::new(), - ) + .init(config, &self.wallet_address) .returns(ReturnsNewAddress) .code(MxscPath::new(CHAIN_CONFIG_CODE_PATH)) .run() @@ -398,10 +393,7 @@ impl ContractInteract { pub async fn deploy_phase_one(&mut self) { let egld_amount = BigUint::::from(100u128); - let min_validators = 1u64; - let max_validators = 3u64; - let min_stake = BigUint::::from(0u128); - let additional_stake_required = MultiValueVec::from(vec![StakeArgs::get_default()]); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); let response = self .interactor @@ -410,12 +402,7 @@ impl ContractInteract { .to(self.state.current_address()) .gas(100_000_000u64) .typed(SovereignForgeProxy) - .deploy_phase_one( - min_validators, - max_validators, - min_stake, - additional_stake_required, - ) + .deploy_phase_one(config) .egld(egld_amount) .returns(ReturnsResultUnmanaged) .run() diff --git a/sovereign-forge/src/common/sc_deploy.rs b/sovereign-forge/src/common/sc_deploy.rs index 7ec2ff61..ae6b851e 100644 --- a/sovereign-forge/src/common/sc_deploy.rs +++ b/sovereign-forge/src/common/sc_deploy.rs @@ -1,27 +1,16 @@ use crate::err_msg; use multiversx_sc::types::{MultiValueEncoded, ReturnsResult}; use proxies::{chain_factory_proxy::ChainFactoryContractProxy, fee_market_proxy::FeeStruct}; -use transaction::StakeArgs; +use transaction::SovereignConfig; #[multiversx_sc::module] pub trait ScDeployModule: super::utils::UtilsModule + super::storage::StorageModule { #[inline] - fn deploy_chain_config( - &self, - min_validators: u64, - max_validators: u64, - min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, - ) -> ManagedAddress { + fn deploy_chain_config(&self, config: SovereignConfig) -> ManagedAddress { self.tx() .to(self.get_chain_factory_address()) .typed(ChainFactoryContractProxy) - .deploy_sovereign_chain_config_contract( - min_validators, - max_validators, - min_stake, - additional_stake_required, - ) + .deploy_sovereign_chain_config_contract(config) .returns(ReturnsResult) .sync_call() } diff --git a/sovereign-forge/src/phases.rs b/sovereign-forge/src/phases.rs index 4c722bfc..b6b46b5b 100644 --- a/sovereign-forge/src/phases.rs +++ b/sovereign-forge/src/phases.rs @@ -3,7 +3,7 @@ use core::ops::Deref; use proxies::fee_market_proxy::FeeStruct; use multiversx_sc::{require, types::MultiValueEncoded}; -use transaction::StakeArgs; +use transaction::SovereignConfig; use crate::common::{ self, @@ -44,13 +44,7 @@ pub trait PhasesModule: #[payable("EGLD")] #[endpoint(deployPhaseOne)] - fn deploy_phase_one( - &self, - min_validators: u64, - max_validators: u64, - min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, - ) { + fn deploy_phase_one(&self, config: SovereignConfig) { self.require_setup_complete(); let call_value = self.call_value().egld_value(); @@ -73,12 +67,7 @@ pub trait PhasesModule: "The Chain-Config contract is already deployed" ); - let chain_config_address = self.deploy_chain_config( - min_validators, - max_validators, - min_stake, - additional_stake_required, - ); + let chain_config_address = self.deploy_chain_config(config); let chain_factory_contract_info = ContractInfo::new(ScArray::ChainConfig, chain_config_address); diff --git a/sovereign-forge/tests/sovereign_forge_unit_tests.rs b/sovereign-forge/tests/sovereign_forge_unit_tests.rs index 23a3bbfc..c7dba427 100644 --- a/sovereign-forge/tests/sovereign_forge_unit_tests.rs +++ b/sovereign-forge/tests/sovereign_forge_unit_tests.rs @@ -16,7 +16,7 @@ use sovereign_forge::common::{ storage::StorageModule, utils::{ScArray, UtilsModule}, }; -use transaction::StakeArgs; +use transaction::SovereignConfig; const FORGE_ADDRESS: TestSCAddress = TestSCAddress::new("sovereign-forge"); const FORGE_CODE_PATH: MxscPath = MxscPath::new("output/sovereign-forge.mxsc.json"); @@ -106,19 +106,13 @@ impl SovereignForgeTestState { } fn deploy_chain_config_template(&mut self) -> &mut Self { - let additional_stake_required = MultiValueEncoded::new(); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); self.world .tx() .from(OWNER_ADDRESS) .typed(ChainConfigContractProxy) - .init( - 1u64, - 2u64, - BigUint::from(1u32), - OWNER_ADDRESS, - additional_stake_required, - ) + .init(config, OWNER_ADDRESS) .code(CONFIG_CODE_PATH) .new_address(CONFIG_ADDRESS) .run(); @@ -241,10 +235,7 @@ impl SovereignForgeTestState { fn deploy_phase_one( &mut self, payment: &BigUint, - min_validators: u64, - max_validators: u64, - min_stake: BigUint, - additional_stake_required: MultiValueEncoded>, + config: &SovereignConfig, expected_result: Option, ) { let transaction = self @@ -253,12 +244,7 @@ impl SovereignForgeTestState { .from(OWNER_ADDRESS) .to(FORGE_ADDRESS) .typed(SovereignForgeProxy) - .deploy_phase_one( - min_validators, - max_validators, - min_stake, - additional_stake_required, - ) + .deploy_phase_one(config) .egld(payment); if let Some(error) = expected_result { @@ -410,13 +396,11 @@ fn deploy_phase_one_deploy_cost_too_low() { state.finish_setup(); let deploy_cost = BigUint::from(1u32); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); state.deploy_phase_one( &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), + &config, Some(ExpectError( 4, "The given deploy cost is not equal to the standard amount", @@ -433,22 +417,12 @@ fn deploy_phase_one_chain_config_already_deployed() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_phase_one( &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); - - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), + &config, Some(ExpectError( 4, "The Chain-Config contract is already deployed", @@ -465,15 +439,9 @@ fn deploy_phase_one() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); + state.deploy_phase_one(&deploy_cost, &config, None); state .world @@ -517,16 +485,9 @@ fn deploy_phase_two() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); - + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); let mut bls_keys = MultiValueEncoded::new(); @@ -556,16 +517,9 @@ fn deploy_phase_two_header_already_deployed() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); - + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); let bls_keys = MultiValueEncoded::new(); @@ -586,16 +540,9 @@ fn deploy_phase_three() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); - + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); state.deploy_esdt_safe_template(); @@ -644,15 +591,8 @@ fn deploy_phase_three_without_phase_two() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); - - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); state.deploy_esdt_safe_template(); @@ -675,15 +615,8 @@ fn deploy_phase_three_already_deployed() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); - - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); state.deploy_esdt_safe_template(); @@ -710,15 +643,8 @@ fn deploy_phase_four() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); - - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); state.deploy_esdt_safe_template(); @@ -753,15 +679,8 @@ fn deploy_phase_four_without_previous_phase() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); - - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); state.deploy_esdt_safe_template(); @@ -790,15 +709,8 @@ fn deploy_phase_four_fee_market_already_deployed() { state.finish_setup(); let deploy_cost = BigUint::from(100_000u32); - - state.deploy_phase_one( - &deploy_cost, - 1, - 2, - BigUint::from(2u32), - MultiValueEncoded::new(), - None, - ); + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_phase_one(&deploy_cost, &config, None); state.deploy_header_verifier_template(); state.deploy_esdt_safe_template(); From 0f718c34657620bed9f561827083df900a113106 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 13:56:22 +0200 Subject: [PATCH 15/27] Removed unused argument Signed-off-by: Andrei Baltariu --- chain-factory/tests/chain_factory_tests.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/chain-factory/tests/chain_factory_tests.rs b/chain-factory/tests/chain_factory_tests.rs index c6547356..1277f73c 100644 --- a/chain-factory/tests/chain_factory_tests.rs +++ b/chain-factory/tests/chain_factory_tests.rs @@ -4,7 +4,7 @@ use multiversx_sc::types::{ BigUint, CodeMetadata, MultiValueEncoded, TestAddress, TestSCAddress, TokenIdentifier, }; use multiversx_sc_scenario::{ - api::StaticApi, imports::MxscPath, managed_biguint, ExpectError, ScenarioTxRun, ScenarioWorld, + api::StaticApi, imports::MxscPath, ExpectError, ScenarioTxRun, ScenarioWorld, }; use proxies::{ chain_config_proxy::ChainConfigContractProxy, chain_factory_proxy::ChainFactoryContractProxy, @@ -38,7 +38,7 @@ struct ChainFactoryTestState { } impl ChainFactoryTestState { - fn new(additional_stake_required: &MultiValueEncoded>) -> Self { + fn new() -> Self { let mut world = world(); world.account(OWNER).balance(OWNER_BALANCE).nonce(1); @@ -102,23 +102,13 @@ impl ChainFactoryTestState { #[test] fn deploy() { - let additional_stake = - StakeArgs::new(TokenIdentifier::from("TEST-TOKEN"), BigUint::from(100u64)); - let mut additional_stake_required = MultiValueEncoded::new(); - additional_stake_required.push(additional_stake); - - let mut state = ChainFactoryTestState::new(&additional_stake_required); + let mut state = ChainFactoryTestState::new(); state.deploy_chain_factory(); } #[test] fn deploy_chain_config_from_factory() { - let additional_stake = - StakeArgs::new(TokenIdentifier::from("TEST-TOKEN"), BigUint::from(100u64)); - let mut additional_stake_required = MultiValueEncoded::new(); - additional_stake_required.push(additional_stake); - - let mut state = ChainFactoryTestState::new(&additional_stake_required); + let mut state = ChainFactoryTestState::new(); state.deploy_chain_factory(); state.deploy_chain_config(); From b47d69cc9d9334581c42ae542b7c5039116f1b38 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:01:10 +0200 Subject: [PATCH 16/27] Build contracts + proxy Signed-off-by: Andrei Baltariu --- chain-config/tests/chain_config_unit_tests.rs | 15 ++++++++++++-- .../wasm-chain-config-full/Cargo.lock | 20 ++++++++++++++++++- .../wasm-chain-config-view/Cargo.lock | 20 ++++++++++++++++++- chain-config/wasm/Cargo.lock | 20 ++++++++++++++++++- .../wasm-chain-factory-full/Cargo.lock | 3 ++- .../wasm-chain-factory-view/Cargo.lock | 3 ++- chain-factory/wasm/Cargo.lock | 3 ++- .../wasm-sovereign-forge-full/Cargo.lock | 1 + .../wasm-soveriegn-forge-view/Cargo.lock | 1 + sovereign-forge/wasm/Cargo.lock | 1 + 10 files changed, 79 insertions(+), 8 deletions(-) diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index 9be79c56..dc862c11 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -1,7 +1,7 @@ -use multiversx_sc::types::{BigUint, MultiValueEncoded, TestAddress, TestSCAddress}; +use multiversx_sc::types::{BigUint, TestAddress, TestSCAddress}; use multiversx_sc_scenario::{api::StaticApi, imports::MxscPath, ScenarioTxRun, ScenarioWorld}; use proxies::chain_config_proxy::ChainConfigContractProxy; -use transaction::{SovereignConfig, StakeArgs}; +use transaction::SovereignConfig; const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("config-address"); const CONFIG_CODE_PATH: MxscPath = MxscPath::new("output/chain-config.mxsc.json"); @@ -40,6 +40,17 @@ impl ChainConfigTestState { .new_address(CONFIG_ADDRESS) .run(); } + + fn update_chain_config(&mut self, config: SovereignConfig) { + self.world + .tx() + .from(OWNER) + .typed(ChainConfigContractProxy) + .update_config(config) + .code(CONFIG_CODE_PATH) + .new_address(CONFIG_ADDRESS) + .run(); + } } #[test] diff --git a/chain-config/wasm-chain-config-full/Cargo.lock b/chain-config/wasm-chain-config-full/Cargo.lock index 4c4742d6..ba5f6567 100644 --- a/chain-config/wasm-chain-config-full/Cargo.lock +++ b/chain-config/wasm-chain-config-full/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] @@ -162,6 +163,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proxies" +version = "0.1.0" +dependencies = [ + "multiversx-sc", + "transaction", + "tx-batch-module", +] + [[package]] name = "quote" version = "1.0.37" @@ -212,6 +222,14 @@ dependencies = [ "multiversx-sc", ] +[[package]] +name = "tx-batch-module" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/chain-config/wasm-chain-config-view/Cargo.lock b/chain-config/wasm-chain-config-view/Cargo.lock index 7bf0f8b4..1b8ef551 100644 --- a/chain-config/wasm-chain-config-view/Cargo.lock +++ b/chain-config/wasm-chain-config-view/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] @@ -162,6 +163,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proxies" +version = "0.1.0" +dependencies = [ + "multiversx-sc", + "transaction", + "tx-batch-module", +] + [[package]] name = "quote" version = "1.0.37" @@ -212,6 +222,14 @@ dependencies = [ "multiversx-sc", ] +[[package]] +name = "tx-batch-module" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/chain-config/wasm/Cargo.lock b/chain-config/wasm/Cargo.lock index 55031d67..c1ca5844 100644 --- a/chain-config/wasm/Cargo.lock +++ b/chain-config/wasm/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] @@ -162,6 +163,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proxies" +version = "0.1.0" +dependencies = [ + "multiversx-sc", + "transaction", + "tx-batch-module", +] + [[package]] name = "quote" version = "1.0.37" @@ -212,6 +222,14 @@ dependencies = [ "multiversx-sc", ] +[[package]] +name = "tx-batch-module" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "transaction", +] + [[package]] name = "unicode-ident" version = "1.0.13" diff --git a/chain-factory/wasm-chain-factory-full/Cargo.lock b/chain-factory/wasm-chain-factory-full/Cargo.lock index 0059c506..38203839 100644 --- a/chain-factory/wasm-chain-factory-full/Cargo.lock +++ b/chain-factory/wasm-chain-factory-full/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/chain-factory/wasm-chain-factory-view/Cargo.lock b/chain-factory/wasm-chain-factory-view/Cargo.lock index b2d77b84..aa9ee222 100644 --- a/chain-factory/wasm-chain-factory-view/Cargo.lock +++ b/chain-factory/wasm-chain-factory-view/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/chain-factory/wasm/Cargo.lock b/chain-factory/wasm/Cargo.lock index beb1bcdc..b84a9b85 100644 --- a/chain-factory/wasm/Cargo.lock +++ b/chain-factory/wasm/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "arrayvec" @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock b/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock index f46cced9..5fd5dabe 100644 --- a/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock +++ b/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock b/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock index 5dcedc03..abcc25d4 100644 --- a/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock +++ b/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] diff --git a/sovereign-forge/wasm/Cargo.lock b/sovereign-forge/wasm/Cargo.lock index bbb98c75..6b162976 100644 --- a/sovereign-forge/wasm/Cargo.lock +++ b/sovereign-forge/wasm/Cargo.lock @@ -26,6 +26,7 @@ version = "0.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-modules", + "proxies", "setup-phase", "transaction", ] From 82ae7d547e558c37e86fe86643f89cf8e18c505a Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:04:16 +0200 Subject: [PATCH 17/27] Added endpoint annotation Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 2 ++ .../wasm-chain-config-full/src/lib.rs | 6 +++-- chain-config/wasm/src/lib.rs | 6 +++-- common/proxies/src/chain_config_proxy.rs | 26 +++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index e04e8558..41e577ca 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -30,6 +30,7 @@ pub trait ChainConfigContract: } #[only_admin] + #[endpoint(updateConfig)] fn update_config(&self, new_config: SovereignConfig) { if !self.is_new_min_validators_value(new_config.min_validators) { self.min_validators().set(new_config.min_validators); @@ -50,6 +51,7 @@ pub trait ChainConfigContract: } #[only_owner] + #[endpoint(completeSetupPhase)] fn complete_setup_phase(&self, header_verifier_address: ManagedAddress) { if self.is_setup_phase_complete() { return; diff --git a/chain-config/wasm-chain-config-full/src/lib.rs b/chain-config/wasm-chain-config-full/src/lib.rs index f5ed49af..ce1ff760 100644 --- a/chain-config/wasm-chain-config-full/src/lib.rs +++ b/chain-config/wasm-chain-config-full/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 9 +// Endpoints: 11 // Async Callback (empty): 1 -// Total number of exported functions: 12 +// Total number of exported functions: 14 #![no_std] @@ -20,6 +20,8 @@ multiversx_sc_wasm_adapter::endpoints! { ( init => init upgrade => upgrade + updateConfig => update_config + completeSetupPhase => complete_setup_phase getMinValidators => min_validators getMaxValidators => max_validators getMinStake => min_stake diff --git a/chain-config/wasm/src/lib.rs b/chain-config/wasm/src/lib.rs index f5ed49af..ce1ff760 100644 --- a/chain-config/wasm/src/lib.rs +++ b/chain-config/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 9 +// Endpoints: 11 // Async Callback (empty): 1 -// Total number of exported functions: 12 +// Total number of exported functions: 14 #![no_std] @@ -20,6 +20,8 @@ multiversx_sc_wasm_adapter::endpoints! { ( init => init upgrade => upgrade + updateConfig => update_config + completeSetupPhase => complete_setup_phase getMinValidators => min_validators getMaxValidators => max_validators getMinStake => min_stake diff --git a/common/proxies/src/chain_config_proxy.rs b/common/proxies/src/chain_config_proxy.rs index 6f7092f7..2b5c45a3 100644 --- a/common/proxies/src/chain_config_proxy.rs +++ b/common/proxies/src/chain_config_proxy.rs @@ -88,6 +88,32 @@ where To: TxTo, Gas: TxGas, { + pub fn update_config< + Arg0: ProxyArg>, + >( + self, + new_config: Arg0, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("updateConfig") + .argument(&new_config) + .original_result() + } + + pub fn complete_setup_phase< + Arg0: ProxyArg>, + >( + self, + header_verifier_address: Arg0, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("completeSetupPhase") + .argument(&header_verifier_address) + .original_result() + } + pub fn min_validators( self, ) -> TxTypedCall { From 382a7a77090b78eaaa8c9fe34e54e8765935a0fb Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:08:18 +0200 Subject: [PATCH 18/27] Added `update_config` test Signed-off-by: Andrei Baltariu --- chain-config/tests/chain_config_unit_tests.rs | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index dc862c11..3da1a897 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -1,5 +1,7 @@ use multiversx_sc::types::{BigUint, TestAddress, TestSCAddress}; -use multiversx_sc_scenario::{api::StaticApi, imports::MxscPath, ScenarioTxRun, ScenarioWorld}; +use multiversx_sc_scenario::{ + api::StaticApi, imports::MxscPath, ExpectError, ScenarioTxRun, ScenarioWorld, +}; use proxies::chain_config_proxy::ChainConfigContractProxy; use transaction::SovereignConfig; @@ -41,15 +43,24 @@ impl ChainConfigTestState { .run(); } - fn update_chain_config(&mut self, config: SovereignConfig) { - self.world + fn update_chain_config( + &mut self, + config: SovereignConfig, + expect_error: Option, + ) { + let transaction = self + .world .tx() .from(OWNER) + .to(CONFIG_ADDRESS) .typed(ChainConfigContractProxy) - .update_config(config) - .code(CONFIG_CODE_PATH) - .new_address(CONFIG_ADDRESS) - .run(); + .update_config(config); + + if let Some(error) = expect_error { + transaction.returns(error).run(); + } else { + transaction.run(); + } } } @@ -60,3 +71,15 @@ fn deploy_chain_config() { let config = SovereignConfig::new(0, 1, BigUint::default(), None); state.deploy_chain_config(config, OWNER); } + +#[test] +fn update_config() { + let mut state = ChainConfigTestState::new(); + + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_chain_config(config, OWNER); + + let new_config = SovereignConfig::new(2, 4, BigUint::default(), None); + + state.update_chain_config(new_config, None); +} From 6e7279433ad9f896ea4100ea9c9076c4ebcc7cb9 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:31:59 +0200 Subject: [PATCH 19/27] Updated `init` and `update_config` logic Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 33 ++---------- chain-config/src/validator_rules.rs | 53 ++++--------------- .../wasm-chain-config-full/src/lib.rs | 9 ++-- chain-config/wasm/src/lib.rs | 9 ++-- common/proxies/src/chain_config_proxy.rs | 33 ++---------- 5 files changed, 25 insertions(+), 112 deletions(-) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index 41e577ca..76c81192 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -13,41 +13,18 @@ pub trait ChainConfigContract: { #[init] fn init(&self, config: SovereignConfig, admin: ManagedAddress) { - require!( - config.min_validators <= config.max_validators, - "Invalid min/max validator numbers" - ); - - self.min_validators().set(config.min_validators); - self.max_validators().set(config.max_validators); - self.min_stake().set(config.min_stake); + self.require_validator_range(config.min_validators, config.max_validators); + self.sovereign_config().set(config); self.add_admin(admin); - - if let Some(additional_stake_required) = config.opt_additional_stake_required { - self.additional_stake_required() - .extend(&additional_stake_required); - } } #[only_admin] #[endpoint(updateConfig)] fn update_config(&self, new_config: SovereignConfig) { - if !self.is_new_min_validators_value(new_config.min_validators) { - self.min_validators().set(new_config.min_validators); - } - - if !self.is_new_max_validators_value(new_config.max_validators) { - self.max_validators().set(new_config.max_validators); - } - - if !self.is_new_min_stake_value(&new_config.min_stake) { - self.min_stake().set(new_config.min_stake); - } + self.require_config_set(); + self.require_validator_range(new_config.min_validators, new_config.max_validators); - if let Some(additional_stake_required) = new_config.opt_additional_stake_required { - self.additional_stake_required() - .extend(&additional_stake_required); - } + self.sovereign_config().set(new_config); } #[only_owner] diff --git a/chain-config/src/validator_rules.rs b/chain-config/src/validator_rules.rs index e715e30c..6e193eaf 100644 --- a/chain-config/src/validator_rules.rs +++ b/chain-config/src/validator_rules.rs @@ -1,4 +1,4 @@ -use transaction::StakeArgs; +use transaction::{SovereignConfig, StakeArgs}; multiversx_sc::imports!(); multiversx_sc::derive_imports!(); @@ -17,52 +17,21 @@ pub struct TokenIdAmountPair { pub trait ValidatorRulesModule { fn require_config_set(&self) { require!( - !self.min_validators().is_empty(), - "The minimum number of validators is not set" - ); - require!( - !self.max_validators().is_empty(), - "The maximum number of validators is not set" - ); - require!( - !self.min_stake().is_empty(), - "The mininum number of stake is not set" + !self.sovereign_config().is_empty(), + "The Sovereign Config is not set" ); } - #[inline] - fn is_new_min_validators_value(&self, new_min_validatrs: u64) -> bool { - self.min_validators().get() == new_min_validatrs - } - - #[inline] - fn is_new_max_validators_value(&self, new_max_validatrs: u64) -> bool { - self.max_validators().get() == new_max_validatrs - } - - #[inline] - fn is_new_min_stake_value(&self, min_stake: &BigUint) -> bool { - self.min_stake().get() == *min_stake + fn require_validator_range(&self, min_validators: u64, max_validators: u64) { + require!( + min_validators <= max_validators, + "Invalid min/max validator numbers" + ); } - #[view(getMinValidators)] - #[storage_mapper("minValidators")] - fn min_validators(&self) -> SingleValueMapper; - - #[view(getMaxValidators)] - #[storage_mapper("maxValidators")] - fn max_validators(&self) -> SingleValueMapper; - - // TODO: Read user stake and verify - #[view(getMinStake)] - #[storage_mapper("minStake")] - fn min_stake(&self) -> SingleValueMapper; - - // NOTE: ManagedVec or MultiValueEncoded ? - // TODO: Read user stake and verify - #[view(getAdditionalStakeRequired)] - #[storage_mapper("additionalStakeRequired")] - fn additional_stake_required(&self) -> UnorderedSetMapper>; + #[view(sovereignConfig)] + #[storage_mapper("sovereignConfig")] + fn sovereign_config(&self) -> SingleValueMapper>; #[view(wasPreviouslySlashed)] #[storage_mapper("wasPreviouslySlashed")] diff --git a/chain-config/wasm-chain-config-full/src/lib.rs b/chain-config/wasm-chain-config-full/src/lib.rs index ce1ff760..a6eae983 100644 --- a/chain-config/wasm-chain-config-full/src/lib.rs +++ b/chain-config/wasm-chain-config-full/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 11 +// Endpoints: 8 // Async Callback (empty): 1 -// Total number of exported functions: 14 +// Total number of exported functions: 11 #![no_std] @@ -22,10 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { upgrade => upgrade updateConfig => update_config completeSetupPhase => complete_setup_phase - getMinValidators => min_validators - getMaxValidators => max_validators - getMinStake => min_stake - getAdditionalStakeRequired => additional_stake_required + sovereignConfig => sovereign_config wasPreviouslySlashed => was_previously_slashed isAdmin => is_admin addAdmin => add_admin diff --git a/chain-config/wasm/src/lib.rs b/chain-config/wasm/src/lib.rs index ce1ff760..a6eae983 100644 --- a/chain-config/wasm/src/lib.rs +++ b/chain-config/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 11 +// Endpoints: 8 // Async Callback (empty): 1 -// Total number of exported functions: 14 +// Total number of exported functions: 11 #![no_std] @@ -22,10 +22,7 @@ multiversx_sc_wasm_adapter::endpoints! { upgrade => upgrade updateConfig => update_config completeSetupPhase => complete_setup_phase - getMinValidators => min_validators - getMaxValidators => max_validators - getMinStake => min_stake - getAdditionalStakeRequired => additional_stake_required + sovereignConfig => sovereign_config wasPreviouslySlashed => was_previously_slashed isAdmin => is_admin addAdmin => add_admin diff --git a/common/proxies/src/chain_config_proxy.rs b/common/proxies/src/chain_config_proxy.rs index 2b5c45a3..4c0c80b0 100644 --- a/common/proxies/src/chain_config_proxy.rs +++ b/common/proxies/src/chain_config_proxy.rs @@ -114,39 +114,12 @@ where .original_result() } - pub fn min_validators( + pub fn sovereign_config( self, - ) -> TxTypedCall { + ) -> TxTypedCall> { self.wrapped_tx .payment(NotPayable) - .raw_call("getMinValidators") - .original_result() - } - - pub fn max_validators( - self, - ) -> TxTypedCall { - self.wrapped_tx - .payment(NotPayable) - .raw_call("getMaxValidators") - .original_result() - } - - pub fn min_stake( - self, - ) -> TxTypedCall> { - self.wrapped_tx - .payment(NotPayable) - .raw_call("getMinStake") - .original_result() - } - - pub fn additional_stake_required( - self, - ) -> TxTypedCall>> { - self.wrapped_tx - .payment(NotPayable) - .raw_call("getAdditionalStakeRequired") + .raw_call("sovereignConfig") .original_result() } From b5d977f7e40e730e74f239e3ff8ddaea46f7bcf8 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:32:39 +0200 Subject: [PATCH 20/27] Removed unused import Signed-off-by: Andrei Baltariu --- chain-config/src/validator_rules.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chain-config/src/validator_rules.rs b/chain-config/src/validator_rules.rs index 6e193eaf..bd13ab58 100644 --- a/chain-config/src/validator_rules.rs +++ b/chain-config/src/validator_rules.rs @@ -1,4 +1,4 @@ -use transaction::{SovereignConfig, StakeArgs}; +use transaction::SovereignConfig; multiversx_sc::imports!(); multiversx_sc::derive_imports!(); From 16dbfb907e1b9320b7baa3b22b900873f18d844c Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:39:56 +0200 Subject: [PATCH 21/27] Added validators array unit test Signed-off-by: Andrei Baltariu --- chain-config/tests/chain_config_unit_tests.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index 3da1a897..02cd78fc 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -83,3 +83,18 @@ fn update_config() { state.update_chain_config(new_config, None); } + +#[test] +fn update_config_wrong_validators_array() { + let mut state = ChainConfigTestState::new(); + + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_chain_config(config, OWNER); + + let new_config = SovereignConfig::new(2, 1, BigUint::default(), None); + + state.update_chain_config( + new_config, + Some(ExpectError(4, "Invalid min/max validator numbers")), + ); +} From 5ac04909f1bbd9fb3879eec419c5d14508d9ddff Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 14:41:55 +0200 Subject: [PATCH 22/27] Clippy fixes Signed-off-by: Andrei Baltariu --- chain-factory/tests/chain_factory_tests.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/chain-factory/tests/chain_factory_tests.rs b/chain-factory/tests/chain_factory_tests.rs index 1277f73c..e3090561 100644 --- a/chain-factory/tests/chain_factory_tests.rs +++ b/chain-factory/tests/chain_factory_tests.rs @@ -1,15 +1,13 @@ use std::env::current_dir; -use multiversx_sc::types::{ - BigUint, CodeMetadata, MultiValueEncoded, TestAddress, TestSCAddress, TokenIdentifier, -}; +use multiversx_sc::types::{BigUint, CodeMetadata, TestAddress, TestSCAddress}; use multiversx_sc_scenario::{ api::StaticApi, imports::MxscPath, ExpectError, ScenarioTxRun, ScenarioWorld, }; use proxies::{ chain_config_proxy::ChainConfigContractProxy, chain_factory_proxy::ChainFactoryContractProxy, }; -use transaction::{SovereignConfig, StakeArgs}; +use transaction::SovereignConfig; const FACTORY_ADDRESS: TestSCAddress = TestSCAddress::new("chain-factory"); const CODE_PATH: MxscPath = MxscPath::new("output/chain-factory.mxsc.json"); From de2c412d8dd57b19ed606d349069e34ef17ed7d0 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Thu, 19 Dec 2024 15:39:09 +0200 Subject: [PATCH 23/27] Removed comment Signed-off-by: Andrei Baltariu --- chain-factory/tests/chain_factory_tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/chain-factory/tests/chain_factory_tests.rs b/chain-factory/tests/chain_factory_tests.rs index e3090561..3b0922a6 100644 --- a/chain-factory/tests/chain_factory_tests.rs +++ b/chain-factory/tests/chain_factory_tests.rs @@ -41,7 +41,6 @@ impl ChainFactoryTestState { world.account(OWNER).balance(OWNER_BALANCE).nonce(1); - // deploy chain-config Self { world } } From 0fe84cf541d61ec7ff1b907e549f64b434df4a6c Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 20 Dec 2024 10:18:29 +0200 Subject: [PATCH 24/27] Added `update_config` test Signed-off-by: Andrei Baltariu --- chain-config/tests/chain_config_unit_tests.rs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index 02cd78fc..94a209aa 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -8,6 +8,8 @@ use transaction::SovereignConfig; const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("config-address"); const CONFIG_CODE_PATH: MxscPath = MxscPath::new("output/chain-config.mxsc.json"); +const HEADER_VERIFIER_ADDRESS: TestSCAddress = TestSCAddress::new("header-verifier"); + const OWNER: TestAddress = TestAddress::new("owner"); const OWNER_BALANCE: u64 = 100_000_000_000; @@ -62,6 +64,22 @@ impl ChainConfigTestState { transaction.run(); } } + + fn complete_setup_phase(&mut self, expect_error: Option) { + let transaction = self + .world + .tx() + .from(OWNER) + .to(CONFIG_ADDRESS) + .typed(ChainConfigContractProxy) + .complete_setup_phase(HEADER_VERIFIER_ADDRESS); + + if let Some(error) = expect_error { + transaction.returns(error).run(); + } else { + transaction.run(); + } + } } #[test] @@ -98,3 +116,13 @@ fn update_config_wrong_validators_array() { Some(ExpectError(4, "Invalid min/max validator numbers")), ); } + +#[test] +fn complete_setup_phase() { + let mut state = ChainConfigTestState::new(); + + let config = SovereignConfig::new(0, 1, BigUint::default(), None); + state.deploy_chain_config(config, OWNER); + + state.complete_setup_phase(None); +} From 1e48365e4c8a3fe61c6d3fa69aa163640b98b6a5 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 20 Dec 2024 10:40:19 +0200 Subject: [PATCH 25/27] Build + updated call to UserBuiltinProxy Signed-off-by: Andrei Baltariu --- Cargo.lock | 1 + chain-config/Cargo.toml | 3 +++ chain-config/src/lib.rs | 2 +- chain-config/tests/chain_config_unit_tests.rs | 6 ++++-- chain-config/wasm-chain-config-full/Cargo.lock | 11 +++++++++++ chain-config/wasm-chain-config-view/Cargo.lock | 11 +++++++++++ chain-config/wasm/Cargo.lock | 11 +++++++++++ chain-factory/wasm-chain-factory-full/Cargo.lock | 11 +++++++++++ chain-factory/wasm-chain-factory-view/Cargo.lock | 11 +++++++++++ chain-factory/wasm/Cargo.lock | 11 +++++++++++ .../wasm-enshrine-esdt-safe-full/Cargo.lock | 1 + .../wasm-enshrine-esdt-safe-view/Cargo.lock | 1 + enshrine-esdt-safe/wasm/Cargo.lock | 1 + sovereign-forge/wasm-sovereign-forge-full/Cargo.lock | 1 + sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock | 1 + sovereign-forge/wasm/Cargo.lock | 1 + token-handler/wasm-token-handler-full/Cargo.lock | 11 +++++++++++ token-handler/wasm-token-handler-view/Cargo.lock | 11 +++++++++++ token-handler/wasm/Cargo.lock | 11 +++++++++++ 19 files changed, 114 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e42fe2ba..94cb8f87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -230,6 +230,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "multiversx-sc-scenario", diff --git a/chain-config/Cargo.toml b/chain-config/Cargo.toml index 2e052264..8bac0437 100644 --- a/chain-config/Cargo.toml +++ b/chain-config/Cargo.toml @@ -20,6 +20,9 @@ version = "=0.54.5" [dependencies.multiversx-sc-modules] version = "=0.54.5" +[dependencies.header-verifier] +path = "../header-verifier" + [dependencies.transaction] path = "../common/transaction" diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index 76c81192..701ede6f 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -37,7 +37,7 @@ pub trait ChainConfigContract: self.require_config_set(); // validator set in header verifier self.tx() - .to(ESDTSystemSCAddress) + .to(ToSelf) .typed(UserBuiltinProxy) .change_owner_address(&header_verifier_address) .sync_call(); diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index 94a209aa..766c254f 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -1,8 +1,10 @@ -use multiversx_sc::types::{BigUint, TestAddress, TestSCAddress}; +use multiversx_sc::types::{BigUint, MultiValueEncoded, TestAddress, TestSCAddress}; use multiversx_sc_scenario::{ api::StaticApi, imports::MxscPath, ExpectError, ScenarioTxRun, ScenarioWorld, }; -use proxies::chain_config_proxy::ChainConfigContractProxy; +use proxies::{ + chain_config_proxy::ChainConfigContractProxy, header_verifier_proxy::HeaderverifierProxy, +}; use transaction::SovereignConfig; const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("config-address"); diff --git a/chain-config/wasm-chain-config-full/Cargo.lock b/chain-config/wasm-chain-config-full/Cargo.lock index ba5f6567..28f3722d 100644 --- a/chain-config/wasm-chain-config-full/Cargo.lock +++ b/chain-config/wasm-chain-config-full/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -45,6 +46,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/chain-config/wasm-chain-config-view/Cargo.lock b/chain-config/wasm-chain-config-view/Cargo.lock index 1b8ef551..e116c21c 100644 --- a/chain-config/wasm-chain-config-view/Cargo.lock +++ b/chain-config/wasm-chain-config-view/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -45,6 +46,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/chain-config/wasm/Cargo.lock b/chain-config/wasm/Cargo.lock index c1ca5844..f54ca040 100644 --- a/chain-config/wasm/Cargo.lock +++ b/chain-config/wasm/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -45,6 +46,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/chain-factory/wasm-chain-factory-full/Cargo.lock b/chain-factory/wasm-chain-factory-full/Cargo.lock index 38203839..7e5eb691 100644 --- a/chain-factory/wasm-chain-factory-full/Cargo.lock +++ b/chain-factory/wasm-chain-factory-full/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -57,6 +58,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/chain-factory/wasm-chain-factory-view/Cargo.lock b/chain-factory/wasm-chain-factory-view/Cargo.lock index aa9ee222..fd711c45 100644 --- a/chain-factory/wasm-chain-factory-view/Cargo.lock +++ b/chain-factory/wasm-chain-factory-view/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -57,6 +58,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/chain-factory/wasm/Cargo.lock b/chain-factory/wasm/Cargo.lock index b84a9b85..b98a25ff 100644 --- a/chain-factory/wasm/Cargo.lock +++ b/chain-factory/wasm/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -57,6 +58,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock index 98e08ff7..45b6f4cd 100644 --- a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock +++ b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-full/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", diff --git a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock index fd9482f1..f07222d3 100644 --- a/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock +++ b/enshrine-esdt-safe/wasm-enshrine-esdt-safe-view/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", diff --git a/enshrine-esdt-safe/wasm/Cargo.lock b/enshrine-esdt-safe/wasm/Cargo.lock index b3db833e..9ff33ede 100644 --- a/enshrine-esdt-safe/wasm/Cargo.lock +++ b/enshrine-esdt-safe/wasm/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", diff --git a/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock b/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock index 5fd5dabe..cd8de51a 100644 --- a/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock +++ b/sovereign-forge/wasm-sovereign-forge-full/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", diff --git a/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock b/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock index abcc25d4..622a0f03 100644 --- a/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock +++ b/sovereign-forge/wasm-soveriegn-forge-view/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", diff --git a/sovereign-forge/wasm/Cargo.lock b/sovereign-forge/wasm/Cargo.lock index 6b162976..cebc1b65 100644 --- a/sovereign-forge/wasm/Cargo.lock +++ b/sovereign-forge/wasm/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", diff --git a/token-handler/wasm-token-handler-full/Cargo.lock b/token-handler/wasm-token-handler-full/Cargo.lock index fb43fd0d..b560deaf 100644 --- a/token-handler/wasm-token-handler-full/Cargo.lock +++ b/token-handler/wasm-token-handler-full/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -49,6 +50,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/token-handler/wasm-token-handler-view/Cargo.lock b/token-handler/wasm-token-handler-view/Cargo.lock index 19ae3a96..0a389c29 100644 --- a/token-handler/wasm-token-handler-view/Cargo.lock +++ b/token-handler/wasm-token-handler-view/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -49,6 +50,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" diff --git a/token-handler/wasm/Cargo.lock b/token-handler/wasm/Cargo.lock index 3f0f1447..4ebd0f5a 100644 --- a/token-handler/wasm/Cargo.lock +++ b/token-handler/wasm/Cargo.lock @@ -24,6 +24,7 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" name = "chain-config" version = "0.0.0" dependencies = [ + "header-verifier", "multiversx-sc", "multiversx-sc-modules", "proxies", @@ -49,6 +50,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" +[[package]] +name = "header-verifier" +version = "0.0.0" +dependencies = [ + "multiversx-sc", + "proxies", + "setup-phase", + "transaction", +] + [[package]] name = "hex" version = "0.4.3" From 76459aeddcc5114704eeb062a60fd99b00641813 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Fri, 20 Dec 2024 10:45:43 +0200 Subject: [PATCH 26/27] Removed unused imports Signed-off-by: Andrei Baltariu --- chain-config/tests/chain_config_unit_tests.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/chain-config/tests/chain_config_unit_tests.rs b/chain-config/tests/chain_config_unit_tests.rs index 766c254f..94a209aa 100644 --- a/chain-config/tests/chain_config_unit_tests.rs +++ b/chain-config/tests/chain_config_unit_tests.rs @@ -1,10 +1,8 @@ -use multiversx_sc::types::{BigUint, MultiValueEncoded, TestAddress, TestSCAddress}; +use multiversx_sc::types::{BigUint, TestAddress, TestSCAddress}; use multiversx_sc_scenario::{ api::StaticApi, imports::MxscPath, ExpectError, ScenarioTxRun, ScenarioWorld, }; -use proxies::{ - chain_config_proxy::ChainConfigContractProxy, header_verifier_proxy::HeaderverifierProxy, -}; +use proxies::chain_config_proxy::ChainConfigContractProxy; use transaction::SovereignConfig; const CONFIG_ADDRESS: TestSCAddress = TestSCAddress::new("config-address"); From a33834531aed46a99e9ef4f16421675ae27fb2f0 Mon Sep 17 00:00:00 2001 From: Andrei Baltariu Date: Mon, 23 Dec 2024 10:49:53 +0200 Subject: [PATCH 27/27] Updated config set logic Signed-off-by: Andrei Baltariu --- chain-config/src/lib.rs | 9 +++------ chain-config/src/validator_rules.rs | 4 +++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/chain-config/src/lib.rs b/chain-config/src/lib.rs index 701ede6f..bc2a9c3e 100644 --- a/chain-config/src/lib.rs +++ b/chain-config/src/lib.rs @@ -13,17 +13,15 @@ pub trait ChainConfigContract: { #[init] fn init(&self, config: SovereignConfig, admin: ManagedAddress) { - self.require_validator_range(config.min_validators, config.max_validators); - self.sovereign_config().set(config); + self.sovereign_config().set(config.clone()); + self.require_valid_config(&config); self.add_admin(admin); } #[only_admin] #[endpoint(updateConfig)] fn update_config(&self, new_config: SovereignConfig) { - self.require_config_set(); - self.require_validator_range(new_config.min_validators, new_config.max_validators); - + self.require_valid_config(&new_config); self.sovereign_config().set(new_config); } @@ -34,7 +32,6 @@ pub trait ChainConfigContract: return; } - self.require_config_set(); // validator set in header verifier self.tx() .to(ToSelf) diff --git a/chain-config/src/validator_rules.rs b/chain-config/src/validator_rules.rs index bd13ab58..fd4ae354 100644 --- a/chain-config/src/validator_rules.rs +++ b/chain-config/src/validator_rules.rs @@ -15,11 +15,13 @@ pub struct TokenIdAmountPair { #[multiversx_sc::module] pub trait ValidatorRulesModule { - fn require_config_set(&self) { + fn require_valid_config(&self, config: &SovereignConfig) { require!( !self.sovereign_config().is_empty(), "The Sovereign Config is not set" ); + + self.require_validator_range(config.min_validators, config.max_validators); } fn require_validator_range(&self, min_validators: u64, max_validators: u64) {