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

Commit 9368712

Browse files
Forward message id to destination chain (#134)
* forward message id to destination chain * fix import
1 parent 7fd2ea4 commit 9368712

File tree

3 files changed

+33
-23
lines changed
  • bridges/snowbridge
    • pallets/inbound-queue/src
    • primitives/router/src/inbound
  • cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests

3 files changed

+33
-23
lines changed

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ use sp_core::{H160, H256};
5252
use sp_runtime::traits::Zero;
5353
use sp_std::{convert::TryFrom, vec};
5454
use xcm::prelude::{
55-
send_xcm, Instruction::SetTopic, Junction::*, Location, SendError as XcmpSendError, SendXcm,
56-
Xcm, XcmContext, XcmHash,
55+
send_xcm, Junction::*, Location, SendError as XcmpSendError, SendXcm, Xcm, XcmContext, XcmHash,
5756
};
5857
use xcm_executor::traits::TransactAsset;
5958

@@ -279,7 +278,8 @@ pub mod pallet {
279278
// Decode message into XCM
280279
let (xcm, fee) =
281280
match inbound::VersionedMessage::decode_all(&mut envelope.payload.as_ref()) {
282-
Ok(message) => Self::do_convert(envelope.message_id, message)?,
281+
Ok(message) => T::MessageConverter::convert(envelope.message_id, message)
282+
.map_err(|e| Error::<T>::ConvertMessage(e))?,
283283
Err(_) => return Err(Error::<T>::InvalidPayload.into()),
284284
};
285285

@@ -321,17 +321,6 @@ pub mod pallet {
321321
}
322322

323323
impl<T: Config> Pallet<T> {
324-
pub fn do_convert(
325-
message_id: H256,
326-
message: inbound::VersionedMessage,
327-
) -> Result<(Xcm<()>, BalanceOf<T>), Error<T>> {
328-
let (mut xcm, fee) =
329-
T::MessageConverter::convert(message).map_err(|e| Error::<T>::ConvertMessage(e))?;
330-
// Append the message id as an XCM topic
331-
xcm.inner_mut().extend(vec![SetTopic(message_id.into())]);
332-
Ok((xcm, fee))
333-
}
334-
335324
pub fn send_xcm(xcm: Xcm<()>, dest: ParaId) -> Result<XcmHash, Error<T>> {
336325
let dest = Location::new(1, [Parachain(dest.into())]);
337326
let (xcm_hash, _) = send_xcm::<T::XcmSender>(dest, xcm).map_err(Error::<T>::from)?;

bridges/snowbridge/primitives/router/src/inbound/mod.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use codec::{Decode, Encode};
99
use core::marker::PhantomData;
1010
use frame_support::{traits::tokens::Balance as BalanceT, weights::Weight, PalletError};
1111
use scale_info::TypeInfo;
12-
use sp_core::{Get, RuntimeDebug, H160};
12+
use sp_core::{Get, RuntimeDebug, H160, H256};
1313
use sp_io::hashing::blake2_256;
1414
use sp_runtime::MultiAddress;
1515
use sp_std::prelude::*;
@@ -115,7 +115,10 @@ pub trait ConvertMessage {
115115
type Balance: BalanceT + From<u128>;
116116
type AccountId;
117117
/// Converts a versioned message into an XCM message and an optional topicID
118-
fn convert(message: VersionedMessage) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError>;
118+
fn convert(
119+
message_id: H256,
120+
message: VersionedMessage,
121+
) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError>;
119122
}
120123

121124
pub type CallIndex = [u8; 2];
@@ -138,14 +141,17 @@ impl<CreateAssetCall, CreateAssetDeposit, InboundQueuePalletInstance, AccountId,
138141
type Balance = Balance;
139142
type AccountId = AccountId;
140143

141-
fn convert(message: VersionedMessage) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError> {
144+
fn convert(
145+
message_id: H256,
146+
message: VersionedMessage,
147+
) -> Result<(Xcm<()>, Self::Balance), ConvertMessageError> {
142148
use Command::*;
143149
use VersionedMessage::*;
144150
match message {
145151
V1(MessageV1 { chain_id, command: RegisterToken { token, fee } }) =>
146-
Ok(Self::convert_register_token(chain_id, token, fee)),
152+
Ok(Self::convert_register_token(message_id, chain_id, token, fee)),
147153
V1(MessageV1 { chain_id, command: SendToken { token, destination, amount, fee } }) =>
148-
Ok(Self::convert_send_token(chain_id, token, destination, amount, fee)),
154+
Ok(Self::convert_send_token(message_id, chain_id, token, destination, amount, fee)),
149155
}
150156
}
151157
}
@@ -159,7 +165,12 @@ where
159165
Balance: BalanceT + From<u128>,
160166
AccountId: Into<[u8; 32]>,
161167
{
162-
fn convert_register_token(chain_id: u64, token: H160, fee: u128) -> (Xcm<()>, Balance) {
168+
fn convert_register_token(
169+
message_id: H256,
170+
chain_id: u64,
171+
token: H160,
172+
fee: u128,
173+
) -> (Xcm<()>, Balance) {
163174
let network = Ethereum { chain_id };
164175
let xcm_fee: Asset = (Location::parent(), fee).into();
165176
let deposit: Asset = (Location::parent(), CreateAssetDeposit::get()).into();
@@ -202,13 +213,16 @@ where
202213
// Clear the origin so that remaining assets in holding
203214
// are claimable by the physical origin (BridgeHub)
204215
ClearOrigin,
216+
// Forward message id to Asset Hub
217+
SetTopic(message_id.into()),
205218
]
206219
.into();
207220

208221
(xcm, total_amount.into())
209222
}
210223

211224
fn convert_send_token(
225+
message_id: H256,
212226
chain_id: u64,
213227
token: H160,
214228
destination: Destination,
@@ -266,6 +280,8 @@ where
266280
BuyExecution { fees: dest_para_fee_asset, weight_limit: Unlimited },
267281
// Deposit asset to beneficiary.
268282
DepositAsset { assets: Definite(asset.into()), beneficiary },
283+
// Forward message id to destination parachain.
284+
SetTopic(message_id.into()),
269285
]
270286
.into(),
271287
},
@@ -279,6 +295,9 @@ where
279295
},
280296
}
281297

298+
// Forward message id to Asset Hub.
299+
instructions.push(SetTopic(message_id.into()));
300+
282301
(instructions.into(), total_fees.into())
283302
}
284303

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use snowbridge_pallet_inbound_queue_fixtures::{
2727
};
2828
use snowbridge_pallet_system;
2929
use snowbridge_router_primitives::inbound::{
30-
Command, GlobalConsensusEthereumConvertsFor, MessageV1, VersionedMessage,
30+
Command, ConvertMessage, GlobalConsensusEthereumConvertsFor, MessageV1, VersionedMessage,
3131
};
3232
use sp_core::H256;
3333
use sp_runtime::{DispatchError::Token, TokenError::FundsUnavailable};
@@ -527,6 +527,8 @@ fn register_weth_token_in_asset_hub_fail_for_insufficient_fee() {
527527
type RuntimeEvent = <BridgeHubRococo as Chain>::RuntimeEvent;
528528
type EthereumInboundQueue =
529529
<BridgeHubRococo as BridgeHubRococoPallet>::EthereumInboundQueue;
530+
type Converter = <bridge_hub_rococo_runtime::Runtime as snowbridge_pallet_inbound_queue::Config>::MessageConverter;
531+
530532
let message_id: H256 = [0; 32].into();
531533
let message = VersionedMessage::V1(MessageV1 {
532534
chain_id: CHAIN_ID,
@@ -536,7 +538,7 @@ fn register_weth_token_in_asset_hub_fail_for_insufficient_fee() {
536538
fee: INSUFFICIENT_XCM_FEE,
537539
},
538540
});
539-
let (xcm, _) = EthereumInboundQueue::do_convert(message_id, message).unwrap();
541+
let (xcm, _) = Converter::convert(message_id, message).unwrap();
540542
let _ = EthereumInboundQueue::send_xcm(xcm, AssetHubRococo::para_id().into()).unwrap();
541543

542544
assert_expected_events!(
@@ -553,7 +555,7 @@ fn register_weth_token_in_asset_hub_fail_for_insufficient_fee() {
553555
assert_expected_events!(
554556
AssetHubRococo,
555557
vec![
556-
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false, .. }) => {},
558+
RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success:false, .. }) => { },
557559
]
558560
);
559561
});

0 commit comments

Comments
 (0)