Skip to content

Commit

Permalink
fix another case
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Sep 20, 2024
1 parent b96caa5 commit bfc90ba
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
8 changes: 7 additions & 1 deletion bridges/snowbridge/primitives/router/src/outbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ impl<UniversalLocation, EthereumNetwork, OutboundQueue, AgentHashedDescription,
let dest = destination.take().ok_or(SendError::MissingArgument)?;
if dest != Here {
log::trace!(target: "xcm::ethereum_blob_exporter", "skipped due to unmatched remote destination {dest:?}.");
// We need to make sure that destination is not consumed in case of `NotApplicable`.
// We need to make sure that mutable variables are not consumed in case of
// `NotApplicable`, since they may be used by a subsequent exporter.
*destination = Some(dest);
return Err(SendError::NotApplicable)
}

let universal_source_value = universal_source.clone();
let (local_net, local_sub) = universal_source
.take()
.ok_or_else(|| {
Expand All @@ -90,6 +92,10 @@ impl<UniversalLocation, EthereumNetwork, OutboundQueue, AgentHashedDescription,

if Ok(local_net) != universal_location.global_consensus() {
log::trace!(target: "xcm::ethereum_blob_exporter", "skipped due to unmatched relay network {local_net:?}.");
// We need to make sure that mutable variables are not consumed in case of
// `NotApplicable`, since they may be used by a subsequent exporter.
*universal_source = universal_source_value.clone();
*destination = Some(dest);
return Err(SendError::NotApplicable)
}

Expand Down
65 changes: 60 additions & 5 deletions bridges/snowbridge/primitives/router/src/outbound/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,10 +1167,61 @@ fn xcm_converter_transfer_native_token_with_invalid_location_will_fail() {
#[test]
fn exporter_validate_with_invalid_dest_does_not_alter_destination() {
let network = BridgedNetwork::get();
let mut destination: InteriorLocation = Parachain(1000).into();
let destination: InteriorLocation = Parachain(1000).into();

let mut universal_source: Option<InteriorLocation> =
Some([GlobalConsensus(Polkadot), Parachain(1000)].into());
let universal_source: InteriorLocation = [GlobalConsensus(Polkadot), Parachain(1000)].into();

let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000");
let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000");

let channel: u32 = 0;
let assets: Assets = vec![Asset {
id: AssetId([AccountKey20 { network: None, key: token_address }].into()),
fun: Fungible(1000),
}]
.into();
let fee = assets.clone().get(0).unwrap().clone();
let filter: AssetFilter = assets.clone().into();
let msg: Xcm<()> = vec![
WithdrawAsset(assets.clone()),
ClearOrigin,
BuyExecution { fees: fee, weight_limit: Unlimited },
DepositAsset {
assets: filter,
beneficiary: AccountKey20 { network: None, key: beneficiary_address }.into(),
},
SetTopic([0; 32]),
]
.into();
let mut msg_wrapper: Option<Xcm<()>> = Some(msg.clone());
let mut dest_wrapper = Some(destination.clone());
let mut universal_source_wrapper = Some(universal_source.clone());

let result =
EthereumBlobExporter::<
UniversalLocation,
BridgedNetwork,
MockOkOutboundQueue,
AgentIdOf,
MockTokenIdConvert,
>::validate(
network, channel, &mut universal_source_wrapper, &mut dest_wrapper, &mut msg_wrapper
);

assert_eq!(result, Err(XcmSendError::NotApplicable));

// ensure mutable variables are not changed
assert_eq!(Some(destination), dest_wrapper);
assert_eq!(Some(msg), msg_wrapper);
assert_eq!(Some(universal_source), universal_source_wrapper);
}

#[test]
fn exporter_validate_with_invalid_universal_source_does_not_alter_universal_source() {
let network = BridgedNetwork::get();
let destination: InteriorLocation = Here.into();

let universal_source: InteriorLocation = [GlobalConsensus(Westend), Parachain(1000)].into();

let token_address: [u8; 20] = hex!("1000000000000000000000000000000000000000");
let beneficiary_address: [u8; 20] = hex!("2000000000000000000000000000000000000000");
Expand All @@ -1196,6 +1247,7 @@ fn exporter_validate_with_invalid_dest_does_not_alter_destination() {
.into();
let mut msg_wrapper: Option<Xcm<()>> = Some(msg.clone());
let mut dest_wrapper = Some(destination.clone());
let mut universal_source_wrapper = Some(universal_source.clone());

let result =
EthereumBlobExporter::<
Expand All @@ -1204,11 +1256,14 @@ fn exporter_validate_with_invalid_dest_does_not_alter_destination() {
MockOkOutboundQueue,
AgentIdOf,
MockTokenIdConvert,
>::validate(network, channel, &mut universal_source, &mut dest_wrapper, &mut msg_wrapper);
>::validate(
network, channel, &mut universal_source_wrapper, &mut dest_wrapper, &mut msg_wrapper
);

assert_eq!(result, Err(XcmSendError::NotApplicable));

// ensure dest and msg are untouched
// ensure mutable variables are not changed
assert_eq!(Some(destination), dest_wrapper);
assert_eq!(Some(msg), msg_wrapper);
assert_eq!(Some(universal_source), universal_source_wrapper);
}

0 comments on commit bfc90ba

Please sign in to comment.