Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/app/restore_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ pub async fn restore_session_action(
) -> Result<(), MostroError> {
// Get user master key from the event sender
let master_key = event.sender.to_string();
// Get trade key from the event rumor
let trade_key = event.rumor.pubkey.to_string();

// Validate the master key format
if !master_key.chars().all(|c| c.is_ascii_hexdigit()) || master_key.len() != 64 {
return Err(MostroCantDo(CantDoReason::InvalidPubkey));
}

// Validate the trade key format
if !trade_key.chars().all(|c| c.is_ascii_hexdigit()) || trade_key.len() != 64 {
return Err(MostroCantDo(CantDoReason::InvalidPubkey));
}

tracing::info!(
"Starting background restore session for master key: {}",
master_key
Expand All @@ -35,22 +42,22 @@ pub async fn restore_session_action(

// Start a background task to handle the results
tokio::spawn(async move {
handle_restore_session_results(manager, master_key).await;
handle_restore_session_results(manager, trade_key).await;
});

Ok(())
}

/// Handle restore session results in the background
async fn handle_restore_session_results(mut manager: RestoreSessionManager, master_key: String) {
async fn handle_restore_session_results(mut manager: RestoreSessionManager, trade_key: String) {
// Wait for the result with a timeout
let timeout = tokio::time::Duration::from_secs(60 * 60); // 1 hour timeout

match tokio::time::timeout(timeout, manager.wait_for_result()).await {
Ok(Some(result)) => {
// Send the restore session response
if let Err(e) = send_restore_session_response(
&master_key,
&trade_key,
result.restore_orders,
result.restore_disputes,
)
Expand All @@ -65,7 +72,7 @@ async fn handle_restore_session_results(mut manager: RestoreSessionManager, mast
Err(_) => {
tracing::error!("Restore session timed out after 1 hour");
// Send timeout message to user
if let Err(e) = send_restore_session_timeout(&master_key).await {
if let Err(e) = send_restore_session_timeout(&trade_key).await {
tracing::error!("Failed to send timeout message: {}", e);
}
}
Expand All @@ -74,38 +81,38 @@ async fn handle_restore_session_results(mut manager: RestoreSessionManager, mast

/// Send restore session response to the user
async fn send_restore_session_response(
master_key: &str,
trade_key: &str,
orders: Vec<RestoredOrdersInfo>,
disputes: Vec<RestoredDisputesInfo>,
) -> Result<(), MostroError> {
// Convert master_key string to PublicKey
let master_pubkey =
PublicKey::from_hex(master_key).map_err(|_| MostroCantDo(CantDoReason::InvalidPubkey))?;
// Convert trade_key string to PublicKey
let trade_pubkey =
PublicKey::from_hex(trade_key).map_err(|_| MostroCantDo(CantDoReason::InvalidPubkey))?;

// Send the order data using the flat structure
enqueue_restore_session_msg(
Some(Payload::RestoreData(RestoreSessionInfo {
restore_orders: orders,
restore_disputes: disputes,
})),
master_pubkey,
trade_pubkey,
)
.await;

tracing::info!("Restore session response sent to user {}", master_key,);
tracing::info!("Restore session response sent to user {}", trade_key,);

Ok(())
}

/// Send timeout message to user
async fn send_restore_session_timeout(master_key: &str) -> Result<(), MostroError> {
let master_pubkey =
PublicKey::from_hex(master_key).map_err(|_| MostroCantDo(CantDoReason::InvalidPubkey))?;
async fn send_restore_session_timeout(trade_key: &str) -> Result<(), MostroError> {
let trade_pubkey =
PublicKey::from_hex(trade_key).map_err(|_| MostroCantDo(CantDoReason::InvalidPubkey))?;

// Send timeout message without payload since Text doesn't exist
enqueue_restore_session_msg(None, master_pubkey).await;
enqueue_restore_session_msg(None, trade_pubkey).await;

tracing::warn!("Restore session timed out for user: {}", master_key);
tracing::warn!("Restore session timed out for user: {}", trade_key);

Ok(())
}
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ impl Default for RestoreSessionManager {

impl RestoreSessionManager {
pub fn new() -> Self {
let (sender, receiver) = tokio::sync::mpsc::channel(100);
let (sender, receiver) = tokio::sync::mpsc::channel(10);
Self { sender, receiver }
}

Expand Down