Skip to content

Commit

Permalink
feat: add switch spirtnet (#701)
Browse files Browse the repository at this point in the history
## fixes: [#3589](KILTprotocol/ticket#3589)

I also made some formatting changes for easier review. The best way to
compare would be to review the lib.rs file for Spiritnet against
Peregrine. Only Peregrine-specific features should differ.
  • Loading branch information
Ad96el authored Aug 29, 2024
1 parent 3bd3efc commit 60f670d
Show file tree
Hide file tree
Showing 11 changed files with 1,371 additions and 46 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions runtimes/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ version = { workspace = true }

[dev-dependencies]
did = { workspace = true, features = ["mock", "std"] }
enum-iterator = { workspace = true }
kilt-dip-primitives = { workspace = true, features = ["std"] }
sp-io = { workspace = true, features = ["std"] }

[dependencies]
# External dependencies
cfg-if = { workspace = true }
kilt-asset-dids = { workspace = true }
log = { workspace = true }
parity-scale-codec = { workspace = true, features = ["derive"] }
Expand All @@ -27,6 +29,7 @@ attestation = { workspace = true }
did = { workspace = true }
kilt-dip-primitives = { workspace = true }
kilt-support = { workspace = true }
pallet-asset-switch = { workspace = true }
pallet-deposit-storage = { workspace = true }
pallet-did-lookup = { workspace = true }
pallet-dip-provider = { workspace = true }
Expand Down Expand Up @@ -80,6 +83,7 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"kilt-dip-primitives/runtime-benchmarks",
"kilt-support/runtime-benchmarks",
"pallet-asset-switch/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-deposit-storage/runtime-benchmarks",
Expand Down Expand Up @@ -107,6 +111,7 @@ std = [
"kilt-dip-primitives/std",
"kilt-support/std",
"log/std",
"pallet-asset-switch/std",
"pallet-assets/std",
"pallet-authorship/std",
"pallet-balances/std",
Expand Down Expand Up @@ -144,6 +149,7 @@ try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"kilt-support/try-runtime",
"pallet-asset-switch/try-runtime",
"pallet-assets/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,35 @@

// If you feel like getting in touch with us, you can do so at info@botlabs.org

use frame_support::traits::fungible::Inspect;
use pallet_asset_switch::traits::SwitchHooks;
use runtime_common::{AccountId, Balance};
use xcm::{
v4::{Junction, Junctions, Location},
VersionedLocation,
};

use crate::{KiltToEKiltSwitchPallet, Runtime};
const LOG_TARGET: &str = "runtime::asset-switch::RestrictTransfersToSameUser";

const LOG_TARGET: &str = "runtime::peregrine::asset-switch::RestrictTransfersToSameUser";
type AccountIdOf<R> = <R as frame_system::Config>::AccountId;
type BalanceOf<R, I> = <<R as pallet_asset_switch::Config<I>>::LocalCurrency as Inspect<AccountIdOf<R>>>::Balance;

/// Check requiring the beneficiary be a single `AccountId32` junction
/// containing the same account ID as the account on this chain initiating the
/// switch.
pub struct RestrictswitchDestinationToSelf;

impl SwitchHooks<Runtime, KiltToEKiltSwitchPallet> for RestrictswitchDestinationToSelf {
pub struct RestrictSwitchDestinationToSelf;

impl<R, I> SwitchHooks<R, I> for RestrictSwitchDestinationToSelf
where
R: pallet_asset_switch::Config<I> + pallet_balances::Config,
I: 'static,
[u8; 32]: From<AccountIdOf<R>>,
{
type Error = Error;

fn pre_local_to_remote_switch(
from: &AccountId,
from: &AccountIdOf<R>,
to: &VersionedLocation,
_amount: Balance,
_amount: BalanceOf<R, I>,
) -> Result<(), Self::Error> {
let to_as: Location = to.clone().try_into().map_err(|e| {
log::error!(
Expand Down Expand Up @@ -70,18 +76,18 @@ impl SwitchHooks<Runtime, KiltToEKiltSwitchPallet> for RestrictswitchDestination

// We don't need to take any actions after the switch is executed
fn post_local_to_remote_switch(
_from: &AccountId,
_from: &AccountIdOf<R>,
_to: &VersionedLocation,
_amount: Balance,
_amount: BalanceOf<R, I>,
) -> Result<(), Self::Error> {
Ok(())
}

fn pre_remote_to_local_switch(_to: &AccountId, _amount: u128) -> Result<(), Self::Error> {
fn pre_remote_to_local_switch(_to: &AccountIdOf<R>, _amount: u128) -> Result<(), Self::Error> {
Ok(())
}

fn post_remote_to_local_switch(_to: &AccountId, _amount: u128) -> Result<(), Self::Error> {
fn post_remote_to_local_switch(_to: &AccountIdOf<R>, _amount: u128) -> Result<(), Self::Error> {
Ok(())
}
}
Expand Down
1 change: 1 addition & 0 deletions runtimes/common/src/asset_switch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

// If you feel like getting in touch with us, you can do so at info@botlabs.org

pub mod hooks;
pub mod runtime_api;

use frame_support::traits::EnsureOrigin;
Expand Down
22 changes: 22 additions & 0 deletions runtimes/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ pub mod attestation {
}
}

pub mod assets {
use super::*;

pub const APPROVAL_DEPOSIT: u128 = 0;
pub const ASSET_ACCOUNT_DEPOSIT: u128 = 0;
pub const ASSET_DEPOSIT: u128 = 0;
pub const META_DEPOSIT_BASE: u128 = 0;
pub const META_DEPOSIT_PER_BYTE: u128 = 0;
pub const REMOVE_ITEMS_LIMIT: u32 = 1000;
pub const STRING_LIMIT: u32 = 4;

parameter_types! {
pub const ApprovalDeposit: u128 = APPROVAL_DEPOSIT;
pub const AssetAccountDeposit: u128 = ASSET_ACCOUNT_DEPOSIT;
pub const AssetDeposit: u128 = ASSET_DEPOSIT;
pub const MetaDepositBase: u128 = META_DEPOSIT_BASE;
pub const MetaDepositPerByte: u128 = META_DEPOSIT_PER_BYTE;
pub const RemoveItemsLimit: u32 = REMOVE_ITEMS_LIMIT;
pub const StringLimit: u32 = STRING_LIMIT;
}
}

pub mod delegation {
use scale_info::TypeInfo;

Expand Down
46 changes: 23 additions & 23 deletions runtimes/peregrine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use frame_system::{pallet_prelude::BlockNumberFor, EnsureRoot, EnsureSigned};
use pallet_asset_switch::xcm::{AccountId32ToAccountId32JunctionConverter, MatchesSwitchPairXcmFeeFungibleAsset};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_api::impl_runtime_apis;
use sp_core::{ConstBool, ConstU128, OpaqueMetadata};
use sp_core::{ConstBool, OpaqueMetadata};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto, OpaqueKeys},
Expand All @@ -59,6 +59,7 @@ use kilt_support::traits::ItemFilter;
use pallet_did_lookup::linkable_account::LinkableAccountId;
pub use parachain_staking::InflationInfo;
pub use public_credentials;

use runtime_common::{
asset_switch::{runtime_api::Error as AssetSwitchApiError, EnsureRootAsTreasury},
assets::{AssetDid, PublicCredentialsFilter},
Expand Down Expand Up @@ -93,7 +94,6 @@ use frame_try_runtime::UpgradeCheckSelect;
#[cfg(test)]
mod tests;

mod asset_switch;
mod dip;
mod weights;
pub mod xcm_config;
Expand Down Expand Up @@ -169,7 +169,6 @@ impl frame_system::Config for Runtime {
type BlockWeights = BlockWeights;
type BlockLength = BlockLength;
type SS58Prefix = SS58Prefix;

/// The set code logic
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Runtime>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
Expand Down Expand Up @@ -261,6 +260,13 @@ parameter_types! {
pub const ReservedDmpWeight: Weight = constants::MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
}

type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
Runtime,
RELAY_CHAIN_SLOT_DURATION_MILLIS,
BLOCK_PROCESSING_VELOCITY,
UNINCLUDED_SEGMENT_CAPACITY,
>;

impl cumulus_pallet_parachain_system::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnSystemEvent = ();
Expand All @@ -275,13 +281,6 @@ impl cumulus_pallet_parachain_system::Config for Runtime {
type WeightInfo = weights::cumulus_pallet_parachain_system::WeightInfo<Runtime>;
}

type ConsensusHook = cumulus_pallet_aura_ext::FixedVelocityConsensusHook<
Runtime,
RELAY_CHAIN_SLOT_DURATION_MILLIS,
BLOCK_PROCESSING_VELOCITY,
UNINCLUDED_SEGMENT_CAPACITY,
>;

impl parachain_info::Config for Runtime {}

impl cumulus_pallet_aura_ext::Config for Runtime {}
Expand Down Expand Up @@ -323,11 +322,11 @@ impl pallet_vesting::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type BlockNumberToBalance = ConvertInto;
type BlockNumberProvider = System;
// disable vested transfers by setting min amount to max balance
type MinVestedTransfer = constants::MinVestedTransfer;
type WeightInfo = weights::pallet_vesting::WeightInfo<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type BlockNumberProvider = System;
const MAX_VESTING_SCHEDULES: u32 = constants::MAX_VESTING_SCHEDULES;
}

Expand Down Expand Up @@ -996,7 +995,7 @@ impl pallet_asset_switch::Config<KiltToEKiltSwitchPallet> for Runtime {
type PauseOrigin = EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
type SubmitterOrigin = EnsureSigned<AccountId>;
type SwitchHooks = asset_switch::RestrictswitchDestinationToSelf;
type SwitchHooks = runtime_common::asset_switch::hooks::RestrictSwitchDestinationToSelf;
type SwitchOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::pallet_asset_switch::WeightInfo<Runtime>;
type XcmRouter = XcmRouter;
Expand All @@ -1008,9 +1007,9 @@ impl pallet_asset_switch::Config<KiltToEKiltSwitchPallet> for Runtime {
// No deposit is taken since creation is permissioned. Only the root origin can
// create new assets, and the owner will be the treasury account.
impl pallet_assets::Config for Runtime {
type ApprovalDeposit = ConstU128<0>;
type AssetAccountDeposit = ConstU128<0>;
type AssetDeposit = ConstU128<0>;
type ApprovalDeposit = runtime_common::constants::assets::ApprovalDeposit;
type AssetAccountDeposit = runtime_common::constants::assets::AssetAccountDeposit;
type AssetDeposit = runtime_common::constants::assets::AssetDeposit;
type AssetId = Location;
type AssetIdParameter = Location;
type Balance = Balance;
Expand All @@ -1020,11 +1019,11 @@ impl pallet_assets::Config for Runtime {
type Extra = ();
type ForceOrigin = EnsureRoot<AccountId>;
type Freezer = ();
type MetadataDepositBase = ConstU128<0>;
type MetadataDepositPerByte = ConstU128<0>;
type RemoveItemsLimit = ConstU32<1_000>;
type MetadataDepositBase = runtime_common::constants::assets::MetaDepositBase;
type MetadataDepositPerByte = runtime_common::constants::assets::MetaDepositPerByte;
type RemoveItemsLimit = runtime_common::constants::assets::RemoveItemsLimit;
type RuntimeEvent = RuntimeEvent;
type StringLimit = ConstU32<4>;
type StringLimit = runtime_common::constants::assets::StringLimit;
type WeightInfo = weights::pallet_assets::WeightInfo<Runtime>;

#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -1443,13 +1442,14 @@ impl_runtime_apis! {
Aura::authorities().into_inner()
}
}

impl cumulus_primitives_aura::AuraUnincludedSegmentApi<Block> for Runtime {
fn can_build_upon(
included_hash: <Block as BlockT>::Hash,
slot: cumulus_primitives_aura::Slot,
) -> bool {
ConsensusHook::can_build_upon(included_hash, slot)
}
) -> bool {
ConsensusHook::can_build_upon(included_hash, slot)
}
}

impl cumulus_primitives_core::CollectCollationInfo<Block> for Runtime {
Expand Down Expand Up @@ -1765,8 +1765,8 @@ impl_runtime_apis! {
}

}

}

cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
Expand Down
Loading

0 comments on commit 60f670d

Please sign in to comment.