Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into feature/code-quality-c…
Browse files Browse the repository at this point in the history
…lippy
  • Loading branch information
rakanalh committed Nov 10, 2023
2 parents 8107abc + e653d20 commit 3b719ec
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 9 deletions.
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.

1 change: 1 addition & 0 deletions pallets/ddc-nodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] }
ddc-primitives = { version = "0.1.0", default-features = false, path = "../../primitives" }
ddc-traits = { version = "0.1.0", default-features = false, path = "../../traits" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
Expand Down
11 changes: 11 additions & 0 deletions pallets/ddc-nodes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#![recursion_limit = "256"]

use ddc_primitives::{CDNNodePubKey, ClusterId, NodePubKey, StorageNodePubKey};
use ddc_traits::node::{NodeVisitor, NodeVisitorError};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_std::prelude::*;
Expand Down Expand Up @@ -205,4 +206,14 @@ pub mod pallet {
}
}
}

impl<T: Config> NodeVisitor<T> for Pallet<T> {
fn get_cluster_id(
node_pub_key: &NodePubKey,
) -> Result<Option<ClusterId>, NodeVisitorError> {
let node =
Self::get(node_pub_key.clone()).map_err(|_| NodeVisitorError::NodeDoesNotExist)?;
Ok(*node.get_cluster_id())
}
}
}
42 changes: 35 additions & 7 deletions pallets/ddc-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use codec::{Decode, Encode, HasCompact};
pub use ddc_primitives::{ClusterId, NodePubKey, NodeType};
use ddc_traits::{
cluster::{ClusterVisitor, ClusterVisitorError},
node::NodeVisitor,
staking::{StakingVisitor, StakingVisitorError},
};

Expand Down Expand Up @@ -157,6 +158,8 @@ pub mod pallet {
type WeightInfo: WeightInfo;

type ClusterVisitor: ClusterVisitor<Self>;

type NodeVisitor: NodeVisitor<Self>;
}

