Skip to content

Commit

Permalink
Merge pull request #2608 from subspace/permissioned_channels_2
Browse files Browse the repository at this point in the history
XDM: Permissioned Channel - 2
  • Loading branch information
vedhavyas authored Mar 14, 2024
2 parents 7c9da56 + 0b9be7c commit 3c32817
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 62 deletions.
12 changes: 12 additions & 0 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,18 @@ pub enum StakingHoldIdentifier {
Staked(OperatorId),
}

/// 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((ChainId, ChannelId)),
}

/// Domains specific Identifier for Balances holds.
#[derive(
PartialEq, Eq, Clone, Encode, Decode, TypeInfo, MaxEncodedLen, Ord, PartialOrd, Copy, Debug,
Expand Down
19 changes: 17 additions & 2 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ use sp_core::crypto::{ByteArray, KeyTypeId};
use sp_core::{OpaqueMetadata, H256};
use sp_domains::bundle_producer_election::BundleProducerElectionParams;
use sp_domains::{
DomainAllowlistUpdates, DomainId, DomainInstanceData, DomainsHoldIdentifier,
ExecutionReceiptFor, OpaqueBundle, OperatorId, OperatorPublicKey, StakingHoldIdentifier,
ChannelId, DomainAllowlistUpdates, DomainId, DomainInstanceData, DomainsHoldIdentifier,
ExecutionReceiptFor, MessengerHoldIdentifier, OpaqueBundle, OperatorId, OperatorPublicKey,
StakingHoldIdentifier,
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId};
Expand Down Expand Up @@ -375,6 +376,7 @@ parameter_types! {
)]
pub enum HoldIdentifier {
Domains(DomainsHoldIdentifier),
Messenger(MessengerHoldIdentifier),
}

impl pallet_domains::HoldIdentifier<Runtime> for HoldIdentifier {
Expand All @@ -393,6 +395,12 @@ impl pallet_domains::HoldIdentifier<Runtime> for HoldIdentifier {
}
}

impl pallet_messenger::HoldIdentifier<Runtime> for HoldIdentifier {
fn messenger_channel(dst_chain_id: ChainId, channel_id: ChannelId) -> Self {
Self::Messenger(MessengerHoldIdentifier::Channel((dst_chain_id, channel_id)))
}
}

impl VariantCount for HoldIdentifier {
const VARIANT_COUNT: u32 = mem::variant_count::<Self>() as u32;
}
Expand Down Expand Up @@ -516,6 +524,11 @@ impl sp_messenger::StorageKeys for StorageKeys {
}
}

parameter_types! {
// TODO: update value
pub const ChannelReserveFee: Balance = 100 * SSC;
}

impl pallet_messenger::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SelfChainId = SelfChainId;
Expand All @@ -537,6 +550,8 @@ impl pallet_messenger::Config for Runtime {
type MmrProofVerifier = MmrProofVerifier;
type StorageKeys = StorageKeys;
type DomainOwner = Domains;
type HoldIdentifier = HoldIdentifier;
type ChannelReserveFee = ChannelReserveFee;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Runtime
Expand Down
43 changes: 33 additions & 10 deletions domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3278,17 +3278,15 @@ async fn test_cross_domains_messages_should_work() {
// Open channel between the Consensus chain and EVM domains
let fee_model = FeeModel { relay_fee: 1 };
alice
.construct_and_send_extrinsic(pallet_sudo::Call::sudo {
call: Box::new(evm_domain_test_runtime::RuntimeCall::Messenger(
pallet_messenger::Call::initiate_channel {
dst_chain_id: ChainId::Consensus,
params: InitiateChannelParams {
max_outgoing_messages: 100,
fee_model,
},
.construct_and_send_extrinsic(evm_domain_test_runtime::RuntimeCall::Messenger(
pallet_messenger::Call::initiate_channel {
dst_chain_id: ChainId::Consensus,
params: InitiateChannelParams {
max_outgoing_messages: 100,
fee_model,
},
)),
})
},
))
.await
.expect("Failed to construct and send extrinsic");
// Wait until channel open
Expand Down Expand Up @@ -3324,6 +3322,31 @@ async fn test_cross_domains_messages_should_work() {
})
.await
.unwrap();

// close channel on consensus chain using sudo since
// channel is opened on domain
let channel_id = alice
.get_open_channel_for_chain(ChainId::Consensus)
.unwrap();
ferdie
.construct_and_send_extrinsic_with(pallet_sudo::Call::sudo {
call: Box::new(subspace_test_runtime::RuntimeCall::Messenger(
pallet_messenger::Call::close_channel {
chain_id: ChainId::Domain(GENESIS_DOMAIN_ID),
channel_id,
},
)),
})
.await
.expect("Failed to construct and send consensus chain to close channel");
// Wait until channel close
produce_blocks_until!(ferdie, alice, {
alice
.get_open_channel_for_chain(ChainId::Consensus)
.is_none()
})
.await
.unwrap();
}

// TODO: Unlock test when multiple domains are supported in DecEx v2.
Expand Down
13 changes: 9 additions & 4 deletions domains/pallets/messenger/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Benchmarking for `pallet-messenger`.
use super::*;
use crate::Pallet as Messenger;
use crate::{CloseChannelBy, Pallet as Messenger};
use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use frame_support::traits::Get;
Expand Down Expand Up @@ -58,7 +58,8 @@ mod benchmarks {
let channel_id = NextChannelId::<T>::get(dst_chain_id);
assert_ok!(Messenger::<T>::do_init_channel(
dst_chain_id,
dummy_channel_params::<T>()
dummy_channel_params::<T>(),
None,
));
let channel = Channels::<T>::get(dst_chain_id, channel_id).expect("channel should exist");
assert_eq!(channel.state, ChannelState::Initiated);
Expand All @@ -80,7 +81,11 @@ mod benchmarks {

#[block]
{
assert_ok!(Messenger::<T>::do_close_channel(dst_chain_id, channel_id));
assert_ok!(Messenger::<T>::do_close_channel(
dst_chain_id,
channel_id,
CloseChannelBy::Sudo
));
}

let channel = Channels::<T>::get(dst_chain_id, channel_id).expect("channel should exist");
Expand Down Expand Up @@ -217,7 +222,7 @@ mod benchmarks {
params: InitiateChannelParams<BalanceOf<T>>,
) -> ChannelId {
let channel_id = NextChannelId::<T>::get(dst_chain_id);
assert_ok!(Messenger::<T>::do_init_channel(dst_chain_id, params));
assert_ok!(Messenger::<T>::do_init_channel(dst_chain_id, params, None));
let channel = Channels::<T>::get(dst_chain_id, channel_id).expect("channel should exist");
assert_eq!(channel.state, ChannelState::Initiated);

Expand Down
Loading

0 comments on commit 3c32817

Please sign in to comment.