Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

Commit 7cc0d59

Browse files
committed
Improve tests
1 parent 0686fcd commit 7cc0d59

6 files changed

Lines changed: 285 additions & 7 deletions

File tree

bridges/snowbridge/pallets/inbound-queue/fixtures/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use sp_core::RuntimeDebug;
88

99
pub mod register_token;
1010
pub mod register_token_with_insufficient_fee;
11+
pub mod send_call_to_penpal;
1112
pub mod send_token;
1213
pub mod send_token_to_penpal;
1314

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
3+
// Generated, do not edit!
4+
// See ethereum client README.md for instructions to generate
5+
6+
use crate::InboundQueueFixture;
7+
use hex_literal::hex;
8+
use snowbridge_beacon_primitives::CompactExecutionHeader;
9+
use snowbridge_core::inbound::{Log, Message, Proof};
10+
use sp_std::vec;
11+
12+
pub fn make_send_call_to_penpal_message() -> InboundQueueFixture {
13+
InboundQueueFixture {
14+
execution_header: CompactExecutionHeader{
15+
parent_hash: hex!("e0fa68ceff08e6f024c84f408540372801b209ff8f11481a9dc6bc5db3ce92d3").into(),
16+
block_number: 831,
17+
state_root: hex!("ccc30635f32d558620d45ead60eabbe70dbf179b475fa9cec5a3791fd5be2b78").into(),
18+
receipts_root: hex!("6bba8f0093461e22cd265bc0de44bf7a596db58ecbf44432e6de8b3490deb077").into(),
19+
},
20+
message: Message {
21+
event_log: Log {
22+
address: hex!("eda338e4dc46038493b885327842fd3e301cab39").into(),
23+
topics: vec![
24+
hex!("7153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84f").into(),
25+
hex!("a69fbbae90bb6096d59b1930bbcfc8a3ef23959d226b1861deb7ad8fb06c6fa3").into(),
26+
hex!("c9ad11f6e2d2b770f52e7cbe22ba779eb8e5e000e0db4e5032488898bd3529ec").into(),
27+
],
28+
data: hex!("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003e00a736aa00000000000290a987b944cb1dcce5564e5fdecd7a54d3de27fe0100902f5009000000000000000000000002688909017d2000071468656c6c6f0000").into(),
29+
},
30+
proof: Proof {
31+
block_hash: hex!("be15f3cdf217baad8cc5c40d7e2dc1f173ab3bdaa58e5819905bb0c73f46cf78").into(),
32+
tx_index: 0,
33+
data: (vec![
34+
hex!("6bba8f0093461e22cd265bc0de44bf7a596db58ecbf44432e6de8b3490deb077").to_vec(),
35+
], vec![
36+
hex!("f90234822080b9022e02f9022a0183016e01b9010000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000200000000000000000020000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000010000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002004000000000000000000000200000000000000f9011ff9011c94eda338e4dc46038493b885327842fd3e301cab39f863a07153f9357c8ea496bba60bf82e67143e27b64462b49041f8e689e1b05728f84fa0a69fbbae90bb6096d59b1930bbcfc8a3ef23959d226b1861deb7ad8fb06c6fa3a0c9ad11f6e2d2b770f52e7cbe22ba779eb8e5e000e0db4e5032488898bd3529ecb8a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000003e00a736aa00000000000290a987b944cb1dcce5564e5fdecd7a54d3de27fe0100902f5009000000000000000000000002688909017d2000071468656c6c6f0000").to_vec(),
37+
]),
38+
},
39+
},
40+
}
41+
}

