Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Feb 26, 2024
1 parent 0686fcd commit 7cc0d59
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sp_core::RuntimeDebug;

pub mod register_token;
pub mod register_token_with_insufficient_fee;
pub mod send_call_to_penpal;
pub mod send_token;
pub mod send_token_to_penpal;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
// Generated, do not edit!
// See ethereum client README.md for instructions to generate

use crate::InboundQueueFixture;
use hex_literal::hex;
use snowbridge_beacon_primitives::CompactExecutionHeader;
use snowbridge_core::inbound::{Log, Message, Proof};
use sp_std::vec;

pub fn make_send_call_to_penpal_message() -> InboundQueueFixture {

Check warning on line 12 in bridges/snowbridge/pallets/inbound-queue/fixtures/src/send_call_to_penpal.rs

View check run for this annotation

Codecov / codecov/patch

bridges/snowbridge/pallets/inbound-queue/fixtures/src/send_call_to_penpal.rs#L12

Added line #L12 was not covered by tests
InboundQueueFixture {
execution_header: CompactExecutionHeader{

Check warning on line 14 in bridges/snowbridge/pallets/inbound-queue/fixtures/src/send_call_to_penpal.rs

View check run for this annotation

Codecov / codecov/patch

bridges/snowbridge/pallets/inbound-queue/fixtures/src/send_call_to_penpal.rs#L14

Added line #L14 was not covered by tests
parent_hash: hex!("e0fa68ceff08e6f024c84f408540372801b209ff8f11481a9dc6bc5db3ce92d3").into(),
block_number: 831,
state_root: hex!("ccc30635f32d558620d45ead60eabbe70dbf179b475fa9cec5a3791fd5be2b78").into(),
receipts_root: hex!("6bba8f0093461e22cd265bc0de44bf7a596db58ecbf44432e6de8b3490deb077").into(),
},
message: Message {

Check warning on line 20 in bridges/snowbridge/pallets/inbound-queue/fixtures/src/send_call_to_penpal.rs

View check run for this annotation

Codecov / codecov/patch

bridges/snowbridge/pallets/inbound-queue/fixtures/src/send_call_to_penpal.rs#L20

Added line #L20 was not covered by tests
event_log: Log {
address: hex!("eda338e4dc46038493b885327842fd3e301cab39").into(),
topics: vec![
hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(),
hex!("a69fbbae90bb6096d59b1930bbcfc8a3ef23959d226b1861deb7ad8fb06c6fa3").into(),
hex!("c9ad11f6e2d2b770f52e7cbe22ba779eb8e5e000e0db4e5032488898bd3529ec").into(),
],
data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003e00a736aa00000000000290a987b944cb1dcce5564e5fdecd7a54d3de27fe0100902f5009000000000000000000000002688909017d2000071468656c6c6f0000").into(),
},
proof: Proof {
block_hash: hex!("be15f3cdf217baad8cc5c40d7e2dc1f173ab3bdaa58e5819905bb0c73f46cf78").into(),
tx_index: 0,
data: (vec![
hex!("6bba8f0093461e22cd265bc0de44bf7a596db58ecbf44432e6de8b3490deb077").to_vec(),
], vec![
hex!("f90234822080b9022e02f9022a0183016e01b9010000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000200000000000000000020000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000010000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002004000000000000000000000200000000000000f9011ff9011c94eda338e4dc46038493b885327842fd3e301cab39f863a07153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84fa0a69fbbae90bb6096d59b1930bbcfc8a3ef23959d226b1861deb7ad8fb06c6fa3a0c9ad11f6e2d2b770f52e7cbe22ba779eb8e5e000e0db4e5032488898bd3529ecb8a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003e00a736aa00000000000290a987b944cb1dcce5564e5fdecd7a54d3de27fe0100902f5009000000000000000000000002688909017d2000071468656c6c6f0000").to_vec(),
]),
},
},
}
}
2 changes: 1 addition & 1 deletion bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
}

