Skip to content
This repository was archived by the owner on Sep 28, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }

# (native)
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
Expand Down
8 changes: 8 additions & 0 deletions chain-extensions/pallet-assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
env_logger = "0.9"
pallet-balances = { workspace = true }
pallet-timestamp = { workspace = true }
sp-io = { workspace = true }
sp-keystore = { workspace = true }

[features]
default = ["std"]
std = [
Expand All @@ -38,4 +45,5 @@ std = [
"sp-runtime/std",
"pallet-assets/std",
"assets-chain-extension-types/std",
"pallet-balances/std",
]
4 changes: 4 additions & 0 deletions chain-extensions/pallet-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

#![cfg_attr(not(feature = "std"), no_std)]

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

use assets_chain_extension_types::{select_origin, Origin, Outcome};
Expand Down
241 changes: 241 additions & 0 deletions chain-extensions/pallet-assets/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar 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.

// Astar 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 Astar. If not, see <http://www.gnu.org/licenses/>.

use crate::weights::SubstrateWeight;
use crate::{weights, AssetsExtension};
use frame_support::traits::{AsEnsureOriginWithArg, ConstU128, Currency, Randomness};
use frame_support::{
parameter_types, sp_io,
traits::{ConstU32, ConstU64, Nothing},
weights::Weight,
};
use pallet_assets::AssetsCallback;
use pallet_contracts::chain_extension::RegisteredChainExtension;
use pallet_contracts::{Config, DefaultAddressGenerator, Frame};
use parity_scale_codec::Encode;
use sp_core::crypto::AccountId32;
use sp_io::storage;
use sp_keystore::{testing::KeyStore, KeystoreExt};
use sp_runtime::generic;
use sp_runtime::testing::H256;
use sp_runtime::traits::{BlakeTwo256, Convert, IdentityLookup, Zero};
use std::sync::Arc;

pub type BlockNumber = u32;
pub type Balance = u128;
pub type AssetId = u128;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(
Weight::from_ref_time(2_000_000_000_000).set_proof_size(u64::MAX),
);
}
impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = BlockWeights;
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u32;
type BlockNumber = BlockNumber;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = AccountId32;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = generic::Header<u32, 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 = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
pub const DeletionWeightLimit: Weight = Weight::from_ref_time(500_000_000_000);
pub static UnstableInterface: bool = true;
pub Schedule: pallet_contracts::Schedule<Test> = Default::default();
pub static DepositPerByte: BalanceOf<Test> = 1;
pub const DepositPerItem: BalanceOf<Test> = 1;
}

pub struct DummyDeprecatedRandomness;
impl Randomness<H256, BlockNumber> for DummyDeprecatedRandomness {
fn random(_: &[u8]) -> (H256, BlockNumber) {
(Default::default(), Zero::zero())
}
}

impl pallet_contracts::Config for Test {
type Time = Timestamp;
type Randomness = DummyDeprecatedRandomness;
type Currency = Balances;
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
type CallFilter = Nothing;
type CallStack = [Frame<Self>; 5];
type WeightPrice = Self;
type WeightInfo = ();
type ChainExtension = AssetsExtension<Self, SubstrateWeight<Self>>;
type DeletionQueueDepth = ConstU32<128>;
type DeletionWeightLimit = DeletionWeightLimit;
type Schedule = Schedule;
type DepositPerByte = DepositPerByte;
type DepositPerItem = DepositPerItem;
type AddressGenerator = DefaultAddressGenerator;
type MaxCodeLen = ConstU32<{ 123 * 1024 }>;
type MaxStorageKeyLen = ConstU32<128>;
type UnsafeUnstableInterface = UnstableInterface;
type MaxDebugBufferLen = ConstU32<{ 2 * 1024 * 1024 }>;
}

impl<W: weights::WeightInfo> RegisteredChainExtension<Test> for AssetsExtension<Test, W> {
const ID: u16 = 02;
}

parameter_types! {
pub static ExistentialDeposit: u64 = 1;
}

impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type Balance = Balance;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
}

impl pallet_timestamp::Config for Test {
type Moment = u64;
type OnTimestampSet = ();
type MinimumPeriod = ConstU64<1>;
type WeightInfo = ();
}

pub struct AssetsCallbackHandle;
impl AssetsCallback<AssetId, AccountId32> for AssetsCallbackHandle {
fn created(_id: &AssetId, _owner: &AccountId32) {
storage::set(b"asset_created", &().encode());
}

fn destroyed(_id: &AssetId) {
storage::set(b"asset_destroyed", &().encode());
}
}

impl pallet_assets::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = AssetId;
type AssetIdParameter = u128;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<AccountId32>>;
type ForceOrigin = frame_system::EnsureRoot<AccountId32>;
type AssetDeposit = ConstU128<1>;
type AssetAccountDeposit = ConstU128<10>;
type MetadataDepositBase = ConstU128<1>;
type MetadataDepositPerByte = ConstU128<1>;
type ApprovalDeposit = ConstU128<1>;
type StringLimit = ConstU32<50>;
type Freezer = ();
type WeightInfo = ();
type CallbackHandle = AssetsCallbackHandle;
type Extra = ();
type RemoveItemsLimit = ConstU32<5>;
}

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Contracts: pallet_contracts::{Pallet, Call, Storage, Event<T>},
}
);

pub const ALICE: AccountId32 = AccountId32::new([1u8; 32]);
pub const BOB: AccountId32 = AccountId32::new([2u8; 32]);
pub const GAS_LIMIT: Weight = Weight::from_ref_time(100_000_000_000).set_proof_size(700000u64);
pub const ONE: u128 = 1_000_000_000_000_000_000;

impl Convert<Weight, BalanceOf<Self>> for Test {
fn convert(w: Weight) -> BalanceOf<Self> {
w.ref_time().into()
}
}

pub struct ExtBuilder {
existential_deposit: u64,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {
existential_deposit: ExistentialDeposit::get(),
}
}
}

impl ExtBuilder {
pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
self.existential_deposit = existential_deposit;
self
}
pub fn set_associated_consts(&self) {
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
}
pub fn build(self) -> sp_io::TestExternalities {
use env_logger::{Builder, Env};
let env = Env::new().default_filter_or("runtime=debug");
let _ = Builder::from_env(env).is_test(true).try_init();
self.set_associated_consts();
let mut t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();
pallet_balances::GenesisConfig::<Test> { balances: vec![] }
.assimilate_storage(&mut t)
.unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.register_extension(KeystoreExt(Arc::new(KeyStore::new())));
ext.execute_with(|| System::set_block_number(1));
ext
}
}
Loading