Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move smart contract logic for request test to substrate #355

Merged
merged 10 commits into from
Sep 29, 2022
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