Skip to content

Commit

Permalink
Merge all HoldIdentifier into one
Browse files Browse the repository at this point in the history
Signed-off-by: linning <[email protected]>
  • Loading branch information
NingLin-P committed Sep 25, 2024
1 parent ddbeb94 commit c798cdb
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 167 deletions.
2 changes: 1 addition & 1 deletion crates/pallet-domains/src/staking_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ mod tests {

assert_ok!(do_unlock_nominator::<Test>(operator_id, operator_account));

let hold_id = crate::tests::HoldIdentifier::staking_staked();
let hold_id = crate::tests::HoldIdentifierWrapper::staking_staked();
for (nominator_id, mut expected_usable_balance) in expected_usable_balances {
expected_usable_balance += minimum_free_balance;
assert_eq!(Deposits::<Test>::get(operator_id, nominator_id), None);
Expand Down
32 changes: 14 additions & 18 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
RuntimeRegistry, ScheduledRuntimeUpgrades,
};
use codec::{Decode, Encode, MaxEncodedLen};
use core::mem;
use domain_runtime_primitives::opaque::Header as DomainHeader;
use domain_runtime_primitives::BlockNumber as DomainBlockNumber;
use frame_support::dispatch::{DispatchInfo, RawOrigin};
Expand All @@ -26,9 +27,9 @@ use sp_core::{Get, H256, U256};
use sp_domains::merkle_tree::MerkleTree;
use sp_domains::storage::RawGenesis;
use sp_domains::{
BundleHeader, ChainId, DomainId, DomainsHoldIdentifier, ExecutionReceipt, InboxedBundle,
OpaqueBundle, OperatorAllowList, OperatorId, OperatorPair, ProofOfElection, RuntimeId,
RuntimeType, SealedBundleHeader,
BundleHeader, ChainId, DomainId, ExecutionReceipt, InboxedBundle, OpaqueBundle,
OperatorAllowList, OperatorId, OperatorPair, ProofOfElection, RuntimeId, RuntimeType,
SealedBundleHeader,
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_runtime::traits::{
Expand All @@ -38,7 +39,7 @@ use sp_runtime::transaction_validity::TransactionValidityError;
use sp_runtime::{BuildStorage, OpaqueExtrinsic, Saturating};
use sp_version::RuntimeVersion;
use subspace_core_primitives::U256 as P256;
use subspace_runtime_primitives::{Moment, StorageFee, SSC};
use subspace_runtime_primitives::{HoldIdentifier, Moment, StorageFee, SSC};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -100,29 +101,24 @@ impl Get<BlockNumber> for ConfirmationDepthK {
#[derive(
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum HoldIdentifier {
Domains(DomainsHoldIdentifier),
}
pub struct HoldIdentifierWrapper(HoldIdentifier);

impl pallet_domains::HoldIdentifier<Test> for HoldIdentifier {
impl pallet_domains::HoldIdentifier<Test> for HoldIdentifierWrapper {
fn staking_staked() -> FungibleHoldId<Test> {
Self::Domains(DomainsHoldIdentifier::Staking)
Self(HoldIdentifier::DomainStaking)
}

fn domain_instantiation_id() -> FungibleHoldId<Test> {
Self::Domains(DomainsHoldIdentifier::DomainInstantiation)
Self(HoldIdentifier::DomainInstantiation)
}

fn storage_fund_withdrawal() -> Self {
Self::Domains(DomainsHoldIdentifier::StorageFund)
Self(HoldIdentifier::DomainStorageFund)
}
}

impl VariantCount for HoldIdentifier {
// TODO: HACK this is not the actual variant count but it is required see
// https://github.com/autonomys/subspace/issues/2674 for more details. It
// will be resolved as https://github.com/paritytech/polkadot-sdk/issues/4033.
const VARIANT_COUNT: u32 = 10;
impl VariantCount for HoldIdentifierWrapper {
const VARIANT_COUNT: u32 = mem::variant_count::<HoldIdentifier>() as u32;
}

parameter_types! {
Expand All @@ -134,7 +130,7 @@ impl pallet_balances::Config for Test {
type Balance = Balance;
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type RuntimeHoldReason = HoldIdentifier;
type RuntimeHoldReason = HoldIdentifierWrapper;
type DustRemoval = ();
}

Expand Down Expand Up @@ -248,7 +244,7 @@ impl pallet_domains::Config for Test {
type ConfirmationDepthK = ConfirmationDepthK;
type DomainRuntimeUpgradeDelay = DomainRuntimeUpgradeDelay;
type Currency = Balances;
type HoldIdentifier = HoldIdentifier;
type HoldIdentifier = HoldIdentifierWrapper;
type WeightInfo = pallet_domains::weights::SubstrateWeight<Test>;
type InitialDomainTxRange = InitialDomainTxRange;
type DomainTxRangeAdjustmentInterval = DomainTxRangeAdjustmentInterval;
Expand Down
19 changes: 0 additions & 19 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,25 +951,6 @@ pub type OperatorId = u64;
/// Channel identity.
pub type ChannelId = sp_core::U256;

/// Messenger specific hold identifier
#[derive(
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum MessengerHoldIdentifier {
/// Holds the current reserved balance for channel opening
Channel,
}

/// Domains specific Identifier for Balances holds.
#[derive(
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum DomainsHoldIdentifier {
Staking,
DomainInstantiation,
StorageFund,
}

/// Domains specific digest item.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo)]
pub enum DomainDigestItem {
Expand Down
11 changes: 11 additions & 0 deletions crates/subspace-runtime-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ impl<Balance: Codec + tokens::Balance> Default for BlockTransactionByteFee<Balan
}
}

#[derive(
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum HoldIdentifier {
DomainStaking,
DomainInstantiation,
DomainStorageFund,
MessengerChannel,
Preimage,
}

#[cfg(feature = "testing")]
pub mod tests_utils {
use frame_support::dispatch::DispatchClass;
Expand Down
43 changes: 19 additions & 24 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ use sp_core::crypto::{ByteArray, KeyTypeId};
use sp_core::{ConstBool, OpaqueMetadata, H256};
use sp_domains::bundle_producer_election::BundleProducerElectionParams;
use sp_domains::{
ChannelId, DomainAllowlistUpdates, DomainId, DomainInstanceData, DomainsHoldIdentifier,
ExecutionReceiptFor, MessengerHoldIdentifier, OpaqueBundle, OperatorId, OperatorPublicKey,
DOMAIN_STORAGE_FEE_MULTIPLIER, INITIAL_DOMAIN_TX_RANGE,
ChannelId, DomainAllowlistUpdates, DomainId, DomainInstanceData, ExecutionReceiptFor,
OpaqueBundle, OperatorId, OperatorPublicKey, DOMAIN_STORAGE_FEE_MULTIPLIER,
INITIAL_DOMAIN_TX_RANGE,
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_domains_fraud_proof::storage_proof::{
Expand Down Expand Up @@ -106,8 +106,9 @@ use subspace_core_primitives::{
};
use subspace_runtime_primitives::{
maximum_normal_block_length, AccountId, Balance, BlockNumber, FindBlockRewardAddress, Hash,
Moment, Nonce, Signature, SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC, MAX_BLOCK_LENGTH,
MIN_REPLICATION_FACTOR, NORMAL_DISPATCH_RATIO, SHANNON, SLOT_PROBABILITY, SSC,
HoldIdentifier, Moment, Nonce, Signature, SlowAdjustingFeeUpdate, BLOCK_WEIGHT_FOR_2_SEC,
MAX_BLOCK_LENGTH, MIN_REPLICATION_FACTOR, NORMAL_DISPATCH_RATIO, SHANNON, SLOT_PROBABILITY,
SSC,
};

sp_runtime::impl_opaque_keys! {
Expand Down Expand Up @@ -328,36 +329,30 @@ parameter_types! {
#[derive(
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum HoldIdentifier {
Domains(DomainsHoldIdentifier),
Messenger(MessengerHoldIdentifier),
Preimage,
}
pub struct HoldIdentifierWrapper(HoldIdentifier);

impl pallet_domains::HoldIdentifier<Runtime> for HoldIdentifier {
impl pallet_domains::HoldIdentifier<Runtime> for HoldIdentifierWrapper {
fn staking_staked() -> Self {
Self::Domains(DomainsHoldIdentifier::Staking)
Self(HoldIdentifier::DomainStaking)
}

fn domain_instantiation_id() -> Self {
Self::Domains(DomainsHoldIdentifier::DomainInstantiation)
Self(HoldIdentifier::DomainInstantiation)
}

fn storage_fund_withdrawal() -> Self {
Self::Domains(DomainsHoldIdentifier::StorageFund)
Self(HoldIdentifier::DomainStorageFund)
}
}

impl pallet_messenger::HoldIdentifier<Runtime> for HoldIdentifier {
impl pallet_messenger::HoldIdentifier<Runtime> for HoldIdentifierWrapper {
fn messenger_channel() -> Self {
Self::Messenger(MessengerHoldIdentifier::Channel)
Self(HoldIdentifier::MessengerChannel)
}
}

impl VariantCount for HoldIdentifier {
const VARIANT_COUNT: u32 = 1
+ mem::variant_count::<DomainsHoldIdentifier>() as u32
+ mem::variant_count::<MessengerHoldIdentifier>() as u32;
impl VariantCount for HoldIdentifierWrapper {
const VARIANT_COUNT: u32 = mem::variant_count::<HoldIdentifier>() as u32;
}

impl pallet_balances::Config for Runtime {
Expand All @@ -375,7 +370,7 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
type FreezeIdentifier = ();
type MaxFreezes = ();
type RuntimeHoldReason = HoldIdentifier;
type RuntimeHoldReason = HoldIdentifierWrapper;
}

parameter_types! {
Expand Down Expand Up @@ -465,7 +460,7 @@ impl pallet_collective::Config<CouncilCollective> for Runtime {
parameter_types! {
pub PreimageBaseDeposit: Balance = 100 * SSC;
pub PreimageByteDeposit: Balance = SSC;
pub const PreImageHoldReason: HoldIdentifier = HoldIdentifier::Preimage;
pub const PreImageHoldReason: HoldIdentifierWrapper = HoldIdentifierWrapper(HoldIdentifier::Preimage);
}

impl pallet_preimage::Config for Runtime {
Expand Down Expand Up @@ -695,7 +690,7 @@ impl pallet_messenger::Config for Runtime {
type MmrProofVerifier = MmrProofVerifier;
type StorageKeys = StorageKeys;
type DomainOwner = Domains;
type HoldIdentifier = HoldIdentifier;
type HoldIdentifier = HoldIdentifierWrapper;
type ChannelReserveFee = ChannelReserveFee;
type ChannelInitReservePortion = ChannelInitReservePortion;
type DomainRegistration = DomainRegistration;
Expand Down Expand Up @@ -818,7 +813,7 @@ impl pallet_domains::Config for Runtime {
type ConfirmationDepthK = ConfirmationDepthK;
type DomainRuntimeUpgradeDelay = DomainRuntimeUpgradeDelay;
type Currency = Balances;
type HoldIdentifier = HoldIdentifier;
type HoldIdentifier = HoldIdentifierWrapper;
type WeightInfo = pallet_domains::weights::SubstrateWeight<Runtime>;
type InitialDomainTxRange = InitialDomainTxRange;
type DomainTxRangeAdjustmentInterval = DomainTxRangeAdjustmentInterval;
Expand Down
12 changes: 5 additions & 7 deletions domains/pallets/messenger/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ macro_rules! impl_runtime {
use crate::mock::MockEndpoint;
use crate::mock::{AccountId, Balance, MessageId, TestExternalities};
use codec::{Decode, Encode};
use domain_runtime_primitives::{MultiAccountId, TryConvertBack};
use domain_runtime_primitives::{MultiAccountId, TryConvertBack, HoldIdentifier};
#[cfg(not(feature = "runtime-benchmarks"))]
use frame_support::pallet_prelude::*;
use frame_support::{derive_impl, parameter_types};
Expand All @@ -31,10 +31,8 @@ macro_rules! impl_runtime {
use sp_messenger::messages::{ChainId, FeeModel};
use sp_runtime::traits::Convert;
use sp_runtime::BuildStorage;
use crate::HoldIdentifier;
use scale_info::TypeInfo;
use codec::MaxEncodedLen;
use sp_domains::MessengerHoldIdentifier;
use frame_support::traits::VariantCount;
use core::mem;
use sp_runtime::Perbill;
Expand Down Expand Up @@ -68,16 +66,16 @@ macro_rules! impl_runtime {
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum MockHoldIdentifer {
Messenger(MessengerHoldIdentifier)
Messenger(HoldIdentifier)
}

impl VariantCount for MockHoldIdentifer {
const VARIANT_COUNT: u32 = mem::variant_count::<Self>() as u32;
const VARIANT_COUNT: u32 = mem::variant_count::<HoldIdentifier>() as u32;
}

impl HoldIdentifier<$runtime> for MockHoldIdentifer {
impl crate::HoldIdentifier<$runtime> for MockHoldIdentifer {
fn messenger_channel() -> Self {
MockHoldIdentifer::Messenger(MessengerHoldIdentifier::Channel)
MockHoldIdentifer::Messenger(HoldIdentifier::MessengerChannel)
}
}

Expand Down
11 changes: 5 additions & 6 deletions domains/pallets/transporter/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate as pallet_transporter;
use crate::{Config, TryConvertBack};
use codec::{Decode, Encode};
use domain_runtime_primitives::MultiAccountId;
use domain_runtime_primitives::{HoldIdentifier, MultiAccountId};
use frame_support::pallet_prelude::{MaxEncodedLen, TypeInfo};
use frame_support::traits::VariantCount;
use frame_support::{derive_impl, parameter_types};
use pallet_balances::AccountData;
use pallet_messenger::HoldIdentifier;
use sp_core::U256;
use sp_domains::{DomainId, MessengerHoldIdentifier};
use sp_domains::DomainId;
use sp_messenger::endpoint::{Endpoint, EndpointHandler, EndpointId, EndpointRequest, Sender};
use sp_messenger::messages::{ChainId, FeeModel, MessageId};
use sp_runtime::traits::{Convert, IdentityLookup};
Expand Down Expand Up @@ -61,7 +60,7 @@ parameter_types! {
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
)]
pub enum MockHoldIdentifer {
Messenger(MessengerHoldIdentifier),
Messenger(HoldIdentifier),
}

impl VariantCount for MockHoldIdentifer {
Expand All @@ -76,9 +75,9 @@ impl sp_messenger::DomainRegistration for DomainRegistration {
}
}

impl HoldIdentifier<MockRuntime> for MockHoldIdentifer {
impl pallet_messenger::HoldIdentifier<MockRuntime> for MockHoldIdentifer {
fn messenger_channel() -> Self {
MockHoldIdentifer::Messenger(MessengerHoldIdentifier::Channel)
MockHoldIdentifer::Messenger(HoldIdentifier::MessengerChannel)
}
}

Expand Down
1 change: 1 addition & 0 deletions domains/primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use sp_runtime::transaction_validity::TransactionValidityError;
use sp_runtime::{MultiAddress, MultiSignature, Perbill};
use sp_weights::constants::WEIGHT_REF_TIME_PER_SECOND;
use sp_weights::Weight;
pub use subspace_runtime_primitives::HoldIdentifier;
use subspace_runtime_primitives::{MAX_BLOCK_LENGTH, SHANNON, SLOT_PROBABILITY};

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
Expand Down
Loading

0 comments on commit c798cdb

Please sign in to comment.