bridges/snowbridge/pallets/system/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ where
101101
}
102102

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3+
// Generated, do not edit!
4+
// See ethereum client README.md for instructions to generate
5+
6+
use crate::InboundQueueFixture;
7+
use hex_literal::hex;
8+
use snowbridge_beacon_primitives::CompactExecutionHeader;
9+
use snowbridge_core::inbound::{Log, Message, Proof};
10+
use sp_std::vec;
11+
12+
pub fn make_send_call_to_penpal_message() -> InboundQueueFixture {
13+
InboundQueueFixture {
14+
execution_header: CompactExecutionHeader{
15+
parent_hash: hex!("{{InboundMessageTest.ExecutionHeader.ParentHash}}").into(),
16+
block_number: {{InboundMessageTest.ExecutionHeader.BlockNumber}},
17+
state_root: hex!("{{InboundMessageTest.ExecutionHeader.StateRoot}}").into(),
18+
receipts_root: hex!("{{InboundMessageTest.ExecutionHeader.ReceiptsRoot}}").into(),
19+
},
20+
message: Message {
21+
event_log: Log {
22+
address: hex!("{{InboundMessageTest.Message.EventLog.Address}}").into(),
23+
topics: vec![
24+
{{#InboundMessageTest.Message.EventLog.Topics}}
25+
hex!("{{.}}").into(),
26+
{{/InboundMessageTest.Message.EventLog.Topics}}
27+
],
28+
data: hex!("{{InboundMessageTest.Message.EventLog.Data}}").into(),
29+
},
30+
proof: Proof {
31+
block_hash: hex!("{{InboundMessageTest.Message.Proof.BlockHash}}").into(),
32+
tx_index: {{InboundMessageTest.Message.Proof.TxIndex}},
33+
data: (vec![
34+
{{#InboundMessageTest.Message.Proof.Data.Keys}}
35+
hex!("{{.}}").to_vec(),
36+
{{/InboundMessageTest.Message.Proof.Data.Keys}}
37+
], vec![
38+
{{#InboundMessageTest.Message.Proof.Data.Values}}
39+
hex!("{{.}}").to_vec(),
40+
{{/InboundMessageTest.Message.Proof.Data.Values}}
41+
]),
42+
},
43+
},
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3+
// Generated, do not edit!
4+
// See ethereum client README.md for instructions to generate
5+
6+
use crate::InboundQueueFixture;
7+
use hex_literal::hex;
8+
use snowbridge_beacon_primitives::CompactExecutionHeader;
9+
use snowbridge_core::inbound::{Log, Message, Proof};
10+
use sp_std::vec;
11+
12+
pub fn make_send_token_to_penpal_message() -> InboundQueueFixture {
13+
InboundQueueFixture {
14+
execution_header: CompactExecutionHeader{
15+
parent_hash: hex!("{{InboundMessageTest.ExecutionHeader.ParentHash}}").into(),
16+
block_number: {{InboundMessageTest.ExecutionHeader.BlockNumber}},
17+
state_root: hex!("{{InboundMessageTest.ExecutionHeader.StateRoot}}").into(),
18+
receipts_root: hex!("{{InboundMessageTest.ExecutionHeader.ReceiptsRoot}}").into(),
19+
},
20+
message: Message {
21+
event_log: Log {
22+
address: hex!("{{InboundMessageTest.Message.EventLog.Address}}").into(),
23+
topics: vec![
24+
{{#InboundMessageTest.Message.EventLog.Topics}}
25+
hex!("{{.}}").into(),
26+
{{/InboundMessageTest.Message.EventLog.Topics}}
27+
],
28+
data: hex!("{{InboundMessageTest.Message.EventLog.Data}}").into(),
29+
},
30+
proof: Proof {
31+
block_hash: hex!("{{InboundMessageTest.Message.Proof.BlockHash}}").into(),
32+
tx_index: {{InboundMessageTest.Message.Proof.TxIndex}},
33+
data: (vec![
34+
{{#InboundMessageTest.Message.Proof.Data.Keys}}
35+
hex!("{{.}}").to_vec(),
36+
{{/InboundMessageTest.Message.Proof.Data.Keys}}
37+
], vec![
38+
{{#InboundMessageTest.Message.Proof.Data.Values}}
39+
hex!("{{.}}").to_vec(),
40+
{{/InboundMessageTest.Message.Proof.Data.Values}}
41+
]),
42+
},
43+
},
44+
}
45+
}

cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/snowbridge.rs

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ use emulated_integration_tests_common::xcm_emulator::ConvertLocation;
1919
use frame_support::pallet_prelude::TypeInfo;
2020
use hex_literal::hex;
2121
use rococo_westend_system_emulated_network::BridgeHubRococoParaSender as BridgeHubRococoSender;
22-
use snowbridge_core::outbound::OperatingMode;
22+
use snowbridge_core::{outbound::OperatingMode, Channel, ChannelId};
2323
use snowbridge_pallet_inbound_queue_fixtures::{
2424
register_token::make_register_token_message,
2525
register_token_with_insufficient_fee::make_register_token_with_infufficient_fee_message,
26-
send_token::make_send_token_message, send_token_to_penpal::make_send_token_to_penpal_message,
27-
InboundQueueFixture,
26+
send_call_to_penpal::make_send_call_to_penpal_message, send_token::make_send_token_message,
27+
send_token_to_penpal::make_send_token_to_penpal_message, InboundQueueFixture,
2828
};
2929
use snowbridge_pallet_system;
3030
use snowbridge_router_primitives::inbound::{
@@ -42,6 +42,10 @@ const TREASURY_ACCOUNT: [u8; 32] =
4242
const WETH: [u8; 20] = hex!("87d1f7fdfEe7f651FaBc8bFCB6E086C278b77A7d");
4343
const ETHEREUM_DESTINATION_ADDRESS: [u8; 20] = hex!("44a57ee2f2FCcb85FDa2B0B18EBD0D8D2333700e");
4444
const XCM_FEE: u128 = 40_000_000_000;
45+
const XCM_WEIGHT: Weight = Weight::from_parts(40_000_000, 8_000);
46+
const INSUFFICIENT_XCM_FEE: u128 = 1_000;
47+
const INSUFFICIENT_XCM_WEIGHT: Weight = Weight::from_parts(1_000, 1_000);
48+
const INSUFFICIENT_FUND: u128 = 1_000;
4549

4650
#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)]
4751
pub enum ControlCall {
@@ -545,6 +549,101 @@ fn transact_from_ethereum_to_penpal() {
545549
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
546550
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
547551
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INITIAL_FUND)]);
552+
BridgeHubRococo::fund_para_sovereign(PenpalA::para_id().into(), INITIAL_FUND);
553+
554+
BridgeHubRococo::execute_with(|| {
555+
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
556+
type Runtime = <BridgeHubRococo as Chain>::Runtime;
557+
558+
let agent_id = snowbridge_pallet_system::agent_id_of::<Runtime>(&Location::new(
559+
1,
560+
[Parachain(PenpalA::para_id().into())],
561+
))
562+
.unwrap();
563+
snowbridge_pallet_system::Agents::<Runtime>::insert(agent_id, ());
564+
let channel_id: ChannelId = PenpalA::para_id().into();
565+
snowbridge_pallet_system::Channels::<Runtime>::insert(
566+
channel_id,
567+
Channel { agent_id, para_id: PenpalA::para_id() },
568+
);
569+
570+
// Construct Send call to penpal message and sent to inbound queue
571+
send_inbound_message(make_send_call_to_penpal_message()).unwrap();
572+
573+
assert_expected_events!(
574+
BridgeHubRococo,
575+
vec![
576+
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
577+
]
578+
);
579+
});
580+
581+
PenpalA::execute_with(|| {
582+
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
583+
// Check that system event remarked on PenPal
584+
assert_expected_events!(
585+
PenpalA,
586+
vec![
587+
RuntimeEvent::System(frame_system::Event::Remarked { .. }) => {},
588+
]
589+
);
590+
});
591+
}
592+
593+
#[test]
594+
fn transact_from_ethereum_to_penpal_insufficient_fee() {
595+
// Fund sender on penpal so that it can pay execution fees.
596+
let sender: H160 = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into();
597+
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
598+
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
599+
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INITIAL_FUND)]);
600+
601+
BridgeHubRococo::execute_with(|| {
602+
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
603+
604+
let message_id: H256 = [1; 32].into();
605+
let message = VersionedMessage::V1(MessageV1 {
606+
chain_id: CHAIN_ID,
607+
command: Command::Transact {
608+
sender,
609+
fee: INSUFFICIENT_XCM_FEE,
610+
weight_at_most: XCM_WEIGHT,
611+
origin_kind: OriginKind::SovereignAccount,
612+
payload: hex!("00071468656c6c6f").to_vec(),
613+
},
614+
});
615+
// Convert the message to XCM
616+
let (xcm, _) = EthereumInboundQueue::do_convert(message_id, message).unwrap();
617+
// Send the XCM
618+
let _ = EthereumInboundQueue::send_xcm(xcm, PenpalA::para_id().into()).unwrap();
619+
620+
assert_expected_events!(
621+
BridgeHubRococo,
622+
vec![
623+
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
624+
]
625+
);
626+
});
627+
628+
PenpalA::execute_with(|| {
629+
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
630+
// Check xcm execution fails on PenPal
631+
assert_expected_events!(
632+
PenpalA,
633+
vec![
634+
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false,.. }) => {},
635+
]
636+
);
637+
});
638+
}
639+
640+
#[test]
641+
fn transact_from_ethereum_to_penpal_sender_insufficient_fund() {
642+
// Fund sender on penpal so that it can pay execution fees.
643+
let sender: H160 = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into();
644+
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
645+
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
646+
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INSUFFICIENT_FUND)]);
548647

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

576675
PenpalA::execute_with(|| {
577676
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
578-
// Check that system event remarked on PenPal
677+
// Check xcm execution fails on PenPal
579678
assert_expected_events!(
580679
PenpalA,
581680
vec![
582-
RuntimeEvent::System(frame_system::Event::Remarked { .. }) => {},
681+
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false,.. }) => {},
682+
]
683+
);
684+
});
685+
}
686+
687+
#[test]
688+
fn transact_from_ethereum_to_penpal_insufficient_weight() {
689+
// Fund sender on penpal so that it can pay execution fees.
690+
let sender: H160 = hex!("90A987B944Cb1dCcE5564e5FDeCD7a54D3de27Fe").into();
691+
let sovereign_of_sender = blake2_256(&(b"AccountKey20", sender).encode());
692+
println!("sovereign account of the sender: {:#?}", hex::encode(sovereign_of_sender.clone()));
693+
PenpalA::fund_accounts(vec![(sovereign_of_sender.into(), INITIAL_FUND)]);
694+
695+
BridgeHubRococo::execute_with(|| {
696+
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
697+
698+
let message_id: H256 = [1; 32].into();
699+
let message = VersionedMessage::V1(MessageV1 {
700+
chain_id: CHAIN_ID,
701+
command: Command::Transact {
702+
sender,
703+
fee: XCM_FEE,
704+
weight_at_most: INSUFFICIENT_XCM_WEIGHT,
705+
origin_kind: OriginKind::SovereignAccount,
706+
payload: hex!("00071468656c6c6f").to_vec(),
707+
},
708+
});
709+
// Convert the message to XCM
710+
let (xcm, _) = EthereumInboundQueue::do_convert(message_id, message).unwrap();
711+
// Send the XCM
712+
let _ = EthereumInboundQueue::send_xcm(xcm, PenpalA::para_id().into()).unwrap();
713+
714+
assert_expected_events!(
715+
BridgeHubRococo,
716+
vec![
717+
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::XcmpMessageSent { .. }) => {},
718+
]
719+
);
720+
});
721+
722+
PenpalA::execute_with(|| {
723+
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
724+
// Check xcm execution fails on PenPal
725+
assert_expected_events!(
726+
PenpalA,
727+
vec![
728+
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false,.. }) => {},
583729
]
584730
);
585731
});

0 commit comments

Comments
 (0)