Skip to content

Commit

Permalink
network config integration with chain-space
Browse files Browse the repository at this point in the history
  • Loading branch information
smohan-dw committed Jun 11, 2024
1 parent d4183e0 commit a1ed408
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
49 changes: 32 additions & 17 deletions pallets/chain-space/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub type SpaceAuthorizationOf<T> = SpaceAuthorization<SpaceIdOf, SpaceCreatorOf<
#[frame_support::pallet]
pub mod pallet {
use super::*;
pub use cord_primitives::StatusOf;
pub use cord_primitives::{IsPermissioned, StatusOf};
use cord_utilities::traits::CallSources;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
Expand All @@ -144,6 +144,7 @@ pub mod pallet {
type OriginSuccess: CallSources<AccountIdOf<Self>, SpaceCreatorOf<Self>>;
type SpaceCreatorId: Parameter + MaxEncodedLen;
type ChainSpaceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
type NetworkPermission: IsPermissioned;

#[pallet::constant]
type MaxSpaceDelegates: Get<u32>;
Expand Down Expand Up @@ -261,6 +262,8 @@ pub mod pallet {
CapacityLimitExceeded,
/// The new capacity value is lower than the current usage
CapacityLessThanUsage,
/// Capacity value missing
CapacityValueMissing,
/// Type capacity overflow
TypeCapacityOverflow,
}
Expand Down Expand Up @@ -549,6 +552,8 @@ pub mod pallet {
},
);

let approved = !T::NetworkPermission::is_permissioned();

<Spaces<T>>::insert(
&identifier,
SpaceDetailsOf::<T> {
Expand All @@ -557,7 +562,7 @@ pub mod pallet {
txn_capacity: 0,
txn_reserve: 0,
txn_count: 0,
approved: false,
approved,
archive: false,
parent: identifier.clone(),
},
Expand Down Expand Up @@ -799,18 +804,18 @@ pub mod pallet {

// Ensure the new capacity is greater than the current usage
ensure!(
(parent_details.txn_capacity >=
(parent_details.txn_count +
parent_details.txn_reserve + new_txn_capacity -
space_details.txn_capacity)),
(parent_details.txn_capacity
>= (parent_details.txn_count
+ parent_details.txn_reserve + new_txn_capacity
- space_details.txn_capacity)),
Error::<T>::CapacityLessThanUsage
);

<Spaces<T>>::insert(
&space_details.parent.clone(),
SpaceDetailsOf::<T> {
txn_reserve: parent_details.txn_reserve - space_details.txn_capacity +
new_txn_capacity,
txn_reserve: parent_details.txn_reserve - space_details.txn_capacity
+ new_txn_capacity,
..parent_details.clone()
},
);
Expand Down Expand Up @@ -993,7 +998,7 @@ pub mod pallet {
pub fn subspace_create(
origin: OriginFor<T>,
space_code: SpaceCodeOf<T>,
count: u64,
count: Option<u64>,
space_id: SpaceIdOf,
) -> DispatchResult {
let creator = <T as Config>::EnsureOrigin::ensure_origin(origin)?.subject();
Expand All @@ -1003,11 +1008,21 @@ pub mod pallet {
ensure!(space_details.approved, Error::<T>::SpaceNotApproved);
ensure!(space_details.creator == creator.clone(), Error::<T>::UnauthorizedOperation);

// Check if the network is permissioned
let is_permissioned = T::NetworkPermission::is_permissioned();

// Ensure count is provided for permissioned networks, else set default for permissionless
let count = match count {
Some(value) => value,
None if is_permissioned => return Err(Error::<T>::CapacityValueMissing.into()),
None => 0,
};

// Ensure the new capacity is greater than the current usage
ensure!(
count <=
(space_details.txn_capacity -
(space_details.txn_count + space_details.txn_reserve)),
count
<= (space_details.txn_capacity
- (space_details.txn_count + space_details.txn_reserve)),
Error::<T>::CapacityLimitExceeded
);

Expand Down Expand Up @@ -1139,17 +1154,17 @@ pub mod pallet {

// Ensure the new capacity is greater than the current usage
ensure!(
(parent_details.txn_capacity >=
(parent_details.txn_count + parent_details.txn_reserve + new_txn_capacity -
space_details.txn_capacity)),
(parent_details.txn_capacity
>= (parent_details.txn_count + parent_details.txn_reserve + new_txn_capacity
- space_details.txn_capacity)),
Error::<T>::CapacityLessThanUsage
);

<Spaces<T>>::insert(
&space_details.parent.clone(),
SpaceDetailsOf::<T> {
txn_reserve: parent_details.txn_reserve - space_details.txn_capacity +
new_txn_capacity,
txn_reserve: parent_details.txn_reserve - space_details.txn_capacity
+ new_txn_capacity,
..parent_details.clone()
},
);
Expand Down
6 changes: 3 additions & 3 deletions pallets/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub mod pallet {

// Stores the network configuration type
#[pallet::storage]
pub type NetworkPermission<T> = StorageValue<_, bool, ValueQuery>;
pub type NetworkPermissioned<T> = StorageValue<_, bool, ValueQuery>;

#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
Expand All @@ -52,15 +52,15 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
NetworkPermission::<T>::put(&self.permissioned);
NetworkPermissioned::<T>::put(&self.permissioned);
}
}
}

impl<T: Config> Pallet<T> {
/// check if the network is permissioned
pub fn is_permissioned() -> bool {
NetworkPermission::<T>::get()
NetworkPermissioned::<T>::get()
}
}

Expand Down
1 change: 1 addition & 0 deletions runtimes/braid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ impl pallet_chain_space::Config for Runtime {
type OriginSuccess = pallet_did::DidRawOrigin<AccountId, DidIdentifier>;
type RuntimeEvent = RuntimeEvent;
type ChainSpaceOrigin = EnsureRoot<AccountId>;
type NetworkPermission = NetworkParameters;
type MaxSpaceDelegates = MaxSpaceDelegates;
type WeightInfo = weights::pallet_chain_space::WeightInfo<Runtime>;
}
Expand Down
1 change: 1 addition & 0 deletions runtimes/loom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ impl pallet_chain_space::Config for Runtime {
type OriginSuccess = pallet_did::DidRawOrigin<AccountId, DidIdentifier>;
type RuntimeEvent = RuntimeEvent;
type ChainSpaceOrigin = MoreThanHalfCouncil;
type NetworkPermission = NetworkParameters;
type MaxSpaceDelegates = MaxSpaceDelegates;
type WeightInfo = weights::pallet_chain_space::WeightInfo<Runtime>;
}
Expand Down
1 change: 1 addition & 0 deletions runtimes/weave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ impl pallet_chain_space::Config for Runtime {
type OriginSuccess = pallet_did::DidRawOrigin<AccountId, DidIdentifier>;
type RuntimeEvent = RuntimeEvent;
type ChainSpaceOrigin = MoreThanHalfCouncil;
type NetworkPermission = NetworkParameters;
type MaxSpaceDelegates = MaxSpaceDelegates;
type WeightInfo = weights::pallet_chain_space::WeightInfo<Runtime>;
}
Expand Down

0 comments on commit a1ed408

Please sign in to comment.