mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 21:01:02 +00:00
Transactions resubmitter (#1083)
* resubmit transactions: start * resubmit transactions: continue * enable resubmitter in deployments * clippy * spellcheck * Update relays/client-substrate/src/chain.rs Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * fix compilation * fix compilation Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
committed by
Bastian Köcher
parent
d59d442e93
commit
1df7076c4f
@@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use bp_runtime::Chain as ChainBase;
|
||||
use bp_runtime::{Chain as ChainBase, TransactionEraOf};
|
||||
use codec::{Codec, Encode};
|
||||
use frame_support::{weights::WeightToFeePolynomial, Parameter};
|
||||
use jsonrpsee_ws_client::{DeserializeOwned, Serialize};
|
||||
use num_traits::{Bounded, CheckedSub, SaturatingAdd, Zero};
|
||||
@@ -58,7 +59,7 @@ pub trait Chain: ChainBase + Clone {
|
||||
/// Block type.
|
||||
type SignedBlock: Member + Serialize + DeserializeOwned + BlockWithJustification<Self::Header>;
|
||||
/// The aggregated `Call` type.
|
||||
type Call: Dispatchable + Debug;
|
||||
type Call: Clone + Dispatchable + Debug;
|
||||
/// Balance of an account in native tokens.
|
||||
///
|
||||
/// The chain may support multiple tokens, but this particular type is for token that is used
|
||||
@@ -96,14 +97,47 @@ pub trait ChainWithBalances: Chain {
|
||||
fn account_info_storage_key(account_id: &Self::AccountId) -> StorageKey;
|
||||
}
|
||||
|
||||
/// SCALE-encoded extrinsic.
|
||||
pub type EncodedExtrinsic = Vec<u8>;
|
||||
|
||||
/// Block with justification.
|
||||
pub trait BlockWithJustification<Header> {
|
||||
/// Return block header.
|
||||
fn header(&self) -> Header;
|
||||
/// Return encoded block extrinsics.
|
||||
fn extrinsics(&self) -> Vec<EncodedExtrinsic>;
|
||||
/// Return block justification, if known.
|
||||
fn justification(&self) -> Option<&EncodedJustification>;
|
||||
}
|
||||
|
||||
/// Transaction before it is signed.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UnsignedTransaction<C: Chain> {
|
||||
/// Runtime call of this transaction.
|
||||
pub call: C::Call,
|
||||
/// Transaction nonce.
|
||||
pub nonce: C::Index,
|
||||
/// Tip included into transaction.
|
||||
pub tip: C::Balance,
|
||||
}
|
||||
|
||||
impl<C: Chain> UnsignedTransaction<C> {
|
||||
/// Create new unsigned transaction with given call, nonce and zero tip.
|
||||
pub fn new(call: C::Call, nonce: C::Index) -> Self {
|
||||
Self {
|
||||
call,
|
||||
nonce,
|
||||
tip: Zero::zero(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set transaction tip.
|
||||
pub fn tip(mut self, tip: C::Balance) -> Self {
|
||||
self.tip = tip;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Substrate-based chain transactions signing scheme.
|
||||
pub trait TransactionSignScheme {
|
||||
/// Chain that this scheme is to be used.
|
||||
@@ -111,16 +145,26 @@ pub trait TransactionSignScheme {
|
||||
/// Type of key pairs used to sign transactions.
|
||||
type AccountKeyPair: Pair;
|
||||
/// Signed transaction.
|
||||
type SignedTransaction;
|
||||
type SignedTransaction: Clone + Debug + Codec + Send + 'static;
|
||||
|
||||
/// Create transaction for given runtime call, signed by given account.
|
||||
fn sign_transaction(
|
||||
genesis_hash: <Self::Chain as ChainBase>::Hash,
|
||||
signer: &Self::AccountKeyPair,
|
||||
era: bp_runtime::TransactionEraOf<Self::Chain>,
|
||||
signer_nonce: <Self::Chain as Chain>::Index,
|
||||
call: <Self::Chain as Chain>::Call,
|
||||
era: TransactionEraOf<Self::Chain>,
|
||||
unsigned: UnsignedTransaction<Self::Chain>,
|
||||
) -> Self::SignedTransaction;
|
||||
|
||||
/// Returns true if transaction is signed.
|
||||
fn is_signed(tx: &Self::SignedTransaction) -> bool;
|
||||
|
||||
/// Returns true if transaction is signed by given signer.
|
||||
fn is_signed_by(signer: &Self::AccountKeyPair, tx: &Self::SignedTransaction) -> bool;
|
||||
|
||||
/// 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>>;
|
||||
}
|
||||
|
||||
impl<Block: BlockT> BlockWithJustification<Block::Header> for SignedBlock<Block> {
|
||||
@@ -128,6 +172,10 @@ impl<Block: BlockT> BlockWithJustification<Block::Header> for SignedBlock<Block>
|
||||
self.block.header().clone()
|
||||
}
|
||||
|
||||
fn extrinsics(&self) -> Vec<EncodedExtrinsic> {
|
||||
self.block.extrinsics().iter().map(Encode::encode).collect()
|
||||
}
|
||||
|
||||
fn justification(&self) -> Option<&EncodedJustification> {
|
||||
self.justifications
|
||||
.as_ref()
|
||||
|
||||
@@ -21,7 +21,8 @@ use crate::rpc::Substrate;
|
||||
use crate::{ConnectionParams, Error, HeaderIdOf, Result};
|
||||
|
||||
use async_std::sync::{Arc, Mutex};
|
||||
use codec::Decode;
|
||||
use async_trait::async_trait;
|
||||
use codec::{Decode, Encode};
|
||||
use frame_system::AccountInfo;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use jsonrpsee_ws_client::{traits::SubscriptionClient, v2::params::JsonRpcParams, DeserializeOwned};
|
||||
@@ -31,12 +32,16 @@ use pallet_balances::AccountData;
|
||||
use pallet_transaction_payment::InclusionFee;
|
||||
use relay_utils::{relay_loop::RECONNECT_DELAY, HeaderId};
|
||||
use sp_core::{storage::StorageKey, Bytes};
|
||||
use sp_runtime::traits::Header as HeaderT;
|
||||
use sp_runtime::{
|
||||
traits::Header as HeaderT,
|
||||
transaction_validity::{TransactionSource, TransactionValidity},
|
||||
};
|
||||
use sp_trie::StorageProof;
|
||||
use sp_version::RuntimeVersion;
|
||||
use std::{convert::TryFrom, future::Future};
|
||||
|
||||
const SUB_API_GRANDPA_AUTHORITIES: &str = "GrandpaApi_grandpa_authorities";
|
||||
const SUB_API_TXPOOL_VALIDATE_TRANSACTION: &str = "TaggedTransactionQueue_validate_transaction";
|
||||
const MAX_SUBSCRIPTION_CAPACITY: usize = 4096;
|
||||
|
||||
/// Opaque justifications subscription type.
|
||||
@@ -63,6 +68,18 @@ pub struct Client<C: Chain> {
|
||||
submit_signed_extrinsic_lock: Arc<Mutex<()>>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<C: Chain> relay_utils::relay_loop::Client for Client<C> {
|
||||
type Error = Error;
|
||||
|
||||
async fn reconnect(&mut self) -> Result<()> {
|
||||
let (tokio, client) = Self::build_client(self.params.clone()).await?;
|
||||
self.tokio = tokio;
|
||||
self.client = client;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Chain> Clone for Client<C> {
|
||||
fn clone(&self) -> Self {
|
||||
Client {
|
||||
@@ -125,14 +142,6 @@ impl<C: Chain> Client<C> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Reopen client connection.
|
||||
pub async fn reconnect(&mut self) -> Result<()> {
|
||||
let (tokio, client) = Self::build_client(self.params.clone()).await?;
|
||||
self.tokio = tokio;
|
||||
self.client = client;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Build client to use in connection.
|
||||
async fn build_client(params: ConnectionParams) -> Result<(Arc<tokio::runtime::Runtime>, Arc<RpcClient>)> {
|
||||
let tokio = tokio::runtime::Runtime::new()?;
|
||||
@@ -309,6 +318,33 @@ impl<C: Chain> Client<C> {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Returns pending extrinsics from transaction pool.
|
||||
pub async fn pending_extrinsics(&self) -> Result<Vec<Bytes>> {
|
||||
self.jsonrpsee_execute(
|
||||
move |client| async move { Ok(Substrate::<C>::author_pending_extrinsics(&*client).await?) },
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Validate transaction at given block state.
|
||||
pub async fn validate_transaction<SignedTransaction: Encode + Send + 'static>(
|
||||
&self,
|
||||
at_block: C::Hash,
|
||||
transaction: SignedTransaction,
|
||||
) -> Result<TransactionValidity> {
|
||||
self.jsonrpsee_execute(move |client| async move {
|
||||
let call = SUB_API_TXPOOL_VALIDATE_TRANSACTION.to_string();
|
||||
let data = Bytes((TransactionSource::External, transaction, at_block).encode());
|
||||
|
||||
let encoded_response = Substrate::<C>::state_call(&*client, call, data, Some(at_block)).await?;
|
||||
let validity =
|
||||
TransactionValidity::decode(&mut &encoded_response.0[..]).map_err(Error::ResponseParseFailed)?;
|
||||
|
||||
Ok(validity)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Estimate fee that will be spent on given extrinsic.
|
||||
pub async fn estimate_extrinsic_fee(&self, transaction: Bytes) -> Result<InclusionFee<C::Balance>> {
|
||||
self.jsonrpsee_execute(move |client| async move {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
use jsonrpsee_ws_client::Error as RpcError;
|
||||
use relay_utils::MaybeConnectionError;
|
||||
use sc_rpc_api::system::Health;
|
||||
use sp_runtime::transaction_validity::TransactionValidityError;
|
||||
|
||||
/// Result type used by Substrate client.
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -44,6 +45,8 @@ pub enum Error {
|
||||
ClientNotSynced(Health),
|
||||
/// An error has happened when we have tried to parse storage proof.
|
||||
StorageProofError(bp_runtime::StorageProofError),
|
||||
/// The Substrate transaction is invalid.
|
||||
TransactionInvalid(TransactionValidityError),
|
||||
/// Custom logic error.
|
||||
Custom(String),
|
||||
}
|
||||
@@ -59,6 +62,7 @@ impl std::error::Error for Error {
|
||||
Self::MissingMandatoryCodeEntry => None,
|
||||
Self::ClientNotSynced(_) => None,
|
||||
Self::StorageProofError(_) => None,
|
||||
Self::TransactionInvalid(_) => None,
|
||||
Self::Custom(_) => None,
|
||||
}
|
||||
}
|
||||
@@ -82,6 +86,12 @@ impl From<tokio::task::JoinError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TransactionValidityError> for Error {
|
||||
fn from(error: TransactionValidityError) -> Self {
|
||||
Error::TransactionInvalid(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl MaybeConnectionError for Error {
|
||||
fn is_connection_error(&self) -> bool {
|
||||
matches!(
|
||||
@@ -105,6 +115,7 @@ impl std::fmt::Display for Error {
|
||||
Self::MissingMandatoryCodeEntry => "Mandatory :code: entry is missing from runtime storage".into(),
|
||||
Self::StorageProofError(e) => format!("Error when parsing storage proof: {:?}", e),
|
||||
Self::ClientNotSynced(health) => format!("Substrate client is not synced: {}", health),
|
||||
Self::TransactionInvalid(e) => format!("Substrate transaction is invalid: {:?}", e),
|
||||
Self::Custom(e) => e.clone(),
|
||||
};
|
||||
|
||||
|
||||
@@ -32,7 +32,8 @@ pub mod metrics;
|
||||
use std::time::Duration;
|
||||
|
||||
pub use crate::chain::{
|
||||
BalanceOf, BlockWithJustification, Chain, ChainWithBalances, IndexOf, TransactionSignScheme, WeightToFeeOf,
|
||||
BalanceOf, BlockWithJustification, Chain, ChainWithBalances, IndexOf, TransactionSignScheme, UnsignedTransaction,
|
||||
WeightToFeeOf,
|
||||
};
|
||||
pub use crate::client::{Client, JustificationsSubscription, OpaqueGrandpaAuthoritiesSet};
|
||||
pub use crate::error::{Error, Result};
|
||||
|
||||
@@ -43,6 +43,8 @@ jsonrpsee_proc_macros::rpc_client_api! {
|
||||
fn system_account_next_index(account_id: C::AccountId) -> C::Index;
|
||||
#[rpc(method = "author_submitExtrinsic", positional_params)]
|
||||
fn author_submit_extrinsic(extrinsic: Bytes) -> C::Hash;
|
||||
#[rpc(method = "author_pendingExtrinsics", positional_params)]
|
||||
fn author_pending_extrinsics() -> Vec<Bytes>;
|
||||
#[rpc(method = "state_call", positional_params)]
|
||||
fn state_call(method: String, data: Bytes, at_block: Option<C::Hash>) -> Bytes;
|
||||
#[rpc(method = "state_getStorage", positional_params)]
|
||||
|
||||
Reference in New Issue
Block a user