Skip to content

Commit

Permalink
Merge pull request #2008 from subspace/enable-balance-transfers
Browse files Browse the repository at this point in the history
Enable balance transfers
  • Loading branch information
nazar-pc authored Sep 27, 2023
2 parents 2a1eedf + e1997e9 commit 4f3bb5b
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 50 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const SEED: u32 = 0;
#[benchmarks]
mod benchmarks {
use super::*;
use sp_std::vec;

/// Benchmark `submit_bundle` extrinsic with the worst possible conditions:
/// - The bundle is the first bundle of the consensus block
Expand Down Expand Up @@ -529,10 +530,10 @@ mod benchmarks {
(operator_account, operator_id)
}

fn run_to_block<T: Config>(block_number: T::BlockNumber, parent_hash: T::Hash) {
fn run_to_block<T: Config>(block_number: BlockNumberFor<T>, parent_hash: T::Hash) {
System::<T>::set_block_number(block_number);
System::<T>::initialize(&block_number, &parent_hash, &Default::default());
<Domains<T> as Hooks<T::BlockNumber>>::on_initialize(block_number);
<Domains<T> as Hooks<BlockNumberFor<T>>>::on_initialize(block_number);
System::<T>::finalize();
}

Expand Down
14 changes: 9 additions & 5 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ mod pallet {
register_runtime_at_genesis, Error as RuntimeRegistryError, RuntimeObject,
ScheduledRuntimeUpgrade,
};
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::staking::do_reward_operators;
use crate::staking::{
do_auto_stake_block_rewards, do_deregister_operator, do_nominate_operator,
do_register_operator, do_reward_operators, do_slash_operators, do_switch_operator_domain,
do_withdraw_stake, Error as StakingError, Nominator, Operator, OperatorConfig,
StakingSummary, Withdraw,
do_register_operator, do_slash_operators, do_switch_operator_domain, do_withdraw_stake,
Error as StakingError, Nominator, Operator, OperatorConfig, StakingSummary, Withdraw,
};
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::staking_epoch::do_unlock_pending_withdrawals;
use crate::staking_epoch::{
do_finalize_domain_current_epoch, do_unlock_pending_withdrawals,
Error as StakingEpochError, PendingNominatorUnlock, PendingOperatorSlashInfo,
do_finalize_domain_current_epoch, Error as StakingEpochError, PendingNominatorUnlock,
PendingOperatorSlashInfo,
};
use crate::weights::WeightInfo;
use crate::{
Expand Down Expand Up @@ -751,6 +754,7 @@ mod pallet {
}
// Add the exeuctione receipt to the block tree
ReceiptType::Accepted(accepted_receipt_type) => {
#[cfg_attr(feature = "runtime-benchmarks", allow(unused_variables))]
let maybe_confirmed_domain_block_info = process_execution_receipt::<T>(
domain_id,
operator_id,
Expand Down
3 changes: 3 additions & 0 deletions crates/pallet-domains/src/staking_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) fn do_finalize_domain_current_epoch<T: Config>(
}

/// Unlocks any operators who are de-registering or nominators who are withdrawing staked funds.
#[cfg(any(not(feature = "runtime-benchmarks"), test))]
pub(crate) fn do_unlock_pending_withdrawals<T: Config>(
domain_id: DomainId,
domain_block_number: T::DomainNumber,
Expand Down Expand Up @@ -206,6 +207,7 @@ fn do_finalize_operator_deregistrations<T: Config>(
Ok(())
}

#[cfg(any(not(feature = "runtime-benchmarks"), test))]
fn unlock_operator<T: Config>(operator_id: OperatorId) -> Result<(), Error> {
Operators::<T>::try_mutate_exists(operator_id, |maybe_operator| {
// take the operator so this operator info is removed once we unlock the operator.
Expand Down Expand Up @@ -288,6 +290,7 @@ fn release_pending_deposits<T: Config>(operator_id: OperatorId) -> Result<(), Tr
Ok(())
}

#[cfg(any(not(feature = "runtime-benchmarks"), test))]
fn unlock_nominator_withdrawals<T: Config>(
operator_id: OperatorId,
domain_block_number: T::DomainNumber,
Expand Down
10 changes: 10 additions & 0 deletions crates/pallet-runtime-configs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.5", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "20be5f33a3d2b3f4b31a894f9829184b29fba3ef", optional = true }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "20be5f33a3d2b3f4b31a894f9829184b29fba3ef" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "20be5f33a3d2b3f4b31a894f9829184b29fba3ef" }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
sp-core = { version = "21.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "20be5f33a3d2b3f4b31a894f9829184b29fba3ef", optional = true }
sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "20be5f33a3d2b3f4b31a894f9829184b29fba3ef" }
sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "20be5f33a3d2b3f4b31a894f9829184b29fba3ef", optional = true }

[features]
default = ["std"]
Expand All @@ -30,5 +33,12 @@ std = [
"frame-system/std",
"scale-info/std",
"sp-runtime/std",
"sp-std?/std",
]
try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"sp-core",
"sp-std",
]
26 changes: 26 additions & 0 deletions crates/pallet-runtime-configs/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Benchmarking for `pallet-subspace`.

use frame_benchmarking::v2::*;

#[benchmarks]
mod benchmarks {
use crate::{Call, Config, Pallet};
use frame_system::RawOrigin;
use sp_std::vec;

#[benchmark]
fn set_enable_domains() {
#[extrinsic_call]
_(RawOrigin::Root, true);

assert!(Pallet::<T>::enable_domains());
}

#[benchmark]
fn set_enable_balance_transfers() {
#[extrinsic_call]
_(RawOrigin::Root, true);

assert!(Pallet::<T>::enable_balance_transfers());
}
}
50 changes: 43 additions & 7 deletions crates/pallet-runtime-configs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;

pub use pallet::*;

#[frame_support::pallet]
mod pallet {
use crate::weights::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Zero;
Expand All @@ -35,19 +40,22 @@ mod pallet {

/// Whether to disable the normal balances transfer calls.
#[pallet::storage]
#[pallet::getter(fn enable_transfer)]
pub type EnableTransfer<T> = StorageValue<_, bool, ValueQuery>;
#[pallet::getter(fn enable_balance_transfers)]
pub type EnableBalanceTransfers<T> = StorageValue<_, bool, ValueQuery>;

#[pallet::storage]
pub type ConfirmationDepthK<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;

#[pallet::config]
pub trait Config: frame_system::Config {}
pub trait Config: frame_system::Config {
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub enable_domains: bool,
pub enable_transfer: bool,
pub enable_balance_transfers: bool,
pub confirmation_depth_k: BlockNumberFor<T>,
}

Expand All @@ -56,7 +64,7 @@ mod pallet {
fn default() -> Self {
Self {
enable_domains: false,
enable_transfer: false,
enable_balance_transfers: false,
confirmation_depth_k: BlockNumberFor::<T>::from(100u32),
}
}
Expand All @@ -67,7 +75,7 @@ mod pallet {
fn build(&self) {
let Self {
enable_domains,
enable_transfer,
enable_balance_transfers,
confirmation_depth_k,
} = self;

Expand All @@ -77,8 +85,36 @@ mod pallet {
);

<EnableDomains<T>>::put(enable_domains);
<EnableTransfer<T>>::put(enable_transfer);
<EnableBalanceTransfers<T>>::put(enable_balance_transfers);
<ConfirmationDepthK<T>>::put(confirmation_depth_k);
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Change enable domains state.
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::set_enable_domains())]
pub fn set_enable_domains(origin: OriginFor<T>, enable_domains: bool) -> DispatchResult {
ensure_root(origin)?;

EnableDomains::<T>::put(enable_domains);

Ok(())
}

/// Enable or disable balance transfers for all users.
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::set_enable_balance_transfers())]
pub fn set_enable_balance_transfers(
origin: OriginFor<T>,
enable_balance_transfers: bool,
) -> DispatchResult {
ensure_root(origin)?;

EnableBalanceTransfers::<T>::put(enable_balance_transfers);

Ok(())
}
}
}
83 changes: 83 additions & 0 deletions crates/pallet-runtime-configs/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

//! Autogenerated weights for pallet_runtime_configs
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-09-26, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `nazar-pc`, CPU: `13th Gen Intel(R) Core(TM) i9-13900K`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
// target/release/subspace-node
// benchmark
// pallet
// --chain=dev
// --steps=50
// --repeat=20
// --pallet=pallet_runtime_configs
// --extrinsic=*
// --heap-pages=4096
// --output=crates/pallet-runtime-configs/src/weights.rs
// --template=frame-weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use core::marker::PhantomData;

/// Weight functions needed for pallet_runtime_configs.
pub trait WeightInfo {
fn set_enable_domains() -> Weight;
fn set_enable_balance_transfers() -> Weight;
}

/// Weights for pallet_runtime_configs using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: `RuntimeConfigs::EnableBalanceTransfers` (r:0 w:1)
/// Proof: `RuntimeConfigs::EnableBalanceTransfers` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
fn set_enable_domains() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 5_640_000 picoseconds.
Weight::from_parts(5_782_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: `RuntimeConfigs::EnableBalanceTransfers` (r:0 w:1)
/// Proof: `RuntimeConfigs::EnableBalanceTransfers` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
fn set_enable_balance_transfers() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 5_726_000 picoseconds.
Weight::from_parts(5_890_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: `RuntimeConfigs::EnableBalanceTransfers` (r:0 w:1)
/// Proof: `RuntimeConfigs::EnableBalanceTransfers` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
fn set_enable_domains() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 5_640_000 picoseconds.
Weight::from_parts(5_782_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: `RuntimeConfigs::EnableBalanceTransfers` (r:0 w:1)
/// Proof: `RuntimeConfigs::EnableBalanceTransfers` (`max_values`: Some(1), `max_size`: Some(1), added: 496, mode: `MaxEncodedLen`)
fn set_enable_balance_transfers() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 5_726_000 picoseconds.
Weight::from_parts(5_890_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
Loading

0 comments on commit 4f3bb5b

Please sign in to comment.