Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pallet-Messenger-3 #852

Merged
merged 9 commits into from
Oct 10, 2022
12 changes: 12 additions & 0 deletions crates/pallet-messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ mod pallet {
Channel, ChannelId, ChannelState, InitiateChannelParams, Nonce, StateRootOf, U256,
};
use frame_support::pallet_prelude::*;
use frame_support::transactional;
use frame_system::pallet_prelude::*;
use sp_core::storage::StorageKey;
use sp_messenger::SystemDomainTracker;
Expand Down Expand Up @@ -204,6 +205,15 @@ mod pallet {
channel_id: ChannelId,
nonce: Nonce,
},

/// Emits when a message response is available for Inbox message.
InboxMessageResponse {
/// Destination domain ID.
domain_id: T::DomainId,
/// Channel Is
channel_id: ChannelId,
nonce: Nonce,
},
}

type Tag<DomainId> = (DomainId, ChannelId, Nonce);
Expand Down Expand Up @@ -327,6 +337,7 @@ mod pallet {
/// Channel is set to initiated and do not accept or receive any messages.
/// Only a root user can create the channel.
#[pallet::weight((10_000, Pays::No))]
#[transactional]
vedhavyas marked this conversation as resolved.
Show resolved Hide resolved
pub fn initiate_channel(
origin: OriginFor<T>,
dst_domain_id: T::DomainId,
Expand Down Expand Up @@ -355,6 +366,7 @@ mod pallet {
/// Channel is set to Closed and do not accept or receive any messages.
/// Only a root user can close an open channel.
#[pallet::weight((10_000, Pays::No))]
#[transactional]
pub fn close_channel(
origin: OriginFor<T>,
domain_id: T::DomainId,
Expand Down
6 changes: 6 additions & 0 deletions crates/pallet-messenger/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ impl<T: Config> Pallet<T> {
},
);

Self::deposit_event(Event::InboxMessageResponse {
domain_id: dst_domain_id,
channel_id,
nonce: next_inbox_nonce,
});

next_inbox_nonce = next_inbox_nonce
.checked_add(Nonce::one())
.ok_or(DispatchError::Arithmetic(ArithmeticError::Overflow))?;
Expand Down
171 changes: 93 additions & 78 deletions crates/pallet-messenger/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,69 +1,107 @@
use crate::{ChannelId, Channels};
use frame_support::parameter_types;
use crate::{ChannelId, Channels, Config};
use frame_support::storage::generator::StorageDoubleMap;
use frame_support::traits::{ConstU16, ConstU32, ConstU64};
use sp_core::storage::StorageKey;
use sp_core::H256;
use sp_runtime::testing::Header;
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
use sp_state_machine::backend::Backend;
use sp_state_machine::{prove_read, InMemoryBackend};
use sp_std::vec::Vec;
use sp_trie::StorageProof;

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

frame_support::construct_runtime!(
pub struct Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
SystemDomainTracker: mock_system_domain_tracker::{Pallet, Storage},
Messenger: crate::{Pallet, Call, Event<T>}
}
);

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
pub(crate) type DomainId = u64;

macro_rules! impl_runtime {
($runtime:ty, $domain_id:literal) => {
use crate::mock::{mock_system_domain_tracker, DomainId};
use frame_support::parameter_types;
use sp_core::H256;
use sp_runtime::testing::Header;
use sp_runtime::traits::{BlakeTwo256, ConstU16, ConstU32, ConstU64, IdentityLookup};
use sp_std::vec::Vec;

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

frame_support::construct_runtime!(
pub struct Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
SystemDomainTracker: mock_system_domain_tracker::{Pallet, Storage},
Messenger: crate::{Pallet, Call, Event<T>}
}
);


impl frame_system::Config for $runtime {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ConstU16<42>;
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

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

impl mock_system_domain_tracker::Config for $runtime {}

parameter_types! {
pub const SelfDomainId: DomainId = $domain_id;
}

impl crate::Config for $runtime {
type Event = Event;
type DomainId = DomainId;
type SelfDomainId = SelfDomainId;
type SystemDomainTracker = SystemDomainTracker;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<Runtime>()
.unwrap();

let mut t: sp_io::TestExternalities = t.into();
t.execute_with(|| System::set_block_number(1));
t
}
};
}

parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub(crate) mod domain_a {
impl_runtime!(Runtime, 1);
}

pub(crate) type DomainId = u64;
pub(crate) mod domain_b {
impl_runtime!(Runtime, 2);
}

#[frame_support::pallet]
mod mock_system_domain_tracker {
pub(crate) mod mock_system_domain_tracker {
use frame_support::pallet_prelude::*;
use sp_core::H256;
use sp_messenger::SystemDomainTracker;
use sp_messenger::SystemDomainTracker as SystemDomainTrackerT;

#[pallet::config]
pub trait Config: frame_system::Config {}
Expand All @@ -77,36 +115,13 @@ mod mock_system_domain_tracker {
#[pallet::storage]
pub(super) type StateRoot<T: Config> = StorageValue<_, H256, ValueQuery>;

impl<T: Config> SystemDomainTracker<H256> for Pallet<T> {
impl<T: Config> SystemDomainTrackerT<H256> for Pallet<T> {
fn latest_state_roots() -> Vec<H256> {
vec![StateRoot::<T>::get()]
}
}
}

impl mock_system_domain_tracker::Config for Test {}

parameter_types! {
pub const SelfDomainId: DomainId = 0;
}

impl crate::Config for Test {
type Event = Event;
type DomainId = DomainId;
type SelfDomainId = SelfDomainId;
type SystemDomainTracker = SystemDomainTracker;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();

let mut t: sp_io::TestExternalities = t.into();
t.execute_with(|| System::set_block_number(1));
t
}

fn storage_proof_for_key(
backend: InMemoryBackend<sp_core::Blake2Hasher>,
key: StorageKey,
Expand All @@ -117,12 +132,12 @@ fn storage_proof_for_key(
(root, proof)
}

pub(crate) fn storage_proof_of_channels(
pub(crate) fn storage_proof_of_channels<T: Config>(
backend: InMemoryBackend<sp_core::Blake2Hasher>,
domain_id: DomainId,
domain_id: T::DomainId,
channel_id: ChannelId,
) -> (H256, StorageKey, StorageProof) {
let key = Channels::<Test>::storage_double_map_final_key(domain_id, channel_id);
let key = Channels::<T>::storage_double_map_final_key(domain_id, channel_id);
let storage_key = StorageKey(key);
let (root, proof) = storage_proof_for_key(backend, storage_key.clone());
(root, storage_key, proof)
Expand Down
Loading