Skip to content

Commit

Permalink
feat(app): Remove arbitrary sleep before on-chain sync
Browse files Browse the repository at this point in the history
A considerably better solution is to ensure that we execute
`keep_wallet_balance_and_history_up_to_date` to completion once before
we spawn the periodic on-chain sync task.

Co-authored-by: Richard Holzeis <[email protected]>
  • Loading branch information
luckysori and holzeis committed Sep 20, 2023
1 parent 0cd3ffa commit 76f386c
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions mobile/native/src/ln_dlc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,44 @@ pub fn run(data_dir: String, seed_dir: String, runtime: &Runtime) -> Result<()>
let _running = node.start(event_handler)?;
let node = Arc::new(Node::new(node, _running));

std::thread::spawn({
// Refresh the wallet balance and history eagerly so that it can complete before the
// triggering the first on-chain sync. This ensures that the UI appears ready as soon as
// possible.
//
// TODO: This might not be necessary once we rewrite the on-chain wallet with bdk:1.0.0.
spawn_blocking({
let node = node.clone();
move || {
// We wait a minute to not interfere with the app's startup. We want to be able to
// quickly refresh the wallet _before_ the first on-chain sync, as this one can take
// a long time and blocks the wallet refresh since they both need to acquire the
// same mutex.
//
// TODO: This should not be necessary as soon as we rewrite the on-chain wallet with
// bdk:1.0.0.
std::thread::sleep(Duration::from_secs(60));
move || keep_wallet_balance_and_history_up_to_date(&node)
})
.await
.expect("task to complete")?;

runtime.spawn({
let node = node.clone();
async move {
loop {
if let Err(e) = node.inner.sync_on_chain_wallet() {
tracing::error!("Failed on-chain sync: {e:#}");
tokio::time::sleep(UPDATE_WALLET_HISTORY_INTERVAL).await;

let node = node.clone();
if let Err(e) =
spawn_blocking(move || keep_wallet_balance_and_history_up_to_date(&node))
.await
.expect("To spawn blocking task")
{
tracing::error!("Failed to sync balance and wallet history: {e:#}");
}
}
}
});

std::thread::sleep(ON_CHAIN_SYNC_INTERVAL);
std::thread::spawn({
let node = node.clone();
move || loop {
if let Err(e) = node.inner.sync_on_chain_wallet() {
tracing::error!("Failed on-chain sync: {e:#}");
}

std::thread::sleep(ON_CHAIN_SYNC_INTERVAL);
}
});

Expand All @@ -290,24 +309,6 @@ pub fn run(data_dir: String, seed_dir: String, runtime: &Runtime) -> Result<()>
}
});

runtime.spawn({
let node = node.clone();
async move {
loop {
let node = node.clone();
if let Err(e) =
spawn_blocking(move || keep_wallet_balance_and_history_up_to_date(&node))
.await
.expect("To spawn blocking task")
{
tracing::error!("Failed to sync balance and wallet history: {e:#}");
}

tokio::time::sleep(UPDATE_WALLET_HISTORY_INTERVAL).await;
}
}
});

runtime.spawn(async move {
loop {
if let Err(e) = spawn_blocking(order::handler::check_open_orders)
Expand Down

0 comments on commit 76f386c

Please sign in to comment.