1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// This file is part of Anagolay Foundation.

// Copyright (C) 2019-2022 Anagolay Foundation.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! `workflows` pallet is the interface for the creation and management of Workflows.
//!
//! A Workflow is composed of Segments, which can be thought of as parts of the Workflow
//! that require other specific parts of the Workflow to be executed beforehand, or otherwise, parts
//! that are in need of some external input and the Workflow execution is paused while waiting for
//! such input.
//!
//! A Segment definition contains a sequence of Operations, the eventual configuration of each one
//! of them, and a reference to the input required to bootstrap the process. In fact, the required
//! input may come from other Segments of the Workflow or from external input as well (eg: end-user
//! interaction)
//!
//! At Segment execution, each Operation of the sequence is executed in order. The previous
//! execution result is passed on to the next execution input, and so on until there are no more
//! Operations to execute in the Segment or a non-recoverable error occurs.
//!
//! The pallet also deals with creation and approval of Workflow Versions.

// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

mod benchmarking;
mod functions;
mod mock;
mod tests;
pub mod types;
pub mod weights;
pub use pallet::*;
pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
  use super::*;
  use crate::types::{
    Workflow, WorkflowData, WorkflowRecord, WorkflowVersion, WorkflowVersionData, WorkflowVersionRecord,
  };
  use anagolay_support::{
    AnagolayStructureData, AnagolayVersionData, AnagolayVersionExtra, Characters, VersionId, WorkflowId,
  };
  use frame_support::{pallet_prelude::*, traits::UnixTime};
  use frame_system::pallet_prelude::*;
  use sp_std::vec::Vec;

  #[pallet::pallet]
  #[pallet::generate_store(pub(super) trait Store)]
  pub struct Pallet<T>(_);

  /// Config of the workflows pallet
  #[pallet::config]
  pub trait Config: frame_system::Config + anagolay_support::Config {
    /// The overarching event type.
    type Event: From<Event<Self>>
      + Into<<Self as frame_system::Config>::Event>
      + IsType<<Self as frame_system::Config>::Event>;

    /// Weight information for extrinsics for this pallet.
    type WeightInfo: WeightInfo;

    /// Timestamps provider
    type TimeProvider: UnixTime;
  }

  /// Retrieve the Workflow Manifest with the WorkflowId and AccountId ( which is the owner )
  #[pallet::storage]
  #[pallet::getter(fn workflow_by_workflow_id_and_account_id)]
  pub type WorkflowByWorkflowIdAndAccountId<T: Config> =
    StorageDoubleMap<_, Blake2_128Concat, WorkflowId, Twox64Concat, T::AccountId, WorkflowRecord<T>, ValueQuery>;

  /// Retrieve all Versions for a single Workflow Manifest.
  #[pallet::storage]
  #[pallet::getter(fn version_ids_by_workflow_id)]
  pub type VersionIdsByWorkflowId<T: Config> = StorageMap<_, Blake2_128Concat, WorkflowId, Vec<VersionId>, ValueQuery>;

  /// Retrieve the Version.
  #[pallet::storage]
  #[pallet::getter(fn version_by_version_id)]
  pub type VersionByVersionId<T: Config> =
    StorageMap<_, Blake2_128Concat, VersionId, WorkflowVersionRecord<T>, ValueQuery>;

  /// Amount of saved workflows
  #[pallet::storage]
  #[pallet::getter(fn total)]
  pub type Total<T: Config> = StorageValue<_, u64, ValueQuery>;

  /// The genesis config type.
  #[pallet::genesis_config]
  pub struct GenesisConfig<T: Config> {
    pub workflows: Vec<WorkflowRecord<T>>,
    pub versions: Vec<WorkflowVersionRecord<T>>,
    pub total: u64,
  }

  /// The default value for the genesis config type.
  #[cfg(feature = "std")]
  impl<T: Config> Default for GenesisConfig<T> {
    fn default() -> Self {
      Self {
        workflows: Default::default(),
        versions: Default::default(),
        total: 0,
      }
    }
  }

  /// The build of genesis for the pallet.
  #[pallet::genesis_build]
  impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
    fn build(&self) {
      Total::<T>::set(self.total);
      for wf_record in &self.workflows {
        let workflow_id = wf_record.record.id.clone();
        WorkflowByWorkflowIdAndAccountId::<T>::insert(&workflow_id, &wf_record.account_id, wf_record);

        for ver_record in &self.versions {
          let version_id = ver_record.record.id.clone();
          if ver_record
            .record
            .data
            .entity_id
            .clone()
            .unwrap_or(WorkflowId::default()) ==
            workflow_id
          {
            VersionIdsByWorkflowId::<T>::mutate(&workflow_id, |version_ids| {
              version_ids.push(version_id.clone());
            });

            VersionByVersionId::<T>::insert(version_id, ver_record);

            anagolay_support::Pallet::<T>::store_artifacts(&ver_record.record.data.artifacts);
          }
        }
      }
    }
  }

  /// Events of the Workflow pallet
  #[pallet::event]
  #[pallet::generate_deposit(pub(super)fn deposit_event)]
  #[pallet::metadata(T::AccountId = "AccountId")]
  pub enum Event<T: Config> {
    /// Workflow Manifest created together with Version and Packages.
    WorkflowCreated(T::AccountId, WorkflowId),
    /// Bad request error occurs and this event propagates a detailed description
    BadRequestError(T::AccountId, Characters),
  }

  /// Errors of the Operations pallet
  #[pallet::error]
  pub enum Error<T> {
    /// Workflow Manifest already exists.
    WorkflowAlreadyExists,
    /// Version package already exists. If you think this is a bug in our system let us know [here](https://matrix.to/#/!FJvAuDoWRoMVuOFYwL:matrix.org?via=matrix.org).
    WorkflowVersionPackageAlreadyExists,
    /// The Workflow already has an initial Version and cannot be published again.
    WorkflowAlreadyInitialized,
    /// A parameter of the request is invalid or does not respect a given constraint
    BadRequest,
  }

  #[pallet::hooks]
  impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

  #[pallet::call]
  impl<T: Config> Pallet<T> {
    /// Create Workflow manifest and the initial Version.
    ///
    /// Once you have created the Manifest this extrinsic will always fail with 3 different
    /// errors, each depend on the parts of the structure.
    /// There is a check that a user cannot cheat and create new package if the package is
    /// connected to other Workflow or any other Version.
    ///
    /// # Arguments
    /// * origin - the call origin
    /// * operation_data - the data section of the Workflow manifest
    /// * version_data - the data section of the Version manifest
    ///
    /// # Errors
    /// * `WorkflowAlreadyExists` - if an Workflow with the same manifest was already created by the
    ///   caller or by another user
    /// * `WorkflowAlreadyInitialized` - if the Workflow already has an initial Version
    /// * `WorkflowVersionPackageAlreadyExists` - one of the packages of the Version is already
    ///   registered to another Workflow
    /// * `BadRequest` - if the request is invalid or does not respect a given constraint
    ///
    /// # Return
    /// `DispatchResultWithPostInfo` containing Unit type
    #[pallet::weight(<T as Config>::WeightInfo::create())]
    pub(super) fn create(
      origin: OriginFor<T>,
      workflow_data: WorkflowData,
      version_data: WorkflowVersionData,
    ) -> DispatchResultWithPostInfo {
      let sender = ensure_signed(origin.clone())?;

      let workflow_data_validation = workflow_data.validate();
      if let Err(ref message) = workflow_data_validation {
        Self::deposit_event(Event::BadRequestError(sender.clone(), message.clone()));
      }
      ensure!(workflow_data_validation.is_ok(), Error::<T>::BadRequest);
      let version_data_validation = version_data.validate();
      if let Err(ref message) = version_data_validation {
        Self::deposit_event(Event::BadRequestError(sender.clone(), message.clone()));
      }
      ensure!(version_data_validation.is_ok(), Error::<T>::BadRequest);

      let workflow = Workflow::new(workflow_data);

      ensure!(
        WorkflowByWorkflowIdAndAccountId::<T>::iter_prefix_values(&workflow.id).count() == 0,
        Error::<T>::WorkflowAlreadyExists
      );
      ensure!(
        !VersionIdsByWorkflowId::<T>::contains_key(&workflow.id) ||
          VersionIdsByWorkflowId::<T>::get(&workflow.id).is_empty(),
        Error::<T>::WorkflowAlreadyInitialized
      );
      ensure!(
        version_data
          .artifacts
          .iter()
          .find(|package| anagolay_support::Pallet::<T>::is_existing_artifact(package))
          .is_none(),
        Error::<T>::WorkflowVersionPackageAlreadyExists
      );

      let current_block = <frame_system::Pallet<T>>::block_number();

      Self::do_create_workflow(&workflow, &sender, &current_block);

      let workflow_version = WorkflowVersion::new_with_extra(
        AnagolayVersionData {
          entity_id: Some(workflow.id.clone()),
          ..version_data.clone()
        },
        AnagolayVersionExtra {
          created_at: T::TimeProvider::now().as_secs(),
        },
      );

      Self::do_create_workflow_version(&workflow_version, &sender, current_block);

      Self::deposit_event(Event::WorkflowCreated(sender, workflow.id.clone()));

      Ok(().into())
    }
  }
}