Skip to content

Commit

Permalink
Merge pull request #37 from digicatapult/feature/in-121
Browse files Browse the repository at this point in the history
Feature/in 121
  • Loading branch information
n3op2 authored Feb 8, 2022
2 parents 5ae3360 + 138e095 commit 42327ac
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 38 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = '2018'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/vitalam-node/'
name = 'vitalam-node'
version = '2.8.1'
version = '2.8.2'

[[bin]]
name = 'vitalam-node'
Expand All @@ -25,7 +25,7 @@ structopt = '0.3.8'
hex-literal = "0.3.1"
bs58 = "0.4.0"

vitalam-node-runtime = { path = '../runtime', version = '2.2.0' }
vitalam-node-runtime = { path = '../runtime', version = '2.2.1' }

# Substrate dependencies
frame-benchmarking = '3.0.0'
Expand Down
2 changes: 1 addition & 1 deletion pallets/process-validation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = '2018'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/vitalam-node/'
name = 'pallet-process-validation'
version = "1.0.0"
version = "1.0.1"


[package.metadata.docs.rs]
Expand Down
157 changes: 136 additions & 21 deletions pallets/process-validation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use frame_support::Parameter;
pub use pallet::*;
use sp_runtime::traits::{AtLeast32Bit, One};
use sp_std::prelude::*;

use vitalam_pallet_traits::{ProcessIO, ProcessValidator};
Expand All @@ -21,6 +23,7 @@ use restrictions::Restriction;
pub enum ProcessStatus {
Disabled,
Enabled,
None,
}

impl Default for ProcessStatus {
Expand All @@ -29,15 +32,23 @@ impl Default for ProcessStatus {
}
}

#[derive(Encode, Decode, Default, Clone, PartialEq)]
#[derive(Encode, Decode, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Process {
status: ProcessStatus,
restrictions: Vec<Restriction>,
}

pub mod weights;
impl Default for Process {
fn default() -> Self {
Process {
status: ProcessStatus::None,
restrictions: vec![],
}
}
}

pub mod weights;
pub use weights::WeightInfo;

#[frame_support::pallet]
Expand All @@ -46,17 +57,17 @@ pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::traits::AtLeast32Bit;

type Restrictions = Vec<Restriction>;

/// The pallet's configuration trait.
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

// The primary identifier for a process (i.e. it's name)
// The primary identifier for a process (i.e. it's name, and version)
type ProcessIdentifier: Parameter;
type ProcessVersion: Parameter + AtLeast32Bit;
type ProcessVersion: Parameter + AtLeast32Bit + Default;

// Origins for calling these extrinsics. For now these are expected to be root
type CreateProcessOrigin: EnsureOrigin<Self::Origin>;
Expand All @@ -79,8 +90,8 @@ pub mod pallet {

/// Storage map definition
#[pallet::storage]
#[pallet::getter(fn processes_by_id_and_version)]
pub(super) type ProcessesByIdAndVersion<T: Config> = StorageDoubleMap<
#[pallet::getter(fn process_model)]
pub(super) type ProcessModel<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::ProcessIdentifier,
Expand All @@ -90,38 +101,142 @@ pub mod pallet {
ValueQuery,
>;

#[pallet::storage]
#[pallet::getter(fn version_model)]
pub(super) type VersionModel<T: Config> =
StorageMap<_, Blake2_128Concat, T::ProcessIdentifier, T::ProcessVersion, ValueQuery>;

#[pallet::event]
#[pallet::metadata(
ProcessIdentifier = "ProcessIdentifier",
ProcessVersion = "ProcessVersion",
Vec<Restriction> = "Restrictions",
bool = "IsNew"
)]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
// TODO: implement correct events for extrinsics including params
ProcessCreated,
ProcessDisabled,
// id, version, restrictions, is_new
ProcessCreated(T::ProcessIdentifier, T::ProcessVersion, Vec<Restriction>, bool),
//id, version
ProcessDisabled(T::ProcessIdentifier, T::ProcessVersion),
}

#[pallet::error]
pub enum Error<T> {
// TODO: implement errors for extrinsics
// process already exists, investigate
AlreadyExists,
// attempting to disable non-existing process
NonExistingProcess,
// process is already disabled
AlreadyDisabled,
// process not found for this versiion
InvalidVersion,
}