/// Hash the location to produce an agent id
fn agent_id_of<T: Config>(location: &Location) -> Result<H256, DispatchError> {
pub fn agent_id_of<T: Config>(location: &Location) -> Result<H256, DispatchError> {
T::AgentIdOf::convert_location(location).ok_or(Error::<T>::LocationConversionFailed.into())
}

Expand Down
45 changes: 45 additions & 0 deletions bridges/snowbridge/templates/send_call_to_penpal.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
// Generated, do not edit!
// See ethereum client README.md for instructions to generate

use crate::InboundQueueFixture;
use hex_literal::hex;
use snowbridge_beacon_primitives::CompactExecutionHeader;
use snowbridge_core::inbound::{Log, Message, Proof};
use sp_std::vec;

pub fn make_send_call_to_penpal_message() -> InboundQueueFixture {
InboundQueueFixture {
execution_header: CompactExecutionHeader{
parent_hash: hex!("{{InboundMessageTest.ExecutionHeader.ParentHash}}").into(),
block_number: {{InboundMessageTest.ExecutionHeader.BlockNumber}},
state_root: hex!("{{InboundMessageTest.ExecutionHeader.StateRoot}}").into(),
receipts_root: hex!("{{InboundMessageTest.ExecutionHeader.ReceiptsRoot}}").into(),
},
message: Message {
event_log: Log {
address: hex!("{{InboundMessageTest.Message.EventLog.Address}}").into(),
topics: vec![
{{#InboundMessageTest.Message.EventLog.Topics}}
hex!("{{.}}").into(),
{{/InboundMessageTest.Message.EventLog.Topics}}
],
data: hex!("{{InboundMessageTest.Message.EventLog.Data}}").into(),
},
proof: Proof {
block_hash: hex!("{{InboundMessageTest.Message.Proof.BlockHash}}").into(),
tx_index: {{InboundMessageTest.Message.Proof.TxIndex}},
data: (vec![
{{#InboundMessageTest.Message.Proof.Data.Keys}}
hex!("{{.}}").to_vec(),
{{/InboundMessageTest.Message.Proof.Data.Keys}}
], vec![
{{#InboundMessageTest.Message.Proof.Data.Values}}
hex!("{{.}}").to_vec(),
{{/InboundMessageTest.Message.Proof.Data.Values}}
]),
},
},
}
}
45 changes: 45 additions & 0 deletions bridges/snowbridge/templates/send_token_to_penpal.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
// Generated, do not edit!
// See ethereum client README.md for instructions to generate

use crate::InboundQueueFixture;
use hex_literal::hex;
use snowbridge_beacon_primitives::CompactExecutionHeader;
use snowbridge_core::inbound::{Log, Message, Proof};
use sp_std::vec;

pub fn make_send_token_to_penpal_message() -> InboundQueueFixture {
InboundQueueFixture {
execution_header: CompactExecutionHeader{
parent_hash: hex!("{{InboundMessageTest.ExecutionHeader.ParentHash}}").into(),
block_number: {{InboundMessageTest.ExecutionHeader.BlockNumber}},
state_root: hex!("{{InboundMessageTest.ExecutionHeader.StateRoot}}").into(),
receipts_root: hex!("{{InboundMessageTest.ExecutionHeader.ReceiptsRoot}}").into(),
},
message: Message {
event_log: Log {
address: hex!("{{InboundMessageTest.Message.EventLog.Address}}").into(),
topics: vec![
{{#InboundMessageTest.Message.EventLog.Topics}}
hex!("{{.}}").into(),
{{/InboundMessageTest.Message.EventLog.Topics}}
],
data: hex!("{{InboundMessageTest.Message.EventLog.Data}}").into(),
},
proof: Proof {
block_hash: hex!("{{InboundMessageTest.Message.Proof.BlockHash}}").into(),
tx_index: {{InboundMessageTest.Message.Proof.TxIndex}},
data: (vec![
{{#InboundMessageTest.Message.Proof.Data.Keys}}
hex!("{{.}}").to_vec(),
{{/InboundMessageTest.Message.Proof.Data.Keys}}
], vec![
{{#InboundMessageTest.Message.Proof.Data.Values}}
hex!("{{.}}").to_vec(),
{{/InboundMessageTest.Message.Proof.Data.Values}}
]),
},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use emulated_integration_tests_common::xcm_emulator::ConvertLocation;
use frame_support::pallet_prelude::TypeInfo;
use hex_literal::hex;
use rococo_westend_system_emulated_network::BridgeHubRococoParaSender as BridgeHubRococoSender;
use snowbridge_core::outbound::OperatingMode;
use snowbridge_core::{outbound::OperatingMode, Channel, ChannelId};
use snowbridge_pallet_inbound_queue_fixtures::{
register_token::make_register_token_message,
register_token_with_insufficient_fee::make_register_token_with_infufficient_fee_message,
send_token::make_send_token_message, send_token_to_penpal::make_send_token_to_penpal_message,
InboundQueueFixture,
send_call_to_penpal::make_send_call_to_penpal_message, send_token::make_send_token_message,
send_token_to_penpal::make_send_token_to_penpal_message, InboundQueueFixture,
};
use snowbridge_pallet_system;
use snowbridge_router_primitives::inbound::{
Expand All @@ -42,6 +42,10 @@ const TREASURY_ACCOUNT: [u8; 32] =
const WETH: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d");
const ETHEREUM_DESTINATION_ADDRESS: [u8; 20] = hex!("44a57ee2f2FCcb85FDa2B0B18EBD0D8D2333700e");
const XCM_FEE: u128 = 40_000_000_000;
const XCM_WEIGHT: Weight = Weight::from_parts(40_000_000, 8_000);
const INSUFFICIENT_XCM_FEE: u128 = 1_000;
const INSUFFICIENT_XCM_WEIGHT: Weight = Weight::from_parts(1_000, 1_000);
const INSUFFICIENT_FUND: u128 = 1_000;

#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
pub enum ControlCall {
Expand Down Expand Up @@ -545,6 +549,101 @@ fn transact_from_ethereum_to_penpal() {
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INITIAL_FUND)]);
BridgeHubRococo::fund_para_sovereign(PenpalA::para_id().into(), INITIAL_FUND);

BridgeHubRococo::execute_with(|| {
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
type Runtime = <BridgeHubRococo as Chain>::Runtime;

let agent_id = snowbridge_pallet_system::agent_id_of::<Runtime>(&Location::new(
1,
[Parachain(PenpalA::para_id().into())],
))
.unwrap();
snowbridge_pallet_system::Agents::<Runtime>::insert(agent_id, ());
let channel_id: ChannelId = PenpalA::para_id().into();
snowbridge_pallet_system::Channels::<Runtime>::insert(
channel_id,
Channel { agent_id, para_id: PenpalA::para_id() },
);

// Construct Send call to penpal message and sent to inbound queue
send_inbound_message(make_send_call_to_penpal_message()).unwrap();

assert_expected_events!(
BridgeHubRococo,
vec![
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
]
);
});

PenpalA::execute_with(|| {
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
// Check that system event remarked on PenPal
assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::System(frame_system::Event::Remarked { .. }) => {},
]
);
});
}

#[test]
fn transact_from_ethereum_to_penpal_insufficient_fee() {
// Fund sender on penpal so that it can pay execution fees.
let sender: H160 = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into();
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INITIAL_FUND)]);

BridgeHubRococo::execute_with(|| {
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;

let message_id: H256 = [1; 32].into();
let message = VersionedMessage::V1(MessageV1 {
chain_id: CHAIN_ID,
command: Command::Transact {
sender,
fee: INSUFFICIENT_XCM_FEE,
weight_at_most: XCM_WEIGHT,
origin_kind: OriginKind::SovereignAccount,
payload: hex!("00071468656c6c6f").to_vec(),
},
});
// Convert the message to XCM
let (xcm, _) = EthereumInboundQueue::do_convert(message_id, message).unwrap();
// Send the XCM
let _ = EthereumInboundQueue::send_xcm(xcm, PenpalA::para_id().into()).unwrap();

assert_expected_events!(
BridgeHubRococo,
vec![
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
]
);
});

PenpalA::execute_with(|| {
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
// Check xcm execution fails on PenPal
assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false,.. }) => {},
]
);
});
}

#[test]
fn transact_from_ethereum_to_penpal_sender_insufficient_fund() {
// Fund sender on penpal so that it can pay execution fees.
let sender: H160 = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into();
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INSUFFICIENT_FUND)]);

BridgeHubRococo::execute_with(|| {
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
Expand All @@ -555,7 +654,7 @@ fn transact_from_ethereum_to_penpal() {
command: Command::Transact {
sender,
fee: XCM_FEE,
weight_at_most: Weight::from_parts(40_000_000, 8_000),
weight_at_most: XCM_WEIGHT,
origin_kind: OriginKind::SovereignAccount,
payload: hex!("00071468656c6c6f").to_vec(),
},
Expand All @@ -575,11 +674,58 @@ fn transact_from_ethereum_to_penpal() {

PenpalA::execute_with(|| {
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
// Check that system event remarked on PenPal
// Check xcm execution fails on PenPal
assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::System(frame_system::Event::Remarked { .. }) => {},
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false,.. }) => {},
]
);
});
}

#[test]
fn transact_from_ethereum_to_penpal_insufficient_weight() {
// Fund sender on penpal so that it can pay execution fees.
let sender: H160 = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into();
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INITIAL_FUND)]);

BridgeHubRococo::execute_with(|| {
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;

let message_id: H256 = [1; 32].into();
let message = VersionedMessage::V1(MessageV1 {
chain_id: CHAIN_ID,
command: Command::Transact {
sender,
fee: XCM_FEE,
weight_at_most: INSUFFICIENT_XCM_WEIGHT,
origin_kind: OriginKind::SovereignAccount,
payload: hex!("00071468656c6c6f").to_vec(),
},
});
// Convert the message to XCM
let (xcm, _) = EthereumInboundQueue::do_convert(message_id, message).unwrap();
// Send the XCM
let _ = EthereumInboundQueue::send_xcm(xcm, PenpalA::para_id().into()).unwrap();

assert_expected_events!(
BridgeHubRococo,
vec![
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
]
);
});

PenpalA::execute_with(|| {
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
// Check xcm execution fails on PenPal
assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false,.. }) => {},
]
);
});
Expand Down

0 comments on commit 7cc0d59

Please sign in to comment.