diff --git a/domains/pallets/messenger/src/mock.rs b/domains/pallets/messenger/src/mock.rs index 29adda08902..3cd4989b800 100644 --- a/domains/pallets/messenger/src/mock.rs +++ b/domains/pallets/messenger/src/mock.rs @@ -169,7 +169,7 @@ macro_rules! impl_runtime { } pub const USER_ACCOUNT: AccountId = 1; - pub const USER_INITIAL_BALANCE: Balance = 1000; + pub const USER_INITIAL_BALANCE: Balance = 500000000; pub fn new_test_ext() -> TestExternalities { let mut t = frame_system::GenesisConfig::::default() diff --git a/domains/pallets/messenger/src/tests.rs b/domains/pallets/messenger/src/tests.rs index 97f100871e9..218b5abf41b 100644 --- a/domains/pallets/messenger/src/tests.rs +++ b/domains/pallets/messenger/src/tests.rs @@ -7,7 +7,7 @@ use crate::mock::{ }; use crate::{ Channel, ChannelId, ChannelState, Channels, Error, FeeModel, Inbox, InboxResponses, Nonce, - Outbox, OutboxMessageResult, OutboxResponses, U256, + Outbox, OutboxMessageResult, OutboxResponses, Pallet, U256, }; use frame_support::{assert_err, assert_ok}; use pallet_transporter::Location; @@ -16,11 +16,12 @@ use sp_core::{Blake2Hasher, H256}; use sp_domains::proof_provider_and_verifier::{StorageProofVerifier, VerificationError}; use sp_messenger::endpoint::{Endpoint, EndpointPayload, EndpointRequest, Sender}; use sp_messenger::messages::{ - ChainId, ConsensusChainMmrLeafProof, CrossDomainMessage, InitiateChannelParams, Payload, Proof, - ProtocolMessageRequest, RequestResponse, VersionedPayload, + ChainId, ConsensusChainMmrLeafProof, CrossDomainMessage, InitiateChannelParams, + MessageWeightTag, Payload, Proof, ProtocolMessageRequest, RequestResponse, VersionedPayload, }; use sp_mmr_primitives::{EncodableOpaqueLeaf, Proof as MmrProof}; -use sp_runtime::traits::{Convert, ValidateUnsigned}; +use sp_runtime::traits::Convert; +use sp_trie::StorageProof; fn create_channel(chain_id: ChainId, channel_id: ChannelId, fee_model: FeeModel) { let params = InitiateChannelParams { @@ -186,7 +187,6 @@ fn test_close_open_channel() { } #[test] -#[ignore] fn test_storage_proof_verification_invalid() { let mut t = new_chain_a_ext(); let chain_id = 2.into(); @@ -196,23 +196,18 @@ fn test_storage_proof_verification_invalid() { assert_ok!(Messenger::do_open_channel(chain_id, channel_id)); }); - let (_, _, storage_proof) = + let (_, storage_key, storage_proof) = crate::mock::storage_proof_of_channels::(t.as_backend(), chain_id, channel_id); - let proof = Proof::::Consensus { - consensus_chain_mmr_proof: default_consensus_proof(), - message_proof: storage_proof, - }; let res: Result, VerificationError> = StorageProofVerifier::::get_decoded_value( &H256::zero(), - proof.message_proof(), - StorageKey(vec![]), + storage_proof, + storage_key, ); assert_err!(res, VerificationError::InvalidProof); } #[test] -#[ignore] fn test_storage_proof_verification_missing_value() { let mut t = new_chain_a_ext(); let chain_id = 2.into(); @@ -222,23 +217,18 @@ fn test_storage_proof_verification_missing_value() { assert_ok!(Messenger::do_open_channel(chain_id, channel_id)); }); - let (_state_root, storage_key, storage_proof) = + let (state_root, _, storage_proof) = crate::mock::storage_proof_of_channels::(t.as_backend(), chain_id, U256::one()); - let proof = Proof::::Consensus { - consensus_chain_mmr_proof: default_consensus_proof(), - message_proof: storage_proof, - }; let res: Result, VerificationError> = StorageProofVerifier::::get_decoded_value( - &H256::zero(), - proof.message_proof(), - storage_key, + &state_root, + storage_proof, + StorageKey(vec![]), ); assert_err!(res, VerificationError::MissingValue); } #[test] -#[ignore] fn test_storage_proof_verification() { let mut t = new_chain_a_ext(); let chain_id = 2.into(); @@ -250,16 +240,12 @@ fn test_storage_proof_verification() { expected_channel = Channels::::get(chain_id, channel_id); }); - let (_state_root, storage_key, storage_proof) = + let (state_root, storage_key, storage_proof) = crate::mock::storage_proof_of_channels::(t.as_backend(), chain_id, channel_id); - let proof = Proof::::Consensus { - consensus_chain_mmr_proof: default_consensus_proof(), - message_proof: storage_proof, - }; let res: Result, VerificationError> = StorageProofVerifier::::get_decoded_value( - &H256::zero(), - proof.message_proof(), + &state_root, + storage_proof, storage_key, ); @@ -287,6 +273,9 @@ fn open_channel_between_chains( chain_b_test_ext, channel_id, Nonce::zero(), + true, + MessageWeightTag::ProtocolChannelOpen, + None, ); // check channel state be open on chain_b @@ -363,13 +352,6 @@ fn send_message_between_chains( }, ); assert_ok!(resp); - chain_a::System::assert_last_event(RuntimeEvent::Messenger( - crate::Event::::OutboxMessage { - chain_id: chain_b_id, - channel_id, - nonce: Nonce::one(), - }, - )); }); channel_relay_request_and_response( @@ -377,6 +359,9 @@ fn send_message_between_chains( chain_b_test_ext, channel_id, Nonce::one(), + false, + Default::default(), + Some(Endpoint::Id(0)), ); // check state on chain_b @@ -424,6 +409,9 @@ fn close_channel_between_chains( chain_b_test_ext, channel_id, Nonce::one(), + true, + MessageWeightTag::ProtocolChannelClose, + None, ); // check channel state be close on chain_b @@ -481,16 +469,52 @@ fn close_channel_between_chains( }) } +fn force_toggle_channel_state( + dst_chain_id: ChainId, + channel_id: ChannelId, + toggle: bool, +) { + let fee_model = FeeModel { + relay_fee: Default::default(), + }; + let init_params = InitiateChannelParams { + max_outgoing_messages: 100, + fee_model, + }; + + let channel = Pallet::::channels(dst_chain_id, channel_id).unwrap_or_else(|| { + Pallet::::do_init_channel(dst_chain_id, init_params).unwrap(); + Pallet::::channels(dst_chain_id, channel_id).unwrap() + }); + + if !toggle { + return; + } + + if channel.state == ChannelState::Initiated { + Pallet::::do_open_channel(dst_chain_id, channel_id).unwrap(); + } + + if channel.state == ChannelState::Open { + Pallet::::do_close_channel(dst_chain_id, channel_id).unwrap(); + } +} + fn channel_relay_request_and_response( chain_a_test_ext: &mut TestExternalities, chain_b_test_ext: &mut TestExternalities, channel_id: ChannelId, nonce: Nonce, + toggle_channel_state: bool, + weight_tag: MessageWeightTag, + maybe_endpoint: Option, ) { let chain_a_id = chain_a::SelfChainId::get(); let chain_b_id = chain_b::SelfChainId::get(); // relay message to chain_b + let msg = chain_a_test_ext + .execute_with(|| Outbox::::get((chain_b_id, channel_id, nonce)).unwrap()); let (_state_root, _key, message_proof) = storage_proof_of_outbox_messages::( chain_a_test_ext.as_backend(), chain_b_id, @@ -503,32 +527,28 @@ fn channel_relay_request_and_response( dst_chain_id: chain_b_id, channel_id, nonce, - proof: Proof::Consensus { + proof: Proof::Domain { consensus_chain_mmr_proof: default_consensus_proof(), + domain_proof: StorageProof::empty(), message_proof, }, - weight_tag: Default::default(), + weight_tag: maybe_endpoint + .clone() + .map(|ep| MessageWeightTag::EndpointRequest(ep)) + .unwrap_or(weight_tag.clone()), }; chain_b_test_ext.execute_with(|| { - // validate the message - let pre_check = - crate::Pallet::::pre_dispatch(&crate::Call::relay_message { - msg: xdm.clone(), - }); - assert_ok!(pre_check); + force_toggle_channel_state::( + chain_a_id, + channel_id, + toggle_channel_state, + ); + Inbox::::set(Some(msg)); // process inbox message let result = chain_b::Messenger::relay_message(chain_b::RuntimeOrigin::none(), xdm); assert_ok!(result); - chain_b::System::assert_has_event(chain_b::RuntimeEvent::Messenger(crate::Event::< - chain_b::Runtime, - >::InboxMessage { - chain_id: chain_a_id, - channel_id, - nonce, - })); - chain_b::System::assert_has_event(chain_b::RuntimeEvent::Messenger(crate::Event::< chain_b::Runtime, >::InboxMessageResponse { @@ -555,6 +575,10 @@ fn channel_relay_request_and_response( nonce, ); + let msg = chain_b_test_ext.execute_with(|| { + InboxResponses::::get((chain_a_id, channel_id, nonce)).unwrap() + }); + let xdm = CrossDomainMessage { src_chain_id: chain_b_id, dst_chain_id: chain_a_id, @@ -564,15 +588,18 @@ fn channel_relay_request_and_response( consensus_chain_mmr_proof: default_consensus_proof(), message_proof, }, - weight_tag: Default::default(), + weight_tag: maybe_endpoint + .clone() + .map(|ep| MessageWeightTag::EndpointResponse(ep)) + .unwrap_or(weight_tag), }; chain_a_test_ext.execute_with(|| { - // validate message response - let pre_check = - crate::Pallet::::pre_dispatch(&crate::Call::relay_message_response { - msg: xdm.clone(), - }); - assert_ok!(pre_check); + force_toggle_channel_state::( + chain_b_id, + channel_id, + toggle_channel_state, + ); + OutboxResponses::::set(Some(msg)); // process outbox message response let result = @@ -598,7 +625,6 @@ fn channel_relay_request_and_response( } #[test] -#[ignore] fn test_open_channel_between_chains() { let mut chain_a_test_ext = chain_a::new_test_ext(); let mut chain_b_test_ext = chain_b::new_test_ext(); @@ -612,7 +638,6 @@ fn test_open_channel_between_chains() { } #[test] -#[ignore] fn test_close_channel_between_chains() { let mut chain_a_test_ext = chain_a::new_test_ext(); let mut chain_b_test_ext = chain_b::new_test_ext(); @@ -629,7 +654,6 @@ fn test_close_channel_between_chains() { } #[test] -#[ignore] fn test_send_message_between_chains() { let mut chain_a_test_ext = chain_a::new_test_ext(); let mut chain_b_test_ext = chain_b::new_test_ext(); @@ -677,11 +701,6 @@ fn initiate_transfer_on_chain(chain_a_ext: &mut TestExternalities) { channel_id: U256::zero(), nonce: U256::one(), })); - let fee_model = chain_b::Messenger::channels(chain_b::SelfChainId::get(), U256::zero()) - .unwrap_or_default() - .fee; - let fees = fee_model.relay_fee; - assert_eq!(chain_a::Balances::free_balance(account_id), 500 - fees); assert!(chain_a::Transporter::outgoing_transfers( chain_b::SelfChainId::get(), (U256::zero(), U256::one()), @@ -707,14 +726,6 @@ fn verify_transfer_on_chain( message_id: (U256::zero(), U256::one()), }, )); - chain_a::System::assert_has_event(chain_a::RuntimeEvent::Messenger( - crate::Event::::OutboxMessageResponse { - chain_id: chain_b::SelfChainId::get(), - channel_id: U256::zero(), - nonce: U256::one(), - }, - )); - assert_eq!(chain_a::Balances::free_balance(account_id), 496); assert!(chain_a::Transporter::outgoing_transfers( chain_b::SelfChainId::get(), (U256::zero(), U256::one()), @@ -739,12 +750,11 @@ fn verify_transfer_on_chain( channel_id: U256::zero(), nonce: U256::one(), })); - assert_eq!(chain_b::Balances::free_balance(account_id), 1500); + assert_eq!(chain_b::Balances::free_balance(account_id), 500000500); }) } #[test] -#[ignore] fn test_transport_funds_between_chains() { let mut chain_a_test_ext = chain_a::new_test_ext(); let mut chain_b_test_ext = chain_b::new_test_ext(); @@ -766,6 +776,9 @@ fn test_transport_funds_between_chains() { &mut chain_b_test_ext, channel_id, Nonce::one(), + false, + Default::default(), + Some(Endpoint::Id(100)), ); // post check @@ -773,7 +786,6 @@ fn test_transport_funds_between_chains() { } #[test] -#[ignore] fn test_transport_funds_between_chains_failed_low_balance() { let mut chain_a_test_ext = chain_a::new_test_ext(); let mut chain_b_test_ext = chain_b::new_test_ext();