refactor transaction sign scheme (#1621)

This commit is contained in:
Svyatoslav Nikolsky
2022-11-02 14:36:48 +03:00
committed by Bastian Köcher
parent 01538bc5fa
commit a979340e49
36 changed files with 188 additions and 269 deletions
+7 -9
View File
@@ -185,12 +185,10 @@ impl<C: Chain> UnsignedTransaction<C> {
}
/// Account key pair used by transactions signing scheme.
pub type AccountKeyPairOf<S> = <S as TransactionSignScheme>::AccountKeyPair;
pub type AccountKeyPairOf<S> = <S as ChainWithTransactions>::AccountKeyPair;
/// Substrate-based chain transactions signing scheme.
pub trait TransactionSignScheme: 'static {
/// Chain that this scheme is to be used.
type Chain: Chain;
pub trait ChainWithTransactions: Chain {
/// Type of key pairs used to sign transactions.
type AccountKeyPair: Pair;
/// Signed transaction.
@@ -199,7 +197,7 @@ pub trait TransactionSignScheme: 'static {
/// Create transaction for given runtime call, signed by given account.
fn sign_transaction(
param: SignParam<Self>,
unsigned: UnsignedTransaction<Self::Chain>,
unsigned: UnsignedTransaction<Self>,
) -> Result<Self::SignedTransaction, crate::Error>
where
Self: Sized;
@@ -213,19 +211,19 @@ pub trait TransactionSignScheme: 'static {
/// Parse signed transaction into its unsigned part.
///
/// Returns `None` if signed transaction has unsupported format.
fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self::Chain>>;
fn parse_transaction(tx: Self::SignedTransaction) -> Option<UnsignedTransaction<Self>>;
}
/// Sign transaction parameters
pub struct SignParam<T: TransactionSignScheme> {
pub struct SignParam<C: ChainWithTransactions> {
/// Version of the runtime specification.
pub spec_version: u32,
/// Transaction version
pub transaction_version: u32,
/// Hash of the genesis block.
pub genesis_hash: <T::Chain as ChainBase>::Hash,
pub genesis_hash: HashOf<C>,
/// Signer account
pub signer: T::AccountKeyPair,
pub signer: AccountKeyPairOf<C>,
}
impl<Block: BlockT> BlockWithJustification<Block::Header> for SignedBlock<Block> {
+16 -12
View File
@@ -17,14 +17,14 @@
//! Substrate node client.
use crate::{
chain::{Chain, ChainWithBalances},
chain::{Chain, ChainWithBalances, ChainWithTransactions},
rpc::{
SubstrateAuthorClient, SubstrateChainClient, SubstrateFinalityClient,
SubstrateFrameSystemClient, SubstrateStateClient, SubstrateSystemClient,
SubstrateTransactionPaymentClient,
},
transaction_stall_timeout, ConnectionParams, Error, HashOf, HeaderIdOf, Result, SignParam,
TransactionSignScheme, TransactionTracker, UnsignedTransaction,
TransactionTracker, UnsignedTransaction,
};
use async_std::sync::{Arc, Mutex};
@@ -421,14 +421,17 @@ impl<C: Chain> Client<C> {
/// if all client instances are clones of the same initial `Client`.
///
/// Note: The given transaction needs to be SCALE encoded beforehand.
pub async fn submit_signed_extrinsic<S: TransactionSignScheme<Chain = C> + 'static>(
pub async fn submit_signed_extrinsic(
&self,
extrinsic_signer: C::AccountId,
signing_data: SignParam<S>,
signing_data: SignParam<C>,
prepare_extrinsic: impl FnOnce(HeaderIdOf<C>, C::Index) -> Result<UnsignedTransaction<C>>
+ Send
+ 'static,
) -> Result<C::Hash> {
) -> Result<C::Hash>
where
C: ChainWithTransactions,
{
let _guard = self.submit_signed_extrinsic_lock.lock().await;
let transaction_nonce = self.next_account_index(extrinsic_signer).await?;
let best_header = self.best_header().await?;
@@ -442,7 +445,7 @@ impl<C: Chain> Client<C> {
self.jsonrpsee_execute(move |client| async move {
let extrinsic = prepare_extrinsic(best_header_id, transaction_nonce)?;
let signed_extrinsic = S::sign_transaction(signing_data, extrinsic)?.encode();
let signed_extrinsic = C::sign_transaction(signing_data, extrinsic)?.encode();
let tx_hash =
SubstrateAuthorClient::<C>::submit_extrinsic(&*client, Bytes(signed_extrinsic))
.await
@@ -458,16 +461,17 @@ impl<C: Chain> Client<C> {
/// Does exactly the same as `submit_signed_extrinsic`, but keeps watching for extrinsic status
/// after submission.
pub async fn submit_and_watch_signed_extrinsic<
S: TransactionSignScheme<Chain = C> + 'static,
>(
pub async fn submit_and_watch_signed_extrinsic(
&self,
extrinsic_signer: C::AccountId,
signing_data: SignParam<S>,
signing_data: SignParam<C>,
prepare_extrinsic: impl FnOnce(HeaderIdOf<C>, C::Index) -> Result<UnsignedTransaction<C>>
+ Send
+ 'static,
) -> Result<TransactionTracker<C, Self>> {
) -> Result<TransactionTracker<C, Self>>
where
C: ChainWithTransactions,
{
let self_clone = self.clone();
let _guard = self.submit_signed_extrinsic_lock.lock().await;
let transaction_nonce = self.next_account_index(extrinsic_signer).await?;
@@ -482,7 +486,7 @@ impl<C: Chain> Client<C> {
C::AVERAGE_BLOCK_INTERVAL,
STALL_TIMEOUT,
);
let signed_extrinsic = S::sign_transaction(signing_data, extrinsic)?.encode();
let signed_extrinsic = C::sign_transaction(signing_data, extrinsic)?.encode();
let tx_hash = C::Hasher::hash(&signed_extrinsic);
let subscription = SubstrateAuthorClient::<C>::submit_and_watch_extrinsic(
&*client,
+1 -1
View File
@@ -34,7 +34,7 @@ use std::time::Duration;
pub use crate::{
chain::{
AccountKeyPairOf, BlockWithJustification, CallOf, Chain, ChainWithBalances,
ChainWithGrandpa, ChainWithMessages, RelayChain, SignParam, TransactionSignScheme,
ChainWithGrandpa, ChainWithMessages, ChainWithTransactions, RelayChain, SignParam,
TransactionStatusOf, UnsignedTransaction, WeightToFeeOf,
},
client::{ChainRuntimeVersion, Client, OpaqueGrandpaAuthoritiesSet, Subscription},