From 7d5edab769209b17485f6d5ad9d70d93f1b66164 Mon Sep 17 00:00:00 2001 From: Or Grinberg Date: Wed, 12 Jun 2024 17:37:54 +0100 Subject: [PATCH 1/5] allow clear_origin in safe xcm builder --- .../xcm/procedural/src/builder_pattern.rs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/polkadot/xcm/procedural/src/builder_pattern.rs b/polkadot/xcm/procedural/src/builder_pattern.rs index 0a33d52580fc..09ead1389d19 100644 --- a/polkadot/xcm/procedural/src/builder_pattern.rs +++ b/polkadot/xcm/procedural/src/builder_pattern.rs @@ -233,6 +233,32 @@ fn generate_builder_impl(name: &Ident, data_enum: &DataEnum) -> Result = data_enum + .variants + .iter() + .filter(|variant| variant.ident == "ClearOrigin") + .map(|variant| { + let variant_name = &variant.ident; + let method_name_string = &variant_name.to_string().to_snake_case(); + let method_name = syn::Ident::new(method_name_string, variant_name.span()); + let docs = get_doc_comments(variant); + let method = match &variant.fields { + Fields::Unit => { + quote! { + #(#docs)* + pub fn #method_name(mut self) -> XcmBuilder { + self.instructions.push(#name::::#variant_name); + self + } + } + }, + _ => return Err(Error::new_spanned(variant, "ClearOrigin should have no fields")), + }; + Ok(method) + }) + .collect::, _>>()?; + // Then we require fees to be paid let buy_execution_method = data_enum .variants @@ -276,6 +302,7 @@ fn generate_builder_impl(name: &Ident, data_enum: &DataEnum) -> Result XcmBuilder { + #(#allowed_after_load_holding_methods)* #buy_execution_method } }; From 806c8de1a4fa0e68330568f7e513a89328e9d44e Mon Sep 17 00:00:00 2001 From: Or Grinberg Date: Tue, 2 Jul 2024 07:31:52 +0100 Subject: [PATCH 2/5] Add test --- .../xcm/procedural/tests/builder_pattern.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/polkadot/xcm/procedural/tests/builder_pattern.rs b/polkadot/xcm/procedural/tests/builder_pattern.rs index 96b16fb7e456..4202309bf3f7 100644 --- a/polkadot/xcm/procedural/tests/builder_pattern.rs +++ b/polkadot/xcm/procedural/tests/builder_pattern.rs @@ -79,3 +79,24 @@ fn default_builder_requires_buy_execution() { ]) ); } + +#[test] +fn default_builder_allows_clear_origin_before_buy_execution() { + let asset: Asset = (Here, 100u128).into(); + let beneficiary: Location = [0u8; 32].into(); + let message: Xcm<()> = Xcm::builder() + .receive_teleported_asset(asset.clone()) + .clear_origin() + .buy_execution(asset.clone(), Unlimited) + .deposit_asset(asset.clone(), beneficiary.clone()) + .build(); + assert_eq!( + message, + Xcm(vec![ + ReceiveTeleportedAsset(asset.clone().into()), + ClearOrigin, + BuyExecution { fees: asset.clone(), weight_limit: Unlimited }, + DepositAsset { assets: asset.into(), beneficiary }, + ]) + ); +} From ef00161dd408d36a23aae80672217d2c463d1e97 Mon Sep 17 00:00:00 2001 From: Or Grinberg Date: Tue, 2 Jul 2024 08:09:01 +0100 Subject: [PATCH 3/5] Add prdoc --- prdoc/pr_4777.prdoc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 prdoc/pr_4777.prdoc diff --git a/prdoc/pr_4777.prdoc b/prdoc/pr_4777.prdoc new file mode 100644 index 000000000000..b08d5df4844e --- /dev/null +++ b/prdoc/pr_4777.prdoc @@ -0,0 +1,26 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: XCM builder pattern allows clear_origin before buy_execution. + +doc: + - audience: Runtime Dev + description: | + Added clear_origin as an allowed command after commands that load the holdings register, in the safe xcm builder. + Previously, although it's logically allowed, an XCM could not be built like this: + ```rust + let xcm = Xcm::builder() + .withdraw_asset((Parent, 100u128).into()) + .clear_origin + .buy_execution((Parent, 1u128).into()) + .deposit_asset(All.into(), AccountId32 { id: [0u8; 32], network: None }.into()) + .build(); + ``` + Now, it can. + +crates: +- name: "xcm-procedural" + bump: minor +- name: "staging-xcm" + bump: minor + From b8509f2421db5916cd7121f8c0dc0b6be34f1c28 Mon Sep 17 00:00:00 2001 From: Or Grinberg Date: Tue, 2 Jul 2024 09:51:04 +0100 Subject: [PATCH 4/5] prdoc improvements --- prdoc/pr_4777.prdoc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/prdoc/pr_4777.prdoc b/prdoc/pr_4777.prdoc index b08d5df4844e..733115fb5fb1 100644 --- a/prdoc/pr_4777.prdoc +++ b/prdoc/pr_4777.prdoc @@ -10,13 +10,14 @@ doc: Previously, although it's logically allowed, an XCM could not be built like this: ```rust let xcm = Xcm::builder() - .withdraw_asset((Parent, 100u128).into()) + .withdraw_asset((Parent, 100u128)) .clear_origin - .buy_execution((Parent, 1u128).into()) - .deposit_asset(All.into(), AccountId32 { id: [0u8; 32], network: None }.into()) + .buy_execution((Parent, 1u128)) + .deposit_asset(All, [0u8; 32]) .build(); ``` - Now, it can. + You had to use the unsafe_builder. + Now, it's allowed using the default builder. crates: - name: "xcm-procedural" From 52e86d5d330af8aaa3d9fc2af54b00ce87dfcd3e Mon Sep 17 00:00:00 2001 From: gupnik Date: Tue, 9 Jul 2024 05:13:15 +0200 Subject: [PATCH 5/5] Update prdoc/pr_4777.prdoc --- prdoc/pr_4777.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4777.prdoc b/prdoc/pr_4777.prdoc index 733115fb5fb1..07fa8decebe0 100644 --- a/prdoc/pr_4777.prdoc +++ b/prdoc/pr_4777.prdoc @@ -11,7 +11,7 @@ doc: ```rust let xcm = Xcm::builder() .withdraw_asset((Parent, 100u128)) - .clear_origin + .clear_origin() .buy_execution((Parent, 1u128)) .deposit_asset(All, [0u8; 32]) .build();