From b92f70cda85acbfed6f5d7dda7b83108abdde1d3 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Tue, 23 Jul 2024 12:06:41 +0100 Subject: [PATCH 1/5] Allow tx payloads to be boxed --- core/src/tx/payload.rs | 26 ++++++++++++++++++++++++++ subxt/src/book/usage/transactions.rs | 8 ++++++++ 2 files changed, 34 insertions(+) diff --git a/core/src/tx/payload.rs b/core/src/tx/payload.rs index bd9c79db17..b68460ffdb 100644 --- a/core/src/tx/payload.rs +++ b/core/src/tx/payload.rs @@ -38,6 +38,32 @@ pub trait Payload { } } +macro_rules! boxed_payload { + ($ty:path) => { + impl Payload for $ty { + fn encode_call_data_to( + &self, + metadata: &Metadata, + out: &mut Vec, + ) -> Result<(), Error> { + self.as_ref().encode_call_data_to(metadata, out) + } + fn encode_call_data(&self, metadata: &Metadata) -> Result, Error> { + self.as_ref().encode_call_data(metadata) + } + fn validation_details(&self) -> Option> { + self.as_ref().validation_details() + } + } + }; +} + +boxed_payload!(Box); +#[cfg(feature = "std")] +boxed_payload!(std::sync::Arc); +#[cfg(feature = "std")] +boxed_payload!(std::rc::Rc); + /// Details required to validate the shape of a transaction payload against some metadata. pub struct ValidationDetails<'a> { /// The pallet name. diff --git a/subxt/src/book/usage/transactions.rs b/subxt/src/book/usage/transactions.rs index b897790599..145277cde2 100644 --- a/subxt/src/book/usage/transactions.rs +++ b/subxt/src/book/usage/transactions.rs @@ -196,6 +196,14 @@ //! This example doesn't wait for the transaction to be included in a block; it just submits it and //! hopes for the best! //! +//! ### Boxing transaction payloads +//! +//! Transaction payloads can be boxed so that they all share a common type and can be stored together. +//! +//! ```rust,ignore +#![doc = include_str!("../../../examples/tx_boxed.rs")] +//! ``` +//! //! ### Custom handling of transaction status updates //! //! If you'd like more control or visibility over exactly which status updates are being emitted for From 9fda8ad833323159c0d1b3c62055295a0899e7ea Mon Sep 17 00:00:00 2001 From: James Wilson Date: Tue, 23 Jul 2024 12:09:44 +0100 Subject: [PATCH 2/5] Add example of boxed payloads --- subxt/examples/tx_boxed.rs | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 subxt/examples/tx_boxed.rs diff --git a/subxt/examples/tx_boxed.rs b/subxt/examples/tx_boxed.rs new file mode 100644 index 0000000000..62812b9891 --- /dev/null +++ b/subxt/examples/tx_boxed.rs @@ -0,0 +1,43 @@ +#![allow(missing_docs)] +use subxt::{OnlineClient, PolkadotConfig}; +use subxt_signer::sr25519::dev; + +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")] +pub mod polkadot {} + +#[tokio::main] +async fn main() -> Result<(), Box> { + let api = OnlineClient::::new().await?; + + // Prepare some extrinsics. These are boxed so that they can live alongside each other. + let txs = [dynamic_remark(), balance_transfer(), remark()]; + + for tx in txs { + let from = dev::alice(); + api.tx() + .sign_and_submit_then_watch_default(&tx, &from) + .await? + .wait_for_finalized_success() + .await?; + + println!("Submitted tx"); + } + + Ok(()) +} + +fn balance_transfer() -> Box { + let dest = dev::bob().public_key().into(); + Box::new(polkadot::tx().balances().transfer_allow_death(dest, 10_000)) +} + +fn remark() -> Box { + Box::new(polkadot::tx().system().remark(vec![1, 2, 3, 4, 5])) +} + +fn dynamic_remark() -> Box { + use subxt::dynamic::{tx, Value}; + let tx_payload = tx("System", "remark", vec![Value::from_bytes("Hello")]); + + Box::new(tx_payload) +} From 3c4010d512e4f5095fe8f1f40bca234d6969bcd8 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 24 Jul 2024 10:21:28 +0100 Subject: [PATCH 3/5] explicit box import --- core/src/tx/payload.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/tx/payload.rs b/core/src/tx/payload.rs index b68460ffdb..07d47df834 100644 --- a/core/src/tx/payload.rs +++ b/core/src/tx/payload.rs @@ -58,7 +58,7 @@ macro_rules! boxed_payload { }; } -boxed_payload!(Box); +boxed_payload!(alloc::boxed::Box); #[cfg(feature = "std")] boxed_payload!(std::sync::Arc); #[cfg(feature = "std")] From bf79e6f7a1dff65e1c6c98e1b3b312e2a1e36933 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 24 Jul 2024 10:22:07 +0100 Subject: [PATCH 4/5] box import at top --- core/src/tx/payload.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/tx/payload.rs b/core/src/tx/payload.rs index 07d47df834..2692a38dc3 100644 --- a/core/src/tx/payload.rs +++ b/core/src/tx/payload.rs @@ -10,6 +10,7 @@ use crate::metadata::Metadata; use crate::Error; use alloc::borrow::{Cow, ToOwned}; use alloc::string::String; +use alloc::boxed::Box; use alloc::vec::Vec; use codec::Encode; @@ -58,7 +59,7 @@ macro_rules! boxed_payload { }; } -boxed_payload!(alloc::boxed::Box); +boxed_payload!(Box); #[cfg(feature = "std")] boxed_payload!(std::sync::Arc); #[cfg(feature = "std")] From 3a775d730304c20ca44d7ca9a8276a63abcdab32 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Wed, 24 Jul 2024 15:20:04 +0100 Subject: [PATCH 5/5] cargo fmt --- core/src/tx/payload.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/tx/payload.rs b/core/src/tx/payload.rs index 2692a38dc3..b2e9682038 100644 --- a/core/src/tx/payload.rs +++ b/core/src/tx/payload.rs @@ -9,8 +9,8 @@ use crate::error::MetadataError; use crate::metadata::Metadata; use crate::Error; use alloc::borrow::{Cow, ToOwned}; -use alloc::string::String; use alloc::boxed::Box; +use alloc::string::String; use alloc::vec::Vec; use codec::Encode;