// The pallet's dispatchable functions.
#[pallet::call]
impl<T: Config> Pallet<T> {
// TODO: implement create_process with correct parameters and impl
#[pallet::weight(T::WeightInfo::create_process())]
pub(super) fn create_process(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
// Check it was signed and get the signer
pub(super) fn create_process(
origin: OriginFor<T>,
id: T::ProcessIdentifier,
restrictions: Vec<Restriction>,
) -> DispatchResultWithPostInfo {
T::CreateProcessOrigin::ensure_origin(origin)?;
let version: T::ProcessVersion = Pallet::<T>::update_version(id.clone()).unwrap();
Pallet::<T>::persist_process(&id, &version, &restrictions)?;

Self::deposit_event(Event::ProcessCreated(
id,
version.clone(),
restrictions,
version == One::one(),
));

Self::deposit_event(Event::ProcessCreated);
Ok(().into())
return Ok(().into());
}

// TODO: implement disable_process with correct parameters and impl
#[pallet::weight(T::WeightInfo::disable_process())]
pub(super) fn disable_process(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
pub(super) fn disable_process(
origin: OriginFor<T>,
id: T::ProcessIdentifier,
version: T::ProcessVersion,
) -> DispatchResultWithPostInfo {
T::DisableProcessOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::ProcessDisabled);
Ok(().into())
Pallet::<T>::validate_version_and_process(&id, &version)?;
Pallet::<T>::set_disabled(&id, &version)?;

Self::deposit_event(Event::ProcessDisabled(id, version));
return Ok(().into());
}
}

// helper methods
impl<T: Config> Pallet<T> {
pub fn get_version(id: &T::ProcessIdentifier) -> T::ProcessVersion {
return match <VersionModel<T>>::contains_key(&id) {
true => <VersionModel<T>>::get(&id) + One::one(),
false => One::one(),
};
}

pub fn update_version(id: T::ProcessIdentifier) -> Result<T::ProcessVersion, Error<T>> {
let version: T::ProcessVersion = Pallet::<T>::get_version(&id);
match version == One::one() {
true => <VersionModel<T>>::insert(&id, version.clone()),
false => <VersionModel<T>>::mutate(&id, |v| *v = version.clone()),
};

return Ok(version);
}

pub fn persist_process(
id: &T::ProcessIdentifier,
v: &T::ProcessVersion,
r: &Restrictions,
) -> Result<(), Error<T>> {
return match <ProcessModel<T>>::contains_key(&id, &v) {
true => Err(Error::<T>::AlreadyExists),
false => {
<ProcessModel<T>>::insert(
id,
v,
Process {
restrictions: r.clone(),
..Default::default()
},
);
return Ok(());
}
};
}

pub fn set_disabled(id: &T::ProcessIdentifier, version: &T::ProcessVersion) -> Result<(), Error<T>> {
let process: Process = <ProcessModel<T>>::get(&id, &version);
return match process.status == ProcessStatus::Disabled {
true => Err(Error::<T>::AlreadyDisabled),
false => {
<ProcessModel<T>>::mutate(id.clone(), version, |process| {
(*process).status = ProcessStatus::Disabled;
});
return Ok(());
}
};
}

pub fn validate_version_and_process(
id: &T::ProcessIdentifier,
version: &T::ProcessVersion,
) -> Result<(), Error<T>> {
ensure!(
<ProcessModel<T>>::contains_key(&id, version.clone()),
Error::<T>::NonExistingProcess,
);
ensure!(<VersionModel<T>>::contains_key(&id), Error::<T>::InvalidVersion);
return match *version != <VersionModel<T>>::get(&id) {
true => Err(Error::<T>::InvalidVersion),
false => Ok(()),
};
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pallets/process-validation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ frame_support::construct_runtime!(
{
System: frame_system::{Module, Call, Config, Storage, Event<T>},
ProcessValidation: pallet_process_validation::{Module, Call, Storage, Event<T>},

}
);
parameter_types! {
Expand Down
Loading

0 comments on commit 42327ac

Please sign in to comment.