/// Map from all locked "stash" accounts to the controller account.
Expand Down Expand Up @@ -187,6 +190,11 @@ pub mod pallet {
#[pallet::getter(fn nodes)]
pub type Nodes<T: Config> = StorageMap<_, Twox64Concat, NodePubKey, T::AccountId>;

/// Map from operator stash account to DDC node ID.
#[pallet::storage]
#[pallet::getter(fn providers)]
pub type Providers<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, NodePubKey>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -332,13 +340,14 @@ pub mod pallet {
}

// Reject a bond with a known DDC node.
if Nodes::<T>::contains_key(&node) {
if Nodes::<T>::contains_key(&node) || Providers::<T>::contains_key(&stash) {
Err(Error::<T>::AlreadyPaired)?
}

frame_system::Pallet::<T>::inc_consumers(&stash).map_err(|_| Error::<T>::BadState)?;

Nodes::<T>::insert(&node, &stash);
Providers::<T>::insert(&stash, &node);

// You're auto-bonded forever, here. We might improve this by only bonding when
// you actually store/serve and remove once you unbond __everything__.
Expand Down Expand Up @@ -425,7 +434,24 @@ pub mod pallet {
T::ClusterVisitor::get_unbonding_delay(&cluster_id, NodeType::Storage)
.map_err(Into::<Error<T>>::into)?
} else {
T::BlockNumber::from(10_000_u32)
let node_pub_key =
<Providers<T>>::get(&ledger.stash).ok_or(Error::<T>::BadState)?;

if let Ok(Some(cluster_id)) = T::NodeVisitor::get_cluster_id(&node_pub_key) {
match node_pub_key {
NodePubKey::CDNPubKey(_) =>
T::ClusterVisitor::get_unbonding_delay(&cluster_id, NodeType::CDN)
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?,
NodePubKey::StoragePubKey(_) => T::ClusterVisitor::get_unbonding_delay(
&cluster_id,
NodeType::Storage,
)
.map_err(|e| Into::<Error<T>>::into(ClusterVisitorError::from(e)))?,
}
} else {
// If node is not a member of any cluster, allow immediate unbonding.
T::BlockNumber::from(0u32)
}
};

let block = <frame_system::Pallet<T>>::block_number() + unbonding_delay_in_blocks;
Expand Down Expand Up @@ -678,7 +704,8 @@ pub mod pallet {
ensure!(!<CDNs<T>>::contains_key(&stash), Error::<T>::AlreadyInRole);
ensure!(!<Storages<T>>::contains_key(&stash), Error::<T>::AlreadyInRole);

<Nodes<T>>::insert(new_node, stash);
<Nodes<T>>::insert(new_node.clone(), stash.clone());
<Providers<T>>::insert(stash, new_node);

Ok(())
}
Expand All @@ -687,10 +714,11 @@ pub mod pallet {
///
/// The dispatch origin for this call must be _Signed_ by the controller.
#[pallet::weight(10_000)]
pub fn fast_chill(origin: OriginFor<T>, node_pub_key: NodePubKey) -> DispatchResult {
pub fn fast_chill(origin: OriginFor<T>) -> DispatchResult {
let controller = ensure_signed(origin)?;

let stash = <Ledger<T>>::get(&controller).ok_or(Error::<T>::NotController)?.stash;
let node_pub_key = <Providers<T>>::get(&stash).ok_or(Error::<T>::BadState)?;
let node_stash = <Nodes<T>>::get(&node_pub_key).ok_or(Error::<T>::BadState)?;
ensure!(stash == node_stash, Error::<T>::NotNodeController);

Expand Down Expand Up @@ -763,9 +791,9 @@ pub mod pallet {
<Bonded<T>>::remove(stash);
<Ledger<T>>::remove(&controller);

if let Some((node, _)) = <Nodes<T>>::iter().find(|(_, v)| v == stash) {
<Nodes<T>>::remove(node);
}
if let Some(node_pub_key) = <Providers<T>>::take(stash) {
<Nodes<T>>::remove(node_pub_key);
};

Self::do_remove_storage(stash);
Self::do_remove_cdn(stash);
Expand Down
15 changes: 14 additions & 1 deletion pallets/ddc-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

use crate::{self as pallet_ddc_staking, *};
use ddc_primitives::{CDNNodePubKey, StorageNodePubKey};
use ddc_traits::cluster::{ClusterVisitor, ClusterVisitorError};
use ddc_traits::{
cluster::{ClusterVisitor, ClusterVisitorError},
node::{NodeVisitor, NodeVisitorError},
};

use frame_support::{
construct_runtime,
traits::{ConstU32, ConstU64, Everything, GenesisBuild},
Expand Down Expand Up @@ -96,6 +100,7 @@ impl crate::pallet::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type ClusterVisitor = TestClusterVisitor;
type NodeVisitor = TestNodeVisitor;
}

pub(crate) type DdcStakingCall = crate::Call<Test>;
Expand Down Expand Up @@ -127,6 +132,14 @@ impl<T: Config> ClusterVisitor<T> for TestClusterVisitor {
Ok(T::BlockNumber::from(10u32))
}
}

pub struct TestNodeVisitor;
impl<T: Config> NodeVisitor<T> for TestNodeVisitor {
fn get_cluster_id(_node_pub_key: &NodePubKey) -> Result<Option<ClusterId>, NodeVisitorError> {
Ok(None)
}
}

pub struct ExtBuilder {
has_cdns: bool,
has_storages: bool,
Expand Down
3 changes: 2 additions & 1 deletion runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 48012,
spec_version: 48013,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 5,
Expand Down Expand Up @@ -1320,6 +1320,7 @@ impl pallet_ddc_staking::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_ddc_staking::weights::SubstrateWeight<Runtime>;
type ClusterVisitor = pallet_ddc_clusters::Pallet<Runtime>;
type NodeVisitor = pallet_ddc_nodes::Pallet<Runtime>;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod cluster;
pub mod node;
pub mod staking;
10 changes: 10 additions & 0 deletions traits/src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use ddc_primitives::{ClusterId, NodePubKey};
use frame_system::Config;

pub trait NodeVisitor<T: Config> {
fn get_cluster_id(node_pub_key: &NodePubKey) -> Result<Option<ClusterId>, NodeVisitorError>;
}

pub enum NodeVisitorError {
NodeDoesNotExist,
}

0 comments on commit 3b719ec

Please sign in to comment.