Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoaguirre committed Mar 21, 2024
1 parent 01c629c commit caa74e6
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,19 @@ fn create_channel() {
assert_ok!(<Rococo as RococoPallet>::XcmPallet::send_blob(
sudo_origin.clone(),
bx!(destination.clone()),
create_agent_xcm.encode().try_into().expect("MaxXcmEncodedSize should be big enough."),
create_agent_xcm
.encode()
.try_into()
.expect("MaxXcmEncodedSize should be big enough."),
));

assert_ok!(<Rococo as RococoPallet>::XcmPallet::send_blob(
sudo_origin,
bx!(destination),
create_channel_xcm.encode().try_into().expect("MaxXcmEncodedSize should be big enough."),
create_channel_xcm
.encode()
.try_into()
.expect("MaxXcmEncodedSize should be big enough."),
));

type RuntimeEvent = <Rococo as Chain>::RuntimeEvent;
Expand Down
5 changes: 4 additions & 1 deletion polkadot/runtime/rococo/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ where
let _ = <pallet_xcm::Pallet<Runtime>>::send_blob(
RawOrigin::Root.into(),
Box::new(VersionedLocation::V4(destination)),
VersionedXcm::V4(program).encode().try_into().expect("MaxXcmEncodedSize should be big enough."),
VersionedXcm::V4(program)
.encode()
.try_into()
.expect("MaxXcmEncodedSize should be big enough."),
)?;
Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion polkadot/runtime/westend/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ where
let _ = <pallet_xcm::Pallet<Runtime>>::send_blob(
RawOrigin::Root.into(),
Box::new(VersionedLocation::V4(destination)),
VersionedXcm::V4(program).encode().try_into().expect("MaxXcmEncodedSize should be big enough."),
VersionedXcm::V4(program)
.encode()
.try_into()
.expect("MaxXcmEncodedSize should be big enough."),
)?;
Ok(())
}
Expand Down
41 changes: 19 additions & 22 deletions polkadot/xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ use sp_runtime::{
use sp_std::{boxed::Box, marker::PhantomData, prelude::*, result::Result, vec};
use xcm::{latest::QueryResponseInfo, prelude::*};
use xcm_builder::{
ExecuteController, ExecuteControllerWeightInfo, QueryController, QueryControllerWeightInfo,
SendController, SendControllerWeightInfo, MaxXcmEncodedSize,
ExecuteController, ExecuteControllerWeightInfo, MaxXcmEncodedSize, QueryController,
QueryControllerWeightInfo, SendController, SendControllerWeightInfo,
};
use xcm_executor::{
traits::{
Expand Down Expand Up @@ -300,21 +300,20 @@ pub mod pallet {
}
}

impl<T: Config> ExecuteController<OriginFor<T>, <T as Config>::RuntimeCall>
for Pallet<T>
{
impl<T: Config> ExecuteController<OriginFor<T>, <T as Config>::RuntimeCall> for Pallet<T> {
type WeightInfo = Self;
fn execute_blob(
origin: OriginFor<T>,
encoded_message: BoundedVec<u8, MaxXcmEncodedSize>,
max_weight: Weight,
) -> Result<Weight, DispatchErrorWithPostInfo> {
let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin)?;
let message = VersionedXcm::<<T as Config>::RuntimeCall>::decode(&mut &encoded_message[..])
.map_err(|error| {
log::error!(target: "xcm::execute_blob", "Unable to decode XCM, error: {:?}", error);
Error::<T>::UnableToDecode
})?;
let message =
VersionedXcm::<<T as Config>::RuntimeCall>::decode(&mut &encoded_message[..])
.map_err(|error| {
log::error!(target: "xcm::execute_blob", "Unable to decode XCM, error: {:?}", error);
Error::<T>::UnableToDecode
})?;
Self::execute_base(origin_location, Box::new(message), max_weight)
}
}
Expand All @@ -333,8 +332,8 @@ pub mod pallet {
encoded_message: BoundedVec<u8, MaxXcmEncodedSize>,
) -> Result<XcmHash, DispatchError> {
let origin_location = T::SendXcmOrigin::ensure_origin(origin)?;
let message = VersionedXcm::<()>::decode(&mut &encoded_message[..])
.map_err(|error| {
let message =
VersionedXcm::<()>::decode(&mut &encoded_message[..]).map_err(|error| {
log::error!(target: "xcm::send_blob", "Unable to decode XCM, error: {:?}", error);
Error::<T>::UnableToDecode
})?;
Expand Down Expand Up @@ -907,19 +906,14 @@ pub mod pallet {
max_weight,
))
})()
.map_err(|e: DispatchError| {
e.with_weight(T::WeightInfo::execute())
})?;
.map_err(|e: DispatchError| e.with_weight(T::WeightInfo::execute()))?;

Self::deposit_event(Event::Attempted { outcome: outcome.clone() });
let weight_used = outcome.weight_used();
outcome.ensure_complete().map_err(|error| {
log::error!(target: "xcm::pallet_xcm::execute", "XCM execution failed with error {:?}", error);
Error::<T>::LocalExecutionIncomplete.with_weight(
weight_used.saturating_add(
T::WeightInfo::execute(),
),
)
Error::<T>::LocalExecutionIncomplete
.with_weight(weight_used.saturating_add(T::WeightInfo::execute()))
})?;
Ok(weight_used)
}
Expand Down Expand Up @@ -1080,7 +1074,8 @@ pub mod pallet {
/// the maximum amount of weight that the message could take to be executed, then no
/// execution attempt will be made.
///
/// WARNING: DEPRECATED. `execute` will be removed after June 2024. Use `execute_blob` instead.
/// WARNING: DEPRECATED. `execute` will be removed after June 2024. Use `execute_blob`
/// instead.
#[pallet::call_index(3)]
#[pallet::weight(max_weight.saturating_add(T::WeightInfo::execute()))]
pub fn execute(
Expand Down Expand Up @@ -1509,7 +1504,9 @@ pub mod pallet {
max_weight: Weight,
) -> DispatchResultWithPostInfo {
let weight_used = <Self as ExecuteController<_, _>>::execute_blob(
origin, encoded_message, max_weight,
origin,
encoded_message,
max_weight,
)?;
Ok(Some(weight_used.saturating_add(T::WeightInfo::execute_blob())).into())
}
Expand Down
52 changes: 25 additions & 27 deletions polkadot/xcm/pallet-xcm/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ pub(crate) mod assets_transfer;

use crate::{
mock::*, pallet::SupportedVersion, AssetTraps, Config, CurrentMigration, Error,
LatestVersionedLocation, Pallet, Queries, QueryStatus,
VersionDiscoveryQueue, VersionMigrationStage, VersionNotifiers, VersionNotifyTargets,
WeightInfo,
LatestVersionedLocation, Pallet, Queries, QueryStatus, VersionDiscoveryQueue,
VersionMigrationStage, VersionNotifiers, VersionNotifyTargets, WeightInfo,
};
use codec::Encode;
use frame_support::{
Expand Down Expand Up @@ -307,7 +306,10 @@ fn send_works() {

let versioned_dest = Box::new(RelayLocation::get().into());
let versioned_message = VersionedXcm::from(message.clone());
let encoded_versioned_message = versioned_message.encode().try_into().expect("MaxXcmEncodedSize should be big enough.");
let encoded_versioned_message = versioned_message
.encode()
.try_into()
.expect("MaxXcmEncodedSize should be big enough.");
assert_ok!(XcmPallet::send_blob(
RuntimeOrigin::signed(ALICE),
versioned_dest,
Expand Down Expand Up @@ -352,7 +354,10 @@ fn send_fails_when_xcm_router_blocks() {
XcmPallet::send_blob(
RuntimeOrigin::signed(ALICE),
Box::new(Location::ancestor(8).into()),
VersionedXcm::from(message.clone()).encode().try_into().expect("MaxXcmEncodedSize should be big enough."),
VersionedXcm::from(message.clone())
.encode()
.try_into()
.expect("MaxXcmEncodedSize should be big enough."),
),
crate::Error::<Test>::SendFailure
);
Expand Down Expand Up @@ -554,9 +559,7 @@ fn incomplete_execute_reverts_side_effects() {
result,
Err(sp_runtime::DispatchErrorWithPostInfo {
post_info: frame_support::dispatch::PostDispatchInfo {
actual_weight: Some(
<<Test as crate::Config>::WeightInfo>::execute() + weight
),
actual_weight: Some(<<Test as crate::Config>::WeightInfo>::execute() + weight),
pays_fee: frame_support::dispatch::Pays::Yes,
},
error: sp_runtime::DispatchError::Module(sp_runtime::ModuleError {
Expand Down Expand Up @@ -1254,24 +1257,20 @@ fn multistage_migration_works() {

#[test]
fn execute_blob_works() {
use codec::Encode;
use bounded_collections::BoundedVec;
let message = VersionedXcm::V4(Xcm::<RuntimeCall>::builder()
.withdraw_asset((Here, SEND_AMOUNT).into())
.buy_execution((Here, FEE_AMOUNT).into(), Unlimited)
.deposit_asset(All.into(), AccountId32 { id: BOB.clone().into(), network: None }.into())
.build()
use codec::Encode;
let message = VersionedXcm::V4(
Xcm::<RuntimeCall>::builder()
.withdraw_asset((Here, SEND_AMOUNT).into())
.buy_execution((Here, FEE_AMOUNT).into(), Unlimited)
.deposit_asset(All.into(), AccountId32 { id: BOB.clone().into(), network: None }.into())
.build(),
);
let encoded_message = BoundedVec::try_from(
message.encode()
).expect("MaxXcmEncodedSize should be big enough to encode simple XCM");
let encoded_message = BoundedVec::try_from(message.encode())
.expect("MaxXcmEncodedSize should be big enough to encode simple XCM");
let weight = BaseXcmWeight::get() * 3; // We get the actual weight since it's fixed per instruction.
new_test_ext_with_balances(vec![(ALICE, INITIAL_BALANCE)]).execute_with(|| {
assert_ok!(XcmPallet::execute_blob(
RuntimeOrigin::signed(ALICE),
encoded_message,
weight,
));
assert_ok!(XcmPallet::execute_blob(RuntimeOrigin::signed(ALICE), encoded_message, weight,));
assert_eq!(Balances::total_balance(&ALICE), INITIAL_BALANCE - SEND_AMOUNT);
assert_eq!(Balances::total_balance(&BOB), SEND_AMOUNT);
assert_eq!(
Expand All @@ -1285,8 +1284,8 @@ fn execute_blob_works() {

#[test]
fn send_blob_works() {
use codec::Encode;
use bounded_collections::BoundedVec;
use codec::Encode;
let sender: Location = AccountId32 { id: ALICE.clone().into(), network: None }.into();
// Have to use `builder_unsafe` because of the `clear_origin`.
// TODO(https://github.com/paritytech/polkadot-sdk/issues/3770): make this work without the `_unsafe`.
Expand All @@ -1296,9 +1295,8 @@ fn send_blob_works() {
.buy_execution((Parent, FEE_AMOUNT).into(), Unlimited)
.deposit_asset(All.into(), sender.clone())
.build();
let encoded_message = BoundedVec::try_from(
VersionedXcm::V4(message.clone()).encode()
).expect("MaxXcmEncodedSize should be big enough to encode simple XCM");
let encoded_message = BoundedVec::try_from(VersionedXcm::V4(message.clone()).encode())
.expect("MaxXcmEncodedSize should be big enough to encode simple XCM");
let versioned_destination = Box::new(RelayLocation::get().into());
new_test_ext_with_balances(vec![(ALICE, INITIAL_BALANCE)]).execute_with(|| {
assert_ok!(XcmPallet::send_blob(
Expand All @@ -1310,7 +1308,7 @@ fn send_blob_works() {
Some(DescendOrigin(sender.clone().try_into().unwrap()))
.into_iter()
.chain(message.0.clone().into_iter())
.collect()
.collect(),
);
let id = fake_message_hash(&sent_message);
assert_eq!(sent_xcm(), vec![(Here.into(), sent_message)]);
Expand Down
11 changes: 5 additions & 6 deletions polkadot/xcm/src/v4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,12 +1493,11 @@ mod tests {
many_assets.push((GeneralIndex(index as u128), 1u128).into());
}

let full_xcm_pass = Xcm::<()>(vec![
TransferAsset {
assets: many_assets,
beneficiary: Here.into(),
}; MAX_INSTRUCTIONS_TO_DECODE as usize
]);
let full_xcm_pass =
Xcm::<()>(vec![
TransferAsset { assets: many_assets, beneficiary: Here.into() };
MAX_INSTRUCTIONS_TO_DECODE as usize
]);
let encoded = full_xcm_pass.encode();
assert_eq!(encoded.len(), 12402);
assert!(Xcm::<()>::decode(&mut &encoded[..]).is_ok());
Expand Down
3 changes: 1 addition & 2 deletions polkadot/xcm/xcm-builder/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
use frame_support::{
dispatch::{DispatchErrorWithPostInfo, WithPostDispatchInfo},
pallet_prelude::DispatchError,
parameter_types,
BoundedVec,
parameter_types, BoundedVec,
};
use sp_std::boxed::Box;
use xcm::prelude::*;
Expand Down
3 changes: 1 addition & 2 deletions polkadot/xcm/xcm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ pub use barriers::{

mod controller;
pub use controller::{
Controller, ExecuteController, ExecuteControllerWeightInfo, QueryController,
Controller, ExecuteController, ExecuteControllerWeightInfo, MaxXcmEncodedSize, QueryController,
QueryControllerWeightInfo, QueryHandler, SendController, SendControllerWeightInfo,
MaxXcmEncodedSize,
};

mod currency_adapter;
Expand Down
6 changes: 5 additions & 1 deletion substrate/frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,11 @@ pub mod env {
ctx.charge_gas(RuntimeCosts::CallRuntime(weight))?;
let origin = crate::RawOrigin::Signed(ctx.ext.address().clone()).into();

match <<E::T as Config>::Xcm>::send_blob(origin, dest.into(), message.encode().try_into().expect("TODO: What to do here?")) {
match <<E::T as Config>::Xcm>::send_blob(
origin,
dest.into(),
message.encode().try_into().expect("TODO: What to do here?"),
) {
Ok(message_id) => {
ctx.write_sandbox_memory(memory, output_ptr, &message_id.encode())?;
Ok(ReturnErrorCode::Success)
Expand Down

0 comments on commit caa74e6

Please sign in to comment.