Skip to content

Commit

Permalink
Add basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dudo50 committed Nov 2, 2024
1 parent 80481fe commit c66b502
Show file tree
Hide file tree
Showing 15 changed files with 2,120 additions and 2,569 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

23 changes: 23 additions & 0 deletions templates/parachain/pallets/xcnft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ codec = { features = [
scale-info = { features = [
"derive",
], workspace = true }
log = { workspace = true }


# Substrate
Expand All @@ -27,11 +28,21 @@ frame-system = { workspace = true }

#XCM
xcm = { workspace = true}
xcm-simulator = { workspace = true, default-features = true }
xcm-executor = { workspace = true, default-features = true }
xcm-builder = { workspace = true, default-features = true }
pallet-xcm = { workspace = true, default-features = true }
polkadot-core-primitives = { workspace = true, default-features = true }
polkadot-runtime-parachains = { workspace = true, default-features = true }
polkadot-parachain-primitives = { workspace = true, default-features = true }

cumulus-primitives-core = { workspace = true }
cumulus-pallet-xcm = { workspace = true }
parachain-info = { workspace = true }

pallet-message-queue = { workspace = true, default-features = true }
sp-tracing = { workspace = true, default-features = true }


sp-runtime = { workspace = true }
sp-std = {workspace = true}
Expand All @@ -47,13 +58,25 @@ sp-io = { workspace = true, default-features = true }

pallet-balances = { workspace = true, default-features = true }




[features]
default = [ "std" ]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"polkadot-parachain-primitives/runtime-benchmarks",
"polkadot-runtime-parachains/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"xcm-executor/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-nfts/runtime-benchmarks",
]
std = [
"codec/std",
Expand Down
63 changes: 31 additions & 32 deletions templates/parachain/pallets/xcnft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@
pub use pallet::*;

#[cfg(test)]
mod mock;

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

pub mod weights;

Expand Down Expand Up @@ -121,9 +120,9 @@ pub mod pallet {
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Default, Debug)]
#[scale_info(skip_type_params(T, I))]
pub struct GeneralizedDestroyWitness {
item_meta: u32,
item_configs: u32,
attributes: u32,
pub item_meta: u32,
pub item_configs: u32,
pub attributes: u32,
}

/// Enum for voting, either Aye or Nay option.
Expand All @@ -139,8 +138,8 @@ pub mod pallet {
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Default, Debug)]
#[scale_info(skip_type_params(T, I))]
pub struct Votes<T: Config<I>, I: 'static = ()> {
aye: BoundedVec<T::AccountId, T::MaxOwners>,
nay: BoundedVec<T::AccountId, T::MaxOwners>,
pub aye: BoundedVec<T::AccountId, T::MaxOwners>,
pub nay: BoundedVec<T::AccountId, T::MaxOwners>,
}

/// Structure of proposal, contains proposal id, collection id, proposed collection owner,
Expand All @@ -149,49 +148,49 @@ pub mod pallet {
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Default, Debug)]
#[scale_info(skip_type_params(T, I))]
pub struct Proposal<T: Config<I>, I: 'static = ()> {
proposal_id: u64,
collection_id: T::CollectionId,
proposed_collection_owner: T::AccountId,
proposed_destination_para: ParaId,
proposed_dest_collection_id: Option<T::CollectionId>,
proposed_destination_config: Option<CollectionConfigFor<T, I>>,
owners: BoundedVec<T::AccountId, T::MaxOwners>,
number_of_votes: Votes<T, I>,
end_time: BlockNumberFor<T>,
pub proposal_id: u64,
pub collection_id: T::CollectionId,
pub proposed_collection_owner: T::AccountId,
pub proposed_destination_para: ParaId,
pub proposed_dest_collection_id: Option<T::CollectionId>,
pub proposed_destination_config: Option<CollectionConfigFor<T, I>>,
pub owners: BoundedVec<T::AccountId, T::MaxOwners>,
pub number_of_votes: Votes<T, I>,
pub end_time: BlockNumberFor<T>,
}

