Skip to content

Commit

Permalink
update and enable pallet-messenger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Feb 27, 2024
1 parent da1ea1e commit 51352b9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 79 deletions.
2 changes: 1 addition & 1 deletion domains/pallets/messenger/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Runtime>::default()
Expand Down
168 changes: 90 additions & 78 deletions domains/pallets/messenger/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Balance>) {
let params = InitiateChannelParams {
Expand Down Expand Up @@ -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();
Expand All @@ -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::<Runtime>(t.as_backend(), chain_id, channel_id);
let proof = Proof::<H256, H256>::Consensus {
consensus_chain_mmr_proof: default_consensus_proof(),
message_proof: storage_proof,
};
let res: Result<Channel<Balance>, VerificationError> =
StorageProofVerifier::<Blake2Hasher>::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();
Expand All @@ -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::<Runtime>(t.as_backend(), chain_id, U256::one());
let proof = Proof::<H256, H256>::Consensus {
consensus_chain_mmr_proof: default_consensus_proof(),
message_proof: storage_proof,
};
let res: Result<Channel<Balance>, VerificationError> =
StorageProofVerifier::<Blake2Hasher>::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();
Expand All @@ -250,16 +240,12 @@ fn test_storage_proof_verification() {
expected_channel = Channels::<Runtime>::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::<Runtime>(t.as_backend(), chain_id, channel_id);
let proof = Proof::<H256, H256>::Consensus {
consensus_chain_mmr_proof: default_consensus_proof(),
message_proof: storage_proof,
};
let res: Result<Channel<Balance>, VerificationError> =
StorageProofVerifier::<Blake2Hasher>::get_decoded_value(
&H256::zero(),
proof.message_proof(),
&state_root,
storage_proof,
storage_key,
);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -363,20 +352,16 @@ fn send_message_between_chains(
},
);
assert_ok!(resp);
chain_a::System::assert_last_event(RuntimeEvent::Messenger(
crate::Event::<Runtime>::OutboxMessage {
chain_id: chain_b_id,
channel_id,
nonce: Nonce::one(),
},
));
});

channel_relay_request_and_response(
chain_a_test_ext,
chain_b_test_ext,
channel_id,
Nonce::one(),
false,
Default::default(),
Some(Endpoint::Id(0)),
);

// check state on chain_b
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -481,16 +469,52 @@ fn close_channel_between_chains(
})
}

fn force_toggle_channel_state<Runtime: crate::Config>(
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::<Runtime>::channels(dst_chain_id, channel_id).unwrap_or_else(|| {
Pallet::<Runtime>::do_init_channel(dst_chain_id, init_params).unwrap();
Pallet::<Runtime>::channels(dst_chain_id, channel_id).unwrap()
});

if !toggle {
return;
}

if channel.state == ChannelState::Initiated {
Pallet::<Runtime>::do_open_channel(dst_chain_id, channel_id).unwrap();
}

if channel.state == ChannelState::Open {
Pallet::<Runtime>::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<Endpoint>,
) {
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::<chain_a::Runtime>::get((chain_b_id, channel_id, nonce)).unwrap());
let (_state_root, _key, message_proof) = storage_proof_of_outbox_messages::<chain_a::Runtime>(
chain_a_test_ext.as_backend(),
chain_b_id,
Expand All @@ -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::<chain_b::Runtime>::pre_dispatch(&crate::Call::relay_message {
msg: xdm.clone(),
});
assert_ok!(pre_check);
force_toggle_channel_state::<chain_b::Runtime>(
chain_a_id,
channel_id,
toggle_channel_state,
);
Inbox::<chain_b::Runtime>::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 {
Expand All @@ -555,6 +575,10 @@ fn channel_relay_request_and_response(
nonce,
);

let msg = chain_b_test_ext.execute_with(|| {
InboxResponses::<chain_b::Runtime>::get((chain_a_id, channel_id, nonce)).unwrap()
});

let xdm = CrossDomainMessage {
src_chain_id: chain_b_id,
dst_chain_id: chain_a_id,
Expand All @@ -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::<chain_a::Runtime>::pre_dispatch(&crate::Call::relay_message_response {
msg: xdm.clone(),
});
assert_ok!(pre_check);
force_toggle_channel_state::<chain_a::Runtime>(
chain_b_id,
channel_id,
toggle_channel_state,
);
OutboxResponses::<chain_a::Runtime>::set(Some(msg));

// process outbox message response
let result =
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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()),
Expand All @@ -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::<chain_a::Runtime>::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()),
Expand All @@ -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();
Expand All @@ -766,14 +776,16 @@ fn test_transport_funds_between_chains() {
&mut chain_b_test_ext,
channel_id,
Nonce::one(),
false,
Default::default(),
Some(Endpoint::Id(100)),
);

// post check
verify_transfer_on_chain(&mut chain_a_test_ext, &mut chain_b_test_ext)
}

#[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();
Expand Down

0 comments on commit 51352b9

Please sign in to comment.