Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
oleonardolima committed Mar 8, 2024
1 parent 41760d9 commit eb59a8b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
4 changes: 3 additions & 1 deletion mutiny-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ pub enum MutinyError {
/// Token already spent.
#[error("Token has been already spent.")]
TokenAlreadySpent,
#[error("Fedimint external note reissuance failed.")]
#[error("Fedimint external note re-issuance failed.")]
FedimintReissueFailed,
#[error("Fedimint external note re-issuance resulted in stale outcome state.")]
FedimintReissueStaleState,
#[error(transparent)]
Other(#[from] anyhow::Error),
}
Expand Down
41 changes: 32 additions & 9 deletions mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
get_payment_info, list_payment_info, persist_payment_info, MutinyStorage, VersionedValue,
},
utils::sleep,
HTLCStatus, MutinyInvoice, DEFAULT_PAYMENT_TIMEOUT, DEFAULT_REISSUE_TIMEOUT,
HTLCStatus, MutinyInvoice, DEFAULT_PAYMENT_TIMEOUT,
};
use async_trait::async_trait;
use bip39::Mnemonic;
Expand Down Expand Up @@ -72,6 +72,12 @@ use std::{
sync::atomic::{AtomicU32, Ordering},
};

// The amount of time in milliseconds to wait for
// checking the status of a fedimint OOB re-issuance. This
// is to work around their stream status checking
// when wanting just the current status.
const FEDIMINT_OOB_REISSUE_TIMEOUT_CHECK_MS: u64 = 30;

// The amount of time in milliseconds to wait for
// checking the status of a fedimint payment. This
// is to work around their stream status checking
Expand Down Expand Up @@ -616,15 +622,31 @@ impl<S: MutinyStorage> FederationClient<S> {
// Reissue `OOBNotes`
let operation_id = mint_module.reissue_external_notes(oob_notes, ()).await?;

// TODO: (@leonardo) re-think about the results and errors that we need/want
match process_reissue_outcome(&mint_module, operation_id, self.logger.clone()).await? {
ReissueExternalNotesState::Created | ReissueExternalNotesState::Failed(_) => {
log_trace!(self.logger, "re-issuance of OOBNotes failed!");
match process_reissue_outcome(
&mint_module,
operation_id,
FEDIMINT_OOB_REISSUE_TIMEOUT_CHECK_MS,
self.logger.clone(),
)
.await?
{
ReissueExternalNotesState::Done => {
log_trace!(self.logger, "re-issuance of OOBNotes was successful!");
Ok(())
}
ReissueExternalNotesState::Failed(reason) => {
log_trace!(
self.logger,
"re-issuance of OOBNotes failed explicitly, reason: {reason}"
);
Err(MutinyError::FedimintReissueFailed)
}
_ => {
log_trace!(self.logger, "re-issuance of OOBNotes was successful!");
Ok(())
log_trace!(
self.logger,
"re-issuance of OOBNotes explicitly failed, due to outcome with a stale state!"
);
Err(MutinyError::FedimintReissueStaleState)
}
}
}
Expand Down Expand Up @@ -889,21 +911,22 @@ where
async fn process_reissue_outcome(
mint_module: &MintClientModule,
operation_id: OperationId,
timeout: u64,
logger: Arc<MutinyLogger>,
) -> Result<ReissueExternalNotesState, MutinyError> {
// Subscribe/Process the outcome based on `ReissueExternalNotesState`
// Subscribe to the outcome based on `ReissueExternalNotesState`
let stream_or_outcome = mint_module
.subscribe_reissue_external_notes(operation_id)
.await
.map_err(MutinyError::Other)?;

// Process the outcome based on `ReissueExternalNotesState`
match stream_or_outcome {
UpdateStreamOrOutcome::Outcome(outcome) => {
log_trace!(logger, "outcome received {:?}", outcome);
Ok(outcome)
}
UpdateStreamOrOutcome::UpdateStream(mut stream) => {
let timeout = DEFAULT_REISSUE_TIMEOUT;
let timeout_fut = sleep(timeout as i32);
pin_mut!(timeout_fut);

Expand Down
1 change: 0 additions & 1 deletion mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ use crate::utils::parse_profile_metadata;
use mockall::{automock, predicate::*};

const DEFAULT_PAYMENT_TIMEOUT: u64 = 30;
const DEFAULT_REISSUE_TIMEOUT: u64 = 50;
const MAX_FEDERATION_INVOICE_AMT: u64 = 200_000;
const SWAP_LABEL: &str = "SWAP";
const MELT_CASHU_TOKEN: &str = "Cashu Token Melt";
Expand Down
5 changes: 4 additions & 1 deletion mutiny-wasm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ pub enum MutinyJsError {
/// Token already spent.
#[error("Token has been already spent.")]
TokenAlreadySpent,
#[error("Fedimint external note reissuance failed.")]
#[error("Fedimint external note re-issuance failed.")]
FedimintReissueFailed,
#[error("Fedimint external note re-issuance resulted in stale outcome state.")]
FedimintReissueStaleState,
/// Unknown error.
#[error("Unknown Error")]
UnknownError,
Expand Down Expand Up @@ -241,6 +243,7 @@ impl From<MutinyError> for MutinyJsError {
MutinyError::PayjoinCreateRequest => MutinyJsError::PayjoinCreateRequest,
MutinyError::PayjoinResponse(e) => MutinyJsError::PayjoinResponse(e.to_string()),
MutinyError::FedimintReissueFailed => MutinyJsError::FedimintReissueFailed,
MutinyError::FedimintReissueStaleState => MutinyJsError::FedimintReissueStaleState,
}
}
}
Expand Down

0 comments on commit eb59a8b

Please sign in to comment.