Skip to content

Commit

Permalink
- Move Retractec from UnexpectedTxStatus to XtStatus
Browse files Browse the repository at this point in the history
- Provide is_final method for TransactionStatus
- Return from watch method as soon as a final state is reached
- Provide separate method to populate events for an ExtrinsicReport
  • Loading branch information
Niederb committed Oct 8, 2024
1 parent ae15187 commit 9db07c2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
67 changes: 38 additions & 29 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub enum XtStatus {
Ready = 1,
Broadcast = 2,
InBlock = 3,
Retracted = 4,
Finalized = 6,
}

Expand All @@ -78,7 +79,6 @@ pub enum XtStatus {
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum UnexpectedTxStatus {
Future,
Retracted,
FinalityTimeout,
Usurped,
Dropped,
Expand Down Expand Up @@ -119,36 +119,32 @@ pub enum TransactionStatus<Hash: Encode + Decode, BlockHash: Encode + Decode> {
impl<Hash: Encode + Decode, BlockHash: Encode + Decode> TransactionStatus<Hash, BlockHash> {
pub fn as_u8(&self) -> u8 {
match self {
TransactionStatus::Future => 0,
TransactionStatus::Ready => 1,
TransactionStatus::Broadcast(_) => 2,
TransactionStatus::InBlock(_) => 3,
TransactionStatus::Retracted(_) => 4,
TransactionStatus::FinalityTimeout(_) => 5,
TransactionStatus::Finalized(_) => 6,
TransactionStatus::Usurped(_) => 7,
TransactionStatus::Dropped => 8,
TransactionStatus::Invalid => 9,
Self::Future => 0,
Self::Ready => 1,
Self::Broadcast(_) => 2,
Self::InBlock(_) => 3,
Self::Retracted(_) => 4,
Self::FinalityTimeout(_) => 5,
Self::Finalized(_) => 6,
Self::Usurped(_) => 7,
Self::Dropped => 8,
Self::Invalid => 9,
}
}

pub fn is_expected(&self) -> Result<()> {
match self {
TransactionStatus::Ready
| TransactionStatus::Broadcast(_)
| TransactionStatus::InBlock(_)
| TransactionStatus::Finalized(_) => Ok(()),
TransactionStatus::Future => Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Future)),
TransactionStatus::Retracted(_) =>
Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Retracted)),
TransactionStatus::FinalityTimeout(_) =>
Self::Ready
| Self::Broadcast(_)
| Self::InBlock(_)
| Self::Retracted(_)
| Self::Finalized(_) => Ok(()),
Self::Future => Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Future)),
Self::FinalityTimeout(_) =>
Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::FinalityTimeout)),
TransactionStatus::Usurped(_) =>
Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Usurped)),
TransactionStatus::Dropped =>
Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Dropped)),
TransactionStatus::Invalid =>
Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Invalid)),
Self::Usurped(_) => Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Usurped)),
Self::Dropped => Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Dropped)),
Self::Invalid => Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Invalid)),
}
}

Expand All @@ -160,13 +156,26 @@ impl<Hash: Encode + Decode, BlockHash: Encode + Decode> TransactionStatus<Hash,

pub fn get_maybe_block_hash(&self) -> Option<&BlockHash> {
match self {
TransactionStatus::InBlock(block_hash) => Some(block_hash),
TransactionStatus::Retracted(block_hash) => Some(block_hash),
TransactionStatus::FinalityTimeout(block_hash) => Some(block_hash),
TransactionStatus::Finalized(block_hash) => Some(block_hash),
Self::InBlock(block_hash) => Some(block_hash),
Self::Retracted(block_hash) => Some(block_hash),
Self::FinalityTimeout(block_hash) => Some(block_hash),
Self::Finalized(block_hash) => Some(block_hash),
_ => None,
}
}

/// Returns true if the Transaction reached its final Status
// See https://github.com/paritytech/polkadot-sdk/blob/289f5bbf7a45dc0380904a435464b15ec711ed03/substrate/client/transaction-pool/api/src/lib.rs#L161
pub fn is_final(&self) -> bool {
matches!(
self,
Self::Usurped(_)
| Self::Finalized(_)
| Self::FinalityTimeout(_)
| Self::Invalid
| Self::Dropped
)
}
}

// Exact structure from
Expand Down
22 changes: 19 additions & 3 deletions src/api/rpc_api/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
api::{rpc_api::events::FetchEvents, Error, Result},
error::FailedExtrinsicError,
rpc::{HandleSubscription, Request, Subscribe},
Api, ExtrinsicReport, TransactionStatus, XtStatus,
Api, ExtrinsicReport, TransactionStatus, UnexpectedTxStatus, XtStatus,
};
use ac_compose_macros::rpc_params;
use ac_primitives::{config::Config, UncheckedExtrinsicV4};
Expand Down Expand Up @@ -211,6 +211,12 @@ pub trait SubmitAndWatch {
encoded_extrinsic: &Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Self::Hash>>;

/// Query the events for the specified `report` and attach them.
async fn populate_events(
&self,
report: ExtrinsicReport<Self::Hash>,
) -> Result<ExtrinsicReport<Self::Hash>>;
}

#[maybe_async::maybe_async(?Send)]
Expand Down Expand Up @@ -269,18 +275,25 @@ where
encoded_extrinsic: &Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Self::Hash>> {
let mut report = self
let report = self
.submit_and_watch_opaque_extrinsic_until_without_events(encoded_extrinsic, watch_until)
.await?;

if watch_until < XtStatus::InBlock {
return Ok(report)
}
self.populate_events(report).await
}

async fn populate_events(
&self,
mut report: ExtrinsicReport<Self::Hash>,
) -> Result<ExtrinsicReport<Self::Hash>> {
let block_hash = report.block_hash.ok_or(Error::BlockHashNotFound)?;
let extrinsic_events =
self.fetch_events_for_extrinsic(block_hash, report.extrinsic_hash).await?;

// Check if the extrinsic was succesfull or not.
// Check if the extrinsic was successful or not.
let mut maybe_dispatch_error = None;
for event in &extrinsic_events {
if let Some(dispatch_error) = event.get_associated_dispatch_error() {
Expand Down Expand Up @@ -352,6 +365,9 @@ where
return Err(e)
},
}
if transaction_status.is_final() {
return Err(Error::UnexpectedTxStatus(UnexpectedTxStatus::Usurped))
}
}
Err(Error::NoStream)
}
Expand Down

0 comments on commit 9db07c2

Please sign in to comment.