Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents EthereumBlobExporter from consuming dest when returning NotApplicable #5789

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bridges/snowbridge/primitives/router/src/outbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl<UniversalLocation, EthereumNetwork, OutboundQueue, AgentHashedDescription,
let dest = destination.take().ok_or(SendError::MissingArgument)?;
Copy link
Contributor

@yrong yrong Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just replace take here with clone without other reset operations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, that is much better. :) 4fe4085

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`.
*destination = Some(dest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claravanstaden
bellow there is a another usage:

if Ok(local_net) != universal_location.global_consensus() {
			log::trace!(target: "xcm::ethereum_blob_exporter", "skipped due to unmatched relay network {local_net:?}.");
			return Err(SendError::NotApplicable)
		}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wondered about that one, because the comment only mentions dest and msg. But basically no mutable parameters should be altered when the error is NotApplicable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bfc90ba. This case is easy to miss... Perhaps the parameters should not be mutable in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced it with a clone, as @yrong suggested. :) 4fe4085

return Err(SendError::NotApplicable)
}

Expand Down
49 changes: 49 additions & 0 deletions bridges/snowbridge/primitives/router/src/outbound/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,3 +1163,52 @@ fn xcm_converter_transfer_native_token_with_invalid_location_will_fail() {
let result = converter.convert();
assert_eq!(result.err(), Some(XcmConverterError::InvalidAsset));
}

#[test]
fn exporter_validate_with_invalid_dest_does_not_alter_destination() {
let network = BridgedNetwork::get();
let mut destination: InteriorLocation = Parachain(1000).into();

let mut universal_source: Option<InteriorLocation> =
Some([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 result =
EthereumBlobExporter::<
UniversalLocation,
BridgedNetwork,
MockOkOutboundQueue,
AgentIdOf,
MockTokenIdConvert,
>::validate(network, channel, &mut universal_source, &mut dest_wrapper, &mut msg_wrapper);

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

// ensure dest and msg are untouched
assert_eq!(Some(destination), dest_wrapper);
assert_eq!(Some(msg), msg_wrapper);
}
Loading