Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reject pending offers #1763

Merged
merged 5 commits into from
Jan 2, 2024
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
2 changes: 2 additions & 0 deletions mobile/native/src/db/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl ToSql<Text, Sqlite> for FailureReason {
FailureReason::TimedOut => "TimedOut",
FailureReason::SubchannelOfferOutdated => "SubchannelOfferOutdated",
FailureReason::SubchannelOfferDateUndetermined => "SubchannelOfferDateUndetermined",
FailureReason::SubchannelOfferUnacceptable => "SubchannelOfferUnacceptable",
};
out.set_value(text);
Ok(IsNull::No)
Expand All @@ -173,6 +174,7 @@ impl FromSql<Text, Sqlite> for FailureReason {
"TimedOut" => Ok(FailureReason::TimedOut),
"SubchannelOfferOutdated" => Ok(FailureReason::SubchannelOfferOutdated),
"SubchannelOfferDateUndetermined" => Ok(FailureReason::SubchannelOfferDateUndetermined),
"SubchannelOfferUnacceptable" => Ok(FailureReason::SubchannelOfferUnacceptable),
_ => Err("Unrecognized enum variant".into()),
};
}
Expand Down
6 changes: 3 additions & 3 deletions mobile/native/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub fn get_filled_orders() -> Result<Vec<trade::order::Order>> {
Ok(orders)
}

/// Returns an order of there is currently an order that is open
/// Returns all open orders
pub fn maybe_get_open_orders() -> Result<Vec<trade::order::Order>> {
let mut db = connection()?;
let orders = Order::get_by_state(OrderState::Open, &mut db)?;
Expand All @@ -228,7 +228,7 @@ pub fn maybe_get_open_orders() -> Result<Vec<trade::order::Order>> {
Ok(orders)
}

/// Returns an order of there is currently an order that is being filled
/// Returns an order if there is currently an order that is being filled
pub fn maybe_get_order_in_filling() -> Result<Option<trade::order::Order>> {
let mut db = connection()?;
let orders = Order::get_by_state(OrderState::Filling, &mut db)?;
Expand All @@ -238,7 +238,7 @@ pub fn maybe_get_order_in_filling() -> Result<Option<trade::order::Order>> {
}

if orders.len() > 1 {
bail!("More than one order is being filled at the same time, this should not happen.")
bail!("More than one order is being filled at the same time, this should not happen. {orders:?}")
}

let first = orders
Expand Down
7 changes: 7 additions & 0 deletions mobile/native/src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ pub enum FailureReason {
TimedOut,
SubchannelOfferOutdated,
SubchannelOfferDateUndetermined,
SubchannelOfferUnacceptable,
}

impl From<FailureReason> for crate::trade::order::FailureReason {
Expand Down Expand Up @@ -669,6 +670,11 @@ impl From<FailureReason> for crate::trade::order::FailureReason {
InvalidSubchannelOffer::UndeterminedMaturityDate,
)
}
FailureReason::SubchannelOfferUnacceptable => {
crate::trade::order::FailureReason::InvalidDlcOffer(
InvalidSubchannelOffer::Unacceptable,
)
}
}
}
}
Expand All @@ -695,6 +701,7 @@ impl From<crate::trade::order::FailureReason> for FailureReason {
InvalidSubchannelOffer::UndeterminedMaturityDate => {
FailureReason::SubchannelOfferDateUndetermined
}
InvalidSubchannelOffer::Unacceptable => FailureReason::SubchannelOfferUnacceptable,
},
}
}
Expand Down
52 changes: 43 additions & 9 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const PROCESS_INCOMING_DLC_MESSAGES_INTERVAL: Duration = Duration::from_millis(2
const UPDATE_WALLET_HISTORY_INTERVAL: Duration = Duration::from_secs(5);
const CHECK_OPEN_ORDERS_INTERVAL: Duration = Duration::from_secs(60);
const ON_CHAIN_SYNC_INTERVAL: Duration = Duration::from_secs(300);
const WAIT_FOR_CONNECTING_TO_COORDINATOR: Duration = Duration::from_secs(2);

/// The weight estimate of the funding transaction
///
Expand Down Expand Up @@ -358,10 +359,12 @@ pub fn run(seed_dir: String, runtime: &Runtime) -> Result<()> {
let node = node.clone();
async move { node.listen_for_lightning_events(event_receiver).await }
});
let coordinator_info = config::get_coordinator_info();
let coordinator_pk = coordinator_info.pubkey;

runtime.spawn({
let node = node.clone();
async move { node.keep_connected(config::get_coordinator_info()).await }
async move { node.keep_connected(coordinator_info).await }
});

runtime.spawn({
Expand Down Expand Up @@ -392,15 +395,46 @@ pub fn run(seed_dir: String, runtime: &Runtime) -> Result<()> {

runtime.spawn(track_channel_status(node.clone()));

if let Err(e) = node.sync_position_with_subchannel_state().await {
tracing::error!("Failed to sync position with subchannel state. Error: {e:#}");
}
let inner_node = node.clone();

if let Err(e) = node.recover_rollover().await {
tracing::error!(
"Failed to check and recover from a stuck rollover state. Error: {e:#}"
);
}
runtime.spawn_blocking(move || {
let node = inner_node.clone();
async move {
let mut iteration_count = 0;

while !node
.inner
.peer_manager
.get_peer_node_ids()
.iter()
.any(|(a, _)| a == &coordinator_pk)
{
tracing::trace!(
"Not yet connecting to coordinator. Waiting with recovery for a connection"
);

tokio::time::sleep(WAIT_FOR_CONNECTING_TO_COORDINATOR).await;

iteration_count += 1;

if iteration_count >= 30 {
// After 30 retries (randonly chosen) we give up and continue with the
// function nevertheless. Which means, we might see
// an error.
break;
}
}
if let Err(e) = node.sync_position_with_subchannel_state().await {
tracing::error!("Failed to sync position with subchannel state. Error: {e:#}");
}

if let Err(e) = node.recover_rollover().await {
tracing::error!(
"Failed to check and recover from a stuck rollover state. Error: {e:#}"
);
}
}
});

state::set_node(node);

Expand Down
Loading
Loading