Skip to content

Commit

Permalink
Merge branch 'master' into vs/substrate-v0.9.43-2
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Nov 8, 2023
2 parents b48e2bd + 35c1234 commit dcbec86
Show file tree
Hide file tree
Showing 33 changed files with 474 additions and 919 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ std = [
"sp-io/std",
"sp-arithmetic/std",
"frame-support/std",
"frame-system/std",
"primitive-types/std",
"gear-wasm-instrument?/std",
]
Expand Down
3 changes: 3 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub mod gas_provider;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

#[cfg(feature = "std")]
pub mod pallet_tests;

use core::fmt;
use frame_support::{
codec::{self, Decode, Encode},
Expand Down
153 changes: 153 additions & 0 deletions common/src/pallet_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// This file is part of Gear.

// Copyright (C) 2023 Gear Technologies Inc.
// 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/>.

//! Module contains macros that help to implement Config type
//! for various pallets of Substrate.
//! All used types should be in scope.
use frame_support::{pallet_prelude::*, weights::RuntimeDbWeight};
use frame_system::limits::BlockWeights;
use sp_arithmetic::Perbill;

#[macro_export]
macro_rules! impl_pallet_balances {
($runtime:ty) => {
impl pallet_balances::Config for $runtime {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = Balance;
type DustRemoval = ();
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type MaxHolds = ();
}
};
}

pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
pub const MAX_BLOCK: u64 = 250_000_000_000;

frame_support::parameter_types! {
pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(
Weight::from_parts(MAX_BLOCK, u64::MAX),
NORMAL_DISPATCH_RATIO,
);
pub const SS58Prefix: u8 = 42;
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1_110, write: 2_300 };
pub const MinimumPeriod: u64 = 500;
}

#[macro_export]
macro_rules! impl_pallet_system {
($( $tokens:tt )*) => {
#[allow(dead_code)]
type SystemConfigDbWeight = $crate::pallet_tests::DbWeight;
#[allow(dead_code)]
type SystemConfigBlockWeights = $crate::pallet_tests::RuntimeBlockWeights;

mod pallet_tests_system_config_impl {
use super::*;

$crate::impl_pallet_system_inner!($( $tokens )*);
}
};
}

#[macro_export]
macro_rules! impl_pallet_system_inner {
($runtime:ty$(,)?) => {
impl frame_system::Config for $runtime {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = SystemConfigBlockWeights;
type BlockLength = ();
type DbWeight = SystemConfigDbWeight;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = generic::Header<BlockNumber, BlakeTwo256>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = $crate::pallet_tests::SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
};

($runtime:ty, DbWeight = $db_weight:ty $(, $( $rest:tt )*)?) => {
type SystemConfigDbWeight = $db_weight;

$crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
};

($runtime:ty, BlockWeights = $block_weights:ty $(, $( $rest:tt )*)?) => {
type SystemConfigBlockWeights = $block_weights;

$crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
};
}

#[macro_export]
macro_rules! impl_pallet_timestamp {
($runtime:ty) => {
impl pallet_timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = $crate::pallet_tests::MinimumPeriod;
type WeightInfo = ();
}
};
}

#[macro_export]
macro_rules! impl_pallet_authorship {
($runtime:ty, EventHandler = $event_handler:ty) => {
pub struct FixedBlockAuthor;

impl FindAuthor<AccountId> for FixedBlockAuthor {
fn find_author<'a, I: 'a>(_: I) -> Option<AccountId> {
Some(BLOCK_AUTHOR)
}
}

impl pallet_authorship::Config for $runtime {
type FindAuthor = FixedBlockAuthor;
type EventHandler = $event_handler;
}
};

($runtime:ty) => {
$crate::impl_pallet_authorship!($runtime, EventHandler = ());
};
}
2 changes: 1 addition & 1 deletion gtest/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl ExtManager {
.map(|dispatches| {
dispatches
.into_iter()
.map(|dispatch| self.run_dispatch(dispatch))
.map(|dispatch| self.with_externalities(|this| this.run_dispatch(dispatch)))
.collect()
})
.unwrap_or_default()
Expand Down
9 changes: 9 additions & 0 deletions pallets/gas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);

#[macro_export]
macro_rules! impl_config {
($runtime:ty) => {
impl pallet_gear_gas::Config for $runtime {
type BlockGasLimit = BlockGasLimit;
}
};
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down
68 changes: 9 additions & 59 deletions pallets/gas/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate as pallet_gas;
use crate as pallet_gear_gas;
use frame_support::{construct_runtime, parameter_types, weights::constants::RocksDbWeight};
use frame_system as system;
use primitive_types::H256;
use sp_runtime::{
testing::Header,
generic,
traits::{BlakeTwo256, IdentityLookup},
};
use sp_std::convert::{TryFrom, TryInto};
Expand All @@ -31,6 +31,7 @@ type Block = frame_system::mocking::MockBlock<Test>;
type AccountId = u64;
type BlockNumber = u64;
type Balance = u128;
type BlockGasLimit = ();

pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
Expand All @@ -45,73 +46,22 @@ construct_runtime!(
{
System: system,
GearMessenger: pallet_gear_messenger,
Gas: pallet_gas,
GearGas: pallet_gear_gas,
Balances: pallet_balances,
}
);

impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = Balance;
type DustRemoval = ();
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type FreezeIdentifier = ();
type MaxFreezes = ();
type HoldIdentifier = ();
type MaxHolds = ();
}
common::impl_pallet_system!(Test, DbWeight = RocksDbWeight, BlockWeights = ());
pallet_gear_messenger::impl_config!(Test, CurrentBlockNumber = GearBlockNumber);
pallet_gear_gas::impl_config!(Test);
common::impl_pallet_balances!(Test);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
pub const BlockHashCount: BlockNumber = 250;
pub const ExistentialDeposit: Balance = 1;
}

impl system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = RocksDbWeight;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u128>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
pub const GearBlockNumber: BlockNumber = 100;
}

impl pallet_gear_messenger::Config for Test {
type BlockLimiter = Gas;
type CurrentBlockNumber = GearBlockNumber;
}

impl pallet_gas::Config for Test {
type BlockGasLimit = ();
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::default()
Expand Down
1 change: 1 addition & 0 deletions pallets/gear-bank/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pallet-authorship.workspace = true
sp-runtime.workspace = true

[dev-dependencies]
common = { workspace = true, features = ["std"] }
primitive-types.workspace = true

# Substrate deps
Expand Down
11 changes: 11 additions & 0 deletions pallets/gear-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ pub use pallet::*;

use frame_support::traits::{Currency, StorageVersion};

#[macro_export]
macro_rules! impl_config {
($runtime:ty) => {
impl pallet_gear_bank::Config for $runtime {
type Currency = Balances;
type BankAddress = BankAddress;
type GasMultiplier = GasMultiplier;
}
};
}

pub(crate) type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub(crate) type BalanceOf<T> = <CurrencyOf<T> as Currency<AccountIdOf<T>>>::Balance;
pub(crate) type CurrencyOf<T> = <T as Config>::Currency;
Expand Down
Loading

0 comments on commit dcbec86

Please sign in to comment.