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
267
268
269
270
271
272
// 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/>.

//! `operations` pallet is the interface for the creation and management of Operations.
//!
//! Operation is an abstraction that represents one task in a sequence of tasks, a Workflow.
//!
//! Every operation has a minimum of one Version which is created when the Operation is created.
//!
//! Each Version contains all the information needed to execute it, download it,
//! and chain it in the Workflow.
//! The pallet also deals with creation and approval of Operation Versions.

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

use sp_std::vec::Vec;

// use frame_support::debug;
mod benchmarking;
mod functions;
pub mod types;
pub mod weights;

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

pub use pallet::*;
pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
  use super::*;
  use crate::types::{
    Operation, OperationData, OperationRecord, OperationVersion, OperationVersionData, OperationVersionRecord,
  };
  use anagolay_support::{
    AnagolayStructureData, AnagolayVersionData, AnagolayVersionExtra, Characters, OperationId, VersionId,
  };
  use frame_support::{pallet_prelude::*, traits::UnixTime};
  use frame_system::pallet_prelude::*;

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

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

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

    /// Timestamps provider
    type TimeProvider: UnixTime;
  }

  /// Retrieve the Operation Manifest with the AccountId ( which is the owner ) and OperationId.
  #[pallet::storage]
  #[pallet::getter(fn operation_by_operation_id_and_account_id)]
  pub type OperationByOperationIdAndAccountId<T: Config> =
    StorageDoubleMap<_, Blake2_128Concat, OperationId, Twox64Concat, T::AccountId, OperationRecord<T>, ValueQuery>;

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

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

  /// Total amount of Operations.
  #[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 operations: Vec<OperationRecord<T>>,
    pub versions: Vec<OperationVersionRecord<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 {
        operations: 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 op_record in &self.operations {
        let operation_id = op_record.record.id.clone();
        OperationByOperationIdAndAccountId::<T>::insert(&operation_id, &op_record.account_id, op_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(OperationId::default()) ==
            operation_id
          {
            VersionIdsByOperationId::<T>::mutate(&operation_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 Operations pallet
  #[pallet::event]
  #[pallet::generate_deposit(pub(crate) fn deposit_event)]
  #[pallet::metadata(T::AccountId = "AccountId")]
  pub enum Event<T: Config> {
    /// Operation Manifest created together with Version and Packages.
    OperationCreated(T::AccountId, OperationId),
    /// 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> {
    /// Operation Manifest already exists.
    OperationAlreadyExists,
    /// 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).
    OperationVersionPackageAlreadyExists,
    /// The Operation already has an initial Version and cannot be published again.
    OperationAlreadyInitialized,
    /// 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 Operation manifest and the initial Version.
    ///
    /// Once you have created the Manifest this extrinsic will always fail with 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 Operation or any other Version.
    ///
    /// # Arguments
    /// * origin - the call origin
    /// * operation_data - the data section of the Operation manifest
    /// * version_data - the data section of the Version manifest
    ///
    /// # Errors
    /// * `OperationAlreadyExists` - if an Operation with the same manifest was already created by
    ///   the caller or by another user
    /// * `OperationAlreadyInitialized` - if the Operation already has an initial Version
    /// * `OperationVersionPackageAlreadyExists` - one of the packages of the Version is already
    ///   registered to another Operation
    /// * `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 fn create(
      origin: OriginFor<T>,
      operation_data: OperationData,
      version_data: OperationVersionData,
    ) -> DispatchResultWithPostInfo {
      let sender = ensure_signed(origin.clone())?;

      let operation_data_validation = operation_data.validate();
      if let Err(ref message) = operation_data_validation {
        Self::deposit_event(Event::BadRequestError(sender.clone(), message.clone()));
      }
      ensure!(operation_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 operation = Operation::new(operation_data);

      ensure!(
        OperationByOperationIdAndAccountId::<T>::iter_prefix_values(&operation.id).count() == 0,
        Error::<T>::OperationAlreadyExists
      );
      ensure!(
        !VersionIdsByOperationId::<T>::contains_key(&operation.id) ||
          VersionIdsByOperationId::<T>::get(&operation.id).is_empty(),
        Error::<T>::OperationAlreadyInitialized
      );
      ensure!(
        version_data
          .artifacts
          .iter()
          .find(|package| anagolay_support::Pallet::<T>::is_existing_artifact(package))
          .is_none(),
        Error::<T>::OperationVersionPackageAlreadyExists
      );

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

      Self::do_create_operation(&operation, &sender, current_block);

      let operation_version = OperationVersion::new_with_extra(
        AnagolayVersionData {
          entity_id: Some(operation.id.clone()),
          ..version_data.clone()
        },
        AnagolayVersionExtra {
          created_at: T::TimeProvider::now().as_secs(),
        },
      );

      Self::do_create_operation_version(&operation_version, &sender, current_block);

      Self::deposit_event(Event::OperationCreated(sender, operation.id.clone()));

      Ok(().into())
    }

    /// Approve Operation Version
    ///
    /// # Arguments
    ///  * origin - The call origin
    ///  * operation_id - The id of the Operation to approve
    #[pallet::weight(<T as Config>::WeightInfo::version_approve())]
    pub fn version_approve(_origin: OriginFor<T>, _operation_id: OperationId) -> DispatchResultWithPostInfo {
      Ok(().into())
    }
  }
}