From eef192925b9aa1fac928479ca45bc59d5a965348 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Mon, 14 Dec 2020 14:09:11 -0800 Subject: [PATCH] Update to set mortal_period in client --- examples/kusama_balance_transfer.rs | 3 -- examples/submit_and_watch.rs | 3 -- examples/transfer_subscribe.rs | 3 -- proc-macro/src/call.rs | 8 +--- src/extrinsic/extra.rs | 13 +++++-- src/extrinsic/mod.rs | 6 +-- src/lib.rs | 60 ++++++++++++++++++----------- 7 files changed, 51 insertions(+), 45 deletions(-) diff --git a/examples/kusama_balance_transfer.rs b/examples/kusama_balance_transfer.rs index d5bd647d0a..87670da2cc 100644 --- a/examples/kusama_balance_transfer.rs +++ b/examples/kusama_balance_transfer.rs @@ -20,8 +20,6 @@ use substrate_subxt::{ ClientBuilder, KusamaRuntime, PairSigner, - SignedOptions, - DEFAULT_ERA_PERIOD }; #[async_std::main] @@ -34,7 +32,6 @@ async fn main() -> Result<(), Box> { let client = ClientBuilder::::new().build().await?; let hash = client.transfer( &signer, - SignedOptions { era_period: Some(DEFAULT_ERA_PERIOD) }, &dest, 10_000 ).await?; diff --git a/examples/submit_and_watch.rs b/examples/submit_and_watch.rs index fe310dea04..d7371015be 100644 --- a/examples/submit_and_watch.rs +++ b/examples/submit_and_watch.rs @@ -23,8 +23,6 @@ use substrate_subxt::{ ClientBuilder, DefaultNodeRuntime, PairSigner, - SignedOptions, - DEFAULT_ERA_PERIOD, }; #[async_std::main] @@ -37,7 +35,6 @@ async fn main() -> Result<(), Box> { let client = ClientBuilder::::new().build().await?; let result = client.transfer_and_watch( &signer, - SignedOptions { era_period: Some(DEFAULT_ERA_PERIOD) }, &dest, 10_000 ).await?; diff --git a/examples/transfer_subscribe.rs b/examples/transfer_subscribe.rs index 0a9ca71ca3..edcef21201 100644 --- a/examples/transfer_subscribe.rs +++ b/examples/transfer_subscribe.rs @@ -27,8 +27,6 @@ use substrate_subxt::{ EventSubscription, EventsDecoder, PairSigner, - SignedOptions, - DEFAULT_ERA_PERIOD, }; #[async_std::main] @@ -46,7 +44,6 @@ async fn main() -> Result<(), Box> { sub.filter_event::>(); client.transfer( &signer, - SignedOptions { era_period: Some(DEFAULT_ERA_PERIOD) }, &dest, 10_000 ).await?; diff --git a/proc-macro/src/call.rs b/proc-macro/src/call.rs index 1cea39521a..cedf993fe0 100644 --- a/proc-macro/src/call.rs +++ b/proc-macro/src/call.rs @@ -64,7 +64,6 @@ pub fn call(s: Structure) -> TokenStream { fn #call<'a>( &'a self, signer: &'a (dyn #subxt::Signer + Send + Sync), - opts: #subxt::SignedOptions, #args ) -> core::pin::Pin> + Send + 'a>>; @@ -72,7 +71,6 @@ pub fn call(s: Structure) -> TokenStream { fn #call_and_watch<'a>( &'a self, signer: &'a (dyn #subxt::Signer + Send + Sync), - opts: #subxt::SignedOptions, #args ) -> core::pin::Pin, #subxt::Error>> + Send + 'a>>; } @@ -84,21 +82,19 @@ pub fn call(s: Structure) -> TokenStream { fn #call<'a>( &'a self, signer: &'a (dyn #subxt::Signer + Send + Sync), - opts: #subxt::SignedOptions, #args ) -> core::pin::Pin> + Send + 'a>> { let #marker = core::marker::PhantomData::; - Box::pin(self.submit(#build_struct, signer, opts)) + Box::pin(self.submit(#build_struct, signer)) } fn #call_and_watch<'a>( &'a self, signer: &'a (dyn #subxt::Signer + Send + Sync), - opts: #subxt::SignedOptions, #args ) -> core::pin::Pin, #subxt::Error>> + Send + 'a>> { let #marker = core::marker::PhantomData::; - Box::pin(self.watch(#build_struct, signer, opts)) + Box::pin(self.watch(#build_struct, signer)) } } } diff --git a/src/extrinsic/extra.rs b/src/extrinsic/extra.rs index 68180b5848..66709f8416 100644 --- a/src/extrinsic/extra.rs +++ b/src/extrinsic/extra.rs @@ -237,7 +237,7 @@ pub trait SignedExtra: SignedExtension { tx_version: u32, nonce: T::Index, genesis_hash: T::Hash, - era_info: (Era, T::Hash) + era_info: (Era, Option) ) -> Self; /// Returns the transaction extra. @@ -252,7 +252,7 @@ pub struct DefaultExtra { nonce: T::Index, genesis_hash: T::Hash, // Era and either the genesis_hash if immortal or the current hash if mortal - era_info: (Era, T::Hash) + era_info: (Era, Option) } impl SignedExtra @@ -273,7 +273,7 @@ impl SignedExtra tx_version: u32, nonce: T::Index, genesis_hash: T::Hash, - era_info: (Era, T::Hash) + era_info: (Era, Option) ) -> Self { DefaultExtra { spec_version, @@ -285,11 +285,16 @@ impl SignedExtra } fn extra(&self) -> Self::Extra { + let era_hash = if let Some(hash) = self.era_info.1 { + hash + } else { + self.genesis_hash + }; ( CheckSpecVersion(PhantomData, self.spec_version), CheckTxVersion(PhantomData, self.tx_version), CheckGenesis(PhantomData, self.genesis_hash), - CheckEra((self.era_info.0, PhantomData), self.era_info.1), + CheckEra((self.era_info.0, PhantomData), era_hash), CheckNonce(self.nonce), CheckWeight(PhantomData), ChargeTransactionPayment(::Balance::default()), diff --git a/src/extrinsic/mod.rs b/src/extrinsic/mod.rs index abb39f829d..8292226b6b 100644 --- a/src/extrinsic/mod.rs +++ b/src/extrinsic/mod.rs @@ -44,8 +44,8 @@ use crate::{ Error, }; -/// A reasonable default for `era_period` -pub const DEFAULT_ERA_PERIOD: u64 = 64; +/// A reasonable default for `mortal_period` +pub const DEFAULT_MORTAL_PERIOD: u64 = 64; /// UncheckedExtrinsic type. pub type UncheckedExtrinsic = sp_runtime::generic::UncheckedExtrinsic< @@ -65,7 +65,7 @@ pub async fn create_signed( nonce: T::Index, call: Encoded, signer: &(dyn Signer + Send + Sync), - era_info: (Era, T::Hash), + era_info: (Era, Option), ) -> Result, Error> where T: Runtime, diff --git a/src/lib.rs b/src/lib.rs index c7b384246a..19a78e91a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,7 @@ pub use crate::{ SignedExtra, Signer, UncheckedExtrinsic, - DEFAULT_ERA_PERIOD + DEFAULT_MORTAL_PERIOD }, frame::*, metadata::{ @@ -184,10 +184,26 @@ impl ClientBuilder { runtime_version: runtime_version?, _marker: PhantomData, page_size: self.page_size.unwrap_or(10), + signed_options: ClientSignedOptions { + mortal_period: Some(DEFAULT_MORTAL_PERIOD), + } }) } } +/// Construction options for a signed extrinsic +#[derive(Copy, Clone)] +struct ClientSignedOptions { + /// The period, measured in blocks, that transaction will live for, starting from a checkpoint + /// block. + /// + /// Logical encoding rules: + /// `mortal_period == None`: immortal transaction + /// `0 <= mortal_period <= 65536`: rounded up to the closest power of 2, starting at 4 + /// `65536 < mortal_period`: Min(65536, mortal_period) + pub mortal_period: Option, +} + /// Client to interface with a substrate node. pub struct Client { rpc: Rpc, @@ -197,18 +213,7 @@ pub struct Client { runtime_version: RuntimeVersion, _marker: PhantomData<(fn() -> T::Signature, T::Extra)>, page_size: u32, -} - -/// Construction options for a signed extrinsic -#[derive(Copy, Clone, Debug, PartialEq)] -pub struct SignedOptions { - /// The period, measured in blocks, that transaction will live for, starting from a checkpoint - /// block. A good default is 64 (64 * 6secs = 6min 40sec). - /// - /// `era_period == None`: immortal transaction. - /// `0 <= era_period <= 65536`: rounded up to the closest power of 2, starting at 4. - /// `65536 < era_period`: 65536. - pub era_period: Option, + signed_options: ClientSignedOptions, } impl Clone for Client { @@ -221,6 +226,7 @@ impl Clone for Client { runtime_version: self.runtime_version.clone(), _marker: PhantomData, page_size: self.page_size, + signed_options: self.signed_options.clone(), } } } @@ -287,6 +293,17 @@ impl Client { &self.properties } + /// Returns the current mortal_period configuration + pub fn mortal_period(&self) -> &Option { + &self.signed_options.mortal_period + } + + /// Set the mortal period for signed extrinsics + pub fn set_mortal_period(mut self, mortal_period: Option) -> Self { + self.signed_options.mortal_period = mortal_period; + self + } + /// Fetch the value under an unhashed storage key pub async fn fetch_unhashed( &self, @@ -461,8 +478,7 @@ impl Client { pub async fn create_signed + Send + Sync>( &self, call: C, - signer: &(dyn Signer + Send + Sync), - opts: SignedOptions, + signer: &(dyn Signer + Send + Sync) ) -> Result, Error> where <>::Extra as SignedExtension>::AdditionalSigned: @@ -474,7 +490,7 @@ impl Client { self.account(signer.account_id(), None).await?.nonce }; let call = self.encode(call)?; - let era_opts = if let Some(era_period) = opts.era_period { + let era_info = if let Some(mortal_period) = self.signed_options.mortal_period { let current_block = match self.block(None::).await? { Some(signed_block) => signed_block.block, None => return Err("RPC chain_getBlock returned None when Some(signed_block) was expected".into()), @@ -482,9 +498,9 @@ impl Client { let current_number = (*current_block.header().number()).saturated_into::(); let current_hash = current_block.hash(); - (Era::mortal(era_period, current_number), current_hash) + (Era::mortal(mortal_period, current_number), Some(current_hash)) } else { - (Era::Immortal, self.genesis_hash) + (Era::Immortal, None) }; let signed = extrinsic::create_signed( &self.runtime_version, @@ -492,7 +508,7 @@ impl Client { account_nonce, call, signer, - era_opts, + era_info, ) .await?; Ok(signed) @@ -530,13 +546,12 @@ impl Client { &self, call: C, signer: &(dyn Signer + Send + Sync), - opts: SignedOptions, ) -> Result where <>::Extra as SignedExtension>::AdditionalSigned: Send + Sync, { - let extrinsic = self.create_signed(call, signer, opts).await?; + let extrinsic = self.create_signed(call, signer).await?; self.submit_extrinsic(extrinsic).await } @@ -545,13 +560,12 @@ impl Client { &self, call: C, signer: &(dyn Signer + Send + Sync), - opts: SignedOptions, ) -> Result, Error> where <>::Extra as SignedExtension>::AdditionalSigned: Send + Sync, { - let extrinsic = self.create_signed(call, signer, opts).await?; + let extrinsic = self.create_signed(call, signer).await?; let decoder = self.events_decoder::(); self.submit_and_watch_extrinsic(extrinsic, decoder).await }