/// Structure of sent assets, contains origin parachain id, origin collection id, origin asset
/// id, destination collection id, and destination asset id.
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Default)]
#[scale_info(skip_type_params(T, I))]
pub struct SentStruct<T: Config<I>, I: 'static = ()> {
origin_para_id: ParaId,
origin_collection_id: T::CollectionId,
origin_asset_id: T::ItemId,
destination_collection_id: T::CollectionId,
destination_asset_id: T::ItemId,
pub origin_para_id: ParaId,
pub origin_collection_id: T::CollectionId,
pub origin_asset_id: T::ItemId,
pub destination_collection_id: T::CollectionId,
pub destination_asset_id: T::ItemId,
}

/// Structure of received assets, contains origin parachain id, origin collection id, origin
/// asset id, received collection id, and received asset id.
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Default)]
#[scale_info(skip_type_params(T, I))]
pub struct ReceivedStruct<T: Config<I>, I: 'static = ()> {
origin_para_id: ParaId,
origin_collection_id: T::CollectionId,
origin_asset_id: T::ItemId,
received_collection_id: T::CollectionId,
received_asset_id: T::ItemId,
pub origin_para_id: ParaId,
pub origin_collection_id: T::CollectionId,
pub origin_asset_id: T::ItemId,
pub received_collection_id: T::CollectionId,
pub received_asset_id: T::ItemId,
}

/// Structure of received collections, contains origin parachain id, origin collection id, and
/// received collection id.
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq, Default)]
#[scale_info(skip_type_params(T, I))]
pub struct ReceivedCols<T: Config<I>, I: 'static = ()> {
origin_para_id: ParaId,
origin_collection_id: T::CollectionId,
received_collection_id: T::CollectionId,
pub origin_para_id: ParaId,
pub origin_collection_id: T::CollectionId,
pub received_collection_id: T::CollectionId,
}

/// Storage for sent assets, contains origin collection id and origin asset id as tuple key and
Expand Down Expand Up @@ -922,7 +921,7 @@ pub mod pallet {
// storage and emit event.
if unwrapped_proposal.number_of_votes.aye.len() < number_of_votes / 2 ||
unwrapped_proposal.number_of_votes.aye.len() == 0 &&
unwrapped_proposal.number_of_votes.nay.len() == 0
unwrapped_proposal.number_of_votes.nay.len() == 0 || unwrapped_proposal.number_of_votes.aye.len() == 0 && unwrapped_proposal.number_of_votes.nay.len() == 1
{
CrossChainProposals::<T, I>::remove(proposal_id);

Expand Down Expand Up @@ -1041,7 +1040,7 @@ pub mod pallet {

if proposal.number_of_votes.aye.len() < number_of_votes / 2 ||
proposal.number_of_votes.aye.len() == 0 &&
proposal.number_of_votes.nay.len() == 0
proposal.number_of_votes.nay.len() == 0 || proposal.number_of_votes.aye.len() == 0 && proposal.number_of_votes.nay.len() == 1
{
Self::deposit_event(Event::ProposalDidNotPass { proposal_id });

Expand Down
174 changes: 174 additions & 0 deletions templates/parachain/pallets/xcnft/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use frame_support::weights::constants::RocksDbWeight;
use frame_system::{mocking::MockBlock, GenesisConfig};
use sp_runtime::{traits::ConstU64, BuildStorage};
use frame_support::parameter_types;
use pallet_balances::AccountData;
use xcm_builder::WithUniqueTopic;
use sp_runtime::MultiSignature;
use sp_runtime::traits::Verify;
use frame_system::EnsureRoot;
use frame_system::EnsureSigned;
use sp_core::ConstU32;
use frame_support::traits::VariantCountOf;
use sp_runtime::AccountId32 as AccountId;

pub type XcmRouter = WithUniqueTopic<(
(),
)>;

// Configure a mock runtime to test the pallet.
#[frame_support::runtime]
mod test_runtime {
#[runtime::runtime]
#[runtime::derive(
RuntimeCall,
RuntimeEvent,
RuntimeError,
RuntimeOrigin,
RuntimeFreezeReason,
RuntimeHoldReason,
RuntimeSlashReason,
RuntimeLockId,
RuntimeTask
)]
pub struct Test;

#[runtime::pallet_index(0)]
pub type System = frame_system;

#[runtime::pallet_index(1)]
pub type XcNFT = crate;

#[runtime::pallet_index(2)]
pub type Balances = pallet_balances;

#[runtime::pallet_index(3)]
pub type NFTs = pallet_nfts;

#[runtime::pallet_index(4)]
pub type ParachainInfo = parachain_info;
}

/// Balance of an account.
pub type Balance = u128;

impl frame_system::Config for Test {
type RuntimeEvent = RuntimeEvent;
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type RuntimeTask = RuntimeTask;
type Hash = sp_core::H256;
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::IdentityLookup<AccountId>;
type Nonce = u64;
type Block = MockBlock<Test>;
type BlockHashCount = ConstU64<250>;
type DbWeight = RocksDbWeight;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type ExtensionsWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
type SingleBlockMigrations = ();
type MultiBlockMigrator = ();
type PreInherents = ();
type PostInherents = ();
type PostTransactions = ();
}

impl parachain_info::Config for Test {}

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type XcmSender = XcmRouter;
type RuntimeCall = RuntimeCall;
type ProposalTimeInBlocks = proposal_time_in_blocks_parameter;
type MaxOwners = max_owners_parameter;
}

pub const UNIT: Balance = 1;

impl pallet_balances::Config for Test {
type MaxLocks = ConstU32<50>;
/// The type for recording an account's balance.
type Balance = Balance;
/// The ubiquitous event type.
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = pallet_balances::weights::SubstrateWeight<Test>;
type MaxReserves = ConstU32<50>;
type ReserveIdentifier = [u8; 8];
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeFreezeReason = RuntimeFreezeReason;
type FreezeIdentifier = RuntimeFreezeReason;
type MaxFreezes = VariantCountOf<RuntimeFreezeReason>;
type DoneSlashHandler = ();
}

parameter_types! {
pub const SS58Prefix: u16 = 42;
pub const ExistentialDeposit: u128 = 500;
pub const CollectionDeposit: Balance = 0 * UNIT; // 1 UNIT deposit to create asset collection
pub const ItemDeposit: Balance = 0 * UNIT; // 1/100 UNIT deposit to create asset item
pub const KeyLimit: u32 = 32;
pub const ValueLimit: u32 = 64;
pub const UniquesMetadataDepositBase: Balance = 0 * UNIT;
pub const AttributeDepositBase: Balance = 0 * UNIT;
pub const DepositPerByte: Balance = 0 * UNIT;
pub const UniquesStringLimit: u32 = 32;
pub const ApprovalsLimit: u32 = 1;
pub const ItemAttributesApprovalsLimit: u32 = 1;
pub const MaxTips: u32 = 1;
pub const MaxDeadlineDuration: u32 = 1;
pub const MaxAttributesPerCall: u32 = 10;
pub NftFeatures: pallet_nfts::PalletFeatures = pallet_nfts::PalletFeatures::all_enabled();
pub const proposal_time_in_blocks_parameter: u32 = 10;
pub const max_owners_parameter: u32 = 1000000;
pub const max_votes: u32 = 1000000;
}

pub type AccountPublic = <MultiSignature as Verify>::Signer;

impl pallet_nfts::Config for Test {
type RuntimeEvent = RuntimeEvent;
type CollectionId = u32;
type ItemId = u32;
type Currency = Balances;
type ForceOrigin = EnsureRoot<AccountId>;
type CreateOrigin = EnsureSigned<AccountId>;
type CollectionDeposit = CollectionDeposit;
type Locker = ();
type ItemDeposit = ItemDeposit;
type MetadataDepositBase = UniquesMetadataDepositBase;
type AttributeDepositBase = AttributeDepositBase;
type DepositPerByte = DepositPerByte;
type StringLimit = UniquesStringLimit;
type KeyLimit = KeyLimit;
type ValueLimit = ValueLimit;
type ApprovalsLimit = ApprovalsLimit;
type ItemAttributesApprovalsLimit = ItemAttributesApprovalsLimit;
type MaxTips = MaxTips;
type MaxDeadlineDuration = MaxDeadlineDuration;
type MaxAttributesPerCall = MaxAttributesPerCall;
type Features = NftFeatures;
type OffchainSignature = MultiSignature;
type OffchainPublic = AccountPublic;
type WeightInfo = ();
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
GenesisConfig::<Test>::default().build_storage().unwrap().into()
}
Loading

0 comments on commit c66b502

Please sign in to comment.