Skip to content

Commit

Permalink
feat: move smart contract logic for request test to substrate (#355)
Browse files Browse the repository at this point in the history
* feat: improved performance and code readability

* feat: implement tx with appchain token in pallet order

* feat: fixed pallet order testing

* feat: fixed benchmarked pallet order

* feat: pallet order migration

* feat: added pallet asset in pallet order at runtime

* feat: fixed pallet order dependency in other pallet

* fix: migration pallet service and pallet genetice analysis service

* fix: revert back currencyType order

* chore: runtime bump version to 2.2.1
  • Loading branch information
abdulhakim2902 authored Sep 29, 2022
1 parent 5b3876e commit 93b8d24
Show file tree
Hide file tree
Showing 44 changed files with 2,723 additions and 980 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion pallets/certifications/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ frame-benchmarking = { default-features = false, git = "https://github.com/parit
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
pallet-randomness-collective-flip = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
pallet-assets = { git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.18', default-features = false }

# Local Dependencies
labs = { path = '../../labs', default-features = false }
Expand Down Expand Up @@ -70,6 +71,7 @@ std = [
'pallet-balances/std',
'pallet-timestamp/std',
'pallet-randomness-collective-flip/std',
'pallet-assets/std',

'traits-order/std',
'traits-services/std',
Expand All @@ -87,6 +89,6 @@ std = [
'primitives-area-code/std',
'primitives-profile-roles/std',
'primitives-ethereum-address/std',

'traits-certifications/std',
]
32 changes: 31 additions & 1 deletion pallets/certifications/tests/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use frame_support::{parameter_types, PalletId};
use frame_support::{parameter_types, traits::ConstU64, PalletId};
use frame_system as system;
use pallet_balances::AccountData;
use sp_core::H256;
Expand Down Expand Up @@ -31,6 +31,7 @@ frame_support::construct_runtime!(
UserProfile: user_profile::{Pallet, Call, Storage, Event<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -105,6 +106,34 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
}

pub type AssetId = u32;
pub type AssetBalance = u128;

parameter_types! {
pub const ApprovalDeposit: Balance = 1;
pub const AssetDeposit: Balance = 1;
pub const MetadataDepositBase: Balance = 1;
pub const MetadataDepositPerByte: Balance = 1;
pub const StringLimit: u32 = 50;
}

impl pallet_assets::Config for Test {
type Event = Event;
type Balance = AssetBalance;
type AssetId = AssetId;
type Currency = Balances;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type AssetAccountDeposit = ConstU64<10>;
type AssetDeposit = AssetDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = StringLimit;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
}

impl labs::Config for Test {
type Event = Event;
type Currency = Balances;
Expand Down Expand Up @@ -143,6 +172,7 @@ impl orders::Config for Test {
type Services = Services;
type GeneticTesting = GeneticTesting;
type Currency = Balances;
type Assets = Assets;
type OrdersWeightInfo = ();
}

Expand Down
18 changes: 15 additions & 3 deletions pallets/genetic-analyst-services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use frame_support::{
codec::{Decode, Encode},
pallet_prelude::*,
sp_runtime::SaturatedConversion,
traits::Currency,
traits::{Currency, StorageVersion},
};
pub use pallet::*;
use primitives_duration::ExpectedDuration;
Expand All @@ -16,7 +16,9 @@ use traits_genetic_analyst_services::{
};

pub mod interface;
pub mod migrations;
pub mod weights;

pub use interface::GeneticAnalystServiceInterface;
use sp_std::prelude::*;

Expand Down Expand Up @@ -71,13 +73,18 @@ where
}
}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[frame_support::pallet]
pub mod pallet {
use super::*;

use crate::{
interface::GeneticAnalystServiceInterface, weights::WeightInfo, Currency,
GeneticAnalystService, GeneticAnalystServiceInfo, GeneticAnalystServiceOwner,
};
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
use frame_support::dispatch::DispatchResultWithPostInfo;
use frame_system::pallet_prelude::*;
pub use sp_std::prelude::*;

Expand All @@ -91,12 +98,17 @@ pub mod pallet {

// ----- This is template code, every pallet needs this ---
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migrations::migrate::<T>()
}
}
// --------------------------------------------------------

// ----- Types -------
Expand Down
96 changes: 96 additions & 0 deletions pallets/genetic-analyst-services/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::{
AccountIdOf, BalanceOf, Config, GeneticAnalystService, GeneticAnalystServiceInfo,
GeneticAnalystServices, HashOf, Pallet,
};
use frame_support::{
pallet_prelude::{Decode, Encode},
traits::Get,
weights::Weight,
};
use primitives_duration::{DurationType, ExpectedDuration};
use primitives_price_and_currency::PriceByCurrency;
use sp_std::vec::Vec;

pub fn migrate<T: Config>() -> Weight {
use frame_support::traits::StorageVersion;

let mut weight: Weight = 0;
let mut version = StorageVersion::get::<Pallet<T>>();

if version < 1 {
weight = weight.saturating_add(version::v1::migrate::<T>());
version = StorageVersion::new(1);
}

version.put::<Pallet<T>>();
weight
}

mod version {
use super::*;

pub mod v1 {
use super::*;

pub fn migrate<T: Config>() -> Weight {
let mut weight = T::DbWeight::get().writes(1);

#[derive(Encode, Decode)]
pub struct OldExpectedDuration {
pub duration: i8,
pub duration_type: DurationType,
}

#[derive(Encode, Decode)]
pub struct OldGeneticAnalystServiceInfo<Balance> {
pub name: Vec<u8>,
pub prices_by_currency: Vec<PriceByCurrency<Balance>>,
pub expected_duration: OldExpectedDuration,
pub description: Vec<u8>,
pub test_result_sample: Vec<u8>,
}

#[derive(Encode, Decode)]
pub struct OldGeneticAnalystService<AccountId, Hash, Balance> {
pub id: Hash,
pub owner_id: AccountId,
pub info: OldGeneticAnalystServiceInfo<Balance>,
}

pub type OldGeneticAnalystServiceOf<T> =
OldGeneticAnalystService<AccountIdOf<T>, HashOf<T>, BalanceOf<T>>;

GeneticAnalystServices::<T>::translate(
|_key, old_services: OldGeneticAnalystServiceOf<T>| {
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));

let old_service_info = &old_services.info;

let old_expected_duration = &old_service_info.expected_duration;
let old_duration = old_expected_duration.duration;
let old_duration_type = old_expected_duration.duration_type.clone();
let expected_duration = ExpectedDuration {
duration: old_duration as u64,
duration_type: old_duration_type,
};

let service_info = GeneticAnalystServiceInfo {
name: old_service_info.name.clone(),
prices_by_currency: old_service_info.prices_by_currency.clone(),
expected_duration,
description: old_service_info.description.clone(),
test_result_sample: old_service_info.test_result_sample.clone(),
};

Some(GeneticAnalystService {
id: old_services.id,
owner_id: old_services.owner_id,
info: service_info,
})
},
);

weight
}
}
}
9 changes: 6 additions & 3 deletions pallets/genetic-testing/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ benchmarks! {
_lab.services[0],
0,
T::Hashing::hash("0xhJ7TRe456FADD2726A132ABJK5RCc9E6fC5869F4".as_bytes()),
RequestTest
RequestTest,
None,
);

let _order_id_list = Orders::<T>::orders_by_lab_id(caller.clone())
Expand Down Expand Up @@ -151,7 +152,8 @@ benchmarks! {
_lab.services[0],
0,
T::Hashing::hash("0xhJ7TRe456FADD2726A132ABJK5RCc9E6fC5869F4".as_bytes()),
RequestTest
RequestTest,
None,
);

let _order_id_list = Orders::<T>::orders_by_lab_id(caller.clone())
Expand Down Expand Up @@ -210,7 +212,8 @@ benchmarks! {
_lab.services[0],
0,
T::Hashing::hash("0xhJ7TRe456FADD2726A132ABJK5RCc9E6fC5869F4".as_bytes()),
StakingRequestService
StakingRequestService,
None,
);

let _order_id_list = Orders::<T>::orders_by_lab_id(caller.clone())
Expand Down
33 changes: 32 additions & 1 deletion pallets/genetic-testing/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::*;

use frame_support::parameter_types;
use frame_support::{parameter_types, types::ConstU64};
use sp_io::TestExternalities;
use sp_runtime::{
testing::Header,
Expand Down Expand Up @@ -32,6 +32,7 @@ frame_support::construct_runtime!(
UserProfile: user_profile::{Pallet, Call, Storage, Event<T>},
Orders: orders::{Pallet, Call, Storage, Config<T>, Event<T>},
GeneticTesting: genetic_testing::{Pallet, Call, Storage, Event<T>},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -85,6 +86,34 @@ impl pallet_balances::Config for Test {
type WeightInfo = ();
}

pub type AssetId = u32;
pub type AssetBalance = u128;

parameter_types! {
pub const ApprovalDeposit: Balance = 1;
pub const AssetDeposit: Balance = 1;
pub const MetadataDepositBase: Balance = 1;
pub const MetadataDepositPerByte: Balance = 1;
pub const StringLimit: u32 = 50;
}

impl pallet_assets::Config for Test {
type Event = Event;
type Balance = AssetBalance;
type AssetId = AssetId;
type Currency = Balances;
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
type AssetAccountDeposit = ConstU64<10>;
type AssetDeposit = AssetDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = StringLimit;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
}

impl labs::Config for Test {
type Event = Event;
type Currency = Balances;
Expand All @@ -111,6 +140,8 @@ impl orders::Config for Test {
type Services = Services;
type GeneticTesting = GeneticTesting;
type Currency = Balances;
type Assets = Assets;
type OrdersWeightInfo = ();
}

impl genetic_testing::Config for Test {
Expand Down
Loading

0 comments on commit 93b8d24

Please sign in to comment.