mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 11:51:12 +00:00
Update to set mortal_period in client
This commit is contained in:
@@ -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<dyn std::error::Error>> {
|
||||
let client = ClientBuilder::<KusamaRuntime>::new().build().await?;
|
||||
let hash = client.transfer(
|
||||
&signer,
|
||||
SignedOptions { era_period: Some(DEFAULT_ERA_PERIOD) },
|
||||
&dest,
|
||||
10_000
|
||||
).await?;
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
let client = ClientBuilder::<DefaultNodeRuntime>::new().build().await?;
|
||||
let result = client.transfer_and_watch(
|
||||
&signer,
|
||||
SignedOptions { era_period: Some(DEFAULT_ERA_PERIOD) },
|
||||
&dest,
|
||||
10_000
|
||||
).await?;
|
||||
|
||||
@@ -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<dyn std::error::Error>> {
|
||||
sub.filter_event::<TransferEvent<_>>();
|
||||
client.transfer(
|
||||
&signer,
|
||||
SignedOptions { era_period: Some(DEFAULT_ERA_PERIOD) },
|
||||
&dest,
|
||||
10_000
|
||||
).await?;
|
||||
|
||||
@@ -64,7 +64,6 @@ pub fn call(s: Structure) -> TokenStream {
|
||||
fn #call<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
opts: #subxt::SignedOptions,
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, #subxt::Error>> + Send + 'a>>;
|
||||
|
||||
@@ -72,7 +71,6 @@ pub fn call(s: Structure) -> TokenStream {
|
||||
fn #call_and_watch<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
opts: #subxt::SignedOptions,
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>> + Send + 'a>>;
|
||||
}
|
||||
@@ -84,21 +82,19 @@ pub fn call(s: Structure) -> TokenStream {
|
||||
fn #call<'a>(
|
||||
&'a self,
|
||||
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
|
||||
opts: #subxt::SignedOptions,
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, #subxt::Error>> + Send + 'a>> {
|
||||
let #marker = core::marker::PhantomData::<T>;
|
||||
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<T> + Send + Sync),
|
||||
opts: #subxt::SignedOptions,
|
||||
#args
|
||||
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>> + Send + 'a>> {
|
||||
let #marker = core::marker::PhantomData::<T>;
|
||||
Box::pin(self.watch(#build_struct, signer, opts))
|
||||
Box::pin(self.watch(#build_struct, signer))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ pub trait SignedExtra<T: System>: SignedExtension {
|
||||
tx_version: u32,
|
||||
nonce: T::Index,
|
||||
genesis_hash: T::Hash,
|
||||
era_info: (Era, T::Hash)
|
||||
era_info: (Era, Option<T::Hash>)
|
||||
) -> Self;
|
||||
|
||||
/// Returns the transaction extra.
|
||||
@@ -252,7 +252,7 @@ pub struct DefaultExtra<T: System> {
|
||||
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<T::Hash>)
|
||||
}
|
||||
|
||||
impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtra<T>
|
||||
@@ -273,7 +273,7 @@ impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtra<T>
|
||||
tx_version: u32,
|
||||
nonce: T::Index,
|
||||
genesis_hash: T::Hash,
|
||||
era_info: (Era, T::Hash)
|
||||
era_info: (Era, Option<T::Hash>)
|
||||
) -> Self {
|
||||
DefaultExtra {
|
||||
spec_version,
|
||||
@@ -285,11 +285,16 @@ impl<T: System + Balances + Clone + Debug + Eq + Send + Sync> SignedExtra<T>
|
||||
}
|
||||
|
||||
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(<T as Balances>::Balance::default()),
|
||||
|
||||
@@ -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<T> = sp_runtime::generic::UncheckedExtrinsic<
|
||||
@@ -65,7 +65,7 @@ pub async fn create_signed<T>(
|
||||
nonce: T::Index,
|
||||
call: Encoded,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
era_info: (Era, T::Hash),
|
||||
era_info: (Era, Option<T::Hash>),
|
||||
) -> Result<UncheckedExtrinsic<T>, Error>
|
||||
where
|
||||
T: Runtime,
|
||||
|
||||
+37
-23
@@ -89,7 +89,7 @@ pub use crate::{
|
||||
SignedExtra,
|
||||
Signer,
|
||||
UncheckedExtrinsic,
|
||||
DEFAULT_ERA_PERIOD
|
||||
DEFAULT_MORTAL_PERIOD
|
||||
},
|
||||
frame::*,
|
||||
metadata::{
|
||||
@@ -184,10 +184,26 @@ impl<T: Runtime> ClientBuilder<T> {
|
||||
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<u64>,
|
||||
}
|
||||
|
||||
/// Client to interface with a substrate node.
|
||||
pub struct Client<T: Runtime> {
|
||||
rpc: Rpc<T>,
|
||||
@@ -197,18 +213,7 @@ pub struct Client<T: Runtime> {
|
||||
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<u64>,
|
||||
signed_options: ClientSignedOptions,
|
||||
}
|
||||
|
||||
impl<T: Runtime> Clone for Client<T> {
|
||||
@@ -221,6 +226,7 @@ impl<T: Runtime> Clone for Client<T> {
|
||||
runtime_version: self.runtime_version.clone(),
|
||||
_marker: PhantomData,
|
||||
page_size: self.page_size,
|
||||
signed_options: self.signed_options.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,6 +293,17 @@ impl<T: Runtime> Client<T> {
|
||||
&self.properties
|
||||
}
|
||||
|
||||
/// Returns the current mortal_period configuration
|
||||
pub fn mortal_period(&self) -> &Option<u64> {
|
||||
&self.signed_options.mortal_period
|
||||
}
|
||||
|
||||
/// Set the mortal period for signed extrinsics
|
||||
pub fn set_mortal_period(mut self, mortal_period: Option<u64>) -> Self {
|
||||
self.signed_options.mortal_period = mortal_period;
|
||||
self
|
||||
}
|
||||
|
||||
/// Fetch the value under an unhashed storage key
|
||||
pub async fn fetch_unhashed<V: Decode>(
|
||||
&self,
|
||||
@@ -461,8 +478,7 @@ impl<T: Runtime> Client<T> {
|
||||
pub async fn create_signed<C: Call<T> + Send + Sync>(
|
||||
&self,
|
||||
call: C,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
opts: SignedOptions,
|
||||
signer: &(dyn Signer<T> + Send + Sync)
|
||||
) -> Result<UncheckedExtrinsic<T>, Error>
|
||||
where
|
||||
<<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned:
|
||||
@@ -474,7 +490,7 @@ impl<T: Runtime> Client<T> {
|
||||
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::<T::Hash>).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<T: Runtime> Client<T> {
|
||||
let current_number = (*current_block.header().number()).saturated_into::<u64>();
|
||||
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<T: Runtime> Client<T> {
|
||||
account_nonce,
|
||||
call,
|
||||
signer,
|
||||
era_opts,
|
||||
era_info,
|
||||
)
|
||||
.await?;
|
||||
Ok(signed)
|
||||
@@ -530,13 +546,12 @@ impl<T: Runtime> Client<T> {
|
||||
&self,
|
||||
call: C,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
opts: SignedOptions,
|
||||
) -> Result<T::Hash, Error>
|
||||
where
|
||||
<<T::Extra as SignedExtra<T>>::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<T: Runtime> Client<T> {
|
||||
&self,
|
||||
call: C,
|
||||
signer: &(dyn Signer<T> + Send + Sync),
|
||||
opts: SignedOptions,
|
||||
) -> Result<ExtrinsicSuccess<T>, Error>
|
||||
where
|
||||
<<T::Extra as SignedExtra<T>>::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::<C>();
|
||||
self.submit_and_watch_extrinsic(extrinsic, decoder).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user