Skip to content

Commit

Permalink
fix(ln-dlc-node): Sync both wallets back to back in tests
Browse files Browse the repository at this point in the history
Whilst we don't usually run into errors in `ln-dlc-node` tests, it's
still better to make sure that both wallets agree on the latest state
of the blockchain before continuing.
  • Loading branch information
luckysori committed Dec 14, 2023
1 parent 5fdbb5d commit ac7cfe4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 46 deletions.
20 changes: 10 additions & 10 deletions crates/ln-dlc-node/src/tests/dlc/non_collaborative_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn force_close_ln_dlc_channel() {
.await
.unwrap();

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Act

Expand All @@ -55,8 +55,8 @@ async fn force_close_ln_dlc_channel() {
// transactions
mine(288).await.unwrap();

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Ensure publication of the glue and buffer transactions (otherwise we need to wait for the
// periodic task)
Expand All @@ -70,17 +70,17 @@ async fn force_close_ln_dlc_channel() {

// Assert

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Mining 288 blocks ensures that we get:
// - 144 required confirmations for the delayed output on the LN commitment transaction to be
// spendable.
// - 288 required confirmations for the CET to be published.
mine(288).await.unwrap();

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Ensure publication of CET (otherwise we need to wait for the periodic task)
sub_channel_manager_periodic_check(
Expand All @@ -95,9 +95,9 @@ async fn force_close_ln_dlc_channel() {
mine(1).await.unwrap();
tracing::info!("Mined 1 block");

coordinator.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
tracing::info!("Coordinator synced on-chain");
app.sync_on_chain().await.unwrap();
app.sync_wallets().await.unwrap();
tracing::info!("App synced on-chain");

let coordinator_on_chain_balance_after_force_close =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn ln_collab_close() {

// Mine one block to confirm the close transaction
bitcoind::mine(1).await.unwrap();
payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

// Assert

Expand Down Expand Up @@ -134,7 +134,7 @@ async fn ln_force_close() {
.force_close_broadcasting_latest_txn(&channel_id, &coordinator.info.pubkey)
.unwrap();

payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

assert_eq!(payee.get_on_chain_balance().unwrap().confirmed, 0);
assert_eq!(payee.get_ldk_balance().available(), 0);
Expand All @@ -155,12 +155,12 @@ async fn ln_force_close() {
// Syncing the payee's wallet should now trigger a `SpendableOutputs` event
// corresponding to their revocable output in the commitment transaction, which they
// will subsequently spend in a new transaction paying to their on-chain wallet
payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

// Mine one more block to confirm the transaction spending the payee's revocable output
// in the commitment transaction
bitcoind::mine(1).await.unwrap();
payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

// Assert

Expand Down
24 changes: 12 additions & 12 deletions crates/ln-dlc-node/src/tests/just_in_time_channel/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ pub(crate) async fn send_interceptable_payment(
let user_channel_id = liquidity_request.user_channel_id;
let jit_channel_fee = liquidity_request.fee_sats;

payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_before = payer.get_ldk_balance();
let coordinator_balance_before = coordinator.get_ldk_balance();
Expand Down Expand Up @@ -310,9 +310,9 @@ pub(crate) async fn send_interceptable_payment(
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_after = payer.get_ldk_balance();
let coordinator_balance_after = coordinator.get_ldk_balance();
Expand Down Expand Up @@ -346,9 +346,9 @@ pub(crate) async fn send_payment(
invoice_amount_sat: u64,
coordinator_just_in_time_channel_creation_outbound_liquidity: Option<u64>,
) -> Result<()> {
payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_before = payer.get_ldk_balance();
let coordinator_balance_before = coordinator.get_ldk_balance();
Expand Down Expand Up @@ -394,9 +394,9 @@ pub(crate) async fn send_payment(
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_after = payer.get_ldk_balance();
let coordinator_balance_after = coordinator.get_ldk_balance();
Expand Down
17 changes: 11 additions & 6 deletions crates/ln-dlc-node/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,20 @@ impl Node<TenTenOneInMemoryStorage, InMemoryStore> {
Ok((node, running))
}

/// Trigger on-chain wallet sync.
/// Trigger on-chain and off-chain wallet syncs.
///
/// We wrap the wallet sync with a `block_in_place` to avoid blocking the async task in
/// `tokio::test`s.
///
/// Because we use `block_in_place`, we must configure the `tokio::test`s with `flavor =
/// "multi_thread"`.
async fn sync_on_chain(&self) -> Result<()> {
block_in_place(|| self.ldk_wallet().sync())
async fn sync_wallets(&self) -> Result<()> {
block_in_place(|| {
self.sync_on_chain_wallet()?;
self.sync_lightning_wallet()?;

Ok(())
})
}

async fn fund(&self, amount: Amount) -> Result<()> {
Expand All @@ -238,7 +243,7 @@ impl Node<TenTenOneInMemoryStorage, InMemoryStore> {
while self.get_confirmed_balance().await.unwrap() < expected_balance {
let interval = Duration::from_millis(200);

self.sync_on_chain().await.unwrap();
self.sync_wallets().await.unwrap();

tokio::time::sleep(interval).await;
tracing::debug!(
Expand Down Expand Up @@ -325,8 +330,8 @@ impl Node<TenTenOneInMemoryStorage, InMemoryStore> {
// We need to sync both parties, even if
// `trust_own_funding_0conf` is true for the creator
// of the channel (`self`)
self.sync_on_chain().await.unwrap();
peer.sync_on_chain().await.unwrap();
self.sync_wallets().await.unwrap();
peer.sync_wallets().await.unwrap();
}

tracing::debug!(
Expand Down
12 changes: 6 additions & 6 deletions crates/ln-dlc-node/src/tests/multi_hop_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ async fn multi_hop_payment() {
let coordinator_balance_before = coordinator.get_ldk_balance();
let payee_balance_before = payee.get_ldk_balance();

payer.sync_on_chain().await.unwrap();
coordinator.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
coordinator.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

// Act

Expand All @@ -74,9 +74,9 @@ async fn multi_hop_payment() {
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.sync_on_chain().await.unwrap();
coordinator.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
coordinator.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

let payer_balance_after = payer.get_ldk_balance();
let coordinator_balance_after = coordinator.get_ldk_balance();
Expand Down
12 changes: 6 additions & 6 deletions crates/ln-dlc-node/src/tests/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ async fn estimate_payment_fee() {
tracing::info!("Waiting for channels to be discovered");

for _ in 0..5 {
payer.sync_on_chain().await.unwrap();
a.sync_on_chain().await.unwrap();
b.sync_on_chain().await.unwrap();
c.sync_on_chain().await.unwrap();
d.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
a.sync_wallets().await.unwrap();
b.sync_wallets().await.unwrap();
c.sync_wallets().await.unwrap();
d.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

payer.broadcast_node_announcement();
a.broadcast_node_announcement();
Expand Down
4 changes: 2 additions & 2 deletions crates/ln-dlc-node/src/tests/single_hop_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async fn single_hop_payment() {
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

let payer_balance_after = payer.get_ldk_balance();
let payee_balance_after = payee.get_ldk_balance();
Expand Down

0 comments on commit ac7cfe4

Please sign in to comment.