Skip to content

Commit

Permalink
fix: invalid transactions after reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
th7nder committed Dec 19, 2024
1 parent a611d50 commit b799819
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 13 additions & 2 deletions storage-provider/client/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! this module covers that, furthermore, the client it provides
//! supports both WebSockets and HTTP.
use std::time::Duration;

use jsonrpsee::{
core::{
client::{BatchResponse, ClientT, Subscription, SubscriptionClientT},
Expand All @@ -21,14 +23,23 @@ pub enum PolkaStorageRpcClient {
Https(jsonrpsee::http_client::HttpClient),
}

/// RPC commands which submit an extrinsic wait for finalization.
/// Finalization takes ~60secs (the default request timeout), however when reorg happens, it's two times that.
const REQUEST_TIMEOUT: Duration = Duration::from_secs(120);

impl PolkaStorageRpcClient {
pub async fn new(url: &Url) -> Result<Self, jsonrpsee::core::ClientError> {
match url.scheme() {
"ws" | "wss" => Ok(PolkaStorageRpcClient::Ws(
WsClientBuilder::new().build(url).await?,
WsClientBuilder::new()
.request_timeout(REQUEST_TIMEOUT)
.build(url)
.await?,
)),
"http" | "https" => Ok(PolkaStorageRpcClient::Https(
HttpClientBuilder::new().build(url)?,
HttpClientBuilder::new()
.request_timeout(REQUEST_TIMEOUT)
.build(url)?,
)),
scheme => Err(jsonrpsee::core::ClientError::Custom(format!(
"unsupported url scheme: {}",
Expand Down
15 changes: 12 additions & 3 deletions storagext/lib/src/runtime/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,26 @@ impl Client {
Call: subxt::tx::Payload,
Keypair: subxt::tx::Signer<PolkaStorageConfig>,
{
let finalized_block_hash = self.legacy_rpc.chain_get_finalized_head().await?;
let finalized_block = self
.legacy_rpc
.chain_get_block(Some(finalized_block_hash))
.await?
.expect("chain to have info about the finalized head");

let current_nonce = self
.legacy_rpc
.system_account_next_index(&account_keypair.account_id())
.await?;
let current_header = self.legacy_rpc.chain_get_header(None).await?.unwrap();
// Default used by subxt
// https://github.com/paritytech/subxt/blob/f363f77a60271b840e8dfb4f6e2f0f728f5ced06/core/src/config/signed_extensions.rs#L231
const TX_VALID_FOR: u64 = 32;
let ext_params = DefaultExtrinsicParamsBuilder::new()
.mortal(&current_header, 8)
.mortal(&finalized_block.block.header, TX_VALID_FOR)
.nonce(current_nonce)
.build();

let ext = self
let ext: SubmittableExtrinsic<PolkaStorageConfig, OnlineClient<PolkaStorageConfig>> = self
.client
.tx()
.create_signed_offline(call, account_keypair, ext_params)?;
Expand Down

0 comments on commit b799819

Please sign in to comment.