Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain-Config update logic #208

Merged
merged 34 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
241c99c
Added new structs for Sovereign Chain Config
andreiblt1304 Dec 13, 2024
42c8e60
Updated `init` and `update` endpoints to use new struct and storage
andreiblt1304 Dec 13, 2024
50d668b
Removed any use of the `StakeMultiArg`
andreiblt1304 Dec 13, 2024
cd3b254
Clippy fix
andreiblt1304 Dec 13, 2024
d86f2d2
Chain-Config tests
andreiblt1304 Dec 19, 2024
e10eb27
Updated root `Cargo.lock`
andreiblt1304 Dec 19, 2024
4f2aeeb
Modified logic for config update
andreiblt1304 Dec 19, 2024
195f96b
Added ownership change transaction
andreiblt1304 Dec 19, 2024
2baa979
Whiteline
andreiblt1304 Dec 19, 2024
ce60191
Build + proxy
andreiblt1304 Dec 19, 2024
f2965ee
Merge branch 'config-update' into config-tests
andreiblt1304 Dec 19, 2024
1518ac2
Added `new()` function for `SovereignConfig`
andreiblt1304 Dec 19, 2024
31d75ab
Update chain-factory endpoint to use new struct
andreiblt1304 Dec 19, 2024
c1609c1
Merge branch 'config-update' into config-tests
andreiblt1304 Dec 19, 2024
2e722c1
Update unit test + build
andreiblt1304 Dec 19, 2024
866dac5
Fixed tests
andreiblt1304 Dec 19, 2024
0f718c3
Removed unused argument
andreiblt1304 Dec 19, 2024
257e3a6
Merge branch 'config-update' into config-tests
andreiblt1304 Dec 19, 2024
b47d69c
Build contracts + proxy
andreiblt1304 Dec 19, 2024
82ae7d5
Added endpoint annotation
andreiblt1304 Dec 19, 2024
245ce77
Merge branch 'config-update' into config-tests
andreiblt1304 Dec 19, 2024
382a7a7
Added `update_config` test
andreiblt1304 Dec 19, 2024
6e72794
Updated `init` and `update_config` logic
andreiblt1304 Dec 19, 2024
b5d977f
Removed unused import
andreiblt1304 Dec 19, 2024
16dbfb9
Added validators array unit test
andreiblt1304 Dec 19, 2024
5ac0490
Clippy fixes
andreiblt1304 Dec 19, 2024
de2c412
Removed comment
andreiblt1304 Dec 19, 2024
0fe84cf
Added `update_config` test
andreiblt1304 Dec 20, 2024
1e48365
Build + updated call to UserBuiltinProxy
andreiblt1304 Dec 20, 2024
76459ae
Removed unused imports
andreiblt1304 Dec 20, 2024
a338345
Updated config set logic
andreiblt1304 Dec 23, 2024
051ae2c
Merge branch 'feat/chain-config' into config-update
andreiblt1304 Dec 27, 2024
a27ace6
Fixes after review
andreiblt1304 Jan 8, 2025
3d334f6
Added use `default_config` function
andreiblt1304 Jan 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 11 additions & 31 deletions chain-config/src/lib.rs
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
andreiblt1304 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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!();

Expand All @@ -19,54 +18,35 @@ pub trait ChainConfigContract:
max_validators: u64,
min_stake: BigUint,
admin: ManagedAddress,
additional_stake_required: MultiValueEncoded<StakeMultiArg<Self::Api>>,
additional_stake_required: MultiValueEncoded<StakeArgs<Self::Api>>,
) {
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<u64>,
opt_max_validators: Option<u64>,
opt_min_stake: Option<BigUint>,
opt_additional_stake_required: Option<MultiValueEncoded<StakeMultiArg<Self::Api>>>,
) {
if let Some(min_validators) = opt_min_validators {
fn update_config(&self, new_config: SovereignConfig<Self::Api>) {
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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions chain-config/src/validator_rules.rs
andreiblt1304 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use transaction::StakeArgs;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

Expand Down Expand Up @@ -49,9 +51,7 @@ pub trait ValidatorRulesModule {
// TODO: Read user stake and verify
#[view(getAdditionalStakeRequired)]
#[storage_mapper("additionalStakeRequired")]
fn additional_stake_required(
&self,
) -> SingleValueMapper<ManagedVec<TokenIdAmountPair<Self::Api>>>;
fn additional_stake_required(&self) -> UnorderedSetMapper<StakeArgs<Self::Api>>;

#[view(wasPreviouslySlashed)]
#[storage_mapper("wasPreviouslySlashed")]
Expand Down
4 changes: 2 additions & 2 deletions chain-factory/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -19,7 +19,7 @@ pub trait FactoryModule: only_admin::OnlyAdminModule {
min_validators: u64,
max_validators: u64,
min_stake: BigUint,
additional_stake_required: MultiValueEncoded<StakeMultiArg<Self::Api>>,
additional_stake_required: MultiValueEncoded<StakeArgs<Self::Api>>,
) -> ManagedAddress {
let caller = self.blockchain().get_caller();
let source_address = self.chain_config_template().get();
Expand Down
16 changes: 7 additions & 9 deletions chain-factory/tests/chain_factory_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -38,9 +38,7 @@ struct ChainFactoryTestState {
}

impl ChainFactoryTestState {
fn new(
additional_stake_required: &MultiValueEncoded<StaticApi, StakeMultiArg<StaticApi>>,
) -> Self {
fn new(additional_stake_required: &MultiValueEncoded<StaticApi, StakeArgs<StaticApi>>) -> Self {
let mut world = world();

world.account(OWNER).balance(OWNER_BALANCE).nonce(1);
Expand Down Expand Up @@ -87,7 +85,7 @@ impl ChainFactoryTestState {
min_validators: usize,
max_validators: usize,
min_stake: BigUint<StaticApi>,
additional_stake_required: MultiValueEncoded<StaticApi, StakeMultiArg<StaticApi>>,
additional_stake_required: MultiValueEncoded<StaticApi, StakeArgs<StaticApi>>,
expected_result: Option<ExpectError<'_>>,
) {
let transaction = self
Expand All @@ -114,8 +112,8 @@ impl ChainFactoryTestState {

#[test]
fn deploy() {
let additional_stake: StakeMultiArg<StaticApi> =
(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);

Expand All @@ -125,8 +123,8 @@ fn deploy() {

#[test]
fn deploy_chain_config_from_factory() {
let additional_stake: StakeMultiArg<StaticApi> =
(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);

Expand Down
14 changes: 2 additions & 12 deletions common/proxies/src/chain_config_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where
Arg1: ProxyArg<u64>,
Arg2: ProxyArg<BigUint<Env::Api>>,
Arg3: ProxyArg<ManagedAddress<Env::Api>>,
Arg4: ProxyArg<MultiValueEncoded<Env::Api, MultiValue2<TokenIdentifier<Env::Api>, BigUint<Env::Api>>>>,
Arg4: ProxyArg<MultiValueEncoded<Env::Api, transaction::StakeArgs<Env::Api>>>,
>(
self,
min_validators: Arg0,
Expand Down Expand Up @@ -126,7 +126,7 @@ where

pub fn additional_stake_required(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedVec<Env::Api, TokenIdAmountPair<Env::Api>>> {
) -> TxTypedCall<Env, From, To, NotPayable, Gas, MultiValueEncoded<Env::Api, transaction::StakeArgs<Env::Api>>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getAdditionalStakeRequired")
Expand Down Expand Up @@ -194,13 +194,3 @@ where
.original_result()
}
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem)]
pub struct TokenIdAmountPair<Api>
where
Api: ManagedTypeApi,
{
pub token_id: TokenIdentifier<Api>,
pub amount: BigUint<Api>,
}
2 changes: 1 addition & 1 deletion common/proxies/src/chain_factory_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<u64>,
Arg2: ProxyArg<BigUint<Env::Api>>,
Arg3: ProxyArg<MultiValueEncoded<Env::Api, MultiValue2<TokenIdentifier<Env::Api>, BigUint<Env::Api>>>>,
Arg3: ProxyArg<MultiValueEncoded<Env::Api, transaction::StakeArgs<Env::Api>>>,
>(
self,
min_validators: Arg0,
Expand Down
2 changes: 1 addition & 1 deletion common/proxies/src/sovereign_forge_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
Arg0: ProxyArg<u64>,
Arg1: ProxyArg<u64>,
Arg2: ProxyArg<BigUint<Env::Api>>,
Arg3: ProxyArg<MultiValueEncoded<Env::Api, MultiValue2<TokenIdentifier<Env::Api>, BigUint<Env::Api>>>>,
Arg3: ProxyArg<MultiValueEncoded<Env::Api, transaction::StakeArgs<Env::Api>>>,
>(
self,
min_validators: Arg0,
Expand Down
30 changes: 29 additions & 1 deletion common/transaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,35 @@ pub type ExtractedFeeResult<M> =
MultiValue2<OptionalValue<EsdtTokenPayment<M>>, ManagedVec<M, EsdtTokenPayment<M>>>;
pub type OptionalValueTransferDataTuple<M> =
OptionalValue<MultiValue3<GasLimit, ManagedBuffer<M>, ManagedVec<M, ManagedBuffer<M>>>>;
pub type StakeMultiArg<M> = MultiValue2<TokenIdentifier<M>, BigUint<M>>;

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)]
pub struct StakeArgs<M: ManagedTypeApi> {
pub token_id: TokenIdentifier<M>,
pub amount: BigUint<M>,
}

impl<M: ManagedTypeApi> StakeArgs<M> {
pub fn new(token_id: TokenIdentifier<M>, amount: BigUint<M>) -> Self {
StakeArgs { token_id, amount }
}

pub fn get_default() -> Self {
andreiblt1304 marked this conversation as resolved.
Show resolved Hide resolved
axenteoctavian marked this conversation as resolved.
Show resolved Hide resolved
StakeArgs {
token_id: TokenIdentifier::from(""),
amount: BigUint::default(),
}
}
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)]
pub struct SovereignConfig<M: ManagedTypeApi> {
pub opt_min_validators: Option<u64>,
pub opt_max_validators: Option<u64>,
pub opt_min_stake: Option<BigUint<M>>,
pub opt_additional_stake_required: Option<ManagedVec<M, StakeArgs<M>>>,
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, ManagedVecItem, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions sovereign-forge/interactor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
9 changes: 2 additions & 7 deletions sovereign-forge/interactor/src/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -400,13 +401,7 @@ impl ContractInteract {
let min_validators = 1u64;
let max_validators = 3u64;
let min_stake = BigUint::<StaticApi>::from(0u128);
let additional_stake_required = MultiValueVec::from(vec![MultiValue2::<
TokenIdentifier<StaticApi>,
BigUint<StaticApi>,
>::from((
TokenIdentifier::from_esdt_bytes(&b""[..]),
BigUint::<StaticApi>::from(0u128),
))]);
let additional_stake_required = MultiValueVec::from(vec![StakeArgs::get_default()]);

let response = self
.interactor
Expand Down
4 changes: 2 additions & 2 deletions sovereign-forge/src/common/sc_deploy.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<StakeMultiArg<Self::Api>>,
additional_stake_required: MultiValueEncoded<StakeArgs<Self::Api>>,
) -> ManagedAddress {
self.tx()
.to(self.get_chain_factory_address())
Expand Down
4 changes: 2 additions & 2 deletions sovereign-forge/src/phases.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub trait PhasesModule:
min_validators: u64,
max_validators: u64,
min_stake: BigUint,
additional_stake_required: MultiValueEncoded<StakeMultiArg<Self::Api>>,
additional_stake_required: MultiValueEncoded<StakeArgs<Self::Api>>,
) {
self.require_setup_complete();

Expand Down
4 changes: 2 additions & 2 deletions sovereign-forge/tests/sovereign_forge_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -244,7 +244,7 @@ impl SovereignForgeTestState {
min_validators: u64,
max_validators: u64,
min_stake: BigUint<StaticApi>,
additional_stake_required: MultiValueEncoded<StaticApi, StakeMultiArg<StaticApi>>,
additional_stake_required: MultiValueEncoded<StaticApi, StakeArgs<StaticApi>>,
expected_result: Option<ExpectError>,
) {
let transaction = self
Expand Down